Skip to content

Commit a377db8

Browse files
committed
[CUDA] Added polyfill to test CUDA code without CUDA
1 parent ebdb659 commit a377db8

15 files changed

+303
-84
lines changed

include/occa/modes/cuda/device.hpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include <occa/defines.hpp>
22

3-
#if OCCA_CUDA_ENABLED
4-
# ifndef OCCA_MODES_CUDA_DEVICE_HEADER
5-
# define OCCA_MODES_CUDA_DEVICE_HEADER
3+
#ifndef OCCA_MODES_CUDA_DEVICE_HEADER
4+
#define OCCA_MODES_CUDA_DEVICE_HEADER
65

76
#include <occa/core/launchedDevice.hpp>
8-
9-
#include <cuda.h>
7+
#include <occa/modes/cuda/polyfill.hpp>
108

119
namespace occa {
1210
namespace cuda {
@@ -100,5 +98,4 @@ namespace occa {
10098
}
10199
}
102100

103-
# endif
104101
#endif

include/occa/modes/cuda/kernel.hpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include <occa/defines.hpp>
22

3-
#if OCCA_CUDA_ENABLED
4-
# ifndef OCCA_MODES_CUDA_KERNEL_HEADER
5-
# define OCCA_MODES_CUDA_KERNEL_HEADER
6-
7-
#include <cuda.h>
3+
#ifndef OCCA_MODES_CUDA_KERNEL_HEADER
4+
#define OCCA_MODES_CUDA_KERNEL_HEADER
85

96
#include <occa/core/launchedKernel.hpp>
7+
#include <occa/modes/cuda/polyfill.hpp>
108

119
namespace occa {
1210
namespace cuda {
@@ -47,5 +45,4 @@ namespace occa {
4745
}
4846
}
4947

50-
# endif
5148
#endif

include/occa/modes/cuda/memory.hpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include <occa/defines.hpp>
22

3-
#if OCCA_CUDA_ENABLED
4-
# ifndef OCCA_MODES_CUDA_MEMORY_HEADER
5-
# define OCCA_MODES_CUDA_MEMORY_HEADER
6-
7-
#include <cuda.h>
3+
#ifndef OCCA_MODES_CUDA_MEMORY_HEADER
4+
#define OCCA_MODES_CUDA_MEMORY_HEADER
85

96
#include <occa/core/memory.hpp>
7+
#include <occa/modes/cuda/polyfill.hpp>
108

119
namespace occa {
1210
namespace cuda {
@@ -60,5 +58,4 @@ namespace occa {
6058
}
6159
}
6260

63-
# endif
6461
#endif

include/occa/modes/cuda/polyfill.hpp

+279
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
#include <occa/defines.hpp>
2+
3+
#ifndef OCCA_MODES_CUDA_POLYFILL_HEADER
4+
#define OCCA_MODES_CUDA_POLYFILL_HEADER
5+
6+
#if OCCA_CUDA_ENABLED
7+
# include <cuda.h>
8+
#else
9+
10+
#define CUDA_VERSION 0
11+
12+
// Wrap in the occa namespace so as long as we don't use ::CUmodule, the two
13+
// - CUmodule
14+
// - occa::CUmodule
15+
// are indistinguisable inside the occa namespace
16+
namespace occa {
17+
//---[ Types ]------------------------
18+
struct _CUdeviceptr {};
19+
typedef struct _CUcontext* CUcontext;
20+
typedef int CUdevice;
21+
typedef struct _CUdeviceptr* CUdeviceptr;
22+
typedef struct _CUevent* CUevent;
23+
typedef struct _CUfunction* CUfunction;
24+
typedef struct _CUfunction_attribute* CUfunction_attribute;
25+
typedef struct _CUmodule* CUmodule;
26+
typedef struct _CUstream* CUstream;
27+
28+
//---[ Enums ]------------------------
29+
static const int CU_CTX_SCHED_AUTO = 0;
30+
static const int CU_DEVICE_CPU = 0;
31+
static const int CU_EVENT_DEFAULT = 0;
32+
static const int CU_MEM_ATTACH_GLOBAL = 0;
33+
static const int CU_MEM_ATTACH_HOST = 0;
34+
static const int CU_STREAM_DEFAULT = 0;
35+
36+
static const CUfunction_attribute CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK = NULL;
37+
38+
enum CUresult {
39+
CUDA_SUCCESS = 0,
40+
CUDA_ERROR_INVALID_VALUE,
41+
CUDA_ERROR_OUT_OF_MEMORY,
42+
CUDA_ERROR_NOT_INITIALIZED,
43+
CUDA_ERROR_DEINITIALIZED,
44+
CUDA_ERROR_PROFILER_DISABLED,
45+
CUDA_ERROR_PROFILER_NOT_INITIALIZED,
46+
CUDA_ERROR_PROFILER_ALREADY_STARTED,
47+
CUDA_ERROR_PROFILER_ALREADY_STOPPED,
48+
CUDA_ERROR_NO_DEVICE,
49+
CUDA_ERROR_INVALID_DEVICE,
50+
CUDA_ERROR_INVALID_IMAGE,
51+
CUDA_ERROR_INVALID_CONTEXT,
52+
CUDA_ERROR_CONTEXT_ALREADY_CURRENT,
53+
CUDA_ERROR_MAP_FAILED,
54+
CUDA_ERROR_UNMAP_FAILED,
55+
CUDA_ERROR_ARRAY_IS_MAPPED,
56+
CUDA_ERROR_ALREADY_MAPPED,
57+
CUDA_ERROR_NO_BINARY_FOR_GPU,
58+
CUDA_ERROR_ALREADY_ACQUIRED,
59+
CUDA_ERROR_NOT_MAPPED,
60+
CUDA_ERROR_NOT_MAPPED_AS_ARRAY,
61+
CUDA_ERROR_NOT_MAPPED_AS_POINTER,
62+
CUDA_ERROR_ECC_UNCORRECTABLE,
63+
CUDA_ERROR_UNSUPPORTED_LIMIT,
64+
CUDA_ERROR_CONTEXT_ALREADY_IN_USE,
65+
CUDA_ERROR_PEER_ACCESS_UNSUPPORTED,
66+
CUDA_ERROR_INVALID_SOURCE,
67+
CUDA_ERROR_FILE_NOT_FOUND,
68+
CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND,
69+
CUDA_ERROR_SHARED_OBJECT_INIT_FAILED,
70+
CUDA_ERROR_OPERATING_SYSTEM,
71+
CUDA_ERROR_INVALID_HANDLE,
72+
CUDA_ERROR_NOT_FOUND,
73+
CUDA_ERROR_NOT_READY,
74+
CUDA_ERROR_LAUNCH_FAILED,
75+
CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES,
76+
CUDA_ERROR_LAUNCH_TIMEOUT,
77+
CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING,
78+
CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED,
79+
CUDA_ERROR_PEER_ACCESS_NOT_ENABLED,
80+
CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE,
81+
CUDA_ERROR_CONTEXT_IS_DESTROYED,
82+
CUDA_ERROR_ASSERT,
83+
CUDA_ERROR_TOO_MANY_PEERS,
84+
CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED,
85+
CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED,
86+
CUDA_ERROR_NOT_PERMITTED,
87+
CUDA_ERROR_NOT_SUPPORTED,
88+
CUDA_ERROR_INVALID_PTX,
89+
CUDA_ERROR_ILLEGAL_ADDRESS,
90+
CUDA_ERROR_HARDWARE_STACK_ERROR,
91+
CUDA_ERROR_ILLEGAL_INSTRUCTION,
92+
CUDA_ERROR_MISALIGNED_ADDRESS,
93+
CUDA_ERROR_INVALID_ADDRESS_SPACE,
94+
CUDA_ERROR_INVALID_PC,
95+
CUDA_ERROR_NVLINK_UNCORRECTABLE,
96+
OCCA_CUDA_IS_NOT_ENABLED
97+
};
98+
99+
//---[ Methods ]----------------------
100+
inline CUresult cuInit(unsigned int Flags) {
101+
return OCCA_CUDA_IS_NOT_ENABLED;
102+
}
103+
104+
inline CUresult cuFuncGetAttribute(int *pi, CUfunction_attribute attrib, CUfunction hfunc) {
105+
return OCCA_CUDA_IS_NOT_ENABLED;
106+
}
107+
108+
inline CUresult cuLaunchKernel(CUfunction f,
109+
unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ,
110+
unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ,
111+
unsigned int sharedMemBytes,
112+
CUstream hStream,
113+
void **kernelParams,
114+
void **extra) {
115+
return OCCA_CUDA_IS_NOT_ENABLED;
116+
}
117+
118+
// ---[ Context ]-------------------
119+
inline CUresult cuCtxCreate(CUcontext *pctx, unsigned int flags, CUdevice dev) {
120+
return OCCA_CUDA_IS_NOT_ENABLED;
121+
}
122+
123+
inline CUresult cuCtxDestroy(CUcontext ctx) {
124+
return OCCA_CUDA_IS_NOT_ENABLED;
125+
}
126+
127+
inline CUresult cuCtxEnablePeerAccess(CUcontext peerContext, unsigned int Flags) {
128+
return OCCA_CUDA_IS_NOT_ENABLED;
129+
}
130+
131+
inline CUresult cuCtxSetCurrent(CUcontext ctx) {
132+
return OCCA_CUDA_IS_NOT_ENABLED;
133+
}
134+
135+
// ---[ Device ]--------------------
136+
inline CUresult cuDeviceCanAccessPeer(int *canAccessPeer, CUdevice dev, CUdevice peerDev) {
137+
return OCCA_CUDA_IS_NOT_ENABLED;
138+
}
139+
140+
inline CUresult cuDeviceComputeCapability(int *major, int *minor, CUdevice dev) {
141+
// [Deprecated]
142+
return OCCA_CUDA_IS_NOT_ENABLED;
143+
}
144+
145+
inline CUresult cuDeviceGet(CUdevice *device, int ordinal) {
146+
return OCCA_CUDA_IS_NOT_ENABLED;
147+
}
148+
149+
inline CUresult cuDeviceGetCount(int *count) {
150+
return OCCA_CUDA_IS_NOT_ENABLED;
151+
}
152+
153+
inline CUresult cuDeviceGetName(char *name, int len, CUdevice dev) {
154+
return OCCA_CUDA_IS_NOT_ENABLED;
155+
}
156+
157+
inline CUresult cuDeviceTotalMem(size_t *bytes, CUdevice dev) {
158+
return OCCA_CUDA_IS_NOT_ENABLED;
159+
}
160+
161+
// ---[ Event ]---------------------
162+
inline CUresult cuEventCreate(CUevent *phEvent, unsigned int Flags) {
163+
return OCCA_CUDA_IS_NOT_ENABLED;
164+
}
165+
166+
inline CUresult cuEventDestroy(CUevent hEvent) {
167+
return OCCA_CUDA_IS_NOT_ENABLED;
168+
}
169+
170+
inline CUresult cuEventElapsedTime(float *pMilliseconds, CUevent hStart, CUevent hEnd) {
171+
return OCCA_CUDA_IS_NOT_ENABLED;
172+
}
173+
174+
inline CUresult cuEventRecord(CUevent hEvent, CUstream hStream) {
175+
return OCCA_CUDA_IS_NOT_ENABLED;
176+
}
177+
178+
inline CUresult cuEventSynchronize(CUevent hEvent) {
179+
return OCCA_CUDA_IS_NOT_ENABLED;
180+
}
181+
182+
183+
// ---[ Memory ]--------------------
184+
inline CUresult cuMemAlloc(CUdeviceptr *dptr, size_t bytesize) {
185+
return OCCA_CUDA_IS_NOT_ENABLED;
186+
}
187+
188+
inline CUresult cuMemAllocHost(void **pp, size_t bytesize) {
189+
return OCCA_CUDA_IS_NOT_ENABLED;
190+
}
191+
192+
inline CUresult cuMemAllocManaged(CUdeviceptr *dptr, size_t bytesize, unsigned int flags) {
193+
return OCCA_CUDA_IS_NOT_ENABLED;
194+
}
195+
196+
inline CUresult cuMemFree(CUdeviceptr dptr) {
197+
return OCCA_CUDA_IS_NOT_ENABLED;
198+
}
199+
200+
inline CUresult cuMemFreeHost(void *p) {
201+
return OCCA_CUDA_IS_NOT_ENABLED;
202+
}
203+
204+
inline CUresult cuMemHostGetDevicePointer(CUdeviceptr *dptr, void *p, unsigned int Flags) {
205+
return OCCA_CUDA_IS_NOT_ENABLED;
206+
}
207+
208+
inline CUresult cuMemPrefetchAsync(CUdeviceptr *devPtr, size_t count, CUdevice dstDevice, CUstream hStream) {
209+
return OCCA_CUDA_IS_NOT_ENABLED;
210+
}
211+
212+
inline CUresult cuMemcpyDtoD(CUdeviceptr dstDevice, const CUdeviceptr srcDevice, size_t ByteCount) {
213+
return OCCA_CUDA_IS_NOT_ENABLED;
214+
}
215+
216+
inline CUresult cuMemcpyDtoDAsync(CUdeviceptr dstDevice, const CUdeviceptr srcDevice, size_t ByteCount, CUstream hstream) {
217+
return OCCA_CUDA_IS_NOT_ENABLED;
218+
}
219+
220+
inline CUresult cuMemcpyDtoH(void *dstHost, const CUdeviceptr srcDevice, size_t ByteCount) {
221+
return OCCA_CUDA_IS_NOT_ENABLED;
222+
}
223+
224+
inline CUresult cuMemcpyDtoHAsync(void *dstHost, const CUdeviceptr srcDevice, size_t ByteCount, CUstream hstream) {
225+
return OCCA_CUDA_IS_NOT_ENABLED;
226+
}
227+
228+
inline CUresult cuMemcpyHtoD(CUdeviceptr dstDevice, const void *srcHost, size_t ByteCount) {
229+
return OCCA_CUDA_IS_NOT_ENABLED;
230+
}
231+
232+
inline CUresult cuMemcpyHtoDAsync(CUdeviceptr dstDevice, const void *srcHost, size_t ByteCount, CUstream hstream) {
233+
return OCCA_CUDA_IS_NOT_ENABLED;
234+
}
235+
236+
inline CUresult cuMemcpyPeer(CUdeviceptr dstDevice, CUcontext dstContext,
237+
CUdeviceptr srcDevice, CUcontext srcContext,
238+
size_t ByteCount) {
239+
return OCCA_CUDA_IS_NOT_ENABLED;
240+
}
241+
242+
inline CUresult cuMemcpyPeerAsync(CUdeviceptr dstDevice, CUcontext dstContext,
243+
CUdeviceptr srcDevice, CUcontext srcContext,
244+
size_t ByteCount, CUstream hStream) {
245+
return OCCA_CUDA_IS_NOT_ENABLED;
246+
}
247+
248+
// Version-dependent methods:
249+
// inline CUresult cuMemAdvise(CUdeviceptr devPtr, size_t count, CUmem_advice advice, CUdevice device)
250+
251+
// ---[ Module ]--------------------
252+
inline CUresult cuModuleGetFunction(CUfunction *hfunc, CUmodule hmod, const char *name) {
253+
return OCCA_CUDA_IS_NOT_ENABLED;
254+
}
255+
256+
inline CUresult cuModuleLoad(CUmodule *module, const char *fname) {
257+
return OCCA_CUDA_IS_NOT_ENABLED;
258+
}
259+
260+
inline CUresult cuModuleUnload(CUmodule hmod) {
261+
return OCCA_CUDA_IS_NOT_ENABLED;
262+
}
263+
264+
// ---[ Stream ]--------------------
265+
inline CUresult cuStreamCreate(CUstream *phStream, unsigned int Flags) {
266+
return OCCA_CUDA_IS_NOT_ENABLED;
267+
}
268+
269+
inline CUresult cuStreamDestroy(CUstream hStream) {
270+
return OCCA_CUDA_IS_NOT_ENABLED;
271+
}
272+
273+
inline CUresult cuStreamSynchronize(CUstream hStream) {
274+
return OCCA_CUDA_IS_NOT_ENABLED;
275+
}
276+
}
277+
278+
#endif
279+
#endif

include/occa/modes/cuda/registration.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include <occa/defines.hpp>
22

3-
#if OCCA_CUDA_ENABLED
4-
# ifndef OCCA_MODES_CUDA_REGISTRATION_HEADER
5-
# define OCCA_MODES_CUDA_REGISTRATION_HEADER
3+
#ifndef OCCA_MODES_CUDA_REGISTRATION_HEADER
4+
#define OCCA_MODES_CUDA_REGISTRATION_HEADER
65

76
#include <occa/modes.hpp>
87
#include <occa/modes/cuda/device.hpp>
@@ -26,5 +25,4 @@ namespace occa {
2625
}
2726
}
2827

29-
# endif
3028
#endif

include/occa/modes/cuda/stream.hpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include <occa/defines.hpp>
22

3-
#if OCCA_CUDA_ENABLED
4-
# ifndef OCCA_MODES_CUDA_STREAM_HEADER
5-
# define OCCA_MODES_CUDA_STREAM_HEADER
6-
7-
#include <cuda.h>
3+
#ifndef OCCA_MODES_CUDA_STREAM_HEADER
4+
#define OCCA_MODES_CUDA_STREAM_HEADER
85

96
#include <occa/core/stream.hpp>
7+
#include <occa/modes/cuda/polyfill.hpp>
108

119
namespace occa {
1210
namespace cuda {
@@ -23,5 +21,4 @@ namespace occa {
2321
}
2422
}
2523

26-
# endif
2724
#endif

0 commit comments

Comments
 (0)