1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/*
* Copyright (c) 2011, Michael Bien
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Created on Tuesday, May 03 2011
*/
package com.jogamp.opencl.util.concurrent;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opencl.CLBuffer;
import com.jogamp.opencl.CLCommandQueue;
import com.jogamp.opencl.CLContext;
import com.jogamp.opencl.CLDevice;
import com.jogamp.opencl.CLKernel;
import com.jogamp.opencl.CLPlatform;
import com.jogamp.opencl.util.concurrent.CLQueueContext.CLSingleProgramQueueContext;
import java.nio.IntBuffer;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.junit.Rule;
import org.junit.rules.MethodRule;
import org.junit.rules.Timeout;
import com.jogamp.opencl.util.CLMultiContext;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import static java.lang.System.*;
/**
*
* @author Michael Bien
*/
public class CLMultiContextTest {
@Rule
public MethodRule methodTimeout= new Timeout(10000);
@Test
public void createMultiContextTest() {
CLMultiContext mc = CLMultiContext.create(CLPlatform.listCLPlatforms());
try{
List<CLContext> contexts = mc.getContexts();
List<CLDevice> devices = mc.getDevices();
assertFalse(contexts.isEmpty());
assertFalse(devices.isEmpty());
for (CLContext context : contexts) {
out.println(context);
}
for (CLDevice device : devices) {
out.println(device);
}
}finally{
mc.release();
}
}
private final static String programSource =
"kernel void compute(global int* array, int numElements) { \n"
+ " int index = get_global_id(0); \n"
+ " if (index >= numElements) { \n"
+ " return; \n"
+ " } \n"
+ " array[index]++; \n"
+ "} \n";
private final class CLTestTask extends CLTask<CLSingleProgramQueueContext, IntBuffer> {
private final IntBuffer data;
private final String source;
public CLTestTask(String source, IntBuffer buffer) {
this.data = buffer;
this.source = source;
}
@Override
public CLSingleProgramQueueContext createQueueContext(CLCommandQueue queue) {
return new CLSingleProgramQueueContext(queue, source);
}
@Override
public IntBuffer execute(CLSingleProgramQueueContext qc) {
CLCommandQueue queue = qc.getQueue();
CLContext context = qc.getCLContext();
CLKernel kernel = qc.getKernel("compute");
// System.out.println(Thread.currentThread().getName()+" / "+queue);
assertFalse(qc.isReleased());
assertFalse(queue.isReleased());
assertFalse(context.isReleased());
assertFalse(kernel.isReleased());
CLBuffer<IntBuffer> buffer = null;
try{
buffer = context.createBuffer(data);
int gws = buffer.getCLCapacity();
kernel.putArg(buffer).putArg(gws).rewind();
queue.putWriteBuffer(buffer, false);
queue.put1DRangeKernel(kernel, 0, gws, 0);
queue.putReadBuffer(buffer, true);
}finally{
if(buffer != null) {
buffer.release();
}
}
return data;
}
@Override
public Object getContextKey() {
return source.hashCode();
}
}
private List<CLTestTask> createTasks(String source, IntBuffer data, int taskCount, int slice) {
List<CLTestTask> tasks = new ArrayList<CLTestTask>(taskCount);
for (int i = 0; i < taskCount; i++) {
IntBuffer subBuffer = Buffers.slice(data, i*slice, slice);
assertEquals(slice, subBuffer.capacity());
tasks.add(new CLTestTask(source, subBuffer));
}
return tasks;
}
@Test
public void commandQueuePoolTest() throws InterruptedException, ExecutionException {
CLMultiContext mc = CLMultiContext.create(CLPlatform.listCLPlatforms());
try {
CLCommandQueuePool pool = CLCommandQueuePool.create(mc);
assertTrue(pool.getPoolSize() > 0);
final int slice = 64;
final int tasksPerQueue = 10;
final int taskCount = pool.getPoolSize() * tasksPerQueue;
IntBuffer data = Buffers.newDirectIntBuffer(slice*taskCount);
List<CLTestTask> tasks = createTasks(programSource, data, taskCount, slice);
out.println("invoking "+tasks.size()+" tasks on "+pool.getPoolSize()+" queues");
// blocking invoke
List<Future<IntBuffer>> results = pool.invokeAll(tasks);
assertNotNull(results);
checkBuffer(1, data);
// submit blocking emediatly
for (CLTestTask task : tasks) {
IntBuffer ret = pool.submit(task).get();
assertNotNull(ret);
checkBuffer(2, ret);
}
checkBuffer(2, data);
// submitAll using futures
List<Future<IntBuffer>> futures = pool.submitAll(tasks);
for (Future<IntBuffer> future : futures) {
IntBuffer ret = future.get();
assertNotNull(ret);
checkBuffer(3, ret);
}
checkBuffer(3, data);
// switching contexts using different program
final String decrementProgramSource = programSource.replaceAll("\\+\\+", "--");
tasks = createTasks(decrementProgramSource, data, taskCount, slice);
List<Future<IntBuffer>> results2 = pool.invokeAll(tasks);
assertNotNull(results2);
checkBuffer(2, data);
// Note: we have to make sure that we don't resubmit old tasks at this point since
// we wait only for completion of a subset of tasks.
// submit any
data = Buffers.newDirectIntBuffer(slice*taskCount);
tasks = createTasks(decrementProgramSource, data, taskCount, slice);
IntBuffer ret1 = pool.invokeAny(tasks);
assertNotNull(ret1);
checkBuffer(-1, ret1);
checkContains(-1, data);
// completionservice take/any test
data = Buffers.newDirectIntBuffer(slice*taskCount);
tasks = createTasks(decrementProgramSource, data, taskCount, slice);
CLTaskCompletionService<IntBuffer> service = new CLTaskCompletionService(pool);
for (CLTestTask task : tasks) {
service.submit(task);
}
IntBuffer ret2 = service.take().get();
assertNotNull(ret2);
checkBuffer(-1, ret2);
checkContains(-1, data);
pool.release();
}finally{
mc.release();
}
}
private void checkBuffer(int expected, IntBuffer data) {
while(data.hasRemaining()) {
assertEquals(expected, data.get());
}
data.rewind();
}
private void checkContains(int expected, IntBuffer data) {
while(data.hasRemaining()) {
if(expected == data.get()){
data.rewind();
return;
}
}
fail();
}
// @Test
public void loadTest() throws InterruptedException, ExecutionException {
for (int i = 0; i < 40; i++) {
commandQueuePoolTest();
}
}
}
|