/
HelloCUDA.java
214 lines (171 loc) · 7.08 KB
/
HelloCUDA.java
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
package org.lwjgl.demo.cuda;
import org.lwjgl.*;
import org.lwjgl.cuda.*;
import org.lwjgl.system.*;
import java.nio.*;
import static org.lwjgl.cuda.CU.*;
import static org.lwjgl.cuda.NVRTC.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
/** Simple CUDA demo. */
public final class HelloCUDA {
private HelloCUDA() {}
private static final int ARRAY_SIZE = 100;
private static final String KERNEL_CU =
"#define N " + ARRAY_SIZE + "\n" +
"\n" +
"extern \"C\" __global__ void matSum(int *a, int *b, int *c)\n" +
"{\n" +
" int tid = blockIdx.x;\n" +
" if (tid < N)\n" +
" c[tid] = a[tid] + b[tid];\n" +
"}\n";
private static final String KERNEL_NAME = "matSum";
private static long ctx;
public static void main(String[] args) {
ByteBuffer PTX;
try (MemoryStack stack = stackPush()) {
IntBuffer major = stack.mallocInt(1);
IntBuffer minor = stack.mallocInt(1);
checkNVRTC(nvrtcVersion(major, minor));
System.out.println("Compiling kernel with NVRTC v" + major.get(0) + "." + minor.get(0));
PointerBuffer pp = stack.mallocPointer(1);
checkNVRTC(nvrtcCreateProgram(pp, KERNEL_CU, "matSum.cu", null, null));
long program = pp.get(0);
int compilationStatus = nvrtcCompileProgram(program, null);
{
checkNVRTC(nvrtcGetProgramLogSize(program, pp));
if (1L < pp.get(0)) {
ByteBuffer log = stack.malloc((int)pp.get(0) - 1);
checkNVRTC(nvrtcGetProgramLog(program, log));
System.err.println("Compilation log:");
System.err.println("----------------");
System.err.println(memASCII(log));
}
}
checkNVRTC(compilationStatus);
checkNVRTC(nvrtcGetPTXSize(program, pp));
PTX = memAlloc((int)pp.get(0));
checkNVRTC(nvrtcGetPTX(program, PTX));
System.out.println("\nCompiled PTX:");
System.out.println("-------------");
System.out.println(memASCII(PTX));
}
IntBuffer hostA = memAllocInt(ARRAY_SIZE);
IntBuffer hostB = memAllocInt(ARRAY_SIZE);
IntBuffer hostC = memAllocInt(ARRAY_SIZE);
// initialize host arrays
for (int i = 0; i < ARRAY_SIZE; ++i) {
hostA.put(i, ARRAY_SIZE - i);
hostB.put(i, i * i);
}
long
deviceA,
deviceB,
deviceC;
try (MemoryStack stack = stackPush()) {
PointerBuffer pp = stack.mallocPointer(1);
IntBuffer pi = stack.mallocInt(1);
// initialize
if (CUDA.isPerThreadDefaultStreamSupported()) {
Configuration.CUDA_API_PER_THREAD_DEFAULT_STREAM.set(true);
}
System.out.format("- Initializing...\n");
check(cuInit(0));
check(cuDeviceGetCount(pi));
if (pi.get(0) == 0) {
throw new IllegalStateException("Error: no devices supporting CUDA");
}
// get first CUDA device
check(cuDeviceGet(pi, 0));
int device = pi.get(0);
// get device name
ByteBuffer pb = stack.malloc(100);
check(cuDeviceGetName(pb, device));
System.out.format("> Using device 0: %s\n", memASCII(memAddress(pb)));
// get compute capabilities and the device name
IntBuffer minor = stack.mallocInt(1);
check(cuDeviceComputeCapability(pi, minor, device));
System.out.format("> GPU Device has SM %d.%d compute capability\n", pi.get(0), minor.get(0));
// get memory size
check(cuDeviceTotalMem(pp, device));
System.out.format(" Total amount of global memory: %d bytes\n", pp.get(0));
System.out.format(" 64-bit Memory Address: %s\n", (pp.get(0) > 4 * 1024 * 1024 * 1024L) ? "YES" : "NO");
// create context
check(cuCtxCreate(pp, 0, device));
ctx = pp.get(0);
// load kernel
check(cuModuleLoadData(pp, PTX));
long module = pp.get(0);
check(cuModuleGetFunction(pp, module, KERNEL_NAME));
long function = pp.get(0);
// allocate memory
check(cuMemAlloc(pp, Integer.BYTES * ARRAY_SIZE));
deviceA = pp.get(0);
check(cuMemAlloc(pp, Integer.BYTES * ARRAY_SIZE));
deviceB = pp.get(0);
check(cuMemAlloc(pp, Integer.BYTES * ARRAY_SIZE));
deviceC = pp.get(0);
// copy arrays to device
check(cuMemcpyHtoD(deviceA, hostA));
check(cuMemcpyHtoD(deviceB, hostB));
// run
System.out.format("# Running the kernel...\n");
// grid for kernel: <<<N, 1>>>
check(cuLaunchKernel(
function, ARRAY_SIZE, 1, 1, // Nx1x1 blocks
1, 1, 1, // 1x1x1 threads
0, 0,
// method 1: unpacked (simple, no alignment requirements)
stack.pointers(
memAddress(stack.longs(deviceA)),
memAddress(stack.longs(deviceB)),
memAddress(stack.longs(deviceC))
),
null/*,
// method 2: packed (user is responsible for correct argument alignment)
stack.pointers(
CU_LAUNCH_PARAM_BUFFER_POINTER, memAddress(stack.longs(
deviceA,
deviceB,
deviceC
)),
CU_LAUNCH_PARAM_BUFFER_SIZE, memAddress(stack.pointers(3 * Long.BYTES)),
CU_LAUNCH_PARAM_END
)*/));
}
System.out.format("# Kernel complete.\n");
// copy results to host and report
check(cuMemcpyDtoH(hostC, deviceC));
for (int i = 0; i < ARRAY_SIZE; ++i) {
if (hostC.get(i) != hostA.get(i) + hostB.get(i)) {
System.out.format("* Error at array position %d: Expected %d, Got %d\n", i, hostA.get(i) + hostB.get(i), hostC.get(i));
}
}
System.out.format("*** All checks complete.\n");
// finish
System.out.format("- Finalizing...\n");
check(cuMemFree(deviceA));
check(cuMemFree(deviceB));
check(cuMemFree(deviceC));
check(cuCtxDetach(ctx));
}
private static void checkNVRTC(int err) {
if (err != NVRTC_SUCCESS) {
throw new IllegalStateException(nvrtcGetErrorString(err));
}
}
private static void check(int err) {
if (err != CUDA_SUCCESS) {
if (ctx != NULL) {
cuCtxDetach(ctx);
ctx = NULL;
}
throw new IllegalStateException(Integer.toString(err));
}
}
}