diff options
-rw-r--r-- | src/com/mbien/opencl/CLBuffer.java | 4 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 10 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLKernel.java | 3 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLProgram.java | 5 | ||||
-rw-r--r-- | test/com/mbien/opencl/LowLevelBindingTest.java | 30 |
5 files changed, 29 insertions, 23 deletions
diff --git a/src/com/mbien/opencl/CLBuffer.java b/src/com/mbien/opencl/CLBuffer.java index 7dcd2928..185389c3 100644 --- a/src/com/mbien/opencl/CLBuffer.java +++ b/src/com/mbien/opencl/CLBuffer.java @@ -77,12 +77,14 @@ public class CLBuffer { } static int flagsToInt(MEM[] flags) { - int clFlags = CL.CL_MEM_READ_WRITE; + int clFlags = 0; if(flags != null) { for (int i = 0; i < flags.length; i++) { clFlags |= flags[i].CL_FLAG; } } + if(clFlags == 0) + clFlags = CL.CL_MEM_READ_WRITE; return clFlags; } diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 7eaada8c..a82092bb 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -2,6 +2,7 @@ package com.mbien.opencl; import com.mbien.opencl.CLBuffer.MEM; import com.sun.gluegen.runtime.BufferFactory; +import com.sun.gluegen.runtime.CPU; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -217,7 +218,7 @@ public final class CLContext { if(devices == null) { - int sizeofDeviceID = 8; // TODO doublecheck deviceID size on 32 bit systems + int sizeofDeviceID = CPU.is32Bit()?4:8; long[] longBuffer = new long[1]; @@ -231,9 +232,10 @@ public final class CLContext { checkForError(ret, "can not enumerate devices"); devices = new CLDevice[deviceIDs.capacity()/sizeofDeviceID]; - for (int i = 0; i < devices.length; i++) - devices[i] = new CLDevice(this, deviceIDs.getLong()); // TODO doublecheck deviceID size on 32 bit systems - + for (int i = 0; i < devices.length; i++) { + devices[i] = new CLDevice(this, + CPU.is32Bit()?deviceIDs.getInt():deviceIDs.getLong()); + } } return devices; diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java index dcf00c9a..838f5969 100644 --- a/src/com/mbien/opencl/CLKernel.java +++ b/src/com/mbien/opencl/CLKernel.java @@ -1,6 +1,7 @@ package com.mbien.opencl; import com.sun.gluegen.runtime.BufferFactory; +import com.sun.gluegen.runtime.CPU; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -38,7 +39,7 @@ public class CLKernel { } public CLKernel setArg(int argumentIndex, CLBuffer value) { - int ret = cl.clSetKernelArg(ID, argumentIndex, 8, wrap(value.ID)); + int ret = cl.clSetKernelArg(ID, argumentIndex, CPU.is32Bit()?4:8, wrap(value.ID)); checkForError(ret, "error on clSetKernelArg"); return this; } diff --git a/src/com/mbien/opencl/CLProgram.java b/src/com/mbien/opencl/CLProgram.java index 566ae6b2..70656373 100644 --- a/src/com/mbien/opencl/CLProgram.java +++ b/src/com/mbien/opencl/CLProgram.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.sun.gluegen.runtime.CPU; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Collections; @@ -158,10 +159,10 @@ public class CLProgram { ret = cl.clGetProgramInfo(ID, CL.CL_PROGRAM_DEVICES, bb.capacity(), bb, null, 0); checkForError(ret, "on clGetProgramInfo"); - int count = bb.capacity() / 8; // TODO sizeof cl_device + int count = bb.capacity() / (CPU.is32Bit()?4:8); CLDevice[] devices = new CLDevice[count]; for (int i = 0; i < count; i++) { - devices[i] = context.getCLDevice(bb.getLong()); + devices[i] = context.getCLDevice(CPU.is32Bit()?bb.getInt():bb.getLong()); } return devices; diff --git a/test/com/mbien/opencl/LowLevelBindingTest.java b/test/com/mbien/opencl/LowLevelBindingTest.java index e5175129..a746f977 100644 --- a/test/com/mbien/opencl/LowLevelBindingTest.java +++ b/test/com/mbien/opencl/LowLevelBindingTest.java @@ -1,6 +1,6 @@ package com.mbien.opencl; -import com.sun.gluegen.runtime.BufferFactory; +import com.sun.gluegen.runtime.CPU; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; @@ -9,6 +9,7 @@ import org.junit.Test; import static org.junit.Assert.*; import static java.lang.System.*; import static com.mbien.opencl.TestUtils.*; +import static com.sun.gluegen.runtime.BufferFactory.*; /** * Test testing the low level bindings. @@ -142,7 +143,7 @@ public class LowLevelBindingTest { ret = cl.clGetContextInfo(context, CL.CL_CONTEXT_DEVICES, 0, null, longArray, 0); checkError("on clGetContextInfo", ret); - int sizeofLong = 8; // TODO sizeof long... + int sizeofLong = (CPU.is32Bit()?4:8); out.println("context created with " + longArray[0]/sizeofLong + " devices"); ret = cl.clGetContextInfo(context, CL.CL_CONTEXT_DEVICES, bb.capacity(), bb, null, 0); @@ -164,11 +165,10 @@ public class LowLevelBindingTest { out.println("allocateing buffers of size: "+globalWorkSize); - ByteBuffer srcA = BufferFactory.newDirectByteBuffer(globalWorkSize*BufferFactory.SIZEOF_INT); - ByteBuffer srcB = BufferFactory.newDirectByteBuffer(globalWorkSize*BufferFactory.SIZEOF_INT); - ByteBuffer dest = BufferFactory.newDirectByteBuffer(globalWorkSize*BufferFactory.SIZEOF_INT); + ByteBuffer srcA = newDirectByteBuffer(globalWorkSize*SIZEOF_INT); + ByteBuffer srcB = newDirectByteBuffer(globalWorkSize*SIZEOF_INT); + ByteBuffer dest = newDirectByteBuffer(globalWorkSize*SIZEOF_INT); - // TODO sizeof int ... // Allocate the OpenCL buffer memory objects for source and result on the device GMEM long devSrcA = cl.clCreateBuffer(context, CL.CL_MEM_READ_ONLY, srcA.capacity(), null, intArray, 0); checkError("on clCreateBuffer", intArray[0]); @@ -192,7 +192,7 @@ public class LowLevelBindingTest { checkError("on clGetProgramInfo1", ret); out.println("program associated with "+bb.getInt(0)+" device(s)"); - ret = cl.clGetProgramInfo(program, CL.CL_PROGRAM_SOURCE, 0, bb, longArray, 0); + ret = cl.clGetProgramInfo(program, CL.CL_PROGRAM_SOURCE, 0, null, longArray, 0); checkError("on clGetProgramInfo CL_PROGRAM_SOURCE", ret); out.println("program source length (cl): "+longArray[0]); out.println("program source length (java): "+programSource.length()); @@ -226,17 +226,17 @@ public class LowLevelBindingTest { long kernel = cl.clCreateKernel(program, "VectorAdd", intArray, 0); checkError("on clCreateKernel", intArray[0]); -// srcA.limit(elementCount*BufferFactory.SIZEOF_FLOAT); -// srcB.limit(elementCount*BufferFactory.SIZEOF_FLOAT); +// srcA.limit(elementCount*SIZEOF_FLOAT); +// srcB.limit(elementCount*SIZEOF_FLOAT); fillBuffer(srcA, 23456); fillBuffer(srcB, 46987); // Set the Argument values - ret = cl.clSetKernelArg(kernel, 0, BufferFactory.SIZEOF_LONG, wrap(devSrcA)); checkError("on clSetKernelArg0", ret); - ret = cl.clSetKernelArg(kernel, 1, BufferFactory.SIZEOF_LONG, wrap(devSrcB)); checkError("on clSetKernelArg1", ret); - ret = cl.clSetKernelArg(kernel, 2, BufferFactory.SIZEOF_LONG, wrap(devDst)); checkError("on clSetKernelArg2", ret); - ret = cl.clSetKernelArg(kernel, 3, BufferFactory.SIZEOF_INT, wrap(elementCount)); checkError("on clSetKernelArg3", ret); + ret = cl.clSetKernelArg(kernel, 0, CPU.is32Bit()?SIZEOF_INT:SIZEOF_LONG, wrap(devSrcA)); checkError("on clSetKernelArg0", ret); + ret = cl.clSetKernelArg(kernel, 1, CPU.is32Bit()?SIZEOF_INT:SIZEOF_LONG, wrap(devSrcB)); checkError("on clSetKernelArg1", ret); + ret = cl.clSetKernelArg(kernel, 2, CPU.is32Bit()?SIZEOF_INT:SIZEOF_LONG, wrap(devDst)); checkError("on clSetKernelArg2", ret); + ret = cl.clSetKernelArg(kernel, 3, SIZEOF_INT, wrap(elementCount)); checkError("on clSetKernelArg3", ret); out.println("used device memory: "+ (srcA.capacity()+srcB.capacity()+dest.capacity())/1000000 +"MB"); @@ -261,7 +261,7 @@ public class LowLevelBindingTest { out.println("a+b=c result snapshot: "); for(int i = 0; i < 10; i++) out.print(dest.getInt()+", "); - out.println("...; "+dest.remaining()/BufferFactory.SIZEOF_INT + " more"); + out.println("...; "+dest.remaining()/SIZEOF_INT + " more"); // cleanup @@ -300,7 +300,7 @@ public class LowLevelBindingTest { } private ByteBuffer wrap(long value) { - return (ByteBuffer) BufferFactory.newDirectByteBuffer(8).putLong(value).rewind(); + return (ByteBuffer) newDirectByteBuffer(8).putLong(value).rewind(); } private final void checkForError(int ret) { |