aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
3 files changed, 113 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);
+
+}