-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcudart-fake.c
145 lines (120 loc) · 3.11 KB
/
cudart-fake.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "fake-cufile-internal.h"
static int device = 0;
static fake_dev_memory * fake_malloc(size_t size, memAttachKind type){
fake_dev_memory * mem;
posix_memalign((void**) & mem, 4096, sizeof(fake_dev_memory) + size);
mem->magic = FMAGIC;
mem->size = size;
mem->type = type;
return mem;
}
fake_dev_memory * fake_get_mem(void const * buf){
fake_dev_memory * mem = (fake_dev_memory*) ((char*)buf - sizeof(fake_dev_memory));
if(mem->magic != FMAGIC){
return NULL;
}
return mem;
}
CUresult cuInit(int dev){
device = dev;
return CUDA_SUCCESS;
}
CUresult cuDeviceGet(CUdevice * device, int dev){
assert(device);
*device = (CUdevice){ .device = dev };
return CUDA_SUCCESS;
}
CUresult cuCtxCreate(CUcontext* pctx, unsigned int flags, CUdevice dev){
assert(pctx);
pctx->flags = flags;
pctx->device = dev;
return CUDA_SUCCESS;
}
CUresult cuCtxDestroy(CUcontext ctx){
return CUDA_SUCCESS;
}
CUresult cuMemAlloc(CUdeviceptr* dptr, size_t size){
return cudaMalloc(dptr, size);
}
CUresult cuMemFree(CUdeviceptr dptr){
return cudaFree(dptr);
}
CUresult cuMemsetD8(CUdeviceptr dptr, unsigned char val, size_t count){
return cudaMemset(dptr, val, count);
}
CUresult cuGetErrorName(CUresult cu, const char ** out){
*out = "FAKE CU ERROR";
return CUDA_SUCCESS;
}
cudaError_t cudaGetLastError(){
return CUDA_SUCCESS;
}
const char * cudaGetErrorString(cudaError_t err){
return "FAKE_NO_ERR";
}
cudaError_t cudaMalloc(void **buf, size_t size){
fake_dev_memory * mem = fake_malloc(size, cudaMemAttachDevice);
*buf = (void*) mem->buf;
return cudaSuccess;
}
cudaError_t cudaFree(void *buf){
fake_dev_memory * mem = fake_get_mem(buf);
if(! mem){
return cudaError;
}
return cudaSuccess;
}
const char * cudaGetErrorName(int err){
return "FAKE NO ERR";
}
cudaError_t cudaMemcpy(void * dst, void *src, size_t count, enum cudaMemcpyKind k){
if(k == cudaMemcpyDeviceToHost){
fake_dev_memory * mem = fake_get_mem(src);
if(! mem){
return cudaError;
}
assert(count <= mem->size);
}else if(k == cudaMemcpyHostToDevice){
fake_dev_memory * mem = fake_get_mem(dst);
if(! mem){
return cudaError;
}
assert(count <= mem->size);
}
memcpy(dst, src, count);
return cudaSuccess;
}
cudaError_t cudaMemset(void* buf, int val, size_t count){
fake_dev_memory * mem = fake_get_mem(buf);
if(! mem){
return cudaError;
}
assert(count <= mem->size);
memset(mem->buf, val, count);
return cudaSuccess;
}
cudaError_t cudaSetDevice(int dev){
device = dev;
return cudaSuccess;
}
cudaError_t cudaGetDevice(int *dev){
*dev = device;
return cudaSuccess;
}
cudaError_t cudaMallocManaged(void **buf, size_t size, memAttachKind type){
fake_dev_memory * mem = fake_malloc(size, type);
*buf = (void*) mem->buf;
return cudaSuccess;
}
cudaError_t cudaFreeHost(void *buf){
fake_dev_memory * mem = fake_get_mem(buf);
if(! mem){
return CUDA_ERROR;
}
return cudaFree(buf);
}
cudaError_t cudaMallocHost(void **buf, size_t size){
fake_dev_memory * mem = fake_malloc(size, cudaMemAttachHost);
*buf = (void*) mem->buf;
return cudaSuccess;
}