JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLPlatformFilters.java
Go to the documentation of this file.
1/*
2 * Copyright 2009 - 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opencl.util;
30
31import com.jogamp.opencl.CLCommandQueue.Mode;
32import com.jogamp.opencl.CLDevice;
33import com.jogamp.opencl.CLPlatform;
34import com.jogamp.opencl.CLVersion;
35import java.util.Arrays;
36import com.jogamp.opengl.GL;
37import com.jogamp.opengl.GLContext;
38
39/**
40 * Pre-defined filters.
41 * @author Michael Bien
42 * @see CLPlatform#getDefault(com.jogamp.opencl.util.Filter[])
43 * @see CLPlatform#listCLPlatforms(com.jogamp.opencl.util.Filter[])
44 */
45public class CLPlatformFilters {
46
47 /**
48 * Accepts all platforms supporting at least the given OpenCL spec version.
49 */
51 return new Filter<CLPlatform>() {
52 public boolean accept(final CLPlatform item) {
53 return item.isAtLeast(version);
54 }
55 };
56 }
57
58 /**
59 * Accepts all platforms containing devices of the given type.
60 */
61 public static Filter<CLPlatform> type(final CLDevice.Type type) {
62 return new Filter<CLPlatform>() {
63 public boolean accept(final CLPlatform item) {
64 return item.listCLDevices(type).length > 0;
65 }
66 };
67 }
68
69 /**
70 * Accepts all platforms containing at least one devices of which supports OpenGL-OpenCL interoperability.
71 */
72 public static Filter<CLPlatform> glSharing() {
73 return new Filter<CLPlatform>() {
74 private final Filter<CLDevice> glFilter = CLDeviceFilters.glSharing();
75 public boolean accept(final CLPlatform item) {
76 final CLDevice[] devices = item.listCLDevices();
77 for (final CLDevice device : devices) {
78 if(glFilter.accept(device)) {
79 return true;
80 }
81 }
82 return false;
83 }
84 };
85 }
86
87 /**
88 * Accepts all with the given OpenGL context compatible platforms containing at least one
89 * devices of which supports OpenGL-OpenCL interoperability.
90 */
91 public static Filter<CLPlatform> glSharing(final GLContext context) {
92 return new Filter<CLPlatform>() {
93 private final Filter<CLPlatform> glFilter = glSharing();
94 public boolean accept(final CLPlatform item) {
95 final String glVendor = context.getGL().glGetString(GL.GL_VENDOR);
96 final String clVendor = item.getVendor();
97 return areVendorsCompatible(glVendor,clVendor) && glFilter.accept(item);
98 }
99 };
100 }
101
102 /**
103 * We need this test because:
104 * - On at least some AMD cards, the GL vendor is ATI, but the CL vendor is AMD.
105 * - On at least some Macs, the GL vendor is Nvidia, but the CL vendor is Apple.
106 * @param glVendor OpenGL vendor string.
107 * @param clVendor OpenCL vendor string.
108 * @return true if the strings are either the same, or indicate that they're part of the same card.
109 */
110 private static boolean areVendorsCompatible(final String glVendor, final String clVendor) {
111 return( clVendor.equals(glVendor)
112 || (glVendor.contains("ATI Technologies") && clVendor.contains("Advanced Micro Devices"))
113 || (glVendor.contains("NVIDIA Corporation") && clVendor.contains("Apple")));
114 }
115
116 /**
117 * Accepts all platforms supporting the given extensions.
118 */
119 public static Filter<CLPlatform> extension(final String... extensions) {
120 return new Filter<CLPlatform>() {
121 public boolean accept(final CLPlatform item) {
122 return item.getExtensions().containsAll(Arrays.asList(extensions));
123 }
124 };
125 }
126
127 /**
128 * Accepts all platforms containing at least one devices supporting the specified command queue modes.
129 */
130 public static Filter<CLPlatform> queueMode(final Mode... modes) {
131 return new Filter<CLPlatform>() {
132 private final Filter<CLDevice> queueModeFilter = CLDeviceFilters.queueMode(modes);
133 public boolean accept(final CLPlatform item) {
134 for (final CLDevice device : item.listCLDevices()) {
135 if(queueModeFilter.accept(device)) {
136 return true;
137 }
138 }
139 return false;
140 }
141 };
142 }
143}
This object represents an OpenCL device.
Definition: CLDevice.java:53
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
CLDevice[] listCLDevices()
Lists all physical devices available on this platform.
boolean isAtLeast(final CLVersion other)
String getVendor()
Returns the platform vendor.
synchronized Set< String > getExtensions()
Returns all platform extension names as unmodifiable Set.
Version of an OpenCL Implementation.
Definition: CLVersion.java:42
static Filter< CLDevice > queueMode(final Mode... modes)
Accepts all devices supporting the specified command queue modes.
static Filter< CLDevice > glSharing()
Accepts all devices which support OpenGL-OpenCL interoperability.
static Filter< CLPlatform > glSharing()
Accepts all platforms containing at least one devices of which supports OpenGL-OpenCL interoperabilit...
static Filter< CLPlatform > queueMode(final Mode... modes)
Accepts all platforms containing at least one devices supporting the specified command queue modes.
static Filter< CLPlatform > glSharing(final GLContext context)
Accepts all with the given OpenGL context compatible platforms containing at least one devices of whi...
static Filter< CLPlatform > extension(final String... extensions)
Accepts all platforms supporting the given extensions.
static Filter< CLPlatform > type(final CLDevice.Type type)
Accepts all platforms containing devices of the given type.
static Filter< CLPlatform > version(final CLVersion version)
Accepts all platforms supporting at least the given OpenCL spec version.
Enumeration for the command-queue settings.
Enumeration for the type of a device.
Definition: CLDevice.java:798
boolean accept(I item)
Returns true only if the item should be accepted.