1 /*
2 * The MIT License (MIT)
3 * This file is part of waifu2x-converter-cpp
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23 
24 #ifndef CUDALIB_H
25 #define CUDALIB_H
26 
27 #include <stddef.h>
28 #include <stdint.h>
29 
30 #ifdef _WIN32
31 #define CUDAAPI __stdcall
32 #else
33 #define CUDAAPI
34 #endif
35 
36 typedef uintptr_t CUdeviceptr;
37 
38 typedef enum cudaError_enum
39 {
40 	CUDA_SUCCESS = 0
41 } CUresult;
42 
43 typedef enum CUjit_option_enum
44 {
45 	CU_JIT_MAX_REGISTERS = 0,
46 	CU_JIT_THREADS_PER_BLOCK = 1,
47 	CU_JIT_WALL_TIME = 2,
48 	CU_JIT_INFO_LOG_BUFFER = 3,
49 	CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES = 4,
50 	CU_JIT_ERROR_LOG_BUFFER = 5,
51 	CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES = 6,
52 	CU_JIT_OPTIMIZATION_LEVEL = 7,
53 	CU_JIT_CACHE_MODE=14,
54 } CUjit_option;
55 
56 typedef enum CUdevice_attribute_enum
57 {
58 	CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT = 16,
59 	CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75,
60 } CUdevice_attribute;
61 
62 typedef enum CUjit_cacheMode_enum
63 {
64 	CU_JIT_CACHE_OPTION_NONE = 0,
65 	CU_JIT_CACHE_OPTION_CG,
66 	CU_JIT_CACHE_OPTION_CA
67 } CUjit_cacheMode;
68 
69 typedef enum CUfunc_cache_enum
70 {
71     CU_FUNC_CACHE_PREFER_NONE = 0,
72     CU_FUNC_CACHE_PREFER_SHARED = 1,
73     CU_FUNC_CACHE_PREFER_L1 = 2,
74     CU_FUNC_CACHE_PREFER_EQUAL = 3
75 } CUfunc_cache;
76 
77 typedef enum CUsharedconfig_enum
78 {
79     CU_SHARED_MEM_CONFIG_DEFAULT_BANK_SIZE = 0,
80     CU_SHARED_MEM_CONFIG_FOUR_BYTE_BANK_SIZE = 1,
81     CU_SHARED_MEM_CONFIG_EIGHT_BYTE_BANK_SIZE = 2
82 } CUsharedconfig;
83 
84 typedef int CUdevice;
85 
86 typedef struct CUctx_st *CUcontext;
87 typedef struct CUmod_st *CUmodule;
88 typedef struct CUfunc_st *CUfunction;
89 typedef struct CUstream_st *CUstream;
90 
91 #define CU_CTX_SCHED_BLOCKING_SYNC 0x4
92 
93 typedef CUresult (CUDAAPI * tcuInit)(unsigned int Flags);
94 typedef CUresult (CUDAAPI * tcuDriverGetVersion)(int *ver);
95 typedef CUresult (CUDAAPI * tcuDeviceGetCount)(int *count);
96 typedef CUresult (CUDAAPI * tcuDeviceGetName)(char *name, int len, CUdevice dev);
97 typedef CUresult (CUDAAPI * tcuCtxCreate)(CUcontext *ret, unsigned int flags, CUdevice dev);
98 typedef CUresult (CUDAAPI * tcuCtxDestroy)(CUcontext ret);
99 typedef CUresult (CUDAAPI * tcuModuleLoadData)(CUmodule *module, const void *image);
100 typedef CUresult (CUDAAPI * tcuModuleLoadDataEx)(CUmodule *module, const void *image, unsigned int n, CUjit_option *o, void **ov);
101 typedef CUresult (CUDAAPI * tcuModuleUnload)(CUmodule mod);
102 typedef CUresult (CUDAAPI * tcuModuleGetFunction)(CUfunction *hfunc, CUmodule mod, const char *name);
103 typedef CUresult (CUDAAPI * tcuStreamCreate)(CUstream *str, unsigned int Flags);
104 typedef CUresult (CUDAAPI * tcuStreamDestroy)(CUstream str);
105 typedef CUresult (CUDAAPI * tcuMemAlloc)(CUdeviceptr *dptr, size_t bytesize);
106 typedef CUresult (CUDAAPI * tcuMemFree)(CUdeviceptr dptr);
107 typedef CUresult (CUDAAPI * tcuCtxSetCurrent)(CUcontext ctxt);
108 typedef CUresult (CUDAAPI * tcuCtxPushCurrent)(CUcontext ctxt);
109 typedef CUresult (CUDAAPI * tcuCtxPopCurrent)(CUcontext *ctxt);
110 typedef CUresult (CUDAAPI * tcuStreamSynchronize)(CUstream stream);
111 typedef CUresult (CUDAAPI * tcuMemcpyHtoD)(CUdeviceptr dst, const void *src, size_t byte);
112 typedef CUresult (CUDAAPI * tcuMemcpyHtoDAsync)(CUdeviceptr dst, const void *src, size_t byte, CUstream str);
113 typedef CUresult (CUDAAPI * tcuMemcpyDtoH)(void *dst, CUdeviceptr src, size_t byte);
114 typedef CUresult (CUDAAPI * tcuLaunchKernel)
115 (
116 	CUfunction f,
117 	unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ,
118 	unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ,
119 	unsigned int sharedMemBytes,
120 	CUstream str, void **kernelParams, void **extra
121 );
122 typedef CUresult (CUDAAPI * tcuCtxSetCacheConfig)(CUfunc_cache c);
123 typedef CUresult (CUDAAPI * tcuFuncSetSharedMemConfig)(CUfunction func, CUsharedconfig config);
124 typedef CUresult (CUDAAPI * tcuCtxSetSharedMemConfig)(CUsharedconfig config);
125 typedef CUresult (CUDAAPI * tcuDeviceGetAttribute)(int *pi, CUdevice_attribute attr, CUdevice dev);
126 typedef CUresult (CUDAAPI * tcuProfilerStart)(void);
127 
128 #define FOR_EACH_CUDA_FUNC(F,F_V2) \
129     F(cuInit)                      \
130     F(cuDriverGetVersion)          \
131     F(cuDeviceGetCount)            \
132     F(cuDeviceGetName)             \
133     F_V2(cuCtxCreate)              \
134     F_V2(cuCtxDestroy)             \
135     F(cuModuleLoadData)            \
136     F(cuModuleLoadDataEx)          \
137     F(cuModuleUnload)              \
138     F(cuModuleGetFunction)         \
139     F(cuStreamCreate)              \
140     F_V2(cuStreamDestroy)          \
141     F_V2(cuMemAlloc)               \
142     F_V2(cuMemFree)                \
143     F_V2(cuMemcpyHtoD)             \
144     F_V2(cuMemcpyHtoDAsync)        \
145     F_V2(cuMemcpyDtoH)             \
146     F(cuCtxSetCurrent)             \
147     F(cuStreamSynchronize)         \
148     F_V2(cuCtxPushCurrent)         \
149     F_V2(cuCtxPopCurrent)          \
150     F(cuLaunchKernel)              \
151     F(cuCtxSetCacheConfig)         \
152     F(cuFuncSetSharedMemConfig)    \
153     F(cuCtxSetSharedMemConfig)     \
154     F(cuDeviceGetAttribute)        \
155     F(cuProfilerStart)             \
156 
157 
158 #define CUDA_PROTOTYPE(name) \
159     extern t##name name;
160 
161 FOR_EACH_CUDA_FUNC(CUDA_PROTOTYPE,CUDA_PROTOTYPE)
162 
163 #endif
164