4package com.jogamp.opencl.util.concurrent;
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;
18import java.io.IOException;
19import java.nio.IntBuffer;
20import java.util.concurrent.ExecutionException;
21import java.util.concurrent.Future;
23import org.junit.FixMethodOrder;
25import org.junit.rules.Timeout;
26import org.junit.runners.MethodSorters;
28import com.jogamp.opencl.util.CLMultiContext;
30import java.nio.Buffer;
31import java.util.ArrayList;
36import static org.junit.Assert.*;
37import static java.lang.System.*;
43@FixMethodOrder(MethodSorters.NAME_ASCENDING)
47 public Timeout methodTimeout=
new Timeout(10000);
56 final List<CLDevice> devices = mc.
getDevices();
58 assertFalse(contexts.isEmpty());
59 assertFalse(devices.isEmpty());
61 for (
final CLContext context : contexts) {
64 for (
final CLDevice device : devices) {
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"
80 +
" array[index]++; \n"
83 private final class CLTestTask
implements CLTask<CLSimpleQueueContext, Buffer> {
85 private final Buffer data;
87 public CLTestTask(
final Buffer buffer) {
91 public Buffer execute(
final CLSimpleQueueContext qc) {
94 final CLContext context = qc.getCLContext();
95 final CLKernel kernel = qc.getKernel(
"compute");
128 assertTrue(pool.
getSize() > 0);
130 final int slice = 64;
131 final int tasksPerQueue = 10;
132 final int taskCount = pool.
getSize() * tasksPerQueue;
134 final IntBuffer data = Buffers.newDirectIntBuffer(slice*taskCount);
136 final List<CLTestTask> tasks =
new ArrayList<CLTestTask>(taskCount);
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));
144 out.println(
"invoking "+tasks.size()+
" tasks on "+pool.
getSize()+
" queues");
147 pool.invokeAll(tasks);
148 checkBuffer(1, data);
151 for (
final CLTestTask task : tasks) {
152 pool.submit(task).get();
154 checkBuffer(2, data);
157 final List<Future<Buffer>> futures = pool.submitAll(tasks);
158 for (
final Future<Buffer> future : futures) {
161 checkBuffer(3, data);
166 pool.invokeAll(tasks);
167 checkBuffer(2, data);
175 private void checkBuffer(
final int expected,
final IntBuffer data) {
176 while(data.hasRemaining()) {
177 assertEquals(expected, data.get());
182 public static void main(
final String[] args)
throws IOException {
184 org.junit.runner.JUnitCore.
main(tstname);
OpenCL buffer object wrapping an optional NIO buffer.
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...
final CLBuffer<?> createBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags.
This object represents an OpenCL device.
High level abstraction for an OpenCL Kernel.
CLKernel rewind()
Resets the argument index to 0.
CLKernel putArg(final CLMemory<?> value)
int getCLCapacity()
Returns the size in buffer elements of this memory object.
Utility for organizing multiple CLContexts.
static CLMultiContext create(final CLPlatform... platforms)
Creates a multi context with all devices of the specified platforms.
List< CLContext > getContexts()
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)
void release()
Releases all queues.
CLCommandQueuePool< C > switchContext(final CLQueueContextFactory< C > factory)
Switches the context of all queues - this operation can be expensive.
static void main(final String[] args)
void createMultiContextTest()
void commandQueuePoolTest()
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.