diff options
-rw-r--r-- | build.xml | 25 | ||||
-rw-r--r-- | etc/FunctionParamUncommenter.java | 94 | ||||
-rw-r--r-- | nbproject/project.properties | 4 | ||||
-rw-r--r-- | resources/CL/cl.h | 14 | ||||
-rw-r--r-- | resources/cl-common.cfg | 5 | ||||
-rw-r--r-- | resources/cl-if.cfg | 31 | ||||
-rw-r--r-- | resources/cl-impl.cfg | 27 | ||||
-rw-r--r-- | resources/clImplCustomCode.c | 44 | ||||
-rw-r--r-- | resources/clImplCustomCode.java | 13 | ||||
-rw-r--r-- | resources/clgl-if.cfg | 4 | ||||
-rw-r--r-- | src/com/mbien/opencl/CreateContextCallback.java | 13 | ||||
-rw-r--r-- | test/com/mbien/opencl/JOCLTest.java | 15 |
12 files changed, 261 insertions, 28 deletions
@@ -28,16 +28,24 @@ <taskdef name="gluegen" classname="com.sun.gluegen.ant.GlueGenTask" classpathref="gluegen.classpath" /> - <!--OpenCL Impl including GL interoperability--> + <!--OpenCL Impl including OpenGL interoperability--> <dirset id="jocl.include.path" dir="${basedir}"> <include name="resources"/> <include name="resources/CL"/> <include name="resources/jvm_stubs"/> </dirset> - <echo message=" - - - generate JOCL java files - - - "/> + <echo message=" - - - generate JOCL binding files - - - "/> - <echo message="generate CLGL interface..."/> + <echo message="generate CL interface..."/> + <gluegen src="resources/opencl.h" + config="resources/cl-if.cfg" + includeRefid="jocl.include.path" + emitter="com.sun.gluegen.JavaEmitter"> + <classpath refid="gluegen.classpath" /> + </gluegen> + + <echo message="generate CLGLI interface..."/> <gluegen src="resources/opencl.h" config="resources/clgl-if.cfg" includeRefid="jocl.include.path" @@ -52,10 +60,7 @@ emitter="com.sun.gluegen.JavaEmitter"> <classpath refid="gluegen.classpath" /> </gluegen> - <echo message=" - - - JOCL java files generated - - - "/> - - <!-- TODO 1. generate CLGLimpl - 2. split interfaces into CL and CLGL --> + <echo message=" - - - JOCL binding files generated - - - "/> </target> @@ -108,6 +113,12 @@ <echo message=" - - - JOCL natives compiled - - - "/> + <copy todir="${natives.jocl.dir}"> + <fileset dir="${gluegen.root}/build/obj"> + <include name="*.so"/> + </fileset> + </copy> + </target> <target name="c.configure.linux.amd64" depends="gluegen.cpptasks.detect.os,gluegen.cpptasks.setup.compiler"> diff --git a/etc/FunctionParamUncommenter.java b/etc/FunctionParamUncommenter.java new file mode 100644 index 00000000..e774121f --- /dev/null +++ b/etc/FunctionParamUncommenter.java @@ -0,0 +1,94 @@ + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import static java.util.regex.Pattern.*; + +/** + * Build setup utility. Uncomments funcion param names in header files. + * + * before: + * foo(int /x bar x/ ) + * + * after: + * foo(int bar ) + * + * @author Michael Bien + */ +public class FunctionParamUncommenter { +//(\(.*\))* cl\w+\(([^\)]+)\) + +// final static String x = "\\s*(const)?\\w+\\s* \\**\\s+ (/\\*) \\s+[^\\*]+ (\\*/)"; + + final static Pattern PARAMS_PATTERN + = compile("cl\\w+ \\( ( \\s* [^\\)]+ ) \\)", MULTILINE|COMMENTS); + + final static Pattern COMMENT_PATTERN + = compile("\\s*(const)?\\w+\\s* \\**\\s+ (/\\*) \\s+[^\\*]+ (\\*/)", MULTILINE|COMMENTS); + + public static void main(String[] args) throws FileNotFoundException, IOException { + uncomment("/home/mbien/NetBeansProjects/JOGL/jocl/resources/CL/cl.h", false); + uncomment("/home/mbien/NetBeansProjects/JOGL/jocl/resources/CL/cl_gl.h", false); + } + + private static void uncomment(String file, boolean replace) throws FileNotFoundException, IOException { + + System.out.println("- - - begin uncomment - - -"); + + StringBuilder src = readSourceFile(new File(file)); + Matcher matcher = PARAMS_PATTERN.matcher(src); + + // iterate through funcions + while (matcher.find()) { + + StringBuilder params = new StringBuilder(matcher.group(1)); +// System.out.println("- - - - "); +// System.out.println(params.toString()); +// System.out.println("- - - - "); + + //iterate through params + Matcher m = COMMENT_PATTERN.matcher(params); + while(m.find()) { + //uncomment param + params.replace(m.start(2), m.end(2), " "); + params.replace(m.start(3), m.end(3), " "); + } + + //replace old params with uncommented params + src.replace(matcher.start(1), matcher.end(1), params.toString()); + } + + if(replace) { + //replace old file + BufferedWriter out = new BufferedWriter(new FileWriter(file)); + out.write(src.toString()); + out.close(); + }else{ + System.out.println(src); + } + + System.out.println("- - - done - - -"); + } + + + private static StringBuilder readSourceFile(File file) throws FileNotFoundException, IOException { + + char[] buffer = new char[(int)file.length()]; + FileReader reader = new FileReader(file); + int length = reader.read(buffer); + if(reader != null) { + reader.close(); + } + + StringBuilder sb = new StringBuilder(); + sb.append(buffer, 0, length); + + return sb; + } + +} diff --git a/nbproject/project.properties b/nbproject/project.properties index 83e4d1cb..67e2f346 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -21,6 +21,7 @@ dist.dir=dist dist.jar=${dist.dir}/JOCL.jar dist.javadoc.dir=${dist.dir}/javadoc excludes= +file.reference.gluegen-rt.jar=../gluegen/build/gluegen-rt.jar file.reference.gluegen.jar=../gluegen/build/gluegen.jar includes=** jar.compress=false @@ -58,7 +59,8 @@ meta.inf.dir=${src.dir}/META-INF platform.active=default_platform run.classpath=\ ${javac.classpath}:\ - ${build.classes.dir} + ${build.classes.dir}:\ + ${file.reference.gluegen-rt.jar} # Space-separated list of JVM arguments used when running the project # (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value # or test-sys-prop.name=value to set system properties for unit tests): diff --git a/resources/CL/cl.h b/resources/CL/cl.h index d353f9ad..4a2c6712 100644 --- a/resources/CL/cl.h +++ b/resources/CL/cl.h @@ -437,15 +437,15 @@ extern CL_API_ENTRY cl_context CL_API_CALL clCreateContext(cl_context_properties * /* properties */, cl_uint /* num_devices */, const cl_device_id * /* devices */, -// void (*pfn_notify)(const char *, const void *, size_t, void *) /* pfn_notify */, -// void * /* user_data */, + void (*pfn_notify)(const char *, const void *, size_t, void *) /* pfn_notify */, + void * /* user_data */, cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; extern CL_API_ENTRY cl_context CL_API_CALL clCreateContextFromType(cl_context_properties * /* properties */, cl_device_type /* device_type */, -// void (*pfn_notify)(const char *, const void *, size_t, void *) /* pfn_notify */, -// void * /* user_data */, + void (*pfn_notify)(const char *, const void *, size_t, void *) /* pfn_notify */, + void * /* user_data */, cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; extern CL_API_ENTRY cl_int CL_API_CALL @@ -594,7 +594,7 @@ clBuildProgram(cl_program /* program */, cl_uint /* num_devices */, const cl_device_id * /* device_list */, const char * /* options */, -// void (*pfn_notify)(cl_program /* program */, void * /* user_data */), + void (*pfn_notify)(cl_program /* program */, void * /* user_data */), void * /* user_data */) CL_API_SUFFIX__VERSION_1_0; extern CL_API_ENTRY cl_int CL_API_CALL @@ -834,8 +834,8 @@ clEnqueueTask(cl_command_queue /* command_queue */, extern CL_API_ENTRY cl_int CL_API_CALL clEnqueueNativeKernel(cl_command_queue /* command_queue */, -// void (*user_func)(void *), -// void * /* args */, + void (*user_func)(void *), + void * /* args */, size_t /* cb_args */, cl_uint /* num_mem_objects */, const cl_mem * /* mem_list */, diff --git a/resources/cl-common.cfg b/resources/cl-common.cfg index bcdf7ee9..58b31880 100644 --- a/resources/cl-common.cfg +++ b/resources/cl-common.cfg @@ -1,6 +1,8 @@ JavaOutputDir gensrc/java NativeOutputDir gensrc/native +Package com.mbien.opencl + #map pointers to long as internal representation Opaque long cl_context Opaque long cl_device_type @@ -15,3 +17,6 @@ Opaque long cl_sampler Opaque long cl_platform_id Opaque long cl_device_id +ArgumentIsString clCreateProgramWithSource 2 +ArgumentIsString clBuildProgram 3 +ArgumentIsString clCreateKernel 1
\ No newline at end of file diff --git a/resources/cl-if.cfg b/resources/cl-if.cfg new file mode 100644 index 00000000..6d23789d --- /dev/null +++ b/resources/cl-if.cfg @@ -0,0 +1,31 @@ +Include cl-common.cfg + +Style InterfaceOnly + +#imports for all generated java files +Import java.nio.IntBuffer + +ClassJavadoc CL /** +ClassJavadoc CL * Java bindings to OpenCL, the Open Computing Language. +ClassJavadoc CL * @autor Michael Bien +ClassJavadoc CL */ +JavaClass CL + +#ignore cl-gl interoperability funcions. Interface 'CL' is pure OpenCL. +Ignore CL_GL_.*|cl.*GL.* + +#custom implementations +Ignore clCreateContext +CustomJavaCode CL /** Interface to C language function: <br> <code> cl_context clCreateContext(intptr_t * , uint32_t, cl_device_id * , void (*pfn_notify)(const char *, const void *, size_t, void *), void *, int32_t * ); </code> */ +CustomJavaCode CL public long clCreateContext(IntBuffer properties, int arg1, long[] devices, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret); + +Ignore clCreateContextFromType +CustomJavaCode CL /** Interface to C language function: <br> <code> cl_context 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) ; </code> */ +CustomJavaCode CL public long clCreateContextFromType(IntBuffer properties, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret); + +Ignore clBuildProgram +#TODO.. + +Ignore clEnqueueNativeKernel +#TODO.. + diff --git a/resources/cl-impl.cfg b/resources/cl-impl.cfg index 5cf23c88..58fc5871 100644 --- a/resources/cl-impl.cfg +++ b/resources/cl-impl.cfg @@ -1,18 +1,15 @@ Include cl-common.cfg -Package com.mbien.opencl - -Style InterfaceAndImpl +Style ImplOnly #imports for all generated java files Import com.mbien.opencl.* -#Import java.nio.* +Import java.nio.IntBuffer -ClassJavadoc CL /** -ClassJavadoc CL * Java bindings to OpenCL, the Open Computing Language. -ClassJavadoc CL * @autor Michael Bien -ClassJavadoc CL */ -JavaClass CL +ClassJavadoc CLImpl /** +ClassJavadoc CLImpl * Java bindings to OpenCL, the Open Computing Language. +ClassJavadoc CLImpl * @autor Michael Bien +ClassJavadoc CLImpl */ ImplJavaClass CLImpl Implements CLImpl CLGLI @@ -20,4 +17,14 @@ Implements CLImpl CLGLI #append to generated c files CustomCCode #include <cl.h> CustomCCode #include <gl3.h> -CustomCCode #include <inttypes.h>
\ No newline at end of file +CustomCCode #include <inttypes.h> + +# implement manually via custom code +Ignore clCreateContext +Ignore clCreateContextFromType +Ignore clBuildProgram +Ignore clEnqueueNativeKernel + +#include custom code +IncludeAs CustomJavaCode CLImpl clImplCustomCode.java +IncludeAs CustomCCode clImplCustomCode.c diff --git a/resources/clImplCustomCode.c b/resources/clImplCustomCode.c new file mode 100644 index 00000000..a281ccc3 --- /dev/null +++ b/resources/clImplCustomCode.c @@ -0,0 +1,44 @@ + +/* +void createContextCallback(const char * c, const void * v, size_t s, void * o) { + //TODO +} +*/ + +/* Java->C glue code: + * Java package: com.mbien.opencl.impl.CLImpl + * Java method: long clCreateContextFromType(java.nio.IntBuffer arg0, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) + * C function: cl_context clCreateContextFromType( cl_context_properties * properties , + * cl_uint num_devices , + * const cl_device_id * devices , + * void (*pfn_notify)(const char *, const void *, size_t, void *) pfn_notify/, + * void * user_data , + * cl_int * errcode_ret ); + */ +//Ljava/nio/IntBuffer;JLjava/lang/Object;Ljava/lang/Object;Ljava/nio/IntBuffer; +//Ljava_lang_Object_2I J Ljava_lang_Object_2 Ljava_lang_Object_2 Ljava_lang_Object_2I +//IntBuffer arg0, long device_type, Object pfn_notify, Object userData, IntBuffer errcode_ret +JNIEXPORT jlong JNICALL +Java_com_mbien_opencl_impl_CLImpl_clCreateContextFromType0__Ljava_lang_Object_2IJLjava_lang_Object_2Ljava_lang_Object_2Ljava_lang_Object_2I(JNIEnv *env, jobject _unused, + jobject arg0, jint arg0_byte_offset, jlong device_type, jobject cb, jobject data, jobject errcode, jint errcode_byte_offset) { + + printf("%s", "function entry"); + + intptr_t * _ptr0 = NULL; + int32_t * _ptr2 = NULL; + + cl_context _res; + + if (arg0 != NULL) { + _ptr0 = (intptr_t *) (((char*) (*env)->GetDirectBufferAddress(env, arg0)) + arg0_byte_offset); + } + if (errcode != NULL) { + _ptr2 = (int32_t *) (((char*) (*env)->GetDirectBufferAddress(env, errcode)) + errcode_byte_offset); + } + + printf("%s", "pre call"); + _res = clCreateContextFromType((intptr_t *) _ptr0, (uint64_t) device_type, NULL, NULL, (int32_t *) _ptr2); + printf("%s", "post call"); + + return (jlong) (intptr_t) _res; +}
\ No newline at end of file diff --git a/resources/clImplCustomCode.java b/resources/clImplCustomCode.java new file mode 100644 index 00000000..7b69330b --- /dev/null +++ b/resources/clImplCustomCode.java @@ -0,0 +1,13 @@ + + public long clCreateContext(IntBuffer properties, int arg1, long[] devices, CreateContextCallback cb, Object userData, IntBuffer errcode_ret) { + return this.clCreateContext0(properties, arg1, devices, cb, null, errcode_ret); + } + + public native long clCreateContext0(IntBuffer properties, int arg1, long[] devices, CreateContextCallback cb, Object userData, IntBuffer errcode_ret); + + public long clCreateContextFromType(IntBuffer arg0, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) { + return this.clCreateContextFromType0(arg0, device_type, pfn_notify, null, errcode_ret); + } + + public native long clCreateContextFromType0(IntBuffer arg0, long device_type, Object pfn_notify, Object userData, IntBuffer errcode_ret); +
\ No newline at end of file diff --git a/resources/clgl-if.cfg b/resources/clgl-if.cfg index 771e39f6..d21e8072 100644 --- a/resources/clgl-if.cfg +++ b/resources/clgl-if.cfg @@ -1,7 +1,5 @@ Include cl-common.cfg -Package com.mbien.opencl - Style InterfaceOnly ClassJavadoc CLGLI /** @@ -11,6 +9,8 @@ ClassJavadoc CLGLI * @autor Michael Bien ClassJavadoc CLGLI */ JavaClass CLGLI +Extends CLGLI CL + #only include token starting with CL_GL_ and methods containing GL IgnoreNot CL_GL_.*|cl.*GL.* diff --git a/src/com/mbien/opencl/CreateContextCallback.java b/src/com/mbien/opencl/CreateContextCallback.java new file mode 100644 index 00000000..b25c05fd --- /dev/null +++ b/src/com/mbien/opencl/CreateContextCallback.java @@ -0,0 +1,13 @@ +package com.mbien.opencl; + +import java.nio.ByteBuffer; + +/** + * + * @author Michael Bien + */ +public interface CreateContextCallback { + + public void createContextCallback(String errinfo, ByteBuffer private_info, long cb, Object user_data); + +} diff --git a/test/com/mbien/opencl/JOCLTest.java b/test/com/mbien/opencl/JOCLTest.java index a3bdee3e..68673746 100644 --- a/test/com/mbien/opencl/JOCLTest.java +++ b/test/com/mbien/opencl/JOCLTest.java @@ -1,6 +1,7 @@ package com.mbien.opencl; import com.mbien.opencl.impl.CLImpl; +import java.nio.ByteBuffer; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -29,10 +30,22 @@ public class JOCLTest { System.out.println(0xFFFFFFFE); System.out.println(0xFFFFFFFD); + System.loadLibrary("gluegen-rt"); System.loadLibrary("jocl"); + CreateContextCallback cb = new CreateContextCallback() { + @Override + public void createContextCallback(String errinfo, ByteBuffer private_info, long cb, Object user_data) { + throw new RuntimeException(errinfo); + } + }; + CLImpl impl = new CLImpl(); - long ctx = impl.clCreateContextFromType(null, CL.CL_DEVICE_TYPE_ALL, null); + + System.out.println("test call1: "+impl.clFinish(1)); + System.out.println("test call2: "+impl.clUnloadCompiler()); + + long ctx = impl.clCreateContextFromType(null, CL.CL_DEVICE_TYPE_ALL, cb, null, null); System.out.println("context handle: "+ctx); |