diff options
author | Michael Bien <[email protected]> | 2010-01-08 00:11:55 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-01-08 00:11:55 +0100 |
commit | 286f94a7b148856666d7c05853ba9b2ba799b638 (patch) | |
tree | f7dc2acfaeb18d11979c58f3e40f5da923955da1 /src/com | |
parent | 14d666509596e5b954a5c20e0be9f5826a3ce733 (diff) |
introduced CLSampler and CLEvent.
refactored code to use internal CLInfoAccessor utility where it makes sense.
static imports.
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/mbien/opencl/CLBuffer.java | 34 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLCommandQueue.java | 38 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 30 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLDevice.java | 176 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLEvent.java | 138 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLException.java | 98 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLGLContext.java | 5 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLInfoAccessor.java | 51 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLKernel.java | 8 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLPlatform.java | 15 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLProgram.java | 60 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLSampler.java | 120 |
12 files changed, 558 insertions, 215 deletions
diff --git a/src/com/mbien/opencl/CLBuffer.java b/src/com/mbien/opencl/CLBuffer.java index f640ac1d..0a001f6b 100644 --- a/src/com/mbien/opencl/CLBuffer.java +++ b/src/com/mbien/opencl/CLBuffer.java @@ -7,7 +7,9 @@ import java.nio.DoubleBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; + import static com.mbien.opencl.CLException.*; +import static com.mbien.opencl.CL.*; /** * @@ -110,7 +112,7 @@ public class CLBuffer<B extends Buffer> implements CLResource { * This flag specifies that the memory object will be read and * written by a kernel. */ - READ_WRITE(CL.CL_MEM_READ_WRITE), + READ_WRITE(CL_MEM_READ_WRITE), /** * This flags specifies that the memory object will be written @@ -118,14 +120,14 @@ public class CLBuffer<B extends Buffer> implements CLResource { * Reading from a buffer or image object created with WRITE_ONLY * inside a kernel is undefined. */ - WRITE_ONLY(CL.CL_MEM_WRITE_ONLY), + WRITE_ONLY(CL_MEM_WRITE_ONLY), /** * This flag specifies that the memory object is a read-only memory * object when used inside a kernel. Writing to a buffer or image object * created withREAD_ONLY inside a kernel is undefined. */ - READ_ONLY(CL.CL_MEM_READ_ONLY); + READ_ONLY(CL_MEM_READ_ONLY); /** * If specified, it indicates that the application wants the OpenCL @@ -134,9 +136,9 @@ public class CLBuffer<B extends Buffer> implements CLResource { * to cache the buffer contents pointed to by host_ptr in device memory. * This cached copy can be used when kernels are executed on a device. */ -// USE_HOST_PTR(CL.CL_MEM_USE_HOST_PTR), +// USE_HOST_PTR(CL_MEM_USE_HOST_PTR), -// ALLOC_HOST_PTR(CL.CL_MEM_ALLOC_HOST_PTR), // this is the default in java world anyway +// ALLOC_HOST_PTR(CL_MEM_ALLOC_HOST_PTR), // this is the default in java world anyway /** * If CL_MEM_COPY_HOST_PTR specified, it indicates that the application @@ -144,28 +146,28 @@ public class CLBuffer<B extends Buffer> implements CLResource { * and copy the data from memory referenced by host_ptr.<br/> * COPY_HOST_PTR and USE_HOST_PTR are mutually exclusive. */ -// COPY_HOST_PTR(CL.CL_MEM_COPY_HOST_PTR); +// COPY_HOST_PTR(CL_MEM_COPY_HOST_PTR); /** * Value of wrapped OpenCL flag. */ - public final int CL_FLAG; + public final int CONFIG; - private Mem(int CL_FLAG) { - this.CL_FLAG = CL_FLAG; + private Mem(int config) { + this.CONFIG = config; } public static Mem valueOf(int bufferFlag) { switch(bufferFlag) { - case(CL.CL_MEM_READ_WRITE): + case(CL_MEM_READ_WRITE): return READ_WRITE; - case(CL.CL_MEM_READ_ONLY): + case(CL_MEM_READ_ONLY): return READ_ONLY; -// case(CL.CL_MEM_USE_HOST_PTR): +// case(CL_MEM_USE_HOST_PTR): // return USE_HOST_PTR; -// case(CL.CL_MEM_ALLOC_HOST_PTR): +// case(CL_MEM_ALLOC_HOST_PTR): // return ALLOC_HOST_PTR; -// case(CL.CL_MEM_COPY_HOST_PTR): +// case(CL_MEM_COPY_HOST_PTR): // return COPY_HOST_PTR; } return null; @@ -175,11 +177,11 @@ public class CLBuffer<B extends Buffer> implements CLResource { int clFlags = 0; if(flags != null) { for (int i = 0; i < flags.length; i++) { - clFlags |= flags[i].CL_FLAG; + clFlags |= flags[i].CONFIG; } } if(clFlags == 0) - clFlags = CL.CL_MEM_READ_WRITE; + clFlags = CL_MEM_READ_WRITE; return clFlags; } diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java index 737e8c81..3b0560ba 100644 --- a/src/com/mbien/opencl/CLCommandQueue.java +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -4,7 +4,9 @@ import java.nio.Buffer; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; + import static com.mbien.opencl.CLException.*; +import static com.mbien.opencl.CL.*; /** * The command-queue can be used to queue a set of operations in order. Having multiple @@ -30,19 +32,19 @@ public class CLCommandQueue implements CLResource { int[] status = new int[1]; this.ID = cl.clCreateCommandQueue(context.ID, device.ID, properties, status, 0); - if(status[0] != CL.CL_SUCCESS) + if(status[0] != CL_SUCCESS) throw new CLException(status[0], "can not create command queue on "+device); } public CLCommandQueue putWriteBuffer(CLBuffer<?> writeBuffer, boolean blockingWrite) { int ret = cl.clEnqueueWriteBuffer( - ID, writeBuffer.ID, blockingWrite ? CL.CL_TRUE : CL.CL_FALSE, + ID, writeBuffer.ID, blockingWrite ? CL_TRUE : CL_FALSE, 0, writeBuffer.getSizeInBytes(), writeBuffer.buffer, // 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) + if(ret != CL_SUCCESS) throw new CLException(ret, "can not enqueue WriteBuffer: " + writeBuffer); return this; @@ -51,12 +53,12 @@ public class CLCommandQueue implements CLResource { public CLCommandQueue putReadBuffer(CLBuffer<?> readBuffer, boolean blockingRead) { int ret = cl.clEnqueueReadBuffer( - ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE, + ID, readBuffer.ID, blockingRead ? CL_TRUE : CL_FALSE, 0, readBuffer.getSizeInBytes(), readBuffer.buffer, // 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) + if(ret != CL_SUCCESS) throw new CLException(ret, "can not enqueue ReadBuffer: " + readBuffer); return this; @@ -65,12 +67,12 @@ public class CLCommandQueue implements CLResource { public CLCommandQueue putReadBuffer(CLBuffer<?> readBuffer, Buffer buffer, boolean blockingRead) { int ret = cl.clEnqueueReadBuffer( - ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE, + ID, readBuffer.ID, blockingRead ? CL_TRUE : CL_FALSE, 0, readBuffer.getSizeInBytes(), buffer, // 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) + if(ret != CL_SUCCESS) throw new CLException(ret, "can not enqueue ReadBuffer: " + readBuffer); return this; @@ -175,7 +177,7 @@ public class CLCommandQueue implements CLResource { null, 0, null, 0 ); - if(ret != CL.CL_SUCCESS) + if(ret != CL_SUCCESS) throw new CLException(ret, "can not enqueue NDRangeKernel: " + kernel); return this; @@ -186,7 +188,7 @@ public class CLCommandQueue implements CLResource { CLGLI xl = (CLGLI) cl; int ret = xl.clEnqueueAcquireGLObjects(ID, 1, new long[] {glObject}, 0, 0, null, 0, null, 0); - if(ret != CL.CL_SUCCESS) + if(ret != CL_SUCCESS) throw new CLException(ret, "can not aquire GLObject: " + glObject); return this; @@ -196,7 +198,7 @@ public class CLCommandQueue implements CLResource { CLGLI xl = (CLGLI) cl; int ret = xl.clEnqueueReleaseGLObjects(ID, 1, new long[] {glObject}, 0, 0, null, 0, null, 0); - if(ret != CL.CL_SUCCESS) + if(ret != CL_SUCCESS) throw new CLException(ret, "can not release GLObject: " + glObject); return this; @@ -252,26 +254,26 @@ public class CLCommandQueue implements CLResource { /** * CL_DEVICE_TYPE_CPU */ - OUT_OF_ORDER_EXEC_MODE(CL.CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE), + OUT_OF_ORDER_EXEC_MODE(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE), /** * CL_DEVICE_TYPE_GPU */ - PROFILING_MODE(CL.CL_QUEUE_PROFILING_ENABLE); + PROFILING_MODE(CL_QUEUE_PROFILING_ENABLE); /** * Value of wrapped OpenCL device type. */ - public final int CL_QUEUE_MODE; + public final int QUEUE_MODE; - private Mode(int CL_VALUE) { - this.CL_QUEUE_MODE = CL_VALUE; + private Mode(int value) { + this.QUEUE_MODE = value; } public static Mode valueOf(int queueMode) { switch(queueMode) { - case(CL.CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE): + case(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE): return OUT_OF_ORDER_EXEC_MODE; - case(CL.CL_QUEUE_PROFILING_ENABLE): + case(CL_QUEUE_PROFILING_ENABLE): return PROFILING_MODE; } return null; @@ -281,7 +283,7 @@ public class CLCommandQueue implements CLResource { List<Mode> matching = new ArrayList<Mode>(); Mode[] values = Mode.values(); for (Mode value : values) { - if((value.CL_QUEUE_MODE & bitfield) != 0) + if((value.QUEUE_MODE & bitfield) != 0) matching.add(value); } if(matching.isEmpty()) diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 75e4d35a..a272fbc3 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -1,6 +1,8 @@ package com.mbien.opencl; import com.mbien.opencl.CLBuffer.Mem; +import com.mbien.opencl.CLSampler.AddressingMode; +import com.mbien.opencl.CLSampler.FilteringMode; import com.sun.gluegen.runtime.CPU; import java.io.BufferedReader; import java.io.IOException; @@ -37,6 +39,7 @@ public class CLContext implements CLResource { protected CLDevice[] devices; protected final List<CLProgram> programs; + protected final List<CLSampler> samplers; protected final List<CLBuffer<? extends Buffer>> buffers; protected final Map<CLDevice, List<CLCommandQueue>> queuesMap; @@ -45,6 +48,7 @@ public class CLContext implements CLResource { this.cl = CLPlatform.getLowLevelBinding(); this.ID = contextID; this.programs = new ArrayList<CLProgram>(); + this.samplers = new ArrayList<CLSampler>(); this.buffers = new ArrayList<CLBuffer<? extends Buffer>>(); this.queuesMap = new HashMap<CLDevice, List<CLCommandQueue>>(); } @@ -110,7 +114,7 @@ public class CLContext implements CLResource { long type = 0; if(deviceTypes != null) { for (int i = 0; i < deviceTypes.length; i++) { - type |= deviceTypes[i].CL_TYPE; + type |= deviceTypes[i].TYPE; } } @@ -170,7 +174,7 @@ public class CLContext implements CLResource { * 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); + CLProgram program = new CLProgram(this, src); programs.add(program); return program; } @@ -277,6 +281,12 @@ public class CLContext implements CLResource { return queue; } + public CLSampler createSampler(AddressingMode addrMode, FilteringMode filtMode, boolean normalizedCoords) { + CLSampler sampler = new CLSampler(this, addrMode, filtMode, normalizedCoords); + samplers.add(sampler); + return sampler; + } + void onProgramReleased(CLProgram program) { programs.remove(program); } @@ -293,6 +303,10 @@ public class CLContext implements CLResource { queuesMap.remove(device); } + void onSamplerReleased(CLSampler sampler) { + samplers.remove(sampler); + } + /** * Releases the context and all resources. */ @@ -305,6 +319,18 @@ public class CLContext implements CLResource { while(!buffers.isEmpty()) buffers.get(0).release(); + while(!samplers.isEmpty()) + samplers.get(0).release(); + + for (CLDevice device : devices) { + List<CLCommandQueue> list = queuesMap.get(device); + if(list != null) { + while(!list.isEmpty()) { + list.get(0).release(); + } + } + } + int ret = cl.clReleaseContext(ID); checkForError(ret, "error releasing context"); } diff --git a/src/com/mbien/opencl/CLDevice.java b/src/com/mbien/opencl/CLDevice.java index c6a632aa..ed745e80 100644 --- a/src/com/mbien/opencl/CLDevice.java +++ b/src/com/mbien/opencl/CLDevice.java @@ -1,7 +1,6 @@ package com.mbien.opencl; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; +import java.nio.Buffer; import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; @@ -10,7 +9,7 @@ import java.util.List; import java.util.Scanner; import java.util.Set; -import static com.mbien.opencl.CLException.*; +import static com.mbien.opencl.CL.*; /** * @@ -20,6 +19,8 @@ public final class CLDevice { private final CL cl; private CLContext context; + + private final CLDeviceInfoAccessor deviceInfo; /** * OpenCL device id for this device. @@ -29,12 +30,14 @@ public final class CLDevice { CLDevice(CL cl, long id) { this.cl = cl; this.ID = id; + this.deviceInfo = new CLDeviceInfoAccessor(); } CLDevice(CLContext context, long id) { this.context = context; this.cl = context.cl; this.ID = id; + this.deviceInfo = new CLDeviceInfoAccessor(); } public CLCommandQueue createCommandQueue() { @@ -42,14 +45,14 @@ public final class CLDevice { } public CLCommandQueue createCommandQueue(CLCommandQueue.Mode property) { - return createCommandQueue(property.CL_QUEUE_MODE); + return createCommandQueue(property.QUEUE_MODE); } public CLCommandQueue createCommandQueue(CLCommandQueue.Mode... properties) { int flags = 0; if(properties != null) { for (int i = 0; i < properties.length; i++) { - flags |= properties[i].CL_QUEUE_MODE; + flags |= properties[i].QUEUE_MODE; } } return createCommandQueue(flags); @@ -70,28 +73,28 @@ public final class CLDevice { * Returns the name of this device. */ public String getName() { - return getInfoString(CL.CL_DEVICE_NAME); + return deviceInfo.getString(CL_DEVICE_NAME); } /** * Returns the OpenCL profile of this device. */ public String getProfile() { - return getInfoString(CL.CL_DEVICE_PROFILE); + return deviceInfo.getString(CL_DEVICE_PROFILE); } /** * Returns the vendor of this device. */ public String getVendor() { - return getInfoString(CL.CL_DEVICE_VENDOR); + return deviceInfo.getString(CL_DEVICE_VENDOR); } /** * Returns the type of this device. */ public Type getType() { - return Type.valueOf((int)getInfoLong(CL.CL_DEVICE_TYPE)); + return Type.valueOf((int)deviceInfo.getLong(CL_DEVICE_TYPE)); } /** @@ -99,7 +102,7 @@ public final class CLDevice { * The minimum value is 1. */ public int getMaxComputeUnits() { - return (int) getInfoLong(CL.CL_DEVICE_MAX_COMPUTE_UNITS); + return (int) deviceInfo.getLong(CL_DEVICE_MAX_COMPUTE_UNITS); } /** @@ -108,14 +111,14 @@ public final class CLDevice { * The minimum value is 1. */ public int getMaxWorkGroupSize() { - return (int) getInfoLong(CL.CL_DEVICE_MAX_WORK_GROUP_SIZE); + return (int) deviceInfo.getLong(CL_DEVICE_MAX_WORK_GROUP_SIZE); } /** * Returns the maximum configured clock frequency of the device in MHz. */ public int getMaxClockFrequency() { - return (int) (getInfoLong(CL.CL_DEVICE_MAX_CLOCK_FREQUENCY)); + return (int) (deviceInfo.getLong(CL_DEVICE_MAX_CLOCK_FREQUENCY)); } /** @@ -124,21 +127,21 @@ public final class CLDevice { * The minimum value is 3. */ public int getMaxWorkItemDimensions() { - return (int) getInfoLong(CL.CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS); + return (int) deviceInfo.getLong(CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS); } /** * Returns the global memory size in bytes. */ public long getGlobalMemSize() { - return getInfoLong(CL.CL_DEVICE_GLOBAL_MEM_SIZE); + return deviceInfo.getLong(CL_DEVICE_GLOBAL_MEM_SIZE); } /** * Returns the local memory size in bytes. */ public long getLocalMemSize() { - return getInfoLong(CL.CL_DEVICE_LOCAL_MEM_SIZE); + return deviceInfo.getLong(CL_DEVICE_LOCAL_MEM_SIZE); } /** @@ -146,28 +149,28 @@ public final class CLDevice { * The minimum value is 64 KB. */ public long getMaxConstantBufferSize() { - return getInfoLong(CL.CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE); + return deviceInfo.getLong(CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE); } /** * Returns the size of global memory cache line in bytes. */ public long getGlobalMemCachlineSize() { - return getInfoLong(CL.CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE); + return deviceInfo.getLong(CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE); } /** * Returns the size of global memory cache in bytes. */ public long getGlobalMemCachSize() { - return getInfoLong(CL.CL_DEVICE_GLOBAL_MEM_CACHE_SIZE); + return deviceInfo.getLong(CL_DEVICE_GLOBAL_MEM_CACHE_SIZE); } /** * Returns true if images are supported by the OpenCL device and false otherwise. */ public boolean isImageSupportAvailable() { - return getInfoLong(CL.CL_DEVICE_IMAGE_SUPPORT) == CL.CL_TRUE; + return deviceInfo.getLong(CL_DEVICE_IMAGE_SUPPORT) == CL_TRUE; } /** @@ -175,7 +178,7 @@ public final class CLDevice { * The minimum value is 128 if image support is available. */ public int getMaxReadImageArgs() { - return (int)getInfoLong(CL.CL_DEVICE_MAX_READ_IMAGE_ARGS); + return (int)deviceInfo.getLong(CL_DEVICE_MAX_READ_IMAGE_ARGS); } /** @@ -183,7 +186,7 @@ public final class CLDevice { * The minimum value is 8 if image support is available. */ public int getMaxWriteImageArgs() { - return (int)getInfoLong(CL.CL_DEVICE_MAX_WRITE_IMAGE_ARGS); + return (int)deviceInfo.getLong(CL_DEVICE_MAX_WRITE_IMAGE_ARGS); } /** @@ -191,7 +194,7 @@ public final class CLDevice { * image support is available. */ public int getMaxImage2dWidth() { - return (int)getInfoLong(CL.CL_DEVICE_IMAGE2D_MAX_WIDTH); + return (int)deviceInfo.getLong(CL_DEVICE_IMAGE2D_MAX_WIDTH); } /** @@ -199,7 +202,7 @@ public final class CLDevice { * image support is available. */ public int getMaxImage2dHeight() { - return (int)getInfoLong(CL.CL_DEVICE_IMAGE2D_MAX_HEIGHT); + return (int)deviceInfo.getLong(CL_DEVICE_IMAGE2D_MAX_HEIGHT); } /** @@ -207,7 +210,7 @@ public final class CLDevice { * image support is available. */ public int getMaxImage3dWidth() { - return (int)getInfoLong(CL.CL_DEVICE_IMAGE3D_MAX_WIDTH); + return (int)deviceInfo.getLong(CL_DEVICE_IMAGE3D_MAX_WIDTH); } /** @@ -215,7 +218,7 @@ public final class CLDevice { * image support is available. */ public int getMaxImage3dHeight() { - return (int)getInfoLong(CL.CL_DEVICE_IMAGE3D_MAX_HEIGHT); + return (int)deviceInfo.getLong(CL_DEVICE_IMAGE3D_MAX_HEIGHT); } /** @@ -223,7 +226,7 @@ public final class CLDevice { * image support is available. */ public int getMaxImage3dDepth() { - return (int)getInfoLong(CL.CL_DEVICE_IMAGE3D_MAX_DEPTH); + return (int)deviceInfo.getLong(CL_DEVICE_IMAGE3D_MAX_DEPTH); } /** @@ -231,49 +234,49 @@ public final class CLDevice { * minimum value is 16 if image support is available. */ public int getMaxSamplers() { - return (int)getInfoLong(CL.CL_DEVICE_MAX_SAMPLERS); + return (int)deviceInfo.getLong(CL_DEVICE_MAX_SAMPLERS); } /** * Returns the resolution of device timer. This is measured in nanoseconds. */ public long getProfilingTimerResolution() { - return getInfoLong(CL.CL_DEVICE_PROFILING_TIMER_RESOLUTION); + return deviceInfo.getLong(CL_DEVICE_PROFILING_TIMER_RESOLUTION); } /** * Returns the single precision floating-point capability of the device. */ public EnumSet<SingleFPConfig> getSingleFPConfig() { - return SingleFPConfig.valuesOf((int)getInfoLong(CL.CL_DEVICE_SINGLE_FP_CONFIG)); + return SingleFPConfig.valuesOf((int)deviceInfo.getLong(CL_DEVICE_SINGLE_FP_CONFIG)); } /** * Returns the local memory type. */ public LocalMemType getLocalMemType() { - return LocalMemType.valueOf((int)getInfoLong(CL.CL_DEVICE_LOCAL_MEM_TYPE)); + return LocalMemType.valueOf((int)deviceInfo.getLong(CL_DEVICE_LOCAL_MEM_TYPE)); } /** * Returns the type of global memory cache supported. */ public GlobalMemCacheType getGlobalMemCacheType() { - return GlobalMemCacheType.valueOf((int)getInfoLong(CL.CL_DEVICE_GLOBAL_MEM_CACHE_TYPE)); + return GlobalMemCacheType.valueOf((int)deviceInfo.getLong(CL_DEVICE_GLOBAL_MEM_CACHE_TYPE)); } /** * Returns the command-queue properties properties supported by the device. */ public EnumSet<CLCommandQueue.Mode> getQueueProperties() { - return CLCommandQueue.Mode.valuesOf((int)getInfoLong(CL.CL_DEVICE_QUEUE_PROPERTIES)); + return CLCommandQueue.Mode.valuesOf((int)deviceInfo.getLong(CL_DEVICE_QUEUE_PROPERTIES)); } /** * Returns true if this device is available. */ public boolean isAvailable() { - return getInfoLong(CL.CL_DEVICE_AVAILABLE) == CL.CL_TRUE; + return deviceInfo.getLong(CL_DEVICE_AVAILABLE) == CL_TRUE; } /** @@ -282,14 +285,14 @@ public final class CLDevice { * This can be false for the OpenCL ES profile only. */ public boolean isCompilerAvailable() { - return getInfoLong(CL.CL_DEVICE_COMPILER_AVAILABLE) == CL.CL_TRUE; + return deviceInfo.getLong(CL_DEVICE_COMPILER_AVAILABLE) == CL_TRUE; } /** * Returns true if the OpenCL device is a little endian device and false otherwise. */ public boolean isLittleEndianAvailable() { - return getInfoLong(CL.CL_DEVICE_ENDIAN_LITTLE) == CL.CL_TRUE; + return deviceInfo.getLong(CL_DEVICE_ENDIAN_LITTLE) == CL_TRUE; } /** @@ -298,7 +301,7 @@ public final class CLDevice { * implement error correction. */ public boolean isErrorCorrectionSupported() { - return getInfoLong(CL.CL_DEVICE_ERROR_CORRECTION_SUPPORT) == CL.CL_TRUE; + return deviceInfo.getLong(CL_DEVICE_ERROR_CORRECTION_SUPPORT) == CL_TRUE; } /** @@ -306,7 +309,7 @@ public final class CLDevice { */ public Set<String> getExtensions() { - String ext = getInfoString(CL.CL_DEVICE_EXTENSIONS); + String ext = deviceInfo.getString(CL_DEVICE_EXTENSIONS); Scanner scanner = new Scanner(ext); Set<String> extSet = new HashSet<String>(); @@ -317,29 +320,14 @@ public final class CLDevice { return Collections.unmodifiableSet(extSet); } - private final long getInfoLong(int key) { - - ByteBuffer bb = ByteBuffer.allocate(8); - bb.order(ByteOrder.nativeOrder()); - - int ret = cl.clGetDeviceInfo(ID, key, bb.capacity(), bb, null, 0); - - checkForError(ret, "can not receive device info"); - - return bb.getLong(); - } - - public final String getInfoString(int key) { - long[] longBuffer = new long[1]; - ByteBuffer bb = ByteBuffer.allocate(512); // TODO use a cache + private class CLDeviceInfoAccessor extends CLInfoAccessor { - int ret = cl.clGetDeviceInfo(ID, key, bb.capacity(), bb, longBuffer, 0); - - checkForError(ret, "can not receive device info string"); + @Override + protected int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset) { + return cl.clGetDeviceInfo(ID, name, valueSize, value, valueSizeRet, valueSizeRetOffset); + } - return CLUtils.clString2JavaString(bb.array(), (int)longBuffer[0]); - } @@ -380,48 +368,48 @@ public final class CLDevice { /** * CL_DEVICE_TYPE_CPU */ - CPU(CL.CL_DEVICE_TYPE_CPU), + CPU(CL_DEVICE_TYPE_CPU), /** * CL_DEVICE_TYPE_GPU */ - GPU(CL.CL_DEVICE_TYPE_GPU), + GPU(CL_DEVICE_TYPE_GPU), /** * CL_DEVICE_TYPE_ACCELERATOR */ - ACCELERATOR(CL.CL_DEVICE_TYPE_ACCELERATOR), + ACCELERATOR(CL_DEVICE_TYPE_ACCELERATOR), /** * CL_DEVICE_TYPE_DEFAULT. This type can be used for creating a context on * the default device, a single device can never have this type. */ - DEFAULT(CL.CL_DEVICE_TYPE_DEFAULT), + DEFAULT(CL_DEVICE_TYPE_DEFAULT), /** * CL_DEVICE_TYPE_ALL. This type can be used for creating a context on * all devices, a single device can never have this type. */ - ALL(CL.CL_DEVICE_TYPE_ALL); + ALL(CL_DEVICE_TYPE_ALL); /** * Value of wrapped OpenCL device type. */ - public final long CL_TYPE; + public final long TYPE; - private Type(long CL_TYPE) { - this.CL_TYPE = CL_TYPE; + private Type(long type) { + this.TYPE = type; } public static Type valueOf(long clDeviceType) { - if(clDeviceType == CL.CL_DEVICE_TYPE_ALL) + if(clDeviceType == CL_DEVICE_TYPE_ALL) return ALL; switch((int)clDeviceType) { - case(CL.CL_DEVICE_TYPE_DEFAULT): + case(CL_DEVICE_TYPE_DEFAULT): return DEFAULT; - case(CL.CL_DEVICE_TYPE_CPU): + case(CL_DEVICE_TYPE_CPU): return CPU; - case(CL.CL_DEVICE_TYPE_GPU): + case(CL_DEVICE_TYPE_GPU): return GPU; - case(CL.CL_DEVICE_TYPE_ACCELERATOR): + case(CL_DEVICE_TYPE_ACCELERATOR): return ACCELERATOR; } return null; @@ -437,41 +425,41 @@ public final class CLDevice { /** * denorms are supported. */ - DENORM(CL.CL_FP_DENORM), + DENORM(CL_FP_DENORM), /** * INF and quiet NaNs are supported. */ - INF_NAN(CL.CL_FP_INF_NAN), + INF_NAN(CL_FP_INF_NAN), /** * round to nearest rounding mode supported. */ - ROUND_TO_NEAREST(CL.CL_FP_ROUND_TO_NEAREST), + ROUND_TO_NEAREST(CL_FP_ROUND_TO_NEAREST), /** * round to +ve and –ve infinity rounding modes supported. */ - ROUND_TO_INF(CL.CL_FP_ROUND_TO_INF), + ROUND_TO_INF(CL_FP_ROUND_TO_INF), /** * round to zero rounding mode supported. */ - ROUND_TO_ZERO(CL.CL_FP_ROUND_TO_ZERO), + ROUND_TO_ZERO(CL_FP_ROUND_TO_ZERO), /** * IEEE754-2008 fused multiply-add is supported. */ - FMA(CL.CL_FP_FMA); + FMA(CL_FP_FMA); /** * Value of wrapped OpenCL bitfield. */ - public final int CL_VALUE; + public final int CONFIG; - private SingleFPConfig(int CL_VALUE) { - this.CL_VALUE = CL_VALUE; + private SingleFPConfig(int config) { + this.CONFIG = config; } /** @@ -481,7 +469,7 @@ public final class CLDevice { List<SingleFPConfig> matching = new ArrayList<SingleFPConfig>(); SingleFPConfig[] values = SingleFPConfig.values(); for (SingleFPConfig value : values) { - if((value.CL_VALUE & bitfield) != 0) + if((value.CONFIG & bitfield) != 0) matching.add(value); } if(matching.isEmpty()) @@ -500,26 +488,26 @@ public final class CLDevice { /** * Global memory cache not supported. */ - NONE(CL.CL_NONE), + NONE(CL_NONE), /** * Read only cache. */ - READ_ONLY(CL.CL_READ_ONLY_CACHE), + READ_ONLY(CL_READ_ONLY_CACHE), /** * Read-write cache. */ - READ_WRITE(CL.CL_READ_WRITE_CACHE); + READ_WRITE(CL_READ_WRITE_CACHE); /** * Value of wrapped OpenCL value. */ - public final int CL_VALUE; + public final int TYPE; - private GlobalMemCacheType(int CL_VALUE) { - this.CL_VALUE = CL_VALUE; + private GlobalMemCacheType(int type) { + this.TYPE = type; } /** @@ -528,7 +516,7 @@ public final class CLDevice { public static GlobalMemCacheType valueOf(int bitfield) { GlobalMemCacheType[] values = GlobalMemCacheType.values(); for (GlobalMemCacheType value : values) { - if(value.CL_VALUE == bitfield) + if(value.TYPE == bitfield) return value; } return null; @@ -543,29 +531,29 @@ public final class CLDevice { /** * GLOBAL implies that no dedicated memory storage is available (global mem is used instead). */ - GLOBAL(CL.CL_GLOBAL), + GLOBAL(CL_GLOBAL), /** * LOCAL implies dedicated local memory storage such as SRAM. */ - LOCAL(CL.CL_LOCAL); + LOCAL(CL_LOCAL); /** * Value of wrapped OpenCL value. */ - public final int CL_VALUE; + public final int TYPE; - private LocalMemType(int CL_VALUE) { - this.CL_VALUE = CL_VALUE; + private LocalMemType(int type) { + this.TYPE = type; } /** * Returns the matching LocalMemCacheType for the given cl type. */ public static LocalMemType valueOf(int clLocalCacheType) { - if(clLocalCacheType == CL.CL_GLOBAL) + if(clLocalCacheType == CL_GLOBAL) return LOCAL; - else if(clLocalCacheType == CL.CL_LOCAL) + else if(clLocalCacheType == CL_LOCAL) return GLOBAL; return null; } diff --git a/src/com/mbien/opencl/CLEvent.java b/src/com/mbien/opencl/CLEvent.java new file mode 100644 index 00000000..c4b62917 --- /dev/null +++ b/src/com/mbien/opencl/CLEvent.java @@ -0,0 +1,138 @@ +package com.mbien.opencl; + +import java.nio.Buffer; + +import static com.mbien.opencl.CL.*; + +/** + * + * @author Michael Bien + */ +public class CLEvent implements CLResource { + + public final CLContext context; + public final long ID; + + private final CL cl; + + private final CLEventInfoAccessor eventInfo; + + CLEvent(CLContext context, int id) { + this.context = context; + this.cl = context.cl; + this.ID = id; + this.eventInfo = new CLEventInfoAccessor(); + } + + public void release() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public ExecutionStatus getStatus() { + int status = (int)eventInfo.getLong(CL_EVENT_COMMAND_EXECUTION_STATUS); + return ExecutionStatus.valueOf(status); + } + + public CommandType getType() { + int status = (int)eventInfo.getLong(CL_EVENT_COMMAND_TYPE); + return CommandType.valueOf(status); + } + + private class CLEventInfoAccessor extends CLInfoAccessor { + + @Override + protected int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset) { + return cl.clGetEventInfo(ID, name, valueSize, value, valueSizeRet, valueSizeRetOffset); + } + + } + + public enum ExecutionStatus { + + /** + * Command has been enqueued in the command-queue. + */ + QUEUED(CL_QUEUED), + + /** + * Enqueued command has been submitted by the host to the device + * associated with the command-queue. + */ + SUBMITTED(CL_SUBMITTED), + + /** + * Device is currently executing this command. + */ + RUNNING(CL_RUNNING), + + /** + * The command has completed. + */ + COMPLETE(CL_COMPLETE); + + + /** + * Value of wrapped OpenCL command excecution status. + */ + public final int STATUS; + + private ExecutionStatus(int status) { + this.STATUS = status; + } + + public static ExecutionStatus valueOf(int status) { + switch(status) { + case(CL_QUEUED): + return QUEUED; + case(CL_SUBMITTED): + return SUBMITTED; + case(CL_RUNNING): + return RUNNING; + case(CL_COMPLETE): + return COMPLETE; + } + return null; + } + } + + public enum CommandType { + + NDRANGE_KERNEL(CL_COMMAND_NDRANGE_KERNEL), + TASK(CL_COMMAND_TASK), + NATIVE_KERNEL(CL_COMMAND_NATIVE_KERNEL), + READ_BUFFER(CL_COMMAND_READ_BUFFER), + WRITE_BUFFER(CL_COMMAND_WRITE_BUFFER), + COPY_BUFFER(CL_COMMAND_COPY_BUFFER), + READ_IMAGE(CL_COMMAND_READ_IMAGE), + WRITE_IMAGE(CL_COMMAND_WRITE_IMAGE), + COPY_IMAGE(CL_COMMAND_COPY_IMAGE), + COPY_BUFFER_TO_IMAGE(CL_COMMAND_COPY_BUFFER_TO_IMAGE), + COPY_IMAGE_TO_BUFFER(CL_COMMAND_COPY_IMAGE_TO_BUFFER), + MAP_BUFFER(CL_COMMAND_MAP_BUFFER), + MAP_IMAGE(CL_COMMAND_MAP_IMAGE), + UNMAP_MEM_OBJECT(CL_COMMAND_UNMAP_MEM_OBJECT), + MARKER(CL_COMMAND_MARKER), + ACQUIRE_GL_OBJECTS(CL_COMMAND_ACQUIRE_GL_OBJECTS), + RELEASE_GL_OBJECTS(CL_COMMAND_RELEASE_GL_OBJECTS); + + /** + * Value of wrapped OpenCL command type. + */ + public final int TYPE; + + private CommandType(int type) { + this.TYPE = type; + } + + public static CommandType valueOf(int commandType) { + CommandType[] values = CommandType.values(); + for (CommandType value : values) { + if(value.TYPE == commandType) + return value; + } + return null; + } + + } + +} diff --git a/src/com/mbien/opencl/CLException.java b/src/com/mbien/opencl/CLException.java index 9e8adb77..35c539c4 100644 --- a/src/com/mbien/opencl/CLException.java +++ b/src/com/mbien/opencl/CLException.java @@ -1,5 +1,7 @@ package com.mbien.opencl; +import static com.mbien.opencl.CL.*; + /** * Main Exception type for runtime OpenCL errors and unsuccessful function calls (e.g. returning other values than CL_SUCCESS). * @author Michael Bien @@ -16,7 +18,7 @@ public class CLException extends RuntimeException { } public static final void checkForError(int status, String message) { - if(status != CL.CL_SUCCESS) + if(status != CL_SUCCESS) throw new CLException(status, message); } @@ -24,146 +26,146 @@ public class CLException extends RuntimeException { private static final String identifyError(int error) { switch (error) { - case CL.CL_DEVICE_NOT_FOUND: + case CL_DEVICE_NOT_FOUND: return "CL_DEVICE_NOT_FOUND"; - case CL.CL_DEVICE_NOT_AVAILABLE: + case CL_DEVICE_NOT_AVAILABLE: return "CL_DEVICE_NOT_AVAILABLE"; - case CL.CL_COMPILER_NOT_AVAILABLE: + case CL_COMPILER_NOT_AVAILABLE: return "CL_COMPILER_NOT_AVAILABLE"; - case CL.CL_MEM_OBJECT_ALLOCATION_FAILURE: + case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "CL_MEM_OBJECT_ALLOCATION_FAILURE"; - case CL.CL_OUT_OF_RESOURCES: + case CL_OUT_OF_RESOURCES: return "CL_OUT_OF_RESOURCES"; - case CL.CL_OUT_OF_HOST_MEMORY: + case CL_OUT_OF_HOST_MEMORY: return "CL_OUT_OF_HOST_MEMORY"; - case CL.CL_PROFILING_INFO_NOT_AVAILABLE: + case CL_PROFILING_INFO_NOT_AVAILABLE: return "CL_PROFILING_INFO_NOT_AVAILABLE"; - case CL.CL_MEM_COPY_OVERLAP: + case CL_MEM_COPY_OVERLAP: return "CL_MEM_COPY_OVERLAP"; - case CL.CL_IMAGE_FORMAT_MISMATCH: + case CL_IMAGE_FORMAT_MISMATCH: return "CL_IMAGE_FORMAT_MISMATCH"; - case CL.CL_IMAGE_FORMAT_NOT_SUPPORTED: + case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "CL_IMAGE_FORMAT_NOT_SUPPORTED"; - case CL.CL_BUILD_PROGRAM_FAILURE: + case CL_BUILD_PROGRAM_FAILURE: return "CL_BUILD_PROGRAM_FAILURE"; - case CL.CL_MAP_FAILURE: + case CL_MAP_FAILURE: return "CL_MAP_FAILURE"; - case CL.CL_INVALID_VALUE: + case CL_INVALID_VALUE: return "CL_INVALID_VALUE"; - case CL.CL_INVALID_DEVICE_TYPE: + case CL_INVALID_DEVICE_TYPE: return "CL_INVALID_DEVICE_TYPE"; - case CL.CL_INVALID_PLATFORM: + case CL_INVALID_PLATFORM: return "CL_INVALID_PLATFORM"; - case CL.CL_INVALID_DEVICE: + case CL_INVALID_DEVICE: return "CL_INVALID_DEVICE"; - case CL.CL_INVALID_CONTEXT: + case CL_INVALID_CONTEXT: return "CL_INVALID_CONTEXT"; - case CL.CL_INVALID_QUEUE_PROPERTIES: + case CL_INVALID_QUEUE_PROPERTIES: return "CL_INVALID_QUEUE_PROPERTIES"; - case CL.CL_INVALID_COMMAND_QUEUE: + case CL_INVALID_COMMAND_QUEUE: return "CL_INVALID_COMMAND_QUEUE"; - case CL.CL_INVALID_HOST_PTR: + case CL_INVALID_HOST_PTR: return "CL_INVALID_HOST_PTR"; - case CL.CL_INVALID_MEM_OBJECT: + case CL_INVALID_MEM_OBJECT: return "CL_INVALID_MEM_OBJECT"; - case CL.CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: + case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR"; - case CL.CL_INVALID_IMAGE_SIZE: + case CL_INVALID_IMAGE_SIZE: return "CL_INVALID_IMAGE_SIZE"; - case CL.CL_INVALID_SAMPLER: + case CL_INVALID_SAMPLER: return "CL_INVALID_SAMPLER"; - case CL.CL_INVALID_BINARY: + case CL_INVALID_BINARY: return "CL_INVALID_BINARY"; - case CL.CL_INVALID_BUILD_OPTIONS: + case CL_INVALID_BUILD_OPTIONS: return "CL_INVALID_BUILD_OPTIONS"; - case CL.CL_INVALID_PROGRAM: + case CL_INVALID_PROGRAM: return "CL_INVALID_PROGRAM"; - case CL.CL_INVALID_PROGRAM_EXECUTABLE: + case CL_INVALID_PROGRAM_EXECUTABLE: return "CL_INVALID_PROGRAM_EXECUTABLE"; - case CL.CL_INVALID_KERNEL_NAME: + case CL_INVALID_KERNEL_NAME: return "CL_INVALID_KERNEL_NAME"; - case CL.CL_INVALID_KERNEL_DEFINITION: + case CL_INVALID_KERNEL_DEFINITION: return "CL_INVALID_KERNEL_DEFINITION"; - case CL.CL_INVALID_KERNEL: + case CL_INVALID_KERNEL: return "CL_INVALID_KERNEL"; - case CL.CL_INVALID_ARG_INDEX: + case CL_INVALID_ARG_INDEX: return "CL_INVALID_ARG_INDEX"; - case CL.CL_INVALID_ARG_VALUE: + case CL_INVALID_ARG_VALUE: return "CL_INVALID_ARG_VALUE"; - case CL.CL_INVALID_ARG_SIZE: + case CL_INVALID_ARG_SIZE: return "CL_INVALID_ARG_SIZE"; - case CL.CL_INVALID_KERNEL_ARGS: + case CL_INVALID_KERNEL_ARGS: return "CL_INVALID_KERNEL_ARGS"; - case CL.CL_INVALID_WORK_DIMENSION: + case CL_INVALID_WORK_DIMENSION: return "CL_INVALID_WORK_DIMENSION"; - case CL.CL_INVALID_WORK_GROUP_SIZE: + case CL_INVALID_WORK_GROUP_SIZE: return "CL_INVALID_WORK_GROUP_SIZE"; - case CL.CL_INVALID_WORK_ITEM_SIZE: + case CL_INVALID_WORK_ITEM_SIZE: return "CL_INVALID_WORK_ITEM_SIZE"; - case CL.CL_INVALID_GLOBAL_OFFSET: + case CL_INVALID_GLOBAL_OFFSET: return "CL_INVALID_GLOBAL_OFFSET"; - case CL.CL_INVALID_EVENT_WAIT_LIST: + case CL_INVALID_EVENT_WAIT_LIST: return "CL_INVALID_EVENT_WAIT_LIST"; - case CL.CL_INVALID_EVENT: + case CL_INVALID_EVENT: return "CL_INVALID_EVENT"; - case CL.CL_INVALID_OPERATION: + case CL_INVALID_OPERATION: return "CL_INVALID_OPERATION"; - case CL.CL_INVALID_GL_OBJECT: + case CL_INVALID_GL_OBJECT: return "CL_INVALID_GL_OBJECT"; - case CL.CL_INVALID_BUFFER_SIZE: + case CL_INVALID_BUFFER_SIZE: return "CL_INVALID_BUFFER_SIZE"; - case CL.CL_INVALID_MIP_LEVEL: + case CL_INVALID_MIP_LEVEL: return "CL_INVALID_MIP_LEVEL"; - case CL.CL_INVALID_GLOBAL_WORK_SIZE: + case CL_INVALID_GLOBAL_WORK_SIZE: return "CL_INVALID_GLOBAL_WORK_SIZE or CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR"; // error-code conflict with CL_INVALID_GLOBAL_WORK_SIZE -// case CL.CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR: +// case CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR: // return "CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR"; default: diff --git a/src/com/mbien/opencl/CLGLContext.java b/src/com/mbien/opencl/CLGLContext.java index 407c7311..abfe4b05 100644 --- a/src/com/mbien/opencl/CLGLContext.java +++ b/src/com/mbien/opencl/CLGLContext.java @@ -6,11 +6,12 @@ import com.sun.opengl.impl.GLContextImpl; import com.sun.opengl.impl.macosx.cgl.MacOSXCGLContext; import com.sun.opengl.impl.windows.wgl.WindowsWGLContext; import com.sun.opengl.impl.x11.glx.X11GLXContext; -import com.sun.opengl.impl.x11.glx.X11GLXGraphicsConfiguration; import java.nio.LongBuffer; import javax.media.nativewindow.DefaultGraphicsConfiguration; import javax.media.opengl.GLContext; +import static com.mbien.opencl.CL.*; + /** * * @author Michael Bien @@ -69,7 +70,7 @@ public final class CLGLContext extends CLContext { properties.put(0).rewind(); // 0 terminated array - long clID = createContextFromType(properties, CL.CL_DEVICE_TYPE_ALL); + long clID = createContextFromType(properties, CL_DEVICE_TYPE_ALL); return new CLGLContext(clID, glID); } diff --git a/src/com/mbien/opencl/CLInfoAccessor.java b/src/com/mbien/opencl/CLInfoAccessor.java new file mode 100644 index 00000000..e366f7ea --- /dev/null +++ b/src/com/mbien/opencl/CLInfoAccessor.java @@ -0,0 +1,51 @@ +package com.mbien.opencl; + +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import static com.mbien.opencl.CLException.*; + +/** + * Internal utility for common OpenCL clGetFooInfo calls. + * @author Michael Bien + */ +abstract class CLInfoAccessor { + + private final static ByteBuffer buffer; + private final static long[] longBuffer; + + // TODO revisit for command queue concurrency + // TODO use direct memory code path as soon gluegen is fixed + static{ + buffer = ByteBuffer.allocate(512); + buffer.order(ByteOrder.nativeOrder()); + longBuffer = new long[1]; + } + + public CLInfoAccessor() { + } + + final long getLong(int key) { + + buffer.rewind(); + int ret = getInfo(key, 8, buffer, null, 0); + checkForError(ret, "error while asking for info value"); + + return buffer.getLong(); + } + + final String getString(int key) { + + buffer.rewind(); + int ret = getInfo(key, buffer.capacity(), buffer, longBuffer, 0); + checkForError(ret, "error while asking for info string"); + + return CLUtils.clString2JavaString(buffer.array(), (int)longBuffer[0]); + + } + + protected abstract int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset); + + +} diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java index 87474878..e7ac4b4d 100644 --- a/src/com/mbien/opencl/CLKernel.java +++ b/src/com/mbien/opencl/CLKernel.java @@ -5,7 +5,9 @@ import com.sun.gluegen.runtime.CPU; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; + import static com.mbien.opencl.CLException.*; +import static com.mbien.opencl.CL.*; /** * High level abstraction for an OpenCL Kernel. @@ -34,18 +36,18 @@ public class CLKernel implements CLResource { long[] longArray = new long[1]; // get function name - int ret = cl.clGetKernelInfo(ID, CL.CL_KERNEL_FUNCTION_NAME, 0, null, longArray, 0); + int ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, 0, null, longArray, 0); checkForError(ret, "error while asking for kernel function name"); ByteBuffer bb = ByteBuffer.allocate((int)longArray[0]).order(ByteOrder.nativeOrder()); - ret = cl.clGetKernelInfo(ID, CL.CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null, 0); + ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null, 0); checkForError(ret, "error while asking for kernel function name"); this.name = CLUtils.clString2JavaString(bb.array(), bb.capacity()); // get number of arguments - ret = cl.clGetKernelInfo(ID, CL.CL_KERNEL_NUM_ARGS, 0, null, longArray, 0); + ret = cl.clGetKernelInfo(ID, CL_KERNEL_NUM_ARGS, 0, null, longArray, 0); checkForError(ret, "error while asking for number of function arguments."); numArgs = (int)longArray[0]; diff --git a/src/com/mbien/opencl/CLPlatform.java b/src/com/mbien/opencl/CLPlatform.java index a0c69084..6b17b01a 100644 --- a/src/com/mbien/opencl/CLPlatform.java +++ b/src/com/mbien/opencl/CLPlatform.java @@ -2,7 +2,10 @@ package com.mbien.opencl; import com.mbien.opencl.impl.CLImpl; import java.nio.ByteBuffer; + import static com.mbien.opencl.CLException.*; +import static com.mbien.opencl.CL.*; + /** * * @author Michael Bien @@ -65,11 +68,11 @@ public final class CLPlatform { int[] intBuffer = new int[1]; //find all devices - int ret = cl.clGetDeviceIDs(ID, CL.CL_DEVICE_TYPE_ALL, 0, null, 0, intBuffer, 0); + int ret = cl.clGetDeviceIDs(ID, CL_DEVICE_TYPE_ALL, 0, null, 0, intBuffer, 0); checkForError(ret, "error while enumerating devices"); long[] deviceIDs = new long[intBuffer[0]]; - ret = cl.clGetDeviceIDs(ID, CL.CL_DEVICE_TYPE_ALL, deviceIDs.length, deviceIDs, 0, null, 0); + ret = cl.clGetDeviceIDs(ID, CL_DEVICE_TYPE_ALL, deviceIDs.length, deviceIDs, 0, null, 0); checkForError(ret, "error while enumerating devices"); CLDevice[] devices = new CLDevice[deviceIDs.length]; @@ -118,28 +121,28 @@ public final class CLPlatform { * Returns the platform name. */ public String getName() { - return getInfoString(CL.CL_PLATFORM_NAME); + return getInfoString(CL_PLATFORM_NAME); } /** * Returns the platform version. */ public String getVersion() { - return getInfoString(CL.CL_PLATFORM_VERSION); + return getInfoString(CL_PLATFORM_VERSION); } /** * Returns the platform profile. */ public String getProfile() { - return getInfoString(CL.CL_PLATFORM_PROFILE); + return getInfoString(CL_PLATFORM_PROFILE); } /** * Returns the platform vendor. */ public String getVendor() { - return getInfoString(CL.CL_PLATFORM_VENDOR); + return getInfoString(CL_PLATFORM_VENDOR); } /** diff --git a/src/com/mbien/opencl/CLProgram.java b/src/com/mbien/opencl/CLProgram.java index 86d573e1..d1645213 100644 --- a/src/com/mbien/opencl/CLProgram.java +++ b/src/com/mbien/opencl/CLProgram.java @@ -6,7 +6,9 @@ import java.nio.ByteOrder; import java.util.Collections; import java.util.HashMap; import java.util.Map; + import static com.mbien.opencl.CLException.*; +import static com.mbien.opencl.CL.*; /** * @@ -24,14 +26,14 @@ public class CLProgram implements CLResource { private boolean executable; - CLProgram(CLContext context, String src, long contextID) { + CLProgram(CLContext context, String src) { this.cl = context.cl; this.context = context; int[] intArray = new int[1]; // Create the program - ID = cl.clCreateProgramWithSource(contextID, 1, new String[] {src}, new long[]{src.length()}, 0, intArray, 0); + ID = cl.clCreateProgramWithSource(context.ID, 1, new String[] {src}, new long[]{src.length()}, 0, intArray, 0); checkForError(intArray[0], "can not create program with source"); } @@ -60,7 +62,7 @@ public class CLProgram implements CLResource { if(!isExecutable()) { // It is illegal to create kernels from a not executable program. // For consistency between AMD and NVIDIA drivers throw an exception at this point. - throw new CLException(CL.CL_INVALID_PROGRAM_EXECUTABLE, + throw new CLException(CL_INVALID_PROGRAM_EXECUTABLE, "can not initialize kernels, program is not executable. status: "+buildStatusMap); } } @@ -183,7 +185,7 @@ public class CLProgram implements CLResource { // Build the program int ret = cl.clBuildProgram(ID, deviceIDs, options, null, null); - if(ret != CL.CL_SUCCESS) { + if(ret != CL_SUCCESS) { throw new CLException(ret, "\n"+getBuildLog()); } @@ -217,11 +219,17 @@ public class CLProgram implements CLResource { } /** - * Returns the kernel with the specified name or null if not found. + * Returns the kernel with the specified name. + * @throws IllegalArgumentException when no kernel with the specified name exists in this program. */ public CLKernel getCLKernel(String kernelName) { initKernels(); - return kernels.get(kernelName); + final CLKernel kernel = kernels.get(kernelName); + if(kernel == null) { + throw new IllegalArgumentException( + this+" does not contain a kernel with the name '"+kernelName+"'"); + } + return kernel; } @@ -240,11 +248,11 @@ public class CLProgram implements CLResource { public CLDevice[] getCLDevices() { long[] longArray = new long[1]; - int ret = cl.clGetProgramInfo(ID, CL.CL_PROGRAM_DEVICES, 0, null, longArray, 0); + int ret = cl.clGetProgramInfo(ID, CL_PROGRAM_DEVICES, 0, null, longArray, 0); checkForError(ret, "on clGetProgramInfo"); ByteBuffer bb = ByteBuffer.allocate((int) longArray[0]).order(ByteOrder.nativeOrder()); - ret = cl.clGetProgramInfo(ID, CL.CL_PROGRAM_DEVICES, bb.capacity(), bb, null, 0); + ret = cl.clGetProgramInfo(ID, CL_PROGRAM_DEVICES, bb.capacity(), bb, null, 0); checkForError(ret, "on clGetProgramInfo"); int count = bb.capacity() / (CPU.is32Bit()?4:8); @@ -297,23 +305,23 @@ public class CLProgram implements CLResource { * of the log are implementation dependent log can be an empty String. */ public String getBuildLog(CLDevice device) { - return getBuildInfoString(device.ID, CL.CL_PROGRAM_BUILD_LOG); + return getBuildInfoString(device.ID, CL_PROGRAM_BUILD_LOG); } /** * Returns the build status enum for this program on the specified device. */ public Status getBuildStatus(CLDevice device) { - int clStatus = getBuildInfoInt(device.ID, CL.CL_PROGRAM_BUILD_STATUS); + int clStatus = getBuildInfoInt(device.ID, CL_PROGRAM_BUILD_STATUS); return Status.valueOf(clStatus); } /** * Returns the source code of this program. Note: sources are not cached, - * each call of this method calls into OpenCL. + * each call of this method calls into Open */ public String getSource() { - return getProgramInfoString(CL.CL_PROGRAM_SOURCE); + return getProgramInfoString(CL_PROGRAM_SOURCE); } /** @@ -325,7 +333,7 @@ public class CLProgram implements CLResource { CLDevice[] devices = getCLDevices(); ByteBuffer sizes = ByteBuffer.allocate(8*devices.length).order(ByteOrder.nativeOrder()); - int ret = cl.clGetProgramInfo(ID, CL.CL_PROGRAM_BINARY_SIZES, sizes.capacity(), sizes, null, 0); + int ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARY_SIZES, sizes.capacity(), sizes, null, 0); checkForError(ret, "on clGetProgramInfo"); int binarySize = 0; @@ -333,7 +341,7 @@ public class CLProgram implements CLResource { binarySize += (int)sizes.getLong(); ByteBuffer binaries = ByteBuffer.allocate(binarySize).order(ByteOrder.nativeOrder()); - ret = cl.clGetProgramInfo(ID, CL.CL_PROGRAM_BINARIES, binaries.capacity(), binaries, null, 0); // TODO crash, driver bug? + ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARIES, binaries.capacity(), binaries, null, 0); // TODO crash, driver bug? checkForError(ret, "on clGetProgramInfo"); Map<CLDevice, byte[]> map = new HashMap<CLDevice, byte[]>(); @@ -381,32 +389,32 @@ public class CLProgram implements CLResource { public enum Status { - BUILD_SUCCESS(CL.CL_BUILD_SUCCESS), - BUILD_NONE(CL.CL_BUILD_NONE), - BUILD_IN_PROGRESS(CL.CL_BUILD_IN_PROGRESS), - BUILD_ERROR(CL.CL_BUILD_ERROR); + BUILD_SUCCESS(CL_BUILD_SUCCESS), + BUILD_NONE(CL_BUILD_NONE), + BUILD_IN_PROGRESS(CL_BUILD_IN_PROGRESS), + BUILD_ERROR(CL_BUILD_ERROR); /** * Value of wrapped OpenCL device type. */ - public final int CL_BUILD_STATUS; + public final int STATUS; - private Status(int CL_BUILD_STATUS) { - this.CL_BUILD_STATUS = CL_BUILD_STATUS; + private Status(int status) { + this.STATUS = status; } public static Status valueOf(int clBuildStatus) { switch(clBuildStatus) { - case(CL.CL_BUILD_SUCCESS): + case(CL_BUILD_SUCCESS): return BUILD_SUCCESS; - case(CL.CL_BUILD_NONE): + case(CL_BUILD_NONE): return BUILD_NONE; - case(CL.CL_BUILD_IN_PROGRESS): + case(CL_BUILD_IN_PROGRESS): return BUILD_IN_PROGRESS; - case(CL.CL_BUILD_ERROR): + case(CL_BUILD_ERROR): return BUILD_ERROR; // is this a standard state? -// case (CL.CL_BUILD_PROGRAM_FAILURE): +// case (CL_BUILD_PROGRAM_FAILURE): // return BUILD_PROGRAM_FAILURE; } return null; diff --git a/src/com/mbien/opencl/CLSampler.java b/src/com/mbien/opencl/CLSampler.java new file mode 100644 index 00000000..6977e2bd --- /dev/null +++ b/src/com/mbien/opencl/CLSampler.java @@ -0,0 +1,120 @@ +package com.mbien.opencl; + +import java.nio.Buffer; + +import static com.mbien.opencl.CLException.*; +import static com.mbien.opencl.CL.*; + +/** + * Object representing an OpenCL sampler. + * @author Michael Bien + */ +public class CLSampler implements CLResource { + + public final long ID; + + private final CLContext context; + private final CL cl; + + private final CLSamplerInfoAccessor samplerInfo; + + CLSampler(CLContext context, AddressingMode addrMode, FilteringMode filtMode, boolean normalizedCoords) { + + this.context = context; + this.cl = context.cl; + + this.samplerInfo = new CLSamplerInfoAccessor(); + + int[] error = new int[1]; + + ID = cl.clCreateSampler(context.ID, normalizedCoords?CL_TRUE:CL_FALSE, addrMode.MODE, filtMode.MODE, error, 0); + + checkForError(error[0], "can not create sampler"); + } + + public FilteringMode getFilteringMode() { + int info = (int)samplerInfo.getLong(CL_SAMPLER_FILTER_MODE); + return FilteringMode.valueOf(info); + } + + public AddressingMode getAddressingMode() { + int info = (int)samplerInfo.getLong(CL_SAMPLER_ADDRESSING_MODE); + return AddressingMode.valueOf(info); + } + + public boolean hasNormalizedCoords() { + return samplerInfo.getLong(CL_SAMPLER_NORMALIZED_COORDS) == CL_TRUE; + } + + public void release() { + int ret = cl.clReleaseSampler(ID); + context.onSamplerReleased(this); + checkForError(ret, "can not release sampler"); + } + + private class CLSamplerInfoAccessor extends CLInfoAccessor { + + @Override + protected int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset) { + return cl.clGetSamplerInfo(ID, name, valueSize, value, valueSizeRet, valueSizeRetOffset); + } + + } + + public enum FilteringMode { + + NEAREST(CL_FILTER_NEAREST), + LINEAR(CL_FILTER_LINEAR); + + /** + * Value of wrapped OpenCL sampler filtering mode type. + */ + public final int MODE; + + private FilteringMode(int mode) { + this.MODE = mode; + } + + public static FilteringMode valueOf(int mode) { + switch(mode) { + case(CL_FILTER_NEAREST): + return NEAREST; + case(CL_FILTER_LINEAR): + return LINEAR; + } + return null; + } + } + + public enum AddressingMode { + + REPEAT(CL_ADDRESS_REPEAT), + CLAMP_TO_EDGE(CL_ADDRESS_CLAMP_TO_EDGE), + CLAMP(CL_ADDRESS_CLAMP), + NONE(CL_ADDRESS_NONE); + + /** + * Value of wrapped OpenCL sampler addressing mode type. + */ + public final int MODE; + + private AddressingMode(int mode) { + this.MODE = mode; + } + + public static AddressingMode valueOf(int mode) { + switch(mode) { + case(CL_ADDRESS_REPEAT): + return REPEAT; + case(CL_ADDRESS_CLAMP_TO_EDGE): + return CLAMP_TO_EDGE; + case(CL_ADDRESS_CLAMP): + return CLAMP; + case(CL_ADDRESS_NONE): + return NONE; + } + return null; + } + } + +} |