summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/mbien/opencl/CLCommandQueue.java83
-rw-r--r--src/com/mbien/opencl/CLContext.java5
-rw-r--r--test/com/mbien/opencl/HighLevelBindingTest.java64
3 files changed, 139 insertions, 13 deletions
diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java
index b0a6980f..7d1d09f9 100644
--- a/src/com/mbien/opencl/CLCommandQueue.java
+++ b/src/com/mbien/opencl/CLCommandQueue.java
@@ -30,8 +30,8 @@ public class CLCommandQueue {
int ret = cl.clEnqueueWriteBuffer(
ID, writeBuffer.ID, blockingWrite ? CL.CL_TRUE : CL.CL_FALSE,
0, writeBuffer.buffer.capacity(), writeBuffer.buffer,
- 0, null, 0,
- null, 0 );
+// 0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null (fast dircet memory path)
+ 0, null, 0, null, 0); //TODO events
if(ret != CL.CL_SUCCESS)
throw new CLException(ret, "can not enqueue WriteBuffer: " + writeBuffer);
@@ -44,8 +44,8 @@ public class CLCommandQueue {
int ret = cl.clEnqueueReadBuffer(
ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE,
0, readBuffer.buffer.capacity(), readBuffer.buffer,
- 0, null, 0,
- null, 0 );
+// 0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null (fast dircet memory path)
+ 0, null, 0, null, 0); //TODO events
if(ret != CL.CL_SUCCESS)
throw new CLException(ret, "can not enqueue ReadBuffer: " + readBuffer);
@@ -53,6 +53,81 @@ public class CLCommandQueue {
return this;
}
+ public CLCommandQueue putBarrier() {
+ int ret = cl.clEnqueueBarrier(ID);
+ checkForError(ret, "can not enqueue Barrier");
+ return this;
+ }
+
+ public CLCommandQueue putCopyBuffer(CLBuffer src, CLBuffer dest, long bytesToCopy) {
+ int ret = cl.clEnqueueCopyBuffer(
+ ID, src.ID, dest.ID, src.buffer.position(), dest.buffer.position(), bytesToCopy,
+// 0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null
+ 0, null, 0, null, 0); //TODO events
+ checkForError(ret, "can not copy Buffer");
+ return this;
+ }
+
+ //TODO implement remaining methods
+ /*
+ public CLCommandQueue putCopyImage() {
+
+ return this;
+ }
+ public CLCommandQueue putCopyBufferToImage() {
+
+ return this;
+ }
+ public CLCommandQueue putCopyImageToBuffer() {
+
+ return this;
+ }
+ public CLCommandQueue putMarker() {
+
+ return this;
+ }
+
+ public CLCommandQueue putWriteImage() {
+
+ return this;
+ }
+
+ public CLCommandQueue putReadImage() {
+
+ return this;
+ }
+
+ public CLCommandQueue putTask() {
+
+ return this;
+ }
+
+ public CLBuffer putMapBuffer() {
+
+ return null;
+ }
+
+ public CLCommandQueue putMapImage() {
+
+ return this;
+ }
+
+ public CLCommandQueue putUnmapMemObject() {
+
+ return this;
+ }
+
+ public CLCommandQueue putWaitForEvents() {
+
+ return this;
+ }
+*/
+
+// public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long globalWorkOffset, long globalWorkSize, long localWorkSize) {
+// return this.putNDRangeKernel(kernel, workDimension,
+// new long[] {globalWorkOffset}, new long[] {globalWorkSize}, new long[] {localWorkSize});
+// }
+
public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long[] globalWorkOffset, long[] globalWorkSize, long[] localWorkSize) {
int ret = cl.clEnqueueNDRangeKernel(
diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java
index db32a446..76fbc2ee 100644
--- a/src/com/mbien/opencl/CLContext.java
+++ b/src/com/mbien/opencl/CLContext.java
@@ -1,5 +1,6 @@
package com.mbien.opencl;
+import com.sun.gluegen.runtime.BufferFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -104,6 +105,10 @@ public final class CLContext {
return buffer;
}
+ public CLBuffer createBuffer(int flags, int size) {
+ return createBuffer(flags, BufferFactory.newDirectByteBuffer(size));
+ }
+
CLCommandQueue createCommandQueue(CLDevice device, long properties) {
CLCommandQueue queue = new CLCommandQueue(this, device, properties);
diff --git a/test/com/mbien/opencl/HighLevelBindingTest.java b/test/com/mbien/opencl/HighLevelBindingTest.java
index a2bfce91..b9643e9b 100644
--- a/test/com/mbien/opencl/HighLevelBindingTest.java
+++ b/test/com/mbien/opencl/HighLevelBindingTest.java
@@ -1,14 +1,15 @@
package com.mbien.opencl;
-import com.sun.gluegen.runtime.BufferFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Map;
import org.junit.BeforeClass;
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 high level bindings.
@@ -92,9 +93,9 @@ public class HighLevelBindingTest {
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);
fillBuffer(srcA, 23456);
fillBuffer(srcB, 46987);
@@ -113,10 +114,10 @@ public class HighLevelBindingTest {
CLKernel vectorAddKernel = kernels.get("VectorAddGM");
- vectorAddKernel.setArg(0, BufferFactory.SIZEOF_LONG, clBufferA)
- .setArg(1, BufferFactory.SIZEOF_LONG, clBufferB)
- .setArg(2, BufferFactory.SIZEOF_LONG, clBufferC)
- .setArg(3, BufferFactory.SIZEOF_INT, elementCount);
+ vectorAddKernel.setArg(0, SIZEOF_LONG, clBufferA)
+ .setArg(1, SIZEOF_LONG, clBufferB)
+ .setArg(2, SIZEOF_LONG, clBufferC)
+ .setArg(3, SIZEOF_INT, elementCount);
CLCommandQueue queue = programDevices[0].createCommandQueue();
@@ -129,7 +130,7 @@ public class HighLevelBindingTest {
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");
assertTrue(3 == context.getCLBuffers().size());
clBufferA.release();
@@ -152,5 +153,50 @@ public class HighLevelBindingTest {
// out.println("max FLOPS device: " + device);
context.release();
}
+
+ @Test
+ public void writeCopyReadBufferTest() throws IOException {
+
+ out.println(" - - - highLevelTest; copy buffer test - - - ");
+
+ final int elements = 10000000; //many..
+
+ CLContext context = CLContext.create();
+
+ // the CL.MEM_* flag is probably completly irrelevant in our case since we do not use a kernel in this test
+ CLBuffer clBufferA = context.createBuffer(CL.CL_MEM_READ_ONLY, elements*SIZEOF_INT);
+ CLBuffer clBufferB = context.createBuffer(CL.CL_MEM_READ_ONLY, elements*SIZEOF_INT);
+
+ // fill only first read buffer -> we will copy the payload to the second later.
+ fillBuffer(clBufferA.buffer, 12345);
+
+ CLCommandQueue queue = context.getCLDevices()[0].createCommandQueue();
+
+ // asynchronous write of data to GPU device, blocking read later to get the computed results back.
+ queue.putWriteBuffer(clBufferA, false) // write A
+ .putCopyBuffer(clBufferA, clBufferB, clBufferA.buffer.capacity()) // copy A -> B
+ .putReadBuffer(clBufferB, true); // read B
+
+ context.release();
+
+ ByteBuffer a = clBufferA.buffer;
+ ByteBuffer b = clBufferB.buffer;
+
+ // print first few elements of the resulting buffer to the console.
+ out.println("validating computed results...");
+ for(int i = 0; i < elements; i++) {
+ int aVal = a.getInt();
+ int bVal = b.getInt();
+ if(aVal != bVal) {
+ out.println("a: "+aVal);
+ out.println("b: "+bVal);
+ out.println("position: "+a.position());
+ fail("a!=b");
+ }
+
+ }
+ out.println("results are valid");
+
+ }
}