summaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/util
diff options
context:
space:
mode:
authorMichael Bien <[email protected]>2010-04-12 22:18:39 +0200
committerMichael Bien <[email protected]>2010-04-12 22:18:39 +0200
commitbf07b44ed6a8958dd321cc4c08fd2bdd08299611 (patch)
treee24b7c4e4197a80e0ecaad75b9b3667299fd8323 /src/com/jogamp/opencl/util
parent7680472b21ec1e2deacb49addae65c820a2e2a4d (diff)
renamed package com.mbien.* in com.jogamp.* JOCL is now officially a JogAmp team player ;).
Diffstat (limited to 'src/com/jogamp/opencl/util')
-rw-r--r--src/com/jogamp/opencl/util/CLBuildConfiguration.java105
-rw-r--r--src/com/jogamp/opencl/util/CLProgramConfiguration.java50
-rw-r--r--src/com/jogamp/opencl/util/CLUtil.java119
-rw-r--r--src/com/jogamp/opencl/util/MultiQueueBarrier.java141
4 files changed, 415 insertions, 0 deletions
diff --git a/src/com/jogamp/opencl/util/CLBuildConfiguration.java b/src/com/jogamp/opencl/util/CLBuildConfiguration.java
new file mode 100644
index 00000000..1de62637
--- /dev/null
+++ b/src/com/jogamp/opencl/util/CLBuildConfiguration.java
@@ -0,0 +1,105 @@
+package com.jogamp.opencl.util;
+
+import com.jogamp.opencl.CLDevice;
+import com.jogamp.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.jogamp.opencl.CLProgramBuilder#createConfiguration()
+ * @see com.jogamp.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 com.jogamp.opencl.CLProgram#define(java.lang.String)
+ */
+ public CLBuildConfiguration withDefines(String... names);
+
+ /**
+ * Adds the definitions to the build configuration.
+ * @see com.jogamp.opencl.CLProgram#define(java.lang.String, java.lang.Object)
+ */
+ public CLBuildConfiguration withDefines(Map<String, ? extends Object> defines);
+
+ /**
+ * Adds the compiler option to the build configuration.
+ * @see com.jogamp.opencl.CLProgram.CompilerOptions
+ */
+ public CLBuildConfiguration withOption(String option);
+
+ /**
+ * Adds the compiler options to the build configuration.
+ * @see com.jogamp.opencl.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/jogamp/opencl/util/CLProgramConfiguration.java b/src/com/jogamp/opencl/util/CLProgramConfiguration.java
new file mode 100644
index 00000000..901e28ce
--- /dev/null
+++ b/src/com/jogamp/opencl/util/CLProgramConfiguration.java
@@ -0,0 +1,50 @@
+package com.jogamp.opencl.util;
+
+import com.jogamp.opencl.CLDevice;
+import com.jogamp.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.jogamp.opencl.CLProgramBuilder#createConfiguration(com.jogamp.opencl.CLProgram)
+ * @see com.jogamp.opencl.CLProgramBuilder#loadConfiguration(java.io.ObjectInputStream, com.jogamp.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<String, ? extends Object> 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/jogamp/opencl/util/CLUtil.java b/src/com/jogamp/opencl/util/CLUtil.java
new file mode 100644
index 00000000..6649698c
--- /dev/null
+++ b/src/com/jogamp/opencl/util/CLUtil.java
@@ -0,0 +1,119 @@
+package com.jogamp.opencl.util;
+
+import com.jogamp.opencl.CL;
+import com.jogamp.opencl.CLDevice;
+import com.jogamp.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<String, String> obtainPlatformProperties(CLPlatform platform) {
+
+ Map<String, String> map = new LinkedHashMap<String, String>();
+ 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<String, String> obtainDeviceProperties(CLDevice dev) {
+
+ Map<String, String> map = new LinkedHashMap<String, String>();
+ 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_EXECUTION_CAPABILITIES", dev.getExecutionCapabilities()+"");
+
+ 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.isLittleEndian()+"");
+ 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/jogamp/opencl/util/MultiQueueBarrier.java b/src/com/jogamp/opencl/util/MultiQueueBarrier.java
new file mode 100644
index 00000000..59398b5e
--- /dev/null
+++ b/src/com/jogamp/opencl/util/MultiQueueBarrier.java
@@ -0,0 +1,141 @@
+package com.jogamp.opencl.util;
+
+import com.jogamp.opencl.CLCommandQueue;
+import com.jogamp.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.
+ * This Barrier can be reused after it has been broken.
+ * @author Michael Bien
+ */
+public class MultiQueueBarrier {
+
+ private CountDownLatch latch;
+ private final Set<CLCommandQueue> queues;
+ private final int count;
+
+ /**
+ * 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) {
+ if(queueCount == 0) {
+ throw new IllegalArgumentException("queueCount was 0");
+ }
+ this.latch = new CountDownLatch(queueCount);
+ this.queues = null;
+ this.count = queueCount;
+ }
+
+ /**
+ * Creates a new MultiQueueBarrier for the given queues.
+ */
+ public MultiQueueBarrier(CLCommandQueue... allowedQueues) {
+ if(allowedQueues.length == 0) {
+ throw new IllegalArgumentException("allowedQueues was empty");
+ }
+ this.latch = new CountDownLatch(allowedQueues.length);
+ this.count = allowedQueues.length;
+
+ HashSet<CLCommandQueue> set = new HashSet<CLCommandQueue>(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();
+ synchronized(this) {
+ 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);
+ synchronized(this) {
+ 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();
+ rebuildBarrierIfBroken();
+ 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);
+ rebuildBarrierIfBroken();
+ return this;
+ }
+
+ /**
+ * Resets this barrier and unblocks all waiting threads.
+ */
+ public void resetBarrier() {
+ synchronized(this) {
+ while(latch.getCount() > 0) {
+ latch.countDown();
+ }
+ // thats OK. Another Thread can not rebuild the barrier since we have the lock.
+ // we have to rebuid it here in case there was no thread waiting.
+ latch = new CountDownLatch(count);
+ }
+ }
+
+ private void rebuildBarrierIfBroken() {
+ synchronized (this) {
+ if (latch.getCount() == 0) {
+ latch = new CountDownLatch(count);
+ }
+ }
+ }
+
+ /**
+ * 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);
+ }
+ }
+
+}