JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLMultiContext.java
Go to the documentation of this file.
1/*
2 * Created on Thursday, April 28 2011 22:10
3 */
4package com.jogamp.opencl.util;
5
6import com.jogamp.opencl.CLContext;
7import com.jogamp.opencl.CLDevice;
8import com.jogamp.opencl.CLPlatform;
9import com.jogamp.opencl.CLResource;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16
17import static java.util.Arrays.*;
18import static com.jogamp.opencl.CLDevice.Type.*;
19
20/**
21 * Utility for organizing multiple {@link CLContext}s.
22 *
23 * @author Michael Bien
24 */
25public class CLMultiContext implements CLResource {
26
27 private final List<CLContext> contexts;
28 private boolean released;
29
30 private CLMultiContext() {
31 contexts = new ArrayList<CLContext>();
32 }
33
34 /**
35 * Creates a multi context with all devices of the specified platforms.
36 */
37 public static CLMultiContext create(final CLPlatform... platforms) {
38 return create(platforms, ALL);
39 }
40
41 /**
42 * Creates a multi context with all devices of the specified platforms and types.
43 */
44 @SuppressWarnings("unchecked")
45 public static CLMultiContext create(final CLPlatform[] platforms, final CLDevice.Type... types) {
46 return create(platforms, CLDeviceFilters.type(types));
47 }
48
49 /**
50 * Creates a multi context with all matching devices of the specified platforms.
51 */
52 @SuppressWarnings("unchecked")
53 public static CLMultiContext create(final CLPlatform[] platforms, final Filter<CLDevice>... filters) {
54
55 if(platforms == null) {
56 throw new NullPointerException("platform list was null");
57 }else if(platforms.length == 0) {
58 throw new IllegalArgumentException("platform list was empty");
59 }
60
61 final List<CLDevice> devices = new ArrayList<CLDevice>();
62 for (final CLPlatform platform : platforms) {
63 devices.addAll(asList(platform.listCLDevices(filters)));
64 }
65 return create(devices);
66 }
67
68 /**
69 * Creates a multi context with the specified devices.
70 * The devices don't have to be from the same platform.
71 */
72 public static CLMultiContext create(final Collection<CLDevice> devices) {
73
74 if(devices.isEmpty()) {
75 throw new IllegalArgumentException("device list was empty");
76 }
77
78 final Map<CLPlatform, List<CLDevice>> platformDevicesMap = filterPlatformConflicts(devices);
79
80 // create contexts
81 final CLMultiContext mc = new CLMultiContext();
82 for (final Map.Entry<CLPlatform, List<CLDevice>> entry : platformDevicesMap.entrySet()) {
83 final List<CLDevice> list = entry.getValue();
84 // one context per device to workaround driver bugs
85 for (final CLDevice device : list) {
86 final CLContext context = CLContext.create(device);
87 mc.contexts.add(context);
88 }
89 }
90
91 return mc;
92 }
93
94 /**
95 * Creates a multi context with specified contexts.
96 */
97 public static CLMultiContext wrap(final CLContext... contexts) {
98 final CLMultiContext mc = new CLMultiContext();
99 mc.contexts.addAll(asList(contexts));
100 return mc;
101 }
102
103 /**
104 * filter devices; don't allow the same device to be used in more than one platform.
105 * example: a CPU available via the AMD and Intel SDKs shouldn't end up in two contexts
106 */
107 private static Map<CLPlatform, List<CLDevice>> filterPlatformConflicts(final Collection<CLDevice> devices) {
108
109 // FIXME: devicename-platform is used as unique device identifier - replace if we have something better
110
111 final Map<CLPlatform, List<CLDevice>> filtered = new HashMap<CLPlatform, List<CLDevice>>();
112 final Map<String, CLPlatform> used = new HashMap<String, CLPlatform>();
113
114 for (final CLDevice device : devices) {
115
116 final String name = device.getName();
117
118 final CLPlatform platform = device.getPlatform();
119 final CLPlatform usedPlatform = used.get(name);
120
121 if(usedPlatform == null || platform.equals(usedPlatform)) {
122 if(!filtered.containsKey(platform)) {
123 filtered.put(platform, new ArrayList<CLDevice>());
124 }
125 filtered.get(platform).add(device);
126 used.put(name, platform);
127 }
128
129 }
130 return filtered;
131 }
132
133
134 /**
135 * Releases all contexts.
136 * @see CLContext#release()
137 */
138 @Override
139 public void release() {
140 if(released) {
141 throw new RuntimeException(getClass().getSimpleName()+" already released");
142 }
143 released = true;
144 for (final CLContext context : contexts) {
145 context.release();
146 }
147 contexts.clear();
148 }
149
150 public List<CLContext> getContexts() {
151 return Collections.unmodifiableList(contexts);
152 }
153
154 /**
155 * Returns a list containing all devices used in this multi context.
156 */
157 public List<CLDevice> getDevices() {
158 final List<CLDevice> devices = new ArrayList<CLDevice>();
159 for (final CLContext context : contexts) {
160 devices.addAll(asList(context.getDevices()));
161 }
162 return devices;
163 }
164
165 public boolean isReleased() {
166 return released;
167 }
168
169 @Override
170 public String toString() {
171 return getClass().getSimpleName()+" [" + contexts.size()+" contexts, "
172 + getDevices().size()+ " devices]";
173 }
174
175
176
177}
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
Definition: CLContext.java:79
static CLContext create()
Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
Definition: CLContext.java:139
This object represents an OpenCL device.
Definition: CLDevice.java:53
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
boolean equals(final Object obj)
static Filter< CLDevice > type(final CLDevice.Type... types)
Accepts all devices of the given type.
Utility for organizing multiple CLContexts.
static CLMultiContext create(final CLPlatform... platforms)
Creates a multi context with all devices of the specified platforms.
static CLMultiContext create(final Collection< CLDevice > devices)
Creates a multi context with the specified devices.
boolean isReleased()
Returns true if release() has been called.
static CLMultiContext wrap(final CLContext... contexts)
Creates a multi context with specified contexts.
List< CLDevice > getDevices()
Returns a list containing all devices used in this multi context.
void release()
Releases all contexts.
Enumeration for the type of a device.
Definition: CLDevice.java:798
Releasable OpenCL resource.
Definition: CLResource.java:35