From 5ad19147a76f80635dcae18693929edbf1da2cbf Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Mon, 8 Mar 2010 16:23:28 +0100 Subject: moved utilities to util package. several smaller improvements and doc fixes. --- src/com/mbien/opencl/CLBuildConfiguration.java | 103 ------------------ src/com/mbien/opencl/CLCommandQueue.java | 2 +- src/com/mbien/opencl/CLException.java | 2 +- src/com/mbien/opencl/CLInfoAccessor.java | 3 +- src/com/mbien/opencl/CLKernel.java | 15 ++- src/com/mbien/opencl/CLPlatform.java | 3 +- src/com/mbien/opencl/CLProgram.java | 34 +++--- src/com/mbien/opencl/CLProgramBuilder.java | 2 + src/com/mbien/opencl/CLProgramConfiguration.java | 48 --------- src/com/mbien/opencl/CLSampler.java | 3 +- src/com/mbien/opencl/CLUtils.java | 115 -------------------- src/com/mbien/opencl/MultiQueueBarrier.java | 109 ------------------- src/com/mbien/opencl/NativeLibLoader.java | 3 +- .../mbien/opencl/util/CLBuildConfiguration.java | 105 ++++++++++++++++++ .../mbien/opencl/util/CLProgramConfiguration.java | 50 +++++++++ src/com/mbien/opencl/util/CLUtil.java | 118 +++++++++++++++++++++ src/com/mbien/opencl/util/MultiQueueBarrier.java | 111 +++++++++++++++++++ 17 files changed, 431 insertions(+), 395 deletions(-) delete mode 100644 src/com/mbien/opencl/CLBuildConfiguration.java delete mode 100644 src/com/mbien/opencl/CLProgramConfiguration.java delete mode 100644 src/com/mbien/opencl/CLUtils.java delete mode 100644 src/com/mbien/opencl/MultiQueueBarrier.java create mode 100644 src/com/mbien/opencl/util/CLBuildConfiguration.java create mode 100644 src/com/mbien/opencl/util/CLProgramConfiguration.java create mode 100644 src/com/mbien/opencl/util/CLUtil.java create mode 100644 src/com/mbien/opencl/util/MultiQueueBarrier.java (limited to 'src') diff --git a/src/com/mbien/opencl/CLBuildConfiguration.java b/src/com/mbien/opencl/CLBuildConfiguration.java deleted file mode 100644 index 875f8d3a..00000000 --- a/src/com/mbien/opencl/CLBuildConfiguration.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.mbien.opencl; - -import java.io.IOException; -import java.io.ObjectOutputStream; -import java.util.Map; - -/** - * Configuration representing everything needed to build an OpenCL program. - * @author Michael Bien - * @see CLProgramBuilder#createConfiguration() - * @see CLProgramBuilder#loadConfiguration(java.io.ObjectInputStream) - */ -public interface CLBuildConfiguration extends Cloneable { - - /** - * Builds or rebuilds the program. - * @param program The program which should be build. - */ - public CLProgram build(CLProgram program); - - /** - * Sets the program which should be build. - */ - public CLProgramConfiguration setProgram(CLProgram program); - - /** - * Adds the device as build target. - */ - public CLBuildConfiguration forDevice(CLDevice device); - - /** - * Adds the devices as build target. - */ - public CLBuildConfiguration forDevices(CLDevice... devices); - - /** - * Resets this builder's configuration like options, devices and definitions. - */ - public CLBuildConfiguration reset(); - - /** - * Resets this builder's configuration options. - */ - public CLBuildConfiguration resetOptions(); - - /** - * Resets this builder's macro definitions. - */ - public CLBuildConfiguration resetDefines(); - - /** - * Resets this builder's device list. - */ - public CLBuildConfiguration resetDevices(); - - /** - * Adds the definition to the build configuration. - * @see CLProgram#define(java.lang.String) - */ - public CLBuildConfiguration withDefine(String name); - - /** - * Adds the definition to the build configuration. - * @see CLProgram#define(java.lang.String, java.lang.Object) - */ - public CLBuildConfiguration withDefine(String name, Object value); - - /** - * Adds the definitions to the build configuration. - * @see CLProgram#define(java.lang.String) - */ - public CLBuildConfiguration withDefines(String... names); - - /** - * Adds the definitions to the build configuration. - * @see CLProgram#define(java.lang.String, java.lang.Object) - */ - public CLBuildConfiguration withDefines(Map defines); - - /** - * Adds the compiler option to the build configuration. - * @see CLProgram.CompilerOptions - */ - public CLBuildConfiguration withOption(String option); - - /** - * Adds the compiler options to the build configuration. - * @see CLProgram.CompilerOptions - */ - public CLBuildConfiguration withOptions(String... options); - - /** - * Clones this configuration. - */ - public CLBuildConfiguration clone(); - - /** - * Saves this configuration to the ObjectOutputStream. - * The caller is responsible for closing the stream. - */ - public void save(ObjectOutputStream oos) throws IOException; - -} diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java index a33593ad..e74ed7ba 100644 --- a/src/com/mbien/opencl/CLCommandQueue.java +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -9,7 +9,7 @@ import java.util.List; import static com.mbien.opencl.CLException.*; import static com.mbien.opencl.CL.*; -import static com.mbien.opencl.CLUtils.*; +import static com.mbien.opencl.util.CLUtil.*; /** * The command queue is used to queue a set of operations for a specific {@link CLDevice}. diff --git a/src/com/mbien/opencl/CLException.java b/src/com/mbien/opencl/CLException.java index baa187a5..63696a81 100644 --- a/src/com/mbien/opencl/CLException.java +++ b/src/com/mbien/opencl/CLException.java @@ -49,7 +49,7 @@ public class CLException extends RuntimeException { specificEx.fillInStackTrace(); return specificEx; } - return new CLException(status, "unknown", "unknown cause: code" + status); + return new CLException(status, "unknown", "unknown cause: code " + status); } /** diff --git a/src/com/mbien/opencl/CLInfoAccessor.java b/src/com/mbien/opencl/CLInfoAccessor.java index d2e58f3d..316c1257 100644 --- a/src/com/mbien/opencl/CLInfoAccessor.java +++ b/src/com/mbien/opencl/CLInfoAccessor.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.mbien.opencl.util.CLUtil; import com.sun.gluegen.runtime.PointerBuffer; import java.nio.Buffer; import java.nio.ByteBuffer; @@ -51,7 +52,7 @@ abstract class CLInfoAccessor { byte[] array = new byte[clSize-1]; // last char is always null buffer.get(array).rewind(); - return CLUtils.clString2JavaString(array, clSize); + return CLUtil.clString2JavaString(array, clSize); } diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java index 696007ba..4b60cab9 100644 --- a/src/com/mbien/opencl/CLKernel.java +++ b/src/com/mbien/opencl/CLKernel.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.mbien.opencl.util.CLUtil; import com.sun.gluegen.runtime.BufferFactory; import com.sun.gluegen.runtime.CPU; import com.sun.gluegen.runtime.PointerBuffer; @@ -17,6 +18,8 @@ import static com.mbien.opencl.CL.*; * function declared in a program and the argument values to be used when executing this * kernel function. * CLKernel is not threadsafe. + * @see CLProgram#createCLKernel(java.lang.String) + * @see CLProgram#createCLKernels() * @author Michael Bien */ public class CLKernel extends CLObject implements CLResource, Cloneable { @@ -47,7 +50,7 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null); checkForError(ret, "error while asking for kernel function name"); - this.name = CLUtils.clString2JavaString(bb, bb.capacity()); + this.name = CLUtil.clString2JavaString(bb, bb.capacity()); // get number of arguments ret = cl.clGetKernelInfo(ID, CL_KERNEL_NUM_ARGS, bb.capacity(), bb, null); @@ -56,6 +59,11 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { numArgs = bb.getInt(0); } + +// public CLKernel putArg(Buffer value) { +// setArg(argIndex++, value); +// return this; +// } public CLKernel putArg(CLMemory value) { setArg(argIndex++, value); @@ -98,6 +106,11 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { return this; } +// public CLKernel setArg(int argumentIndex, Buffer value) { +// setArgument(argumentIndex, CLMemory.sizeOfBufferElem(value)*value.capacity(), value); +// return this; +// } + public CLKernel setArg(int argumentIndex, CLMemory value) { setArgument(argumentIndex, CPU.is32Bit()?4:8, wrap(value.ID)); return this; diff --git a/src/com/mbien/opencl/CLPlatform.java b/src/com/mbien/opencl/CLPlatform.java index 597878b3..76284dcf 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.util.CLUtil; import com.mbien.opencl.impl.CLImpl; import com.sun.gluegen.runtime.PointerBuffer; import java.nio.ByteBuffer; @@ -243,7 +244,7 @@ public final class CLPlatform { int ret = cl.clGetPlatformInfo(ID, key, bb.capacity(), bb, pb); checkForError(ret, "can not receive info string"); - return CLUtils.clString2JavaString(bb, (int)pb.get(0)); + return CLUtil.clString2JavaString(bb, (int)pb.get(0)); } @Override diff --git a/src/com/mbien/opencl/CLProgram.java b/src/com/mbien/opencl/CLProgram.java index df556937..58071a59 100644 --- a/src/com/mbien/opencl/CLProgram.java +++ b/src/com/mbien/opencl/CLProgram.java @@ -1,5 +1,7 @@ package com.mbien.opencl; +import com.mbien.opencl.util.CLProgramConfiguration; +import com.mbien.opencl.util.CLUtil; import com.sun.gluegen.runtime.BufferFactory; import com.sun.gluegen.runtime.CPU; import com.sun.gluegen.runtime.PointerBuffer; @@ -25,6 +27,8 @@ import static com.mbien.opencl.CL.*; * @author Michael Bien */ public class CLProgram extends CLObject implements CLResource { + +// private final static Object buildLock = new Object(); private final Set kernels; private Map buildStatusMap; @@ -87,16 +91,18 @@ public class CLProgram extends CLObject implements CLResource { private void initBuildStatus() { if(buildStatusMap == null) { - Map map = new HashMap(); - CLDevice[] devices = getCLDevices(); - for (CLDevice device : devices) { - Status status = getBuildStatus(device); - if(status == Status.BUILD_SUCCESS) { - executable = true; +// synchronized(buildLock) { + Map map = new HashMap(); + CLDevice[] devices = getCLDevices(); + for (CLDevice device : devices) { + Status status = getBuildStatus(device); + if(status == Status.BUILD_SUCCESS) { + executable = true; + } + map.put(device, status); } - map.put(device, status); - } - this.buildStatusMap = Collections.unmodifiableMap(map); + this.buildStatusMap = Collections.unmodifiableMap(map); +// } } } @@ -116,7 +122,7 @@ public class CLProgram extends CLObject implements CLResource { ret = cl.clGetProgramBuildInfo(ID, device, flag, bb.capacity(), bb, null); checkForError(ret, "on clGetProgramBuildInfo"); - return CLUtils.clString2JavaString(bb, (int)pb.get(0)); + return CLUtil.clString2JavaString(bb, (int)pb.get(0)); } private String getProgramInfoString(int flag) { @@ -135,7 +141,7 @@ public class CLProgram extends CLObject implements CLResource { ret = cl.clGetProgramInfo(ID, flag, bb.capacity(), bb, null); checkForError(ret, "on clGetProgramInfo"); - return CLUtils.clString2JavaString(bb, (int)pb.get(0)); + return CLUtil.clString2JavaString(bb, (int)pb.get(0)); } // private int getProgramInfoInt(int flag) { @@ -238,7 +244,11 @@ public class CLProgram extends CLObject implements CLResource { executable = false; // Build the program - int ret = cl.clBuildProgram(ID, count, deviceIDs, options, null, null); + int ret = 0; + // building programs is not threadsafe +// synchronized(buildLock) { + ret = cl.clBuildProgram(ID, count, deviceIDs, options, null, null); +// } if(ret != CL_SUCCESS) { throw newException(ret, "\n"+getBuildLog()); diff --git a/src/com/mbien/opencl/CLProgramBuilder.java b/src/com/mbien/opencl/CLProgramBuilder.java index 14300ef0..fea611a9 100644 --- a/src/com/mbien/opencl/CLProgramBuilder.java +++ b/src/com/mbien/opencl/CLProgramBuilder.java @@ -1,5 +1,7 @@ package com.mbien.opencl; +import com.mbien.opencl.util.CLBuildConfiguration; +import com.mbien.opencl.util.CLProgramConfiguration; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; diff --git a/src/com/mbien/opencl/CLProgramConfiguration.java b/src/com/mbien/opencl/CLProgramConfiguration.java deleted file mode 100644 index 405b911d..00000000 --- a/src/com/mbien/opencl/CLProgramConfiguration.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.mbien.opencl; - -import java.util.Map; - -/** - * Configuration representing everything needed to build an OpenCL program (program included). - * CLProgramConfiguration is a helper for building programs with more complex configurations or - * building multiple programs with the similar configuration. - * @see CLProgram#prepare() - * @see CLProgramBuilder#createConfiguration(com.mbien.opencl.CLProgram) - * @see CLProgramBuilder#loadConfiguration(java.io.ObjectInputStream, com.mbien.opencl.CLContext) - * @author Michael Bien - */ -public interface CLProgramConfiguration extends CLBuildConfiguration { - - /** - * Builds or rebuilds a program. - */ - public CLProgram build(); - - /** - * Returns the program. - */ - public CLProgram getProgram(); - - /** - * Returns a new instance of of this configuration without a {@link CLProgram}, - * program binaries or sources associated with it. - */ - public CLBuildConfiguration asBuildConfiguration(); - - - // overwrite with CLProgramConfiguration as return type - @Override public CLProgramConfiguration forDevice(CLDevice device); - @Override public CLProgramConfiguration forDevices(CLDevice... devices); - @Override public CLProgramConfiguration withDefine(String name); - @Override public CLProgramConfiguration withDefine(String name, Object value); - @Override public CLProgramConfiguration withDefines(String... names); - @Override public CLProgramConfiguration withDefines(Map defines); - @Override public CLProgramConfiguration withOption(String option); - @Override public CLProgramConfiguration withOptions(String... options); - @Override public CLProgramConfiguration reset(); - @Override public CLProgramConfiguration resetOptions(); - @Override public CLProgramConfiguration resetDefines(); - @Override public CLProgramConfiguration resetDevices(); - @Override public CLProgramConfiguration clone(); - -} diff --git a/src/com/mbien/opencl/CLSampler.java b/src/com/mbien/opencl/CLSampler.java index 28b712a9..73e98013 100644 --- a/src/com/mbien/opencl/CLSampler.java +++ b/src/com/mbien/opencl/CLSampler.java @@ -5,10 +5,11 @@ import java.nio.Buffer; import static com.mbien.opencl.CLException.*; import static com.mbien.opencl.CL.*; -import static com.mbien.opencl.CLUtils.*; +import static com.mbien.opencl.util.CLUtil.*; /** * Object representing an OpenCL sampler. + * @see CLContext#createSampler(com.mbien.opencl.CLSampler.AddressingMode, com.mbien.opencl.CLSampler.FilteringMode, boolean) * @author Michael Bien */ public class CLSampler extends CLObject implements CLResource { diff --git a/src/com/mbien/opencl/CLUtils.java b/src/com/mbien/opencl/CLUtils.java deleted file mode 100644 index 8f89980e..00000000 --- a/src/com/mbien/opencl/CLUtils.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.mbien.opencl; - -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * - * @author Michael Bien - */ -class CLUtils { - - public static String clString2JavaString(byte[] chars, int clLength) { - return clLength==0 ? "" : new String(chars, 0, clLength-1); - } - - public static String clString2JavaString(ByteBuffer chars, int clLength) { - if (clLength==0) { - return ""; - }else{ - byte[] array = new byte[clLength-1]; // last char is always null - chars.get(array).rewind(); - return new String(array, 0, clLength-1); - } - } - - /** - * Returns true if clBoolean == CL.CL_TRUE. - */ - public static boolean clBoolean(int clBoolean) { - return clBoolean == CL.CL_TRUE; - } - - /** - * Returns b ? CL.CL_TRUE : CL.CL_FALSE - */ - public static int clBoolean(boolean b) { - return b ? CL.CL_TRUE : CL.CL_FALSE; - } - - public static Map obtainPlatformProperties(CLPlatform platform) { - - Map map = new LinkedHashMap(); - map.put("CL_PLATFORM_NAME", platform.getName()); - map.put("CL_PLATFORM_PROFILE", platform.getProfile()); - map.put("CL_PLATFORM_VERSION", platform.getVersion()); - map.put("CL_PLATFORM_VENDOR", platform.getVendor()); - map.put("CL_PLATFORM_EXTENSIONS", platform.getExtensions().toString()); -// map.put("fastest device (estimated)", platform.getMaxFlopsDevice().toString()); - - return map; - } - - public static Map obtainDeviceProperties(CLDevice dev) { - - Map map = new LinkedHashMap(); - map.put("CL_DEVICE_NAME", dev.getName()); - map.put("CL_DEVICE_PROFILE", dev.getProfile()); - map.put("CL_DEVICE_VENDOR", dev.getVendor()); - map.put("CL_DEVICE_VENDOR_ID", dev.getVendorID()+""); - map.put("CL_DEVICE_VERSION", dev.getVersion()); - map.put("CL_DRIVER_VERSION", dev.getDriverVersion()); - map.put("CL_DEVICE_TYPE", dev.getType().toString()); - - map.put("CL_DEVICE_GLOBAL_MEM_SIZE", dev.getGlobalMemSize()/(1024*1024)+" MB"); - map.put("CL_DEVICE_MAX_MEM_ALLOC_SIZE", dev.getMaxMemAllocSize()/(1024*1024)+" MB"); - map.put("CL_DEVICE_MAX_PARAMETER_SIZE", dev.getMaxParameterSize()+" Byte"); - map.put("CL_DEVICE_LOCAL_MEM_SIZE", dev.getLocalMemSize()/1024+" KB"); - map.put("CL_DEVICE_LOCAL_MEM_TYPE", dev.getLocalMemType()+""); - map.put("CL_DEVICE_GLOBAL_MEM_CACHE_SIZE", dev.getGlobalMemCacheSize()+""); - map.put("CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE", dev.getGlobalMemCachelineSize()+""); - map.put("CL_DEVICE_GLOBAL_MEM_CACHE_TYPE", dev.getGlobalMemCacheType()+""); - map.put("CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE", dev.getMaxConstantBufferSize()+""); - map.put("CL_DEVICE_MAX_CONSTANT_ARGS", dev.getMaxConstantArgs()+""); - map.put("CL_DEVICE_ERROR_CORRECTION_SUPPORT", dev.isErrorCorrectionSupported()+""); - - map.put("CL_DEVICE_MAX_CLOCK_FREQUENCY", dev.getMaxClockFrequency()+" MHz"); - map.put("CL_DEVICE_PROFILING_TIMER_RESOLUTION", dev.getProfilingTimerResolution()+" ns"); - map.put("CL_DEVICE_QUEUE_PROPERTIES", dev.getQueueProperties()+""); - map.put("CL_DEVICE_MAX_WORK_GROUP_SIZE", dev.getMaxWorkGroupSize()+""); - map.put("CL_DEVICE_MAX_COMPUTE_UNITS", dev.getMaxComputeUnits()+""); - map.put("CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS", dev.getMaxWorkItemDimensions()+""); - map.put("CL_DEVICE_MAX_WORK_ITEM_SIZES", Arrays.toString(dev.getMaxWorkItemSizes())); - map.put("CL_DEVICE_COMPILER_AVAILABLE", dev.isCompilerAvailable()+""); - - map.put("CL_DEVICE_IMAGE_SUPPORT", dev.isImageSupportAvailable()+""); - map.put("CL_DEVICE_MAX_READ_IMAGE_ARGS", dev.getMaxReadImageArgs()+""); - map.put("CL_DEVICE_MAX_WRITE_IMAGE_ARGS", dev.getMaxWriteImageArgs()+""); - map.put("CL_DEVICE_IMAGE2D_MAX_DIMENSIONS", Arrays.asList(dev.getMaxImage2dWidth(), dev.getMaxImage2dHeight()).toString()); - map.put("CL_DEVICE_IMAGE3D_MAX_DIMENSIONS", Arrays.asList(dev.getMaxImage2dWidth(), dev.getMaxImage2dHeight(), dev.getMaxImage3dDepth()).toString()); - map.put("CL_DEVICE_MAX_SAMPLERS", dev.getMaxSamplers()+""); - - map.put("CL_DEVICE_ADDRESS_BITS", dev.getAddressBits()+""); - map.put("cl_khr_fp16", dev.isHalfFPAvailable()+""); - map.put("cl_khr_fp64", dev.isDoubleFPAvailable()+""); - map.put("CL_DEVICE_ENDIAN_LITTLE", dev.isLittleEndianAvailable()+""); - map.put("CL_DEVICE_HALF_FP_CONFIG", dev.getHalfFPConfig()+""); - map.put("CL_DEVICE_SINGLE_FP_CONFIG", dev.getSingleFPConfig()+""); - map.put("CL_DEVICE_DOUBLE_FP_CONFIG", dev.getDoubleFPConfig()+""); - map.put("CL_DEVICE_EXTENSIONS", dev.getExtensions()+""); - - map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT", dev.getPreferredShortVectorWidth()+""); - map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR", dev.getPreferredCharVectorWidth()+""); - map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", dev.getPreferredIntVectorWidth()+""); - map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG", dev.getPreferredLongVectorWidth()+""); - map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT", dev.getPreferredFloatVectorWidth()+""); - map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE", dev.getPreferredDoubleVectorWidth()+""); - - //TODO device extensions -> properties - - return map; - } - -} diff --git a/src/com/mbien/opencl/MultiQueueBarrier.java b/src/com/mbien/opencl/MultiQueueBarrier.java deleted file mode 100644 index 1158a446..00000000 --- a/src/com/mbien/opencl/MultiQueueBarrier.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.mbien.opencl; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -/** - * A high-level utility to synchronize multiple {@link CLCommandQueue}s. - * @author Michael Bien - */ -public class MultiQueueBarrier { - - private CountDownLatch latch; - private final Set queues; - - /** - * Creates a new MultiQueueBarrier with the given queueCount. - * It is recommented to use {@link #MultiQueueBarrier(CLCommandQueue... allowedQueues)} if possible - * which restricts the set of allowed queues for the barrier. - */ - public MultiQueueBarrier(int queueCount) { - this.latch = new CountDownLatch(queueCount); - this.queues = null; - } - - /** - * Creates a new MultiQueueBarrier for the given queues. - */ - public MultiQueueBarrier(CLCommandQueue... allowedQueues) { - this.latch = new CountDownLatch(allowedQueues.length); - - HashSet set = new HashSet(allowedQueues.length); - for (CLCommandQueue queue : allowedQueues) { - set.add(queue); - } - this.queues = Collections.unmodifiableSet(set); - } - - /** - * Blocks the current Thread until all commands on the {@link CLCommandQueue} finished excecution. - * This method may be invoked concurrently without synchronization on the MultiQueueBarrier object - * as long each Thread passes a distinct CLCommandQueue as parameter to this method. - */ - public MultiQueueBarrier waitFor(CLCommandQueue queue) { - checkQueue(queue); - - queue.putBarrier(); - latch.countDown(); - return this; - } - - /** - * Blocks the current Thread until the given events on the {@link CLCommandQueue} occurred. - * This method may be invoked concurrently without synchronization on the MultiQueueBarrier object - * as long each Thread passes a distinct CLCommandQueue as parameter to this method. - */ - public MultiQueueBarrier waitFor(CLCommandQueue queue, CLEventList events) { - checkQueue(queue); - - queue.putWaitForEvents(events, true); - latch.countDown(); - return this; - } - - /** - * Blocks until all Threads which called {@link #waitFor} - * continue execution. - * This method blocks only once, all subsequent calls are ignored. - */ - public MultiQueueBarrier await() throws InterruptedException { - latch.await(); - return this; - } - - /** - * @see #await() - * @param timeout the maximum time to wait - * @param unit the time unit of the {@code timeout} argument - */ - public MultiQueueBarrier await(long timeout, TimeUnit unit) throws InterruptedException { - latch.await(timeout, unit); - return this; - } - - /** - * Cancels this barrier and unblocks all waiting threads. - */ - public void cancelBarrier() { - while(latch.getCount() > 0) - latch.countDown(); - } - - /** - * Returns the current number of events which must occure before this barrier unblocks the waiting threads. - * This method is typically used for debugging and testing purposes. - */ - public long getCount() { - return latch.getCount(); - } - - private void checkQueue(CLCommandQueue queue) throws IllegalArgumentException { - if (queues != null && !queues.contains(queue)) { - throw new IllegalArgumentException(queue + " is not in the allowedQueues Set: " + queues); - } - } - -} diff --git a/src/com/mbien/opencl/NativeLibLoader.java b/src/com/mbien/opencl/NativeLibLoader.java index b8148432..d6808dbc 100644 --- a/src/com/mbien/opencl/NativeLibLoader.java +++ b/src/com/mbien/opencl/NativeLibLoader.java @@ -10,9 +10,8 @@ import com.sun.nativewindow.impl.NativeLibLoaderBase; */ class NativeLibLoader extends NativeLibLoaderBase { - @SuppressWarnings("unchecked") public static void loadJOCL() { - AccessController.doPrivileged(new PrivilegedAction() { + AccessController.doPrivileged(new PrivilegedAction() { public Object run() { loadLibrary("jocl", null, true); return null; diff --git a/src/com/mbien/opencl/util/CLBuildConfiguration.java b/src/com/mbien/opencl/util/CLBuildConfiguration.java new file mode 100644 index 00000000..e65a9f98 --- /dev/null +++ b/src/com/mbien/opencl/util/CLBuildConfiguration.java @@ -0,0 +1,105 @@ +package com.mbien.opencl.util; + +import com.mbien.opencl.CLDevice; +import com.mbien.opencl.CLProgram; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.Map; + +/** + * Configuration representing everything needed to build an OpenCL program. + * @author Michael Bien + * @see com.mbien.opencl.CLProgramBuilder#createConfiguration() + * @see com.mbien.opencl.CLProgramBuilder#loadConfiguration(java.io.ObjectInputStream) + */ +public interface CLBuildConfiguration extends Cloneable { + + /** + * Builds or rebuilds the program. + * @param program The program which should be build. + */ + public CLProgram build(CLProgram program); + + /** + * Sets the program which should be build. + */ + public CLProgramConfiguration setProgram(CLProgram program); + + /** + * Adds the device as build target. + */ + public CLBuildConfiguration forDevice(CLDevice device); + + /** + * Adds the devices as build target. + */ + public CLBuildConfiguration forDevices(CLDevice... devices); + + /** + * Resets this builder's configuration like options, devices and definitions. + */ + public CLBuildConfiguration reset(); + + /** + * Resets this builder's configuration options. + */ + public CLBuildConfiguration resetOptions(); + + /** + * Resets this builder's macro definitions. + */ + public CLBuildConfiguration resetDefines(); + + /** + * Resets this builder's device list. + */ + public CLBuildConfiguration resetDevices(); + + /** + * Adds the definition to the build configuration. + * @see CLProgram#define(java.lang.String) + */ + public CLBuildConfiguration withDefine(String name); + + /** + * Adds the definition to the build configuration. + * @see CLProgram#define(java.lang.String, java.lang.Object) + */ + public CLBuildConfiguration withDefine(String name, Object value); + + /** + * Adds the definitions to the build configuration. + * @see CLProgram#define(java.lang.String) + */ + public CLBuildConfiguration withDefines(String... names); + + /** + * Adds the definitions to the build configuration. + * @see CLProgram#define(java.lang.String, java.lang.Object) + */ + public CLBuildConfiguration withDefines(Map defines); + + /** + * Adds the compiler option to the build configuration. + * @see CLProgram.CompilerOptions + */ + public CLBuildConfiguration withOption(String option); + + /** + * Adds the compiler options to the build configuration. + * @see CLProgram.CompilerOptions + */ + public CLBuildConfiguration withOptions(String... options); + + /** + * Clones this configuration. + */ + public CLBuildConfiguration clone(); + + /** + * Saves this configuration to the ObjectOutputStream. + * The caller is responsible for closing the stream. + */ + public void save(ObjectOutputStream oos) throws IOException; + +} diff --git a/src/com/mbien/opencl/util/CLProgramConfiguration.java b/src/com/mbien/opencl/util/CLProgramConfiguration.java new file mode 100644 index 00000000..105a4b72 --- /dev/null +++ b/src/com/mbien/opencl/util/CLProgramConfiguration.java @@ -0,0 +1,50 @@ +package com.mbien.opencl.util; + +import com.mbien.opencl.CLDevice; +import com.mbien.opencl.CLProgram; +import java.util.Map; + +/** + * Configuration representing everything needed to build an OpenCL program (program included). + * CLProgramConfiguration is a helper for building programs with more complex configurations or + * building multiple programs with the similar configuration. + * @see CLProgram#prepare() + * @see com.mbien.opencl.CLProgramBuilder#createConfiguration(com.mbien.opencl.CLProgram) + * @see com.mbien.opencl.CLProgramBuilder#loadConfiguration(java.io.ObjectInputStream, com.mbien.opencl.CLContext) + * @author Michael Bien + */ +public interface CLProgramConfiguration extends CLBuildConfiguration { + + /** + * Builds or rebuilds a program. + */ + public CLProgram build(); + + /** + * Returns the program. + */ + public CLProgram getProgram(); + + /** + * Returns a new instance of of this configuration without a {@link CLProgram}, + * program binaries or sources associated with it. + */ + public CLBuildConfiguration asBuildConfiguration(); + + + // overwrite with CLProgramConfiguration as return type + @Override public CLProgramConfiguration forDevice(CLDevice device); + @Override public CLProgramConfiguration forDevices(CLDevice... devices); + @Override public CLProgramConfiguration withDefine(String name); + @Override public CLProgramConfiguration withDefine(String name, Object value); + @Override public CLProgramConfiguration withDefines(String... names); + @Override public CLProgramConfiguration withDefines(Map defines); + @Override public CLProgramConfiguration withOption(String option); + @Override public CLProgramConfiguration withOptions(String... options); + @Override public CLProgramConfiguration reset(); + @Override public CLProgramConfiguration resetOptions(); + @Override public CLProgramConfiguration resetDefines(); + @Override public CLProgramConfiguration resetDevices(); + @Override public CLProgramConfiguration clone(); + +} diff --git a/src/com/mbien/opencl/util/CLUtil.java b/src/com/mbien/opencl/util/CLUtil.java new file mode 100644 index 00000000..f33451f3 --- /dev/null +++ b/src/com/mbien/opencl/util/CLUtil.java @@ -0,0 +1,118 @@ +package com.mbien.opencl.util; + +import com.mbien.opencl.CL; +import com.mbien.opencl.CLDevice; +import com.mbien.opencl.CLPlatform; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * + * @author Michael Bien + */ +public class CLUtil { + + public static String clString2JavaString(byte[] chars, int clLength) { + return clLength==0 ? "" : new String(chars, 0, clLength-1); + } + + public static String clString2JavaString(ByteBuffer chars, int clLength) { + if (clLength==0) { + return ""; + }else{ + byte[] array = new byte[clLength-1]; // last char is always null + chars.get(array).rewind(); + return new String(array, 0, clLength-1); + } + } + + /** + * Returns true if clBoolean == CL.CL_TRUE. + */ + public static boolean clBoolean(int clBoolean) { + return clBoolean == CL.CL_TRUE; + } + + /** + * Returns b ? CL.CL_TRUE : CL.CL_FALSE + */ + public static int clBoolean(boolean b) { + return b ? CL.CL_TRUE : CL.CL_FALSE; + } + + public static Map obtainPlatformProperties(CLPlatform platform) { + + Map map = new LinkedHashMap(); + map.put("CL_PLATFORM_NAME", platform.getName()); + map.put("CL_PLATFORM_PROFILE", platform.getProfile()); + map.put("CL_PLATFORM_VERSION", platform.getVersion()); + map.put("CL_PLATFORM_VENDOR", platform.getVendor()); + map.put("CL_PLATFORM_EXTENSIONS", platform.getExtensions().toString()); +// map.put("fastest device (estimated)", platform.getMaxFlopsDevice().toString()); + + return map; + } + + public static Map obtainDeviceProperties(CLDevice dev) { + + Map map = new LinkedHashMap(); + map.put("CL_DEVICE_NAME", dev.getName()); + map.put("CL_DEVICE_PROFILE", dev.getProfile()); + map.put("CL_DEVICE_VENDOR", dev.getVendor()); + map.put("CL_DEVICE_VENDOR_ID", dev.getVendorID()+""); + map.put("CL_DEVICE_VERSION", dev.getVersion()); + map.put("CL_DRIVER_VERSION", dev.getDriverVersion()); + map.put("CL_DEVICE_TYPE", dev.getType().toString()); + + map.put("CL_DEVICE_GLOBAL_MEM_SIZE", dev.getGlobalMemSize()/(1024*1024)+" MB"); + map.put("CL_DEVICE_MAX_MEM_ALLOC_SIZE", dev.getMaxMemAllocSize()/(1024*1024)+" MB"); + map.put("CL_DEVICE_MAX_PARAMETER_SIZE", dev.getMaxParameterSize()+" Byte"); + map.put("CL_DEVICE_LOCAL_MEM_SIZE", dev.getLocalMemSize()/1024+" KB"); + map.put("CL_DEVICE_LOCAL_MEM_TYPE", dev.getLocalMemType()+""); + map.put("CL_DEVICE_GLOBAL_MEM_CACHE_SIZE", dev.getGlobalMemCacheSize()+""); + map.put("CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE", dev.getGlobalMemCachelineSize()+""); + map.put("CL_DEVICE_GLOBAL_MEM_CACHE_TYPE", dev.getGlobalMemCacheType()+""); + map.put("CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE", dev.getMaxConstantBufferSize()+""); + map.put("CL_DEVICE_MAX_CONSTANT_ARGS", dev.getMaxConstantArgs()+""); + map.put("CL_DEVICE_ERROR_CORRECTION_SUPPORT", dev.isErrorCorrectionSupported()+""); + + map.put("CL_DEVICE_MAX_CLOCK_FREQUENCY", dev.getMaxClockFrequency()+" MHz"); + map.put("CL_DEVICE_PROFILING_TIMER_RESOLUTION", dev.getProfilingTimerResolution()+" ns"); + map.put("CL_DEVICE_QUEUE_PROPERTIES", dev.getQueueProperties()+""); + map.put("CL_DEVICE_MAX_WORK_GROUP_SIZE", dev.getMaxWorkGroupSize()+""); + map.put("CL_DEVICE_MAX_COMPUTE_UNITS", dev.getMaxComputeUnits()+""); + map.put("CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS", dev.getMaxWorkItemDimensions()+""); + map.put("CL_DEVICE_MAX_WORK_ITEM_SIZES", Arrays.toString(dev.getMaxWorkItemSizes())); + map.put("CL_DEVICE_COMPILER_AVAILABLE", dev.isCompilerAvailable()+""); + + map.put("CL_DEVICE_IMAGE_SUPPORT", dev.isImageSupportAvailable()+""); + map.put("CL_DEVICE_MAX_READ_IMAGE_ARGS", dev.getMaxReadImageArgs()+""); + map.put("CL_DEVICE_MAX_WRITE_IMAGE_ARGS", dev.getMaxWriteImageArgs()+""); + map.put("CL_DEVICE_IMAGE2D_MAX_DIMENSIONS", Arrays.asList(dev.getMaxImage2dWidth(), dev.getMaxImage2dHeight()).toString()); + map.put("CL_DEVICE_IMAGE3D_MAX_DIMENSIONS", Arrays.asList(dev.getMaxImage2dWidth(), dev.getMaxImage2dHeight(), dev.getMaxImage3dDepth()).toString()); + map.put("CL_DEVICE_MAX_SAMPLERS", dev.getMaxSamplers()+""); + + map.put("CL_DEVICE_ADDRESS_BITS", dev.getAddressBits()+""); + map.put("cl_khr_fp16", dev.isHalfFPAvailable()+""); + map.put("cl_khr_fp64", dev.isDoubleFPAvailable()+""); + map.put("CL_DEVICE_ENDIAN_LITTLE", dev.isLittleEndianAvailable()+""); + map.put("CL_DEVICE_HALF_FP_CONFIG", dev.getHalfFPConfig()+""); + map.put("CL_DEVICE_SINGLE_FP_CONFIG", dev.getSingleFPConfig()+""); + map.put("CL_DEVICE_DOUBLE_FP_CONFIG", dev.getDoubleFPConfig()+""); + map.put("CL_DEVICE_EXTENSIONS", dev.getExtensions()+""); + + map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT", dev.getPreferredShortVectorWidth()+""); + map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR", dev.getPreferredCharVectorWidth()+""); + map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", dev.getPreferredIntVectorWidth()+""); + map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG", dev.getPreferredLongVectorWidth()+""); + map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT", dev.getPreferredFloatVectorWidth()+""); + map.put("CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE", dev.getPreferredDoubleVectorWidth()+""); + + //TODO device extensions -> properties + + return map; + } + +} diff --git a/src/com/mbien/opencl/util/MultiQueueBarrier.java b/src/com/mbien/opencl/util/MultiQueueBarrier.java new file mode 100644 index 00000000..a23ca9ad --- /dev/null +++ b/src/com/mbien/opencl/util/MultiQueueBarrier.java @@ -0,0 +1,111 @@ +package com.mbien.opencl.util; + +import com.mbien.opencl.CLCommandQueue; +import com.mbien.opencl.CLEventList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * An utility for synchronizing multiple concurrent {@link CLCommandQueue}s. + * @author Michael Bien + */ +public class MultiQueueBarrier { + + private CountDownLatch latch; + private final Set queues; + + /** + * Creates a new MultiQueueBarrier with the given queueCount. + * It is recommented to use {@link #MultiQueueBarrier(CLCommandQueue... allowedQueues)} if possible + * which restricts the set of allowed queues for the barrier. + */ + public MultiQueueBarrier(int queueCount) { + this.latch = new CountDownLatch(queueCount); + this.queues = null; + } + + /** + * Creates a new MultiQueueBarrier for the given queues. + */ + public MultiQueueBarrier(CLCommandQueue... allowedQueues) { + this.latch = new CountDownLatch(allowedQueues.length); + + HashSet set = new HashSet(allowedQueues.length); + for (CLCommandQueue queue : allowedQueues) { + set.add(queue); + } + this.queues = Collections.unmodifiableSet(set); + } + + /** + * Blocks the current Thread until all commands on the {@link CLCommandQueue} finished excecution. + * This method may be invoked concurrently without synchronization on the MultiQueueBarrier object + * as long each Thread passes a distinct CLCommandQueue as parameter to this method. + */ + public MultiQueueBarrier waitFor(CLCommandQueue queue) { + checkQueue(queue); + + queue.putBarrier(); + latch.countDown(); + return this; + } + + /** + * Blocks the current Thread until the given events on the {@link CLCommandQueue} occurred. + * This method may be invoked concurrently without synchronization on the MultiQueueBarrier object + * as long each Thread passes a distinct CLCommandQueue as parameter to this method. + */ + public MultiQueueBarrier waitFor(CLCommandQueue queue, CLEventList events) { + checkQueue(queue); + + queue.putWaitForEvents(events, true); + latch.countDown(); + return this; + } + + /** + * Blocks until all Threads which called {@link #waitFor} + * continue execution. + * This method blocks only once, all subsequent calls are ignored. + */ + public MultiQueueBarrier await() throws InterruptedException { + latch.await(); + return this; + } + + /** + * @see #await() + * @param timeout the maximum time to wait + * @param unit the time unit of the {@code timeout} argument + */ + public MultiQueueBarrier await(long timeout, TimeUnit unit) throws InterruptedException { + latch.await(timeout, unit); + return this; + } + + /** + * Cancels this barrier and unblocks all waiting threads. + */ + public void cancelBarrier() { + while(latch.getCount() > 0) + latch.countDown(); + } + + /** + * Returns the current number of events which must occure before this barrier unblocks the waiting threads. + * This method is typically used for debugging and testing purposes. + */ + public long getCount() { + return latch.getCount(); + } + + private void checkQueue(CLCommandQueue queue) throws IllegalArgumentException { + if (queues != null && !queues.contains(queue)) { + throw new IllegalArgumentException(queue + " is not in the allowedQueues Set: " + queues); + } + } + +} -- cgit v1.2.3