-
Notifications
You must be signed in to change notification settings - Fork 0
/
nabeatsu-rtc.c
177 lines (154 loc) · 4.33 KB
/
nabeatsu-rtc.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <nvrtc.h>
#define KERNEL_CODE \
"extern \"C\" __global__ void nabeatsuKernel(bool* result, int result_len) \n" \
"{ \n" \
" int i = (blockIdx.x * blockDim.x) + threadIdx.x; \n" \
\
" if (i < result_len) { \n" \
" int val = i + 1; \n" \
" result[i] = (val % 3) == 0; \n" \
\
" while (val > 0) { \n" \
" result[i] |= (val % 10) == 3; \n" \
" val /= 10; \n" \
" } \n" \
" } \n" \
"}"
void GetInfo(struct cudaDeviceProp *devProp) {
cudaGetDeviceProperties(devProp, 0);
printf("Devuce: %s\n", devProp->name);
printf("Global memory available on device in bytes: %lu\n", devProp->totalGlobalMem);
printf("Shared memory available per block in bytes: %lu\n", devProp->sharedMemPerBlock);
printf("Warp size in threads: %d\n", devProp->warpSize);
printf("Maximum number of threads per block: %d\n", devProp->maxThreadsPerBlock);
printf("Compute capacity: %d.%d\n", devProp->major, devProp->minor);
printf("Clock frequency in kilohertz: %d\n", devProp->clockRate);
printf("Number of multiprocessors on device: %d\n", devProp->multiProcessorCount);
}
char *CreatePTX(){
nvrtcResult result;
nvrtcProgram prog;
size_t ptx_size;
char *ptx;
result = nvrtcCreateProgram(&prog, KERNEL_CODE, NULL, 0, NULL, NULL);
if(result != NVRTC_SUCCESS){
fprintf(stderr, "ERROR: %s:%d: %s\n", __FILE__, __LINE__, nvrtcGetErrorString(result));
return NULL;
}
result = nvrtcCompileProgram(prog, 0, NULL);
if(result != NVRTC_SUCCESS){
fprintf(stderr, "ERROR: %s:%d: %s\n", __FILE__, __LINE__, nvrtcGetErrorString(result));
nvrtcDestroyProgram(&prog);
return NULL;
}
nvrtcGetPTXSize(prog, &ptx_size);
ptx = (char *)malloc(ptx_size);
nvrtcGetPTX(prog, ptx);
nvrtcDestroyProgram(&prog);
return ptx;
}
#define PRINT_CU_ERROR(errcode) \
{ \
const char *str, *desc; \
cuGetErrorName(cu_result, &str); \
cuGetErrorString(cu_result, &desc); \
fprintf(stderr, "ERROR: %s:%d: %s (%s)\n", __FILE__, __LINE__, str, desc); \
}
CUresult InitCtx(CUcontext *ctx){
CUresult cu_result;
CUdevice device;
cu_result = cuInit(0);
if(cu_result != CUDA_SUCCESS){
PRINT_CU_ERROR(cu_result)
}
else{
cu_result = cuDeviceGet(&device, 0);
if(cu_result != CUDA_SUCCESS){
PRINT_CU_ERROR(cu_result)
}
else{
cu_result = cuCtxCreate(ctx, 0, device);
if(cu_result != CUDA_SUCCESS){
PRINT_CU_ERROR(cu_result)
}
}
}
return cu_result;
}
CUresult GetKernelFunc(CUmodule *module, CUfunction *func, const char *ptx){
CUresult cu_result;
cu_result = cuModuleLoadData(module, ptx);
if(cu_result != CUDA_SUCCESS){
PRINT_CU_ERROR(cu_result)
}
else{
cu_result = cuModuleGetFunction(func, *module, "nabeatsuKernel");
if(cu_result != CUDA_SUCCESS){
PRINT_CU_ERROR(cu_result)
}
}
return cu_result;
}
int main(int argc, char *argv[]){
int len;
int blocks;
int threads;
struct cudaDeviceProp devProp;
char *ptx;
CUresult cu_result;
CUcontext context;
CUmodule module;
CUfunction func;
CUdeviceptr dev_result;
unsigned char *result;
int i;
len = atoi(argv[1]);
cu_result = InitCtx(&context);
if(cu_result != CUDA_SUCCESS){
return -1;
}
GetInfo(&devProp);
printf("\n");
ptx = CreatePTX();
if(ptx == NULL){
return -1;
}
cu_result = GetKernelFunc(&module, &func, ptx);
free(ptx);
if(cu_result != CUDA_SUCCESS){
cuModuleUnload(module); // it will fail when cuModuleLoadData() fails.
cuCtxDestroy(context);
return -1;
}
cu_result = cuMemAlloc(&dev_result, len);
if(cu_result == CUDA_SUCCESS){
threads = devProp.maxThreadsPerBlock;
blocks = (len / threads) + 1;
{
void *args[] = {&dev_result, &len};
cu_result = cuLaunchKernel(func, blocks, 1, 1, threads, 1, 1, 0, NULL, args, NULL);;
if(cu_result == CUDA_SUCCESS){
result = (unsigned char *)malloc(len);
cuMemcpyDtoH(result, dev_result, len);
for(i = 0; i < len; i++){
printf("%d%s\n", i + 1, result[i] == 1 ? " [aho]" : "");
}
free(result);
}
else{
PRINT_CU_ERROR(cu_result)
}
}
cuMemFree(dev_result);
}
else{
PRINT_CU_ERROR(cu_result)
}
cuModuleUnload(module);
cuCtxDestroy(context);
return cu_result;
}