From f129ec5b006062e7a63cd9afbb92057b8faaeef9 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 25 Apr 2010 14:27:17 +0200 Subject: implemented low level BuildProgramCallbacks. - removed userdata arguments from createContext* and buildProgram bindings - updated LowLevelBindingTest --- resources/cl-if.cfg | 6 +-- resources/clImplCustomCode.c | 61 ++++++++++++++++++++---- resources/clImplCustomCode.java | 44 +++++++---------- src/com/jogamp/opencl/BuildProgramCallback.java | 13 ++++- src/com/jogamp/opencl/CLContext.java | 4 +- src/com/jogamp/opencl/CLProgram.java | 2 +- src/com/jogamp/opencl/CreateContextCallback.java | 2 +- test/com/jogamp/opencl/LowLevelBindingTest.java | 27 +++++++++-- 8 files changed, 108 insertions(+), 51 deletions(-) diff --git a/resources/cl-if.cfg b/resources/cl-if.cfg index 7ba340f7..cbf2d9b8 100644 --- a/resources/cl-if.cfg +++ b/resources/cl-if.cfg @@ -20,17 +20,17 @@ Ignore CL_GL_.*|cl.*GL.* Ignore clCreateContext CustomJavaCode CL CustomJavaCode CL /** Interface to C language function:
cl_context {@native clCreateContext}(intptr_t * , uint32_t, cl_device_id * , void (*pfn_notify)(const char *, const void *, size_t, void *), void *, int32_t * ); */ -CustomJavaCode CL public long clCreateContext(PointerBuffer properties, PointerBuffer devices, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret); +CustomJavaCode CL public long clCreateContext(PointerBuffer properties, PointerBuffer devices, CreateContextCallback pfn_notify, IntBuffer errcode_ret); Ignore clCreateContextFromType CustomJavaCode CL CustomJavaCode CL /** Interface to C language function:
cl_context {@native clCreateContextFromType}(cl_context_properties *properties, cl_device_type device_type, void (*pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data), void *user_data, cl_int *errcode_ret) ; */ -CustomJavaCode CL public long clCreateContextFromType(PointerBuffer properties, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret); +CustomJavaCode CL public long clCreateContextFromType(PointerBuffer properties, long device_type, CreateContextCallback pfn_notify, IntBuffer errcode_ret); Ignore clBuildProgram CustomJavaCode CL CustomJavaCode CL /** Interface to C language function:
int32_t {@native clBuildProgram}(cl_program, uint32_t, cl_device_id * , const char * , void (*pfn_notify)(cl_program, void *user_data), void * ); */ -CustomJavaCode CL public int clBuildProgram(long program, int deviceCount, PointerBuffer devices, String options, BuildProgramCallback cb, Object userData); +CustomJavaCode CL public int clBuildProgram(long program, int deviceCount, PointerBuffer devices, String options, BuildProgramCallback cb); Ignore clEnqueueNativeKernel #TODO.. diff --git a/resources/clImplCustomCode.c b/resources/clImplCustomCode.c index ec4735b3..b24f69ab 100644 --- a/resources/clImplCustomCode.c +++ b/resources/clImplCustomCode.c @@ -7,12 +7,49 @@ void checkStatus(const char* msg, int status) { } */ +JavaVM * jvm; +jmethodID bcb_mid; -/* -void createContextCallback(const char * c, const void * v, size_t s, void * o) { - //TODO + +JNIEXPORT jint JNICALL +JNI_OnLoad(JavaVM * _jvm, void *reserved) { + + JNIEnv *env; + jvm = _jvm; + + if ((*jvm)->GetEnv(_jvm, (void **)&env, JNI_VERSION_1_2)) { + return JNI_ERR; + } + + // throws ClassNotFoundException (or other reflection stuff) + jclass classID = (*env)->FindClass(env, "com/jogamp/opencl/BuildProgramCallback"); + + if (classID != NULL) { + // throws even more reflection Exceptions + // IDs are unique and do not change + bcb_mid = (*env)->GetMethodID(env, classID, "buildProgramCallback", "(J)V"); + } + + return JNI_VERSION_1_2; } -*/ + + +void buildProgramCallback(cl_program id, void * object) { + + JNIEnv *env; + jobject obj = (jobject)object; + + (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL); + + (*env)->CallVoidMethod(env, obj, bcb_mid, (jlong)id); + (*env)->DeleteGlobalRef(env, obj); + +} + +void createContextCallback(const char * errinfo, const void * private_info, size_t cb, void * object) { + printf(" - - - - - - >error info:\n%s", errinfo); +} + /* Java->C glue code: * Java package: com.jogamp.opencl.impl.CLImpl @@ -26,7 +63,7 @@ void createContextCallback(const char * c, const void * v, size_t s, void * o) { */ JNIEXPORT jlong JNICALL Java_com_jogamp_opencl_impl_CLImpl_clCreateContextFromType0(JNIEnv *env, jobject _unused, - jobject props, jint props_byte_offset, jobject device_type, jobject cb, jobject data, jobject errcode, jint errcode_byte_offset) { + jobject props, jint props_byte_offset, jobject device_type, jobject cb, jobject errcode, jint errcode_byte_offset) { cl_context_properties* _props_ptr = NULL; int32_t * _errcode_ptr = NULL; @@ -58,7 +95,7 @@ Java_com_jogamp_opencl_impl_CLImpl_clCreateContextFromType0(JNIEnv *env, jobject */ JNIEXPORT jlong JNICALL Java_com_jogamp_opencl_impl_CLImpl_clCreateContext0(JNIEnv *env, jobject _unused, - jobject props, jint props_byte_offset, jint numDevices, jobject deviceList, jint device_type_offset, jobject cb, jobject data, jobject errcode, jint errcode_byte_offset) { + jobject props, jint props_byte_offset, jint numDevices, jobject deviceList, jint device_type_offset, jobject cb, jobject errcode, jint errcode_byte_offset) { cl_context_properties* _props_ptr = NULL; int32_t * _errcode_ptr = NULL; @@ -94,11 +131,13 @@ Java_com_jogamp_opencl_impl_CLImpl_clCreateContext0(JNIEnv *env, jobject _unused */ JNIEXPORT jint JNICALL Java_com_jogamp_opencl_impl_CLImpl_clBuildProgram0(JNIEnv *env, jobject _unused, - jlong program, jint deviceCount, jobject deviceList, jint device_type_offset, jstring options, jobject cb, jobject data) { + jlong program, jint deviceCount, jobject deviceList, jint device_type_offset, jstring options, jobject cb) { const char* _strchars_options = NULL; cl_int _res; size_t * _deviceListPtr = NULL; + void (*pfn_notify)(cl_program, void *user_data) = NULL; + jobject globalCB = NULL; if (options != NULL) { _strchars_options = (*env)->GetStringUTFChars(env, options, (jboolean*)NULL); @@ -113,8 +152,12 @@ Java_com_jogamp_opencl_impl_CLImpl_clBuildProgram0(JNIEnv *env, jobject _unused, _deviceListPtr = (void *) (((char*) (*env)->GetDirectBufferAddress(env, deviceList)) + device_type_offset); } - // TODO payload, callback... - _res = clBuildProgram((cl_program)program, (cl_uint)deviceCount, (cl_device_id *)_deviceListPtr, _strchars_options, NULL, NULL); + if (cb != NULL) { + pfn_notify = &buildProgramCallback; + globalCB = (*env)->NewGlobalRef(env, cb); + } + + _res = clBuildProgram((cl_program)program, (cl_uint)deviceCount, (cl_device_id *)_deviceListPtr, _strchars_options, pfn_notify, globalCB); if (options != NULL) { (*env)->ReleaseStringUTFChars(env, options, _strchars_options); diff --git a/resources/clImplCustomCode.java b/resources/clImplCustomCode.java index 9edbd575..5ebf12aa 100644 --- a/resources/clImplCustomCode.java +++ b/resources/clImplCustomCode.java @@ -5,63 +5,48 @@ this.addressTable = addressTable; } - public long clCreateContext(PointerBuffer properties, PointerBuffer devices, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) { + public long clCreateContext(PointerBuffer properties, PointerBuffer devices, CreateContextCallback pfn_notify, IntBuffer errcode_ret) { if(properties!=null && !properties.isDirect()) throw new RuntimeException("Argument \"properties\" was not a direct buffer"); if(pfn_notify != null) - throw new RuntimeException("asynchronous execution with callback is not yet implemented, pass null through this method to block until complete."); - - if(userData != null) - System.err.println("WARNING: userData not yet implemented... ignoring"); + throw new RuntimeException("asynchronous errorhandler are not supported yet, pass null through this method to block until complete."); return this.clCreateContext0( properties!=null?properties.getBuffer():null, Buffers.getDirectBufferByteOffset(properties), devices!=null?devices.remaining():0, devices!=null?devices.getBuffer():null, Buffers.getDirectBufferByteOffset(devices), - null, null, - errcode_ret, Buffers.getDirectBufferByteOffset(errcode_ret) ); + null, errcode_ret, Buffers.getDirectBufferByteOffset(errcode_ret) ); } - private native long clCreateContext0(Object cl_context_properties, int props_offset, int numDevices, Object devices, int devices_offset, CreateContextCallback pfn_notify, Object userData, Object errcode_ret, int err_offset); + private native long clCreateContext0(Object cl_context_properties, int props_offset, int numDevices, Object devices, int devices_offset, CreateContextCallback pfn_notify, Object errcode_ret, int err_offset); - public long clCreateContextFromType(PointerBuffer properties, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) { + public long clCreateContextFromType(PointerBuffer properties, long device_type, CreateContextCallback pfn_notify, IntBuffer errcode_ret) { if(properties!=null && !properties.isDirect()) throw new RuntimeException("Argument \"properties\" was not a direct buffer"); if(pfn_notify != null) - throw new RuntimeException("asynchronous execution with callback is not yet implemented, pass null through this method to block until complete."); - - if(userData != null) - System.err.println("WARNING: userData not yet implemented... ignoring"); + throw new RuntimeException("asynchronous errorhandler are not supported yet, pass null through this method to block until complete."); return this.clCreateContextFromType0( properties!=null?properties.getBuffer():null, Buffers.getDirectBufferByteOffset(properties), - device_type, pfn_notify, null, - errcode_ret, Buffers.getDirectBufferByteOffset(errcode_ret) ); + device_type, pfn_notify, errcode_ret, Buffers.getDirectBufferByteOffset(errcode_ret) ); } - private native long clCreateContextFromType0(Object properties, int props_offset, long device_type, CreateContextCallback pfn_notify, Object userData, Object errcode_ret, int err_offset); + private native long clCreateContextFromType0(Object properties, int props_offset, long device_type, CreateContextCallback pfn_notify, Object errcode_ret, int err_offset); /** Interface to C language function:
int32_t clBuildProgram(cl_program, uint32_t, cl_device_id * , const char * , void * ); */ - public int clBuildProgram(long program, int deviceCount, PointerBuffer deviceList, String options, BuildProgramCallback cb, Object userData) { + public int clBuildProgram(long program, int deviceCount, PointerBuffer deviceList, String options, BuildProgramCallback cb) { if(deviceList!=null && !deviceList.isDirect()) throw new RuntimeException("Argument \"properties\" was not a direct buffer"); - if(cb != null) - throw new RuntimeException("asynchronous execution with callback is not yet implemented, pass null through this method to block until complete."); - - if(userData != null) - System.err.println("WARNING: userData not yet implemented... ignoring"); - - return clBuildProgram0(program, deviceCount, - deviceList!=null?deviceList.getBuffer():null, Buffers.getDirectBufferByteOffset(deviceList), - options, cb, userData); + return clBuildProgram0(program, deviceCount, deviceList!=null?deviceList.getBuffer():null, + Buffers.getDirectBufferByteOffset(deviceList), options, cb); } /** Entry point to C language function: int32_t clBuildProgram(cl_program, uint32_t, cl_device_id * , const char * , void * ); */ - private native int clBuildProgram0(long program, int deviceCount, Object deviceList, int deviceListOffset, String options, BuildProgramCallback cb, Object userData); + private native int clBuildProgram0(long program, int deviceCount, Object deviceList, int deviceListOffset, String options, BuildProgramCallback cb); /** Interface to C language function:
void * {@native clEnqueueMapImage}(cl_command_queue command_queue, cl_mem image, uint32_t blocking_map, uint64_t map_flags, const size_t * , const size_t * , size_t * image_row_pitch, size_t * image_slice_pitch, uint32_t num_events_in_wait_list, cl_event * event_wait_list, cl_event * event, int32_t * errcode_ret); @@ -115,9 +100,12 @@ @param event_wait_list a direct {@link com.jogamp.gluegen.runtime.PointerBuffer} @param event a direct {@link com.jogamp.gluegen.runtime.PointerBuffer} @param errcode_ret a direct {@link java.nio.IntBuffer} */ - private native java.nio.ByteBuffer clEnqueueMapImage0(long command_queue, long image, int blocking_map, long map_flags, Object origin, int origin_byte_offset, Object range, int range_byte_offset, Object image_row_pitch, int image_row_pitch_byte_offset, Object image_slice_pitch, int image_slice_pitch_byte_offset, int num_events_in_wait_list, Object event_wait_list, int event_wait_list_byte_offset, Object event, int event_byte_offset, Object errcode_ret, int errcode_ret_byte_offset); + private native ByteBuffer clEnqueueMapImage0(long command_queue, long image, int blocking_map, long map_flags, Object origin, int origin_byte_offset, Object range, int range_byte_offset, Object image_row_pitch, int image_row_pitch_byte_offset, Object image_slice_pitch, int image_slice_pitch_byte_offset, int num_events_in_wait_list, Object event_wait_list, int event_wait_list_byte_offset, Object event, int event_byte_offset, Object errcode_ret, int errcode_ret_byte_offset); + /** + * Returns the extension function address for the given function name. + */ public long clGetExtensionFunctionAddress(String name) { ByteBuffer res = clGetExtensionFunctionAddressImpl(name); if(Platform.is32Bit()) { diff --git a/src/com/jogamp/opencl/BuildProgramCallback.java b/src/com/jogamp/opencl/BuildProgramCallback.java index fe2b29b9..9969c918 100644 --- a/src/com/jogamp/opencl/BuildProgramCallback.java +++ b/src/com/jogamp/opencl/BuildProgramCallback.java @@ -1,10 +1,19 @@ package com.jogamp.opencl; /** + * A callback an application can register to be called when the program executable + * has been built (successfully or unsuccessfully).
+ * Note1: registering a build callback can make {@link CL#clBuildProgram} non blocking (OpenCL implementation dependent).
+ * Note2: the thread which calls this method is unspecified. The Application should ensure propper synchronization. * @author Michael Bien + * @see CL#clBuildProgram(long, int, com.jogamp.common.nio.PointerBuffer, java.lang.String, com.jogamp.opencl.BuildProgramCallback) */ -// TODO implement callbacks public interface BuildProgramCallback { + + /** + * Called when the program executable + * has been built (successfully or unsuccessfully). + */ + public void buildProgramCallback(long cl_program); - public void buildProgramCallback(long cl_program, Object user_data); } diff --git a/src/com/jogamp/opencl/CLContext.java b/src/com/jogamp/opencl/CLContext.java index 4e172630..e9e0a8a8 100644 --- a/src/com/jogamp/opencl/CLContext.java +++ b/src/com/jogamp/opencl/CLContext.java @@ -146,7 +146,7 @@ public class CLContext extends CLObject implements CLResource { protected static long createContextFromType(PointerBuffer properties, long deviceType) { IntBuffer status = IntBuffer.allocate(1); - long context = CLPlatform.getLowLevelCLInterface().clCreateContextFromType(properties, deviceType, null, null, status); + long context = CLPlatform.getLowLevelCLInterface().clCreateContextFromType(properties, deviceType, null, status); checkForError(status.get(), "can not create CL context"); @@ -167,7 +167,7 @@ public class CLContext extends CLObject implements CLResource { pb.put(i, device.ID); } } - long context = CLPlatform.getLowLevelCLInterface().clCreateContext(properties, pb, null, null, status); + long context = CLPlatform.getLowLevelCLInterface().clCreateContext(properties, pb, null, status); checkForError(status.get(), "can not create CL context"); diff --git a/src/com/jogamp/opencl/CLProgram.java b/src/com/jogamp/opencl/CLProgram.java index ce2b9dff..43fd9665 100644 --- a/src/com/jogamp/opencl/CLProgram.java +++ b/src/com/jogamp/opencl/CLProgram.java @@ -256,7 +256,7 @@ public class CLProgram extends CLObject implements CLResource { int ret = 0; // building programs is not threadsafe // synchronized(buildLock) { - ret = cl.clBuildProgram(ID, count, deviceIDs, options, null, null); + ret = cl.clBuildProgram(ID, count, deviceIDs, options, null); // } if(ret != CL_SUCCESS) { diff --git a/src/com/jogamp/opencl/CreateContextCallback.java b/src/com/jogamp/opencl/CreateContextCallback.java index dc25860a..a73b6298 100644 --- a/src/com/jogamp/opencl/CreateContextCallback.java +++ b/src/com/jogamp/opencl/CreateContextCallback.java @@ -9,6 +9,6 @@ import java.nio.ByteBuffer; // TODO implement callbacks public interface CreateContextCallback { - public void createContextCallback(String errinfo, ByteBuffer private_info, long cb, Object user_data); + public void createContextCallback(String errinfo, ByteBuffer private_info, long cb); } diff --git a/test/com/jogamp/opencl/LowLevelBindingTest.java b/test/com/jogamp/opencl/LowLevelBindingTest.java index 8d6aba41..f5b5a27b 100644 --- a/test/com/jogamp/opencl/LowLevelBindingTest.java +++ b/test/com/jogamp/opencl/LowLevelBindingTest.java @@ -6,6 +6,7 @@ import com.jogamp.opencl.impl.CLImpl; import java.nio.ByteBuffer; import java.nio.IntBuffer; +import java.util.concurrent.CountDownLatch; import org.junit.BeforeClass; import org.junit.Test; @@ -151,7 +152,7 @@ public class LowLevelBindingTest { PointerBuffer devices = PointerBuffer.allocateDirect(intBuffer.get(0)); ret = cl.clGetDeviceIDs(platform, CL.CL_DEVICE_TYPE_ALL, devices.capacity(), devices, null); - long context = cl.clCreateContext(null, devices, null, null, intBuffer); + long context = cl.clCreateContext(null, devices, null, intBuffer); checkError("on clCreateContext", intBuffer.get()); //get number of devices @@ -170,7 +171,7 @@ public class LowLevelBindingTest { } @Test - public void lowLevelVectorAddTest() { + public void lowLevelVectorAddTest() throws InterruptedException { out.println(" - - - lowLevelTest2; VectorAdd kernel - - - "); @@ -201,7 +202,7 @@ public class LowLevelBindingTest { PointerBuffer properties = (PointerBuffer)PointerBuffer.allocateDirect(3).put(CL.CL_CONTEXT_PLATFORM) .put(platform).put(0) // 0 terminated array .rewind(); - long context = cl.clCreateContextFromType(properties, CL.CL_DEVICE_TYPE_ALL, null, null, null); + long context = cl.clCreateContextFromType(properties, CL.CL_DEVICE_TYPE_ALL, null, null); out.println("context handle: "+context); ret = cl.clGetContextInfo(context, CL.CL_CONTEXT_DEVICES, 0, null, longBuffer); @@ -245,13 +246,29 @@ public class LowLevelBindingTest { // Create the program Int64Buffer lengths = (Int64Buffer)Int64Buffer.allocateDirect(1).put(programSource.length()); - long program = cl.clCreateProgramWithSource(context, 1, new String[] {programSource}, lengths, intBuffer); + final long program = cl.clCreateProgramWithSource(context, 1, new String[] {programSource}, lengths, intBuffer); checkError("on clCreateProgramWithSource", intBuffer.get(0)); + // tests if the callback is called + final CountDownLatch latch = new CountDownLatch(1); + BuildProgramCallback callback = new BuildProgramCallback() { + public void buildProgramCallback(long cl_program) { + try{ + assertEquals(program, cl_program); + }finally{ + latch.countDown(); + } + } + }; + // Build the program - ret = cl.clBuildProgram(program, 0, null, null, null, null); + ret = cl.clBuildProgram(program, 0, null, null, callback); checkError("on clBuildProgram", ret); + out.println("waiting for program to build..."); + latch.await(); + out.println("done"); + // Read program infos ret = cl.clGetProgramInfo(program, CL.CL_PROGRAM_NUM_DEVICES, bb.capacity(), bb, null); checkError("on clGetProgramInfo1", ret); -- cgit v1.2.3