summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Bien <[email protected]>2010-09-21 00:48:32 +0200
committerMichael Bien <[email protected]>2010-09-21 00:48:32 +0200
commitd2f1955832dda891cb6d711c43bcb59e7199a38c (patch)
treec1f4087ec751de624ff50e5812dddeeb1e1e478b
parenta5b0d5948765a1695d9c02a30be599f905152668 (diff)
initial import of utility API for filtering platforms.
-rw-r--r--src/com/jogamp/opencl/CLPlatform.java55
-rw-r--r--src/com/jogamp/opencl/util/CLPlatformFilters.java48
-rw-r--r--src/com/jogamp/opencl/util/Filter.java18
-rw-r--r--test/com/jogamp/opencl/HighLevelBindingTest.java18
4 files changed, 131 insertions, 8 deletions
diff --git a/src/com/jogamp/opencl/CLPlatform.java b/src/com/jogamp/opencl/CLPlatform.java
index aef828d1..2fd7d3f1 100644
--- a/src/com/jogamp/opencl/CLPlatform.java
+++ b/src/com/jogamp/opencl/CLPlatform.java
@@ -10,6 +10,7 @@ import com.jogamp.gluegen.runtime.FunctionAddressResolver;
import com.jogamp.opencl.util.CLUtil;
import com.jogamp.opencl.impl.CLImpl;
import com.jogamp.opencl.impl.CLProcAddressTable;
+import com.jogamp.opencl.util.Filter;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
@@ -118,10 +119,25 @@ public final class CLPlatform {
*/
public static CLPlatform getDefault() {
initialize();
- CLPlatform[] platforms = listCLPlatforms();
+ return latest(listCLPlatforms());
+ }
+
+ /**
+ * Returns the default OpenCL platform or null when no platform found.
+ */
+ public static CLPlatform getDefault(Filter<CLPlatform>... filter) {
+ CLPlatform[] platforms = listCLPlatforms(filter);
+ if(platforms.length > 0) {
+ return latest(platforms);
+ }else{
+ return null;
+ }
+ }
+
+ private static CLPlatform latest(CLPlatform[] platforms) {
CLPlatform best = platforms[0];
for (CLPlatform platform : platforms) {
- if(platform.version.compareTo(best.version) > 0) {
+ if (platform.version.compareTo(best.version) > 0) {
best = platform;
}
}
@@ -133,6 +149,15 @@ public final class CLPlatform {
* @throws CLException if something went wrong initializing OpenCL
*/
public static CLPlatform[] listCLPlatforms() {
+ return listCLPlatforms((Filter<CLPlatform>[])null);
+ }
+
+ /**
+ * Lists all available OpenCL implementations. The platforms returned must pass all filters.
+ * @param filter Acceptance filter for the returned platforms.
+ * @throws CLException if something went wrong initializing OpenCL
+ */
+ public static CLPlatform[] listCLPlatforms(Filter<CLPlatform>... filter) {
initialize();
IntBuffer ib = Buffers.newDirectIntBuffer(1);
@@ -145,12 +170,27 @@ public final class CLPlatform {
ret = cl.clGetPlatformIDs(platformId.capacity(), platformId, null);
checkForError(ret, "can not enumerate platforms");
- CLPlatform[] platforms = new CLPlatform[platformId.capacity()];
-
- for (int i = 0; i < platformId.capacity(); i++)
- platforms[i] = new CLPlatform(platformId.get(i));
+ List<CLPlatform> platforms = new ArrayList<CLPlatform>();
+
+ for (int i = 0; i < platformId.capacity(); i++) {
+ CLPlatform platform = new CLPlatform(platformId.get(i));
+ if(filter == null) {
+ platforms.add(platform);
+ }else{
+ boolean accepted = true;
+ for (Filter<CLPlatform> f : filter) {
+ if(!f.accept(platform)) {
+ accepted = false;
+ break;
+ }
+ }
+ if(accepted) {
+ platforms.add(platform);
+ }
+ }
+ }
- return platforms;
+ return platforms.toArray(new CLPlatform[platforms.size()]);
}
/**
@@ -411,5 +451,4 @@ public final class CLPlatform {
return hash;
}
-
}
diff --git a/src/com/jogamp/opencl/util/CLPlatformFilters.java b/src/com/jogamp/opencl/util/CLPlatformFilters.java
new file mode 100644
index 00000000..a1279f10
--- /dev/null
+++ b/src/com/jogamp/opencl/util/CLPlatformFilters.java
@@ -0,0 +1,48 @@
+package com.jogamp.opencl.util;
+
+import com.jogamp.opencl.CLDevice;
+import com.jogamp.opencl.CLPlatform;
+import com.jogamp.opencl.CLVersion;
+import java.util.Arrays;
+
+/**
+ * Pre-defined filters.
+ * @author Michael Bien
+ * @see CLPlatform#getDefault(com.jogamp.opencl.util.Filter<com.jogamp.opencl.CLPlatform>[])
+ * @see CLPlatform#listCLPlatforms(com.jogamp.opencl.util.Filter<com.jogamp.opencl.CLPlatform>[])
+ */
+public class CLPlatformFilters {
+
+ /**
+ * Accepts all platforms supporting at least the given OpenCL spec version.
+ */
+ public static Filter<CLPlatform> version(final CLVersion version) {
+ return new Filter<CLPlatform>() {
+ public boolean accept(CLPlatform item) {
+ return item.isAtLeast(version);
+ }
+ };
+ }
+
+ /**
+ * Accepts all platforms containing devices of the given type.
+ */
+ public static Filter<CLPlatform> type(final CLDevice.Type type) {
+ return new Filter<CLPlatform>() {
+ public boolean accept(CLPlatform item) {
+ return item.listCLDevices(type).length > 0;
+ }
+ };
+ }
+
+ /**
+ * Accepts all platforms containing devices of the given extensions.
+ */
+ public static Filter<CLPlatform> extensions(final String... extensions) {
+ return new Filter<CLPlatform>() {
+ public boolean accept(CLPlatform item) {
+ return item.getExtensions().containsAll(Arrays.asList(extensions));
+ }
+ };
+ }
+}
diff --git a/src/com/jogamp/opencl/util/Filter.java b/src/com/jogamp/opencl/util/Filter.java
new file mode 100644
index 00000000..a1bd57a2
--- /dev/null
+++ b/src/com/jogamp/opencl/util/Filter.java
@@ -0,0 +1,18 @@
+/*
+ * Created on Sunday, September 19 2010
+ */
+
+package com.jogamp.opencl.util;
+
+/**
+ *
+ * @author Michael Bien
+ */
+public interface Filter<I> {
+
+ /**
+ * Returns true only if the item should be accepted.
+ */
+ public boolean accept(I item);
+
+}
diff --git a/test/com/jogamp/opencl/HighLevelBindingTest.java b/test/com/jogamp/opencl/HighLevelBindingTest.java
index 0c0aa63d..695da7b2 100644
--- a/test/com/jogamp/opencl/HighLevelBindingTest.java
+++ b/test/com/jogamp/opencl/HighLevelBindingTest.java
@@ -22,6 +22,9 @@ import org.junit.Test;
import static org.junit.Assert.*;
import static java.lang.System.*;
import static com.jogamp.opencl.TestUtils.*;
+import static com.jogamp.opencl.util.CLPlatformFilters.*;
+import static com.jogamp.opencl.CLVersion.*;
+import static com.jogamp.opencl.CLDevice.Type.*;
import static com.jogamp.common.nio.Buffers.*;
/**
@@ -164,6 +167,21 @@ public class HighLevelBindingTest {
}
@Test
+ public void platformTest() {
+
+ CLPlatform platformGPU = CLPlatform.getDefault(version(CL_1_0), type(GPU));
+ CLPlatform platformCPU = CLPlatform.getDefault(version(CL_1_0), type(CPU));
+
+ if(platformGPU != null) {
+ assertTrue(platformGPU.listCLDevices(GPU).length > 0);
+ }else if(platformCPU != null) {
+ assertTrue(platformCPU.listCLDevices(CPU).length > 0);
+ }else{
+ fail("please tell us about your hardware");
+ }
+ }
+
+ @Test
public void createContextTest() {
out.println(" - - - highLevelTest; create context - - - ");