diff options
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 76 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLKernel.java | 2 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLPlatform.java | 43 |
3 files changed, 74 insertions, 47 deletions
diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index dda8eb05..db32a446 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -1,6 +1,9 @@ package com.mbien.opencl; -import com.mbien.opencl.impl.CLImpl; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; @@ -18,7 +21,7 @@ import static com.mbien.opencl.CLException.*; */ public final class CLContext { - final static CL cl; + final CL cl; public final long ID; private CLDevice[] devices; @@ -27,13 +30,9 @@ public final class CLContext { private final List<CLBuffer> buffers; private final Map<CLDevice, List<CLCommandQueue>> queuesMap; - static{ - System.loadLibrary("gluegen-rt"); - System.loadLibrary("jocl"); - cl = new CLImpl(); - } private CLContext(long contextID) { + this.cl = CLPlatform.getLowLevelBinding(); this.ID = contextID; this.programs = new ArrayList<CLProgram>(); this.buffers = new ArrayList<CLBuffer>(); @@ -62,20 +61,43 @@ public final class CLContext { private static final CLContext createContext(long deviceType) { - IntBuffer error = IntBuffer.allocate(1); - long context = cl.clCreateContextFromType(null, 0, deviceType, null, null, error, 0); + IntBuffer status = IntBuffer.allocate(1); + long context = CLPlatform.getLowLevelBinding().clCreateContextFromType(null, 0, deviceType, null, null, status, 0); - checkForError(error.get(), "can not create CL context"); + checkForError(status.get(), "can not create CL context"); return new CLContext(context); } - + + /** + * Creates a program from the given sources, the program is not build yet. + */ public CLProgram createProgram(String src) { CLProgram program = new CLProgram(this, src, ID); programs.add(program); return program; } + /** + * Creates a program and reads the sources from stream, the program is not build yet. + * @throws IOException when a IOException occurred while reading or closing the stream. + */ + public CLProgram createProgram(InputStream sources) throws IOException { + + BufferedReader reader = new BufferedReader(new InputStreamReader(sources)); + StringBuilder sb = new StringBuilder(); + + String line = null; + try { + while ((line = reader.readLine()) != null) + sb.append(line).append("\n"); + } finally { + sources.close(); + } + + return createProgram(sb.toString()); + } + public CLBuffer createBuffer(int flags, ByteBuffer directBuffer) { CLBuffer buffer = new CLBuffer(this, flags, directBuffer); buffers.add(buffer); @@ -218,38 +240,6 @@ public final class CLContext { return null; } - /** - * Lists all available OpenCL implementaitons. - * @throws CLException if something went wrong initializing OpenCL - */ - public static CLPlatform[] listCLPlatforms() { - - int[] intBuffer = new int[1]; - // find all available OpenCL platforms - int ret = cl.clGetPlatformIDs(0, null, 0, intBuffer, 0); - checkForError(ret, "can not enumerate platforms"); - - // receive platform ids - long[] platformId = new long[intBuffer[0]]; - ret = cl.clGetPlatformIDs(platformId.length, platformId, 0, null, 0); - checkForError(ret, "can not enumerate platforms"); - - CLPlatform[] platforms = new CLPlatform[platformId.length]; - - for (int i = 0; i < platformId.length; i++) - platforms[i] = new CLPlatform(cl, platformId[i]); - - return platforms; - } - - /** - * Returns the low level binding interface to the OpenCL APIs. - */ - public static CL getLowLevelBinding() { - return cl; - } - - @Override public String toString() { return "CLContext [id: " + ID diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java index 1db3da38..9f184ce4 100644 --- a/src/com/mbien/opencl/CLKernel.java +++ b/src/com/mbien/opencl/CLKernel.java @@ -32,7 +32,7 @@ public class CLKernel { ret = cl.clGetKernelInfo(ID, CL.CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null, 0); checkForError(ret, "error while asking for kernel function name"); - this.name = new String(bb.array(), 0, (int)longArray[0]).trim(); + this.name = new String(bb.array(), 0, bb.capacity()).trim(); } diff --git a/src/com/mbien/opencl/CLPlatform.java b/src/com/mbien/opencl/CLPlatform.java index dde9994b..56ef3713 100644 --- a/src/com/mbien/opencl/CLPlatform.java +++ b/src/com/mbien/opencl/CLPlatform.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.mbien.opencl.impl.CLImpl; import java.nio.ByteBuffer; import static com.mbien.opencl.CLException.*; /** @@ -13,11 +14,47 @@ public final class CLPlatform { */ public final long ID; - private final CL cl; + private static final CL cl; - CLPlatform(CL cl, long id) { + static{ + System.loadLibrary("gluegen-rt"); + System.loadLibrary("jocl"); + cl = new CLImpl(); + } + + CLPlatform(long id) { this.ID = id; - this.cl = cl; + } + + /** + * Lists all available OpenCL implementaitons. + * @throws CLException if something went wrong initializing OpenCL + */ + public static CLPlatform[] listCLPlatforms() { + + int[] intBuffer = new int[1]; + // find all available OpenCL platforms + int ret = cl.clGetPlatformIDs(0, null, 0, intBuffer, 0); + checkForError(ret, "can not enumerate platforms"); + + // receive platform ids + long[] platformId = new long[intBuffer[0]]; + ret = cl.clGetPlatformIDs(platformId.length, platformId, 0, null, 0); + checkForError(ret, "can not enumerate platforms"); + + CLPlatform[] platforms = new CLPlatform[platformId.length]; + + for (int i = 0; i < platformId.length; i++) + platforms[i] = new CLPlatform(platformId[i]); + + return platforms; + } + + /** + * Returns the low level binding interface to the OpenCL APIs. + */ + public static CL getLowLevelBinding() { + return cl; } /** |