summaryrefslogtreecommitdiffstats
path: root/test/com/jogamp/opencl/util/concurrent/CLMultiContextTest.java
blob: 1b2575f552d84b34d20414db52103dbc7e9dd46c (plain)
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
/*
 * 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.CLSimpleQueueContext;
import com.jogamp.opencl.util.concurrent.CLQueueContextFactory.CLSimpleContextFactory;
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 implements CLTask<CLSimpleQueueContext, IntBuffer> {

        private final IntBuffer data;

        public CLTestTask(IntBuffer buffer) {
            this.data = buffer;
        }

        @Override
        public IntBuffer execute(CLSimpleQueueContext 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;
        }

    }

    private List<CLTestTask> createTasks(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(subBuffer));
        }
        return tasks;
    }

    @Test
    public void commandQueuePoolTest() throws InterruptedException, ExecutionException {

        CLMultiContext mc = CLMultiContext.create(CLPlatform.listCLPlatforms());

        try {

            CLSimpleContextFactory factory = CLQueueContextFactory.createSimple(programSource);
            CLCommandQueuePool<CLSimpleQueueContext> pool = CLCommandQueuePool.create(factory, 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(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
            factory = CLQueueContextFactory.createSimple(programSource.replaceAll("\\+\\+", "--"));
            pool.switchContext(factory);
            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(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(data, taskCount, slice);

            CLTaskCompletionService<CLSimpleQueueContext, 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();
        }
    }

}