JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLMultiContextTest.java
Go to the documentation of this file.
1/*
2 * Created on Tuesday, May 03 2011
3 */
4package com.jogamp.opencl.util.concurrent;
5
6import com.jogamp.common.nio.Buffers;
7import com.jogamp.opencl.CLBuffer;
8import com.jogamp.opencl.CLCommandQueue;
9import com.jogamp.opencl.CLContext;
10import com.jogamp.opencl.CLDevice;
11import com.jogamp.opencl.CLKernel;
12import com.jogamp.opencl.CLPlatform;
13import com.jogamp.opencl.test.util.MiscUtils;
14import com.jogamp.opencl.test.util.UITestCase;
15import com.jogamp.opencl.util.concurrent.CLQueueContext.CLSimpleQueueContext;
16import com.jogamp.opencl.util.concurrent.CLQueueContextFactory.CLSimpleContextFactory;
17
18import java.io.IOException;
19import java.nio.IntBuffer;
20import java.util.concurrent.ExecutionException;
21import java.util.concurrent.Future;
22
23import org.junit.FixMethodOrder;
24import org.junit.Rule;
25import org.junit.rules.Timeout;
26import org.junit.runners.MethodSorters;
27
28import com.jogamp.opencl.util.CLMultiContext;
29
30import java.nio.Buffer;
31import java.util.ArrayList;
32import java.util.List;
33
34import org.junit.Test;
35
36import static org.junit.Assert.*;
37import static java.lang.System.*;
38
39/**
40 *
41 * @author Michael Bien, et.al
42 */
43@FixMethodOrder(MethodSorters.NAME_ASCENDING)
44public class CLMultiContextTest extends UITestCase {
45
46 @Rule
47 public Timeout methodTimeout= new Timeout(10000);
48
49 @Test
50 public void createMultiContextTest() {
51
53
54 try{
55 final List<CLContext> contexts = mc.getContexts();
56 final List<CLDevice> devices = mc.getDevices();
57
58 assertFalse(contexts.isEmpty());
59 assertFalse(devices.isEmpty());
60
61 for (final CLContext context : contexts) {
62 out.println(context);
63 }
64 for (final CLDevice device : devices) {
65 out.println(device);
66 }
67
68 }finally{
69 mc.release();
70 }
71
72 }
73
74 private final static String programSource =
75 "kernel void compute(global int* array, int numElements) { \n"
76 + " int index = get_global_id(0); \n"
77 + " if (index >= numElements) { \n"
78 + " return; \n"
79 + " } \n"
80 + " array[index]++; \n"
81 + "} \n";
82
83 private final class CLTestTask implements CLTask<CLSimpleQueueContext, Buffer> {
84
85 private final Buffer data;
86
87 public CLTestTask(final Buffer buffer) {
88 this.data = buffer;
89 }
90
91 public Buffer execute(final CLSimpleQueueContext qc) {
92
93 final CLCommandQueue queue = qc.getQueue();
94 final CLContext context = qc.getCLContext();
95 final CLKernel kernel = qc.getKernel("compute");
96
97 CLBuffer<Buffer> buffer = null;
98 try{
99 buffer = context.createBuffer(data);
100 final int gws = buffer.getCLCapacity();
101
102 kernel.putArg(buffer).putArg(gws).rewind();
103
104 queue.putWriteBuffer(buffer, true);
105 queue.put1DRangeKernel(kernel, 0, gws, 0);
106 queue.putReadBuffer(buffer, true);
107 }finally{
108 if(buffer != null) {
109 buffer.release();
110 }
111 }
112
113 return data;
114 }
115
116 }
117
118 @Test
119 public void commandQueuePoolTest() throws InterruptedException, ExecutionException {
120
122
123 try {
124
127
128 assertTrue(pool.getSize() > 0);
129
130 final int slice = 64;
131 final int tasksPerQueue = 10;
132 final int taskCount = pool.getSize() * tasksPerQueue;
133
134 final IntBuffer data = Buffers.newDirectIntBuffer(slice*taskCount);
135
136 final List<CLTestTask> tasks = new ArrayList<CLTestTask>(taskCount);
137
138 for (int i = 0; i < taskCount; i++) {
139 final IntBuffer subBuffer = Buffers.slice(data, i*slice, slice);
140 assertEquals(slice, subBuffer.capacity());
141 tasks.add(new CLTestTask(subBuffer));
142 }
143
144 out.println("invoking "+tasks.size()+" tasks on "+pool.getSize()+" queues");
145
146 // blocking invoke
147 pool.invokeAll(tasks);
148 checkBuffer(1, data);
149
150 // submit blocking emediatly
151 for (final CLTestTask task : tasks) {
152 pool.submit(task).get();
153 }
154 checkBuffer(2, data);
155
156 // submitAll using futures
157 final List<Future<Buffer>> futures = pool.submitAll(tasks);
158 for (final Future<Buffer> future : futures) {
159 future.get();
160 }
161 checkBuffer(3, data);
162
163 // switching contexts using different program
164 factory = CLQueueContextFactory.createSimple(programSource.replaceAll("\\+\\+", "--"));
165 pool.switchContext(factory);
166 pool.invokeAll(tasks);
167 checkBuffer(2, data);
168
169 pool.release();
170 }finally{
171 mc.release();
172 }
173 }
174
175 private void checkBuffer(final int expected, final IntBuffer data) {
176 while(data.hasRemaining()) {
177 assertEquals(expected, data.get());
178 }
179 data.rewind();
180 }
181
182 public static void main(final String[] args) throws IOException {
183 final String tstname = CLMultiContextTest.class.getName();
184 org.junit.runner.JUnitCore.main(tstname);
185 }
186
187}
OpenCL buffer object wrapping an optional NIO buffer.
Definition: CLBuffer.java:46
The command queue is used to queue a set of operations for a specific CLDevice.
CLCommandQueue put1DRangeKernel(final CLKernel kernel, final long globalWorkOffset, final long globalWorkSize, final long localWorkSize)
Calls {@native clEnqueueNDRangeKernel}.
CLCommandQueue putWriteBuffer(final CLBuffer<?> writeBuffer, final boolean blockingRead)
Calls {@native clEnqueueWriteBuffer}.
CLCommandQueue putReadBuffer(final CLBuffer<?> readBuffer, final boolean blockingRead)
Calls {@native clEnqueueReadBuffer}.
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
Definition: CLContext.java:79
final CLBuffer<?> createBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags.
Definition: CLContext.java:341
This object represents an OpenCL device.
Definition: CLDevice.java:53
High level abstraction for an OpenCL Kernel.
Definition: CLKernel.java:53
CLKernel rewind()
Resets the argument index to 0.
Definition: CLKernel.java:158
CLKernel putArg(final CLMemory<?> value)
Definition: CLKernel.java:107
int getCLCapacity()
Returns the size in buffer elements of this memory object.
Definition: CLMemory.java:171
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
static CLPlatform[] listCLPlatforms()
Lists all available OpenCL implementations.
Utility for organizing multiple CLContexts.
static CLMultiContext create(final CLPlatform... platforms)
Creates a multi context with all devices of the specified platforms.
List< CLDevice > getDevices()
Returns a list containing all devices used in this multi context.
void release()
Releases all contexts.
A multithreaded, fixed size pool of OpenCL command queues.
int getSize()
Returns the size of this pool (number of command queues).
static< C extends CLQueueContext > CLCommandQueuePool< C > create(final CLQueueContextFactory< C > factory, final CLMultiContext mc, final CLCommandQueue.Mode... modes)
CLCommandQueuePool< C > switchContext(final CLQueueContextFactory< C > factory)
Switches the context of all queues - this operation can be expensive.
Creates CLSimpleQueueContexts containing a precompiled program.
static CLSimpleContextFactory createSimple(final String source)
Creates a simple context factory producing single program contexts.
A task executed on a command queue.
Definition: CLTask.java:11