14 import java.math.BigInteger;
16 import java.nio.file.Files;
17 import java.nio.file.Paths;
20 import static org.jocl.CL.*;
59 assert assertsEnabled =
true;
68 static String
fileToString(
final String filename)
throws Exception {
69 return (
"#line 1 \"" + filename +
"\"\n" 70 +
new String(Files.readAllBytes(Paths.get(filename))));
78 static cl_device_id
getDeviceId(
int platformI,
int deviceI)
throws Exception {
83 final int[] platformNbArray =
new int[1];
85 clGetPlatformIDs(0, null, platformNbArray);
87 final int platformNb = platformNbArray[0];
89 if (platformI >= platformNb) {
90 throw new Exception(
"Wrong platformI: " + platformI);
94 final cl_platform_id[] platformIds =
new cl_platform_id[platformNb];
96 clGetPlatformIDs(platformIds.length, platformIds, null);
99 final cl_platform_id platformId = platformIds[platformI];
102 final int[] deviceNbArray =
new int[1];
104 clGetDeviceIDs(platformId, CL_DEVICE_TYPE_ALL, 0, null, deviceNbArray);
106 final int deviceNb = deviceNbArray[0];
108 if (deviceI >= deviceNb) {
109 throw new Exception(
"Wrong deviceI: " + deviceI);
113 final cl_device_id[] deviceIds =
new cl_device_id[deviceNb];
115 clGetDeviceIDs(platformId, CL_DEVICE_TYPE_ALL, deviceNb, deviceIds, null);
118 return deviceIds[deviceI];
127 final long[] size =
new long[1];
129 clGetDeviceInfo(deviceId, paramName, 0, null, size);
132 final byte[] buffer =
new byte[(int) size[0]];
134 clGetDeviceInfo(deviceId, paramName, buffer.length, Pointer.to(buffer), null);
136 return new String(buffer, 0, buffer.length - 1);
147 static void runExample(
int nbWorkGroup,
int nbWorkItemsByWorkGroup,
148 cl_device_id deviceId,
boolean debug)
throws Exception {
150 final int[] hOuts =
new int[2];
151 final int hOutsByteSize = INT_FIELD_SIZE * 2;
153 final long[] hAsserts = {0, 0};
154 final int hAssertsByteSize = LONG_FIELD_SIZE * 2;
156 final float[] hAssertFloat = {0};
161 final cl_device_id[] devicesIds =
new cl_device_id[]{deviceId};
162 final cl_context context = clCreateContext(null, 1, devicesIds, null, null, null);
166 String options =
"-I../../OpenCL/";
169 System.err.println(
"OpenCL in DEBUG mode!");
172 options +=
" -DNDEBUG";
175 final String kernelFilename =
"../kernel/example.cl";
177 final cl_program program = clCreateProgramWithSource(context,
178 1,
new String[]{kernelSrc},
181 clBuildProgram(program, 1, devicesIds, options, null, null);
183 final cl_kernel kernel = clCreateKernel(program,
"example", null);
187 final cl_command_queue queue = clCreateCommandQueue(context, deviceId,
188 CL_QUEUE_PROFILING_ENABLE, null);
192 final cl_mem dOuts = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
193 hOutsByteSize, Pointer.to(hOuts), null);
195 cl_mem dAsserts = null;
196 cl_mem dAssertFloat = null;
199 dAsserts = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
200 hAssertsByteSize, Pointer.to(hAsserts), null);
201 dAssertFloat = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
202 hAssertFloatByteSize, Pointer.to(hAssertFloat), null);
207 clSetKernelArg(kernel, 0, Sizeof.cl_uint, Pointer.to(
new int[]{666}));
208 clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to(dOuts));
211 final int nbArgs = 2;
213 clSetKernelArg(kernel, nbArgs, Sizeof.cl_mem, Pointer.to(dAsserts));
214 clSetKernelArg(kernel, nbArgs + 1, Sizeof.cl_mem, Pointer.to(dAssertFloat));
219 final int globalSize = nbWorkItemsByWorkGroup * nbWorkGroup;
221 System.out.println(
"===== run kernel =====");
224 clEnqueueNDRangeKernel(queue, kernel,
227 new long[]{globalSize},
228 new long[]{nbWorkItemsByWorkGroup},
237 System.out.println(
"===== end kernel =====");
242 clEnqueueReadBuffer(queue, dOuts, CL_TRUE, 0,
243 hOutsByteSize, Pointer.to(hOuts), 0, null, null);
246 clEnqueueReadBuffer(queue, dAsserts, CL_TRUE, 0,
247 hAssertsByteSize, Pointer.to(hAsserts), 0, null, null);
248 clEnqueueReadBuffer(queue, dAssertFloat, CL_TRUE, 0,
249 hAssertFloatByteSize, Pointer.to(hAssertFloat), 0, null, null);
251 final long line = hAsserts[0];
254 final BigInteger uint64Value =
uint64(hAsserts[1]);
255 final long sint64Value = hAsserts[1];
256 final float floatValue = hAssertFloat[0];
258 System.err.println(String.format(
"%s:%d\tAssertion failed | Maybe\t%s\t%d | Maybe\t%f",
259 kernelFilename, line,
260 uint64Value, sint64Value, floatValue));
269 System.out.println(String.format(
"Results: (%d, %d)", hOuts[0], hOuts[1]));
273 clReleaseMemObject(dOuts);
275 clReleaseMemObject(dAsserts);
276 clReleaseMemObject(dAssertFloat);
279 clReleaseCommandQueue(queue);
280 clReleaseProgram(program);
281 clReleaseKernel(kernel);
282 clReleaseContext(context);
300 final Long longN = n;
301 final BigInteger bigN =
new BigInteger(longN.toString());
305 :
new BigInteger(
"18446744073709551616").add(bigN));
314 public static void main(String[] args)
throws Exception {
315 CL.setExceptionsEnabled(
true);
326 while (i < args.length) {
327 final String arg = args[i];
329 if (
"--debug".equals(arg) ||
"--ndebug".equals(arg)) {
330 debug =
"--debug".equals(arg);
331 }
else if (
"--device".equals(arg)) {
334 if (i >= args.length) {
335 throw new Exception(
"Missing parameter");
338 final String[] bothI = args[i].split(
":");
340 platformI = Integer.parseInt(bothI[0]);
341 if (bothI.length >= 2) {
342 deviceI = Integer.parseInt(bothI[1]);
345 if ((platformI < 0) || (deviceI < 0)) {
346 throw new Exception(
"Wrong parameter: " + args[i]);
354 final cl_device_id deviceId =
getDeviceId(platformI, deviceI);
357 System.out.println(String.format(
"Device %d:%d %s",
static cl_device_id getDeviceId(int platformI, int deviceI)
Return the given id device of the given platform OpenCL, or exit if doesn't exists.
static final int LONG_FIELD_SIZE
Number of bytes for the long type.
static final int FLOAT_FIELD_SIZE
Number of bytes for the float type.
static boolean assertsEnabled
True iff assertions are enabled.
static String fileToString(final String filename)
Read the file and return its content to a string. If failed then throw an exception.
static BigInteger uint64(long n)
Return in a BigInteger the value of n considered as an unsigned integer on 64 bits.
static final int INT_FIELD_SIZE
Number of bytes for the int type.
static String getDeviceInfoString(cl_device_id deviceId, int paramName)
Return a string corresponding to device info parameter.
#define assert(test)
If test is true then do nothing. Else init (if they are still null) assert_result and assert_result_f...
static void runExample(int nbWorkGroup, int nbWorkItemsByWorkGroup, cl_device_id deviceId, boolean debug)
Run the kernel ../kernel/example.cl.
static long uint32(int n)
Return in a long the value of n considered as an unsigned integer on 32 bits.
static void main(String[] args)
Get the optional parameter –device platform:device and run the kernel ../kernel/example.cl .
Simple Java example to show how run a kernel with assert*() and PRINT*() macros and test them...