summaryrefslogtreecommitdiffstats
path: root/test/com/mbien/opencl/CLCommandQueueTest.java
blob: 5857bc46e33c459cecd6b28bb06d3a348d3334c4 (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
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
266
package com.mbien.opencl;

import com.mbien.opencl.util.MultiQueueBarrier;
import com.mbien.opencl.CLCommandQueue.Mode;
import com.mbien.opencl.CLMemory.Mem;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.EnumSet;
import org.junit.Test;

import static org.junit.Assert.*;
import static java.lang.System.*;
import static com.mbien.opencl.TestUtils.*;
import static com.mbien.opencl.CLEvent.*;
import static com.jogamp.gluegen.runtime.BufferFactory.*;

/**
 *
 * @author Michael Bien
 */
public class CLCommandQueueTest {

    private final int groupSize = 256;

    @Test
    public void enumsTest() {

        //CLCommandQueueEnums
        EnumSet<Mode> queueMode = Mode.valuesOf(CL.CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL.CL_QUEUE_PROFILING_ENABLE);
        assertTrue(queueMode.contains(Mode.OUT_OF_ORDER_MODE));
        assertTrue(queueMode.contains(Mode.PROFILING_MODE));

        assertNotNull(Mode.valuesOf(0));
        assertEquals(0, Mode.valuesOf(0).size());
        for (Mode mode : Mode.values()) {
            assertEquals(mode, Mode.valueOf(mode.QUEUE_MODE));
        }

        // CLEvent enums
        for (ProfilingCommand cmd : ProfilingCommand.values()) {
            assertEquals(cmd, ProfilingCommand.valueOf(cmd.COMMAND));
        }

        for (CommandType type : CommandType.values()) {
            assertEquals(type, CommandType.valueOf(type.TYPE));
        }

        for (ExecutionStatus status : ExecutionStatus.values()) {
            assertEquals(status, ExecutionStatus.valueOf(status.STATUS));
        }

    }

    @Test
    public void eventsTest() throws IOException {

        out.println(" - - - event synchronization test - - - ");

        final int elements = roundUp(groupSize, ONE_MB / SIZEOF_INT * 5); // 5MB per buffer

        CLContext context = CLContext.create();

        CLBuffer<ByteBuffer> clBufferA = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        CLBuffer<ByteBuffer> clBufferB = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        CLBuffer<ByteBuffer> clBufferC = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        CLBuffer<ByteBuffer> clBufferD = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);

        fillBuffer(clBufferA.buffer, 12345);
        fillBuffer(clBufferB.buffer, 67890);

        CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl")).build();
        CLKernel vectorAddKernel = program.createCLKernel("VectorAddGM").setArg(3, elements);
        CLCommandQueue queue = context.getDevices()[0].createCommandQueue();

        final CLEventList events = new CLEventList(2);

        assertEquals(0, events.size());

        queue.putWriteBuffer(clBufferA, false, events) // write A
             .putWriteBuffer(clBufferB, false, events);// write B

        assertEquals(2, events.size());
        queue.putWaitForEvents(events, true);

        events.release();
        assertEquals(0, events.size());

        vectorAddKernel.setArgs(clBufferA, clBufferB, clBufferC); // C = A+B
        queue.put1DRangeKernel(vectorAddKernel, 0, elements, groupSize, events);

        vectorAddKernel.setArgs(clBufferA, clBufferB, clBufferD); // D = A+B
        queue.put1DRangeKernel(vectorAddKernel, 0, elements, groupSize, events);

        assertEquals(2, events.size());
        queue.putWaitForEvent(events, 0, false)
             .putWaitForEvent(events, 1, true);

        queue.putReadBuffer(clBufferC, false)
             .putReadBuffer(clBufferD, true);

        events.release();

        checkIfEqual(clBufferC.buffer, clBufferD.buffer, elements);


        context.release();


        out.println("results are valid");

    }
    @Test
    public void profilingEventsTest() throws IOException {

        out.println(" - - - event synchronization test - - - ");

        final int elements = roundUp(groupSize, ONE_MB / SIZEOF_INT * 5); // 5MB per buffer

        CLContext context = CLContext.create();

        CLBuffer<ByteBuffer> clBufferA = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        CLBuffer<ByteBuffer> clBufferB = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        CLBuffer<ByteBuffer> clBufferC = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);

        fillBuffer(clBufferA.buffer, 12345);
        fillBuffer(clBufferB.buffer, 67890);

        CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl")).build();
        CLKernel vectorAddKernel = program.createCLKernel("VectorAddGM").setArg(3, elements);
        CLCommandQueue queue = context.getDevices()[0].createCommandQueue(Mode.PROFILING_MODE);

        queue.putWriteBuffer(clBufferA, true) // write A
             .putWriteBuffer(clBufferB, true);// write B

        final CLEventList events = new CLEventList(1);

        assertEquals(0, events.size());

        vectorAddKernel.setArgs(clBufferA, clBufferB, clBufferC); // C = A+B
        queue.put1DRangeKernel(vectorAddKernel, 0, elements, groupSize, events);

        assertEquals(1, events.size());
        CLEvent probe = events.getEvent(0);
        out.println(probe);

        queue.putWaitForEvents(events, true);
        assertEquals(CLEvent.ExecutionStatus.COMPLETE, probe.getStatus());

        out.println(probe);
        long time = probe.getProfilingInfo(CLEvent.ProfilingCommand.END)
                  - probe.getProfilingInfo(CLEvent.ProfilingCommand.START);
        out.println("time: "+time);
        assertTrue(time > 0);

        events.release();
        context.release();

    }

    @Test
    public void concurrencyTest() throws IOException, InterruptedException {

        out.println(" - - - QueueBarrier test - - - ");

        final int elements = ONE_MB / SIZEOF_INT * 10; // 20MB per buffer

        CLContext context = CLContext.create();

        CLDevice[] devices = context.getDevices();

        if (devices.length < 2) {
            out.println("aborting test... need at least 2 devices");
            context.release();
            return;
        }

        final CLBuffer<ByteBuffer> clBufferC = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        final CLBuffer<ByteBuffer> clBufferD = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);

        final CLBuffer<ByteBuffer> clBufferA1 = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        final CLBuffer<ByteBuffer> clBufferB1 = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        final CLBuffer<ByteBuffer> clBufferA2 = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
        final CLBuffer<ByteBuffer> clBufferB2 = context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);

        CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl")).build();

        final CLKernel vectorAddKernel1 = program.createCLKernel("VectorAddGM").setArg(3, elements);
        final CLKernel vectorAddKernel2 = program.createCLKernel("VectorAddGM").setArg(3, elements);

        int secondDevice = devices.length > 1 ? 1 : 0;

        final CLCommandQueue queue1 = devices[0           ].createCommandQueue();
        final CLCommandQueue queue2 = devices[secondDevice].createCommandQueue();

        fillBuffer(clBufferC.buffer, 12345);

        if (secondDevice > 0) {
            System.out.println("using two devices");
        }

        final MultiQueueBarrier barrier = new MultiQueueBarrier(2);

        Thread thread1 = new Thread("C") {

            @Override
            public void run() {

                fillBuffer(clBufferA1.buffer, 12345);
                fillBuffer(clBufferB1.buffer, 67890);

//                System.out.println("C buffer");
                queue1.putWriteBuffer(clBufferA1, false)  // write A
                      .putWriteBuffer(clBufferB1, false); // write B

//                System.out.println("C args");
                vectorAddKernel1.setArgs(clBufferA1, clBufferB1, clBufferC); // C = A+B

//                System.out.println("C kernels");
                CLEventList events1 = new CLEventList(2);
                queue1.put1DRangeKernel(vectorAddKernel1, 0, elements, groupSize, events1)
                      .putReadBuffer(clBufferC, false, events1);

                barrier.waitFor(queue1, events1);

            }
        };

        Thread thread2 = new Thread("D") {

            @Override
            public void run() {

                fillBuffer(clBufferA2.buffer, 12345);
                fillBuffer(clBufferB2.buffer, 67890);

//                System.out.println("D buffer");
                queue2.putWriteBuffer(clBufferA2, false)  // write A
                      .putWriteBuffer(clBufferB2, false); // write B

//                System.out.println("D args");
                vectorAddKernel2.setArgs(clBufferA2, clBufferB2, clBufferD); // D = A+B

//                System.out.println("D kernels");
                CLEventList events2 = new CLEventList(2);
                queue2.put1DRangeKernel(vectorAddKernel2, 0, elements, groupSize, events2)
                      .putReadBuffer(clBufferD, false, events2);

                barrier.waitFor(queue2, events2);

            }
        };

        out.println("starting threads");
        thread1.start();
        thread2.start();
        barrier.await();
        out.println("done");

        checkIfEqual(clBufferC.buffer, clBufferD.buffer, elements);

        context.release();

        out.println("results are valid");

    }
}