29package com.jogamp.opencl;
31import java.util.Random;
33import com.jogamp.common.nio.PointerBuffer;
34import com.jogamp.opencl.llb.impl.BuildProgramCallback;
35import com.jogamp.opencl.llb.impl.CLImpl12;
36import com.jogamp.opencl.llb.impl.CLImpl20;
37import com.jogamp.opencl.llb.CL;
38import com.jogamp.opencl.test.util.MiscUtils;
39import com.jogamp.opencl.test.util.UITestCase;
41import java.io.IOException;
42import java.nio.ByteBuffer;
43import java.nio.IntBuffer;
44import java.util.ArrayList;
46import java.util.concurrent.Callable;
47import java.util.concurrent.CountDownLatch;
48import java.util.concurrent.ExecutionException;
49import java.util.concurrent.ExecutorService;
50import java.util.concurrent.Executors;
51import java.util.concurrent.Future;
52import java.util.concurrent.TimeUnit;
54import org.junit.BeforeClass;
55import org.junit.FixMethodOrder;
57import org.junit.runners.MethodSorters;
59import static java.lang.System.*;
60import static org.junit.Assert.*;
61import static com.jogamp.common.nio.Buffers.*;
62import static com.jogamp.common.os.Platform.*;
63import static com.jogamp.opencl.test.util.MiscUtils.*;
64import static com.jogamp.opencl.util.CLUtil.*;
70@FixMethodOrder(MethodSorters.NAME_ASCENDING)
73 private final static String programSource =
74 " // OpenCL Kernel Function for element by element vector addition \n"
75 +
"kernel void VectorAdd(global const int* a, global const int* b, global int* c, int iNumElements) { \n"
76 +
" // get index in global data array \n"
77 +
" int iGID = get_global_id(0); \n"
78 +
" // bound check (equivalent to the limit on a 'for' loop for standard/serial C code \n"
79 +
" if (iGID >= iNumElements) { \n"
82 +
" // add the vector elements \n"
83 +
" c[iGID] = a[iGID] + b[iGID]; \n"
85 +
"kernel void Test(global const int* a, global const int* b, global int* c, int iNumElements) { \n"
86 +
" // get index in global data array \n"
87 +
" int iGID = get_global_id(0); \n"
88 +
" // bound check (equivalent to the limit on a 'for' loop for standard/serial C code \n"
89 +
" if (iGID >= iNumElements) { \n"
92 +
" c[iGID] = iGID; \n"
95 private int ELEMENT_COUNT = 11444777;
100 out.println(
"OS: " + System.getProperty(
"os.name"));
101 out.println(
"VM: " + System.getProperty(
"java.vm.name"));
107 out.println(
" - - - lowLevelTest; contextless binding - - - ");
116 final IntBuffer intBuffer = newDirectIntBuffer(1);
120 out.println(
"#platforms: "+intBuffer.get(0));
122 final PointerBuffer platformId = PointerBuffer.allocateDirect(intBuffer.get(0));
127 final PointerBuffer longBuffer = PointerBuffer.allocateDirect(1);
128 final ByteBuffer bb = newDirectByteBuffer(128);
130 for (
int i = 0; i < platformId.capacity(); i++) {
132 final long platform = platformId.get(i);
133 out.println(
"platform id: "+platform);
137 out.println(
" profile: " + clString2JavaString(bb, (
int)longBuffer.get(0)));
141 out.println(
" version: " + clString2JavaString(bb, (
int)longBuffer.get(0)));
145 out.println(
" name: " + clString2JavaString(bb, (
int)longBuffer.get(0)));
149 out.println(
" vendor: " + clString2JavaString(bb, (
int)longBuffer.get(0)));
154 out.println(
"#devices: "+intBuffer.get(0));
156 final PointerBuffer devices = PointerBuffer.allocateDirect(intBuffer.get(0));
160 for (
int j = 0; j < devices.capacity(); j++) {
161 final long device = devices.get(j);
164 out.println(
" device: " + clString2JavaString(bb, (
int)longBuffer.get(0)));
168 if(deviceInterface instanceof
CLImpl12)
169 out.println(
" CL impl: 1.2");
170 else if(deviceInterface instanceof
CLImpl20)
171 out.println(
" CL impl: 2.0");
173 out.println(
" CL impl: 1.1");
188 out.println(
" - - - createContextTest - - - ");
192 final IntBuffer intBuffer = newDirectIntBuffer(1);
196 out.println(
"#platforms: "+intBuffer.get(0));
198 final PointerBuffer pb = PointerBuffer.allocateDirect(intBuffer.get(0));
202 final long platform = pb.get(0);
207 out.println(
"#devices: "+intBuffer.get(0));
209 final PointerBuffer devices = PointerBuffer.allocateDirect(intBuffer.get(0));
212 final long context = cl.
clCreateContext(
null, devices,
null, intBuffer);
213 checkError(
"on clCreateContext", intBuffer.get());
216 final PointerBuffer longBuffer = PointerBuffer.allocateDirect(1);
218 checkError(
"on clGetContextInfo", ret);
220 final long contextDevices = longBuffer.get(0)/(is32Bit()?4:8);
221 out.println(
"context created on " + contextDevices +
" devices");
224 assertEquals(
"context was not created on all devices specified", devices.capacity(), contextDevices);
227 checkError(
"on clReleaseContext", ret);
233 out.println(
" - - - lowLevelTest2; VectorAdd kernel - - - ");
238 final IntBuffer intBuffer = newDirectIntBuffer(1);
243 assertTrue(intBuffer.get(0) > 0);
245 final PointerBuffer pb = PointerBuffer.allocateDirect(intBuffer.get(0));
249 final long platform = pb.get(0);
251 .put(platform).put(0)
254 out.println(
"context handle: "+context);
255 checkError(
"on clCreateContextFromType", ret);
257 final PointerBuffer longBuffer = PointerBuffer.allocateDirect(1);
259 checkError(
"on clGetContextInfo", ret);
261 final int deviceCount = (int) (longBuffer.get(0) / (is32Bit() ? 4 : 8));
262 out.println(
"context created with " + deviceCount +
" devices");
267 final ByteBuffer bb = newDirectByteBuffer(32768);
269 checkError(
"on clGetContextInfo", ret);
271 for (
int i = 0; i < deviceCount; i++) {
272 out.println(
"device id: " + (is32Bit()?bb.getInt():bb.getLong()));
276 int offset =
new Random().nextInt(deviceCount);
277 out.println(
"using device# " + offset);
278 offset *= (is32Bit() ? 4 : 8);
279 final long device = is32Bit()?bb.getInt(offset):bb.getLong(offset);
283 checkError(
"on clGetDeviceInfo", ret);
284 final int maxWGS = bb.getInt();
285 out.println(
"max WGS: " + maxWGS);
289 checkError(
"on clCreateCommandQueue", intBuffer.get(0));
291 final int localWorkSize = Math.min(128, maxWGS);
292 int elementCount = ELEMENT_COUNT;
293 int globalWorkSize = 0;
295 ByteBuffer srcA =
null;
296 ByteBuffer srcB =
null;
297 ByteBuffer dest =
null;
298 boolean allocated =
false;
300 while( !allocated ) {
303 globalWorkSize = roundUp(localWorkSize, elementCount);
304 out.println(
"allocating three buffers of size: "+globalWorkSize);
305 srcA = newDirectByteBuffer(globalWorkSize*SIZEOF_INT);
306 srcB = newDirectByteBuffer(globalWorkSize*SIZEOF_INT);
307 dest = newDirectByteBuffer(globalWorkSize*SIZEOF_INT);
310 catch(
final OutOfMemoryError oome ) {
312 elementCount /= divisor;
313 out.println(
"not enough direct buffer memory; retrying with smaller buffers");
319 checkError(
"on clCreateBuffer", intBuffer.get(0));
321 checkError(
"on clCreateBuffer", intBuffer.get(0));
323 checkError(
"on clCreateBuffer", intBuffer.get(0));
327 final PointerBuffer lengths = PointerBuffer.allocateDirect(1).put(programSource.length()).rewind();
329 out.println(
"program id: "+program);
330 checkError(
"on clCreateProgramWithSource", intBuffer.get(0));
333 final CountDownLatch latch =
new CountDownLatch(1);
336 public void buildFinished(
final long cl_program) {
338 assertEquals(program, cl_program);
349 checkError(
"on clBuildProgram", ret);
351 out.println(
"waiting for program to build...");
359 checkError(
"on clGetProgramInfo1", ret);
360 out.println(
"program associated with "+bb.getInt(0)+
" device(s)");
363 checkError(
"on clGetProgramInfo CL_PROGRAM_SOURCE", ret);
364 out.println(
"program source length (cl): "+longBuffer.get(0));
365 out.println(
"program source length (java): "+programSource.length());
369 checkError(
"on clGetProgramInfo CL_PROGRAM_SOURCE", ret);
370 out.println(
"program source:\n" + clString2JavaString(bb, (
int)longBuffer.get(0)));
375 checkError(
"on clGetProgramBuildInfo1", ret);
382 checkError(
"on clGetProgramBuildInfo2", ret);
383 out.println(
"program log length: " + longBuffer.get(0));
387 checkError(
"on clGetProgramBuildInfo3", ret);
388 out.println(
"log:\n" + clString2JavaString(bb, (
int)longBuffer.get(0)));
391 final long kernel = cl.
clCreateKernel(program,
"VectorAdd", intBuffer);
392 out.println(
"kernel id: "+kernel);
393 checkError(
"on clCreateKernel", intBuffer.get(0));
398 fillBuffer(srcA, 23456);
399 fillBuffer(srcB, 46987);
402 ret = cl.
clSetKernelArg(kernel, 0, is32Bit()?SIZEOF_INT:SIZEOF_LONG, wrap(devSrcA)); checkError(
"on clSetKernelArg0", ret);
403 ret = cl.
clSetKernelArg(kernel, 1, is32Bit()?SIZEOF_INT:SIZEOF_LONG, wrap(devSrcB)); checkError(
"on clSetKernelArg1", ret);
404 ret = cl.
clSetKernelArg(kernel, 2, is32Bit()?SIZEOF_INT:SIZEOF_LONG, wrap(devDst)); checkError(
"on clSetKernelArg2", ret);
405 ret = cl.
clSetKernelArg(kernel, 3, SIZEOF_INT, wrap(elementCount)); checkError(
"on clSetKernelArg3", ret);
407 out.println(
"used device memory: "+ (srcA.capacity()+srcB.capacity()+dest.capacity())/1000000 +
"MB");
411 checkError(
"on clEnqueueWriteBuffer", ret);
413 checkError(
"on clEnqueueWriteBuffer", ret);
416 final PointerBuffer gWS = PointerBuffer.allocateDirect(1).put(globalWorkSize).rewind();
417 final PointerBuffer lWS = PointerBuffer.allocateDirect(1).put(localWorkSize).rewind();
419 checkError(
"on clEnqueueNDRangeKernel", ret);
423 checkError(
"on clEnqueueReadBuffer", ret);
425 out.println(
"a+b=c result snapshot: ");
426 for(
int i = 0; i < 10; i++)
427 out.print(dest.getInt()+
", ");
428 out.println(
"...; "+dest.remaining()/SIZEOF_INT +
" more");
433 checkError(
"on clReleaseCommandQueue", ret);
436 checkError(
"on clReleaseMemObject", ret);
438 checkError(
"on clReleaseMemObject", ret);
440 checkError(
"on clReleaseMemObject", ret);
443 checkError(
"on clReleaseProgram", ret);
446 checkError(
"on clReleaseKernel", ret);
452 checkError(
"on clReleaseContext", ret);
457 public void loadTest() throws InterruptedException {
459 out.println(
" - - - loadTest - - - ");
461 final ExecutorService pool = Executors.newFixedThreadPool(8);
462 final List<Callable<Object>> tasks =
new ArrayList<Callable<Object>>();
464 for(
int i = 0; i < 100; i++) {
466 tasks.add(
new Callable<Object>() {
468 public Object call() {
470 out.println(
"###start iteration " + n);
472 test.ELEMENT_COUNT = 123456;
474 out.println(
"###end iteration " + n);
475 }
catch (
final InterruptedException ex) {
476 throw new RuntimeException(ex);
483 for (
final Future<Object> future : pool.invokeAll(tasks)) {
486 }
catch (
final ExecutionException ex) {
487 ex.printStackTrace();
493 pool.awaitTermination(300, TimeUnit.SECONDS);
496 private ByteBuffer wrap(
final long value) {
497 return (ByteBuffer) newDirectByteBuffer(8).putLong(value).rewind();
500 private void checkForError(
final int ret) {
501 this.checkError(
"", ret);
504 private void checkError(
final String msg,
final int ret) {
505 if(ret != CL.CL_SUCCESS)
506 throw CLException.newException(ret, msg);
509 public static void main(
final String[] args)
throws IOException {
511 org.junit.runner.JUnitCore.
main(tstname);
This object represents an OpenCL device.
Represents a OpenCL program executed on one or more CLDevices.
Test testing the low level bindings.
static void main(final String[] args)
void lowLevelVectorAddTest()
Java bindings to OpenCL, the Open Computing Language (generated).
Java bindings to OpenCL, the Open Computing Language (generated).
Enumeration for the type of a device.
static Type valueOf(final long clDeviceType)
static Status valueOf(final int clBuildStatus)
Java bindings to OpenCL, the Open Computing Language.
long clCreateKernel(long program, String kernel_name, IntBuffer errcode_ret)
Interface to C language function: cl_kernel {@native clCreateKernel}(cl_program program,...
int clBuildProgram(long program, int deviceCount, PointerBuffer devices, String options, BuildProgramCallback cb)
Interface to C language function: int32_t {@native clBuildProgram}(cl_program, uint32_t,...
static final int CL_PROGRAM_NUM_DEVICES
Define "CL_PROGRAM_NUM_DEVICES" with expression '0x1162', CType: int.
int clGetDeviceIDs(long platform, long device_type, int num_entries, PointerBuffer devices, IntBuffer num_devices)
Interface to C language function: cl_int {@native clGetDeviceIDs}(cl_platform_id platform,...
static final int CL_PLATFORM_VERSION
Define "CL_PLATFORM_VERSION" with expression '0x0901', CType: int.
static final int CL_PROGRAM_BUILD_STATUS
Define "CL_PROGRAM_BUILD_STATUS" with expression '0x1181', CType: int.
int clGetProgramInfo(long program, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetProgramInfo}(cl_program program,...
static final int CL_DEVICE_NAME
Define "CL_DEVICE_NAME" with expression '0x102B', CType: int.
int clReleaseKernel(long kernel)
Interface to C language function: cl_int {@native clReleaseKernel}(cl_kernel kernel)
long clCreateBuffer(long context, long flags, long size, Buffer host_ptr, IntBuffer errcode_ret)
Interface to C language function: cl_mem {@native clCreateBuffer}(cl_context context,...
static final int CL_PROGRAM_SOURCE
Define "CL_PROGRAM_SOURCE" with expression '0x1164', CType: int.
int clReleaseContext(long context)
Interface to C language function: cl_int {@native clReleaseContext}(cl_context context)
static final int CL_DEVICE_TYPE
Define "CL_DEVICE_TYPE" with expression '0x1000', CType: int.
static final int CL_DEVICE_MAX_WORK_GROUP_SIZE
Define "CL_DEVICE_MAX_WORK_GROUP_SIZE" with expression '0x1004', CType: int.
int clGetContextInfo(long context, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetContextInfo}(cl_context context,...
int clGetPlatformIDs(int num_entries, PointerBuffer platforms, IntBuffer num_platforms)
Interface to C language function: cl_int {@native clGetPlatformIDs}(cl_uint num_entries,...
static final int CL_PLATFORM_NAME
Define "CL_PLATFORM_NAME" with expression '0x0902', CType: int.
static final int CL_CONTEXT_DEVICES
Define "CL_CONTEXT_DEVICES" with expression '0x1081', CType: int.
int clGetDeviceInfo(long device, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetDeviceInfo}(cl_device_id device,...
int clEnqueueWriteBuffer(long command_queue, long buffer, int blocking_write, long offset, long cb, Buffer ptr, int num_events_in_wait_list, PointerBuffer event_wait_list, PointerBuffer event)
Interface to C language function: cl_int {@native clEnqueueWriteBuffer}(cl_command_queue command_qu...
static final int CL_MEM_READ_ONLY
Define "CL_MEM_READ_ONLY" with expression '(1 << 2)', CType: int.
static final int CL_TRUE
Define "CL_TRUE" with expression '1', CType: int.
int clReleaseMemObject(long memobj)
Interface to C language function: cl_int {@native clReleaseMemObject}(cl_mem memobj)
static final int CL_FALSE
Define "CL_FALSE" with expression '0', CType: int.
static final int CL_PROGRAM_BUILD_LOG
Define "CL_PROGRAM_BUILD_LOG" with expression '0x1183', CType: int.
int clSetKernelArg(long kernel, int arg_index, long arg_size, Buffer arg_value)
Interface to C language function: cl_int {@native clSetKernelArg}(cl_kernel kernel,...
int clGetProgramBuildInfo(long program, long device, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetProgramBuildInfo}(cl_program program,...
int clReleaseProgram(long program)
Interface to C language function: cl_int {@native clReleaseProgram}(cl_program program)
static final int CL_PLATFORM_PROFILE
Define "CL_PLATFORM_PROFILE" with expression '0x0900', CType: int.
int clEnqueueReadBuffer(long command_queue, long buffer, int blocking_read, long offset, long cb, Buffer ptr, int num_events_in_wait_list, PointerBuffer event_wait_list, PointerBuffer event)
Interface to C language function: cl_int {@native clEnqueueReadBuffer}(cl_command_queue command_que...
static final int CL_SUCCESS
Define "CL_SUCCESS" with expression '0', CType: int.
int clReleaseCommandQueue(long command_queue)
Interface to C language function: cl_int {@native clReleaseCommandQueue}(cl_command_queue command_q...
static final long CL_DEVICE_TYPE_ALL
Define "CL_DEVICE_TYPE_ALL" with expression '0xFFFFFFFF', CType: long.
int clGetPlatformInfo(long platform, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetPlatformInfo}(cl_platform_id platform,...
long clCreateContextFromType(PointerBuffer properties, long device_type, CLErrorHandler pfn_notify, IntBuffer errcode_ret)
Interface to C language function: cl_context {@native clCreateContextFromType}(cl_context_properti...
long clCreateProgramWithSource(long context, int count, String[] strings, PointerBuffer lengths, IntBuffer errcode_ret)
Interface to C language function: cl_program {@native clCreateProgramWithSource}(cl_context context...
static final int CL_BUILD_SUCCESS
Define "CL_BUILD_SUCCESS" with expression '0', CType: int.
int clEnqueueNDRangeKernel(long command_queue, long kernel, int work_dim, PointerBuffer global_work_offset, PointerBuffer global_work_size, PointerBuffer local_work_size, int num_events_in_wait_list, PointerBuffer event_wait_list, PointerBuffer event)
Interface to C language function: cl_int {@native clEnqueueNDRangeKernel}(cl_command_queue command_...
static final int CL_MEM_WRITE_ONLY
Define "CL_MEM_WRITE_ONLY" with expression '(1 << 1)', CType: int.
long clCreateCommandQueue(long context, long device, long properties, IntBuffer errcode_ret)
Interface to C language function: cl_command_queue {@native clCreateCommandQueue}(cl_context contex...
static final int CL_CONTEXT_PLATFORM
Define "CL_CONTEXT_PLATFORM" with expression '0x1084', CType: int.
static final int CL_PLATFORM_VENDOR
Define "CL_PLATFORM_VENDOR" with expression '0x0903', CType: int.
long clCreateContext(PointerBuffer properties, PointerBuffer devices, CLErrorHandler pfn_notify, IntBuffer errcode_ret)
Interface to C language function: cl_context {@native clCreateContext}(intptr_t * ,...
A callback an application can register to be called when the program executable has been built (succe...