| 1 | #include "civlc.h"
|
|---|
| 2 | #include <string.h>
|
|---|
| 3 |
|
|---|
| 4 | $scope _global_scope = $here;
|
|---|
| 5 | $scope _host_scope;
|
|---|
| 6 |
|
|---|
| 7 | // Cuda data types
|
|---|
| 8 |
|
|---|
| 9 | typedef struct {
|
|---|
| 10 | unsigned int x, y, z;
|
|---|
| 11 | } dim3;
|
|---|
| 12 |
|
|---|
| 13 | typedef struct {
|
|---|
| 14 | unsigned int x, y, z;
|
|---|
| 15 | } uint3;
|
|---|
| 16 |
|
|---|
| 17 | enum cudaMemcpyKind {
|
|---|
| 18 | cudaMemcpyHostToHost,
|
|---|
| 19 | cudaMemcpyHostToDevice,
|
|---|
| 20 | cudaMemcpyDeviceToHost,
|
|---|
| 21 | cudaMemcpyDeviceToDevice,
|
|---|
| 22 | cudaMemcpyDefault
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | enum cudaError {
|
|---|
| 26 | cudaSuccess,
|
|---|
| 27 | cudaErrorMissingConfiguration,
|
|---|
| 28 | cudaErrorMemoryAllocation,
|
|---|
| 29 | cudaErrorInitializationError,
|
|---|
| 30 | cudaErrorLaunchFailure,
|
|---|
| 31 | cudaErrorPriorLaunchFailure,
|
|---|
| 32 | cudaErrorLaunchTimeout,
|
|---|
| 33 | cudaErrorLaunchOutOfResources,
|
|---|
| 34 | cudaErrorInvalidDeviceFunction,
|
|---|
| 35 | cudaErrorInvalidConfiguration,
|
|---|
| 36 | cudaErrorInvalidDevice,
|
|---|
| 37 | cudaErrorInvalidValue,
|
|---|
| 38 | cudaErrorInvalidPitchValue,
|
|---|
| 39 | cudaErrorInvalidSymbol,
|
|---|
| 40 | cudaErrorMapBufferObjectFailed,
|
|---|
| 41 | cudaErrorUnmapBufferObjectFailed,
|
|---|
| 42 | cudaErrorInvalidHostPointer,
|
|---|
| 43 | cudaErrorInvalidDevicePointer,
|
|---|
| 44 | cudaErrorInvalidTexture,
|
|---|
| 45 | cudaErrorInvalidTextureBinding,
|
|---|
| 46 | cudaErrorInvalidChannelDescriptor,
|
|---|
| 47 | cudaErrorInvalidMemcpyDirection,
|
|---|
| 48 | cudaErrorAddressOfConstant,
|
|---|
| 49 | cudaErrorTextureFetchFailed,
|
|---|
| 50 | cudaErrorTextureNotBound,
|
|---|
| 51 | cudaErrorSynchronizationError,
|
|---|
| 52 | cudaErrorInvalidFilterSetting,
|
|---|
| 53 | cudaErrorInvalidNormSetting,
|
|---|
| 54 | cudaErrorMixedDeviceExecution,
|
|---|
| 55 | cudaErrorCudartUnloading,
|
|---|
| 56 | cudaErrorUnknown,
|
|---|
| 57 | cudaErrorNotYetImplemented,
|
|---|
| 58 | cudaErrorMemoryValueTooLarge,
|
|---|
| 59 | cudaErrorInvalidResourceHandle,
|
|---|
| 60 | cudaErrorNotReady,
|
|---|
| 61 | cudaErrorInsufficientDriver,
|
|---|
| 62 | cudaErrorSetOnActiveProcess,
|
|---|
| 63 | cudaErrorInvalidSurface,
|
|---|
| 64 | cudaErrorNoDevice,
|
|---|
| 65 | cudaErrorECCUncorrectable,
|
|---|
| 66 | cudaErrorSharedObjectSymbolNotFound,
|
|---|
| 67 | cudaErrorSharedObjectInitFailed,
|
|---|
| 68 | cudaErrorUnsupportedLimit,
|
|---|
| 69 | cudaErrorDuplicateVariableName,
|
|---|
| 70 | cudaErrorDuplicateTextureName,
|
|---|
| 71 | cudaErrorDuplicateSurfaceName,
|
|---|
| 72 | cudaErrorDevicesUnavailable,
|
|---|
| 73 | cudaErrorInvalidKernelImage,
|
|---|
| 74 | cudaErrorNoKernelImageForDevice,
|
|---|
| 75 | cudaErrorIncompatibleDriverContext,
|
|---|
| 76 | cudaErrorPeerAccessAlreadyEnabled,
|
|---|
| 77 | cudaErrorPeerAccessNotEnabled,
|
|---|
| 78 | cudaErrorDeviceAlreadyInUse,
|
|---|
| 79 | cudaErrorProfilerDisabled,
|
|---|
| 80 | cudaErrorProfilerNotInitialized,
|
|---|
| 81 | cudaErrorProfilerAlreadyStarted,
|
|---|
| 82 | cudaErrorProfilerAlreadyStopped,
|
|---|
| 83 | cudaErrorAssert,
|
|---|
| 84 | cudaErrorTooManyPeers,
|
|---|
| 85 | cudaErrorHostMemoryAlreadyRegistered,
|
|---|
| 86 | cudaErrorHostMemoryNotRegistered,
|
|---|
| 87 | cudaErrorOperatingSystem,
|
|---|
| 88 | cudaErrorStartupFailure,
|
|---|
| 89 | cudaErrorApiFailureBase
|
|---|
| 90 | };
|
|---|
| 91 | typedef enum cudaError cudaError_t;
|
|---|
| 92 |
|
|---|
| 93 | // civl versions of cuda functions
|
|---|
| 94 | uint3 make_uint3(unsigned int a, unsigned int b, unsigned int c) {
|
|---|
| 95 | uint3 t;
|
|---|
| 96 | t.x = a; t.y = b; t.z = c;
|
|---|
| 97 | return t;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | cudaError_t cudaMemcpy ( void *dst, const void *src, size_t count, enum cudaMemcpyKind kind ) {
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | switch (kind) {
|
|---|
| 104 | case cudaMemcpyHostToHost:
|
|---|
| 105 | $assert($scopeof(*src) <= _host_scope);
|
|---|
| 106 | $assert($scopeof(*dst) <= _host_scope);
|
|---|
| 107 | break;
|
|---|
| 108 | case cudaMemcpyHostToDevice:
|
|---|
| 109 | $assert($scopeof(*src) <= _host_scope);
|
|---|
| 110 | $assert($scopeof(*dst) > _host_scope);
|
|---|
| 111 | break;
|
|---|
| 112 | case cudaMemcpyDeviceToHost:
|
|---|
| 113 | $assert($scopeof(*src) > _host_scope);
|
|---|
| 114 | $assert($scopeof(*dst) <= _host_scope);
|
|---|
| 115 | break;
|
|---|
| 116 | case cudaMemcpyDeviceToDevice:
|
|---|
| 117 | $assert($scopeof(*src) > _host_scope);
|
|---|
| 118 | $assert($scopeof(*dst) > _host_scope);
|
|---|
| 119 | break;
|
|---|
| 120 | default:
|
|---|
| 121 | break;
|
|---|
| 122 | }
|
|---|
| 123 | */
|
|---|
| 124 | memcpy(dst, src, count);
|
|---|
| 125 | return cudaSuccess;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | cudaError_t cudaFree(void *devPtr) {
|
|---|
| 129 | $assert($scopeof(*devPtr) > _host_scope);
|
|---|
| 130 | $free(devPtr);
|
|---|
| 131 | return cudaSuccess;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | cudaError_t cudaSetDevice(int device_id) {
|
|---|
| 135 | return cudaSuccess;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | cudaError_t cudaGetDeviceCount(int *count) {
|
|---|
| 139 | // possibly this should return an value specified as $input?
|
|---|
| 140 | *count = 1;
|
|---|
| 141 | return cudaSuccess;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | // helper function, only called by transformed program
|
|---|
| 145 | int _index (dim3 size, uint3 indexes) {
|
|---|
| 146 |
|
|---|
| 147 | return indexes.x + size.x * (indexes.y + size.y * indexes.z);
|
|---|
| 148 |
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | dim3 _toDim3(int x) {
|
|---|
| 152 | dim3 d;
|
|---|
| 153 | d.x = x; d.y = 1; d.z = 1;
|
|---|
| 154 | return d;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | void _createProcs(dim3 dim, void procFn(uint3)) {
|
|---|
| 158 | $proc procs[dim.x][dim.y][dim.z];
|
|---|
| 159 | for (int x = 0; x < dim.x; x++) {
|
|---|
| 160 | for (int y = 0; y < dim.y; y++) {
|
|---|
| 161 | for (int z = 0; z < dim.z; z++) {
|
|---|
| 162 | uint3 bid = make_uint3(x, y, z);
|
|---|
| 163 | procs[x][y][z] = $spawn procFn(bid);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 | for (int x = 0; x < dim.x; x++) {
|
|---|
| 169 | for (int y = 0; y < dim.y; y++) {
|
|---|
| 170 | for (int z = 0; z < dim.z; z++) {
|
|---|
| 171 | $wait(procs[x][y][z]);
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | typedef struct {
|
|---|
| 178 | $proc finalProcess;
|
|---|
| 179 | } _CUstream;
|
|---|
| 180 | typedef _CUstream* cudaStream_t;
|
|---|
| 181 |
|
|---|
| 182 | struct {
|
|---|
| 183 | cudaStream_t stream;
|
|---|
| 184 | struct _cudaStreamNode_ *next;
|
|---|
| 185 | } _cudaStreamNode_;
|
|---|
| 186 | typedef struct _cudaStreamNode_ _cudaStreamNode;
|
|---|
| 187 |
|
|---|
| 188 | typedef struct {
|
|---|
| 189 | _cudaStreamNode *headNode;
|
|---|
| 190 | $proc lastProc;
|
|---|
| 191 | } _cudaContext;
|
|---|
| 192 |
|
|---|
| 193 | _cudaContext _context = { .headNode = NULL, .lastProc = $proc_null };
|
|---|
| 194 |
|
|---|
| 195 | cudaError_t cudaStreamCreate(cudaStream_t *pStream) {
|
|---|
| 196 | *pStream = (cudaStream_t)$malloc($root, sizeof(_CUstream));
|
|---|
| 197 | (*pStream)->finalProcess = $proc_null;
|
|---|
| 198 | _cudaStreamNode *newNode = (_cudaStreamNode*)$malloc($root, sizeof(_cudaStreamNode));
|
|---|
| 199 | _cudaStreamNode *oldHead = _context.headNode;
|
|---|
| 200 | newNode->stream = *pStream;
|
|---|
| 201 | newNode->next = oldHead;
|
|---|
| 202 | _context.headNode = newNode;
|
|---|
| 203 |
|
|---|
| 204 | return cudaSuccess;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | cudaError_t cudaStreamDestroy(cudaStream_t pStream) {
|
|---|
| 208 | _cudaStreamNode *prevNode = NULL;
|
|---|
| 209 | _cudaStreamNode *curNode = _context.headNode;
|
|---|
| 210 | while (curNode != NULL) {
|
|---|
| 211 | if (pStream == curNode->stream) {
|
|---|
| 212 | if (prevNode == NULL) {
|
|---|
| 213 | _context.headNode = NULL;
|
|---|
| 214 | } else {
|
|---|
| 215 | prevNode->next = curNode->next;
|
|---|
| 216 | $free(pStream);
|
|---|
| 217 | $free(curNode);
|
|---|
| 218 | }
|
|---|
| 219 | return cudaSuccess;
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | $assert($false);
|
|---|
| 223 | return cudaErrorInvalidResourceHandle;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | void _tryWait($proc p) {
|
|---|
| 227 | if (p != $proc_null) $wait(p);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | void _initKernel(cudaStream_t stream, void (*k)($proc)) {
|
|---|
| 231 | $proc prevProc;
|
|---|
| 232 | if (stream == NULL) {
|
|---|
| 233 | cudaDeviceSynchronize();
|
|---|
| 234 | prevProc = _context.lastProc;
|
|---|
| 235 | _context.lastProc = $spawn k(prevProc);
|
|---|
| 236 | } else {
|
|---|
| 237 | prevProc = stream->lastProc;
|
|---|
| 238 | stream->lastProc = $spawn k(prevProc);
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|