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
|
package com.mbien.opencl;
import com.mbien.opencl.CLBuffer.Mem;
import com.mbien.opencl.CLCommandQueue.Mode;
import com.mbien.opencl.CLDevice.SingleFPConfig;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.EnumSet;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import static java.lang.System.*;
import static com.mbien.opencl.TestUtils.*;
import static com.sun.gluegen.runtime.BufferFactory.*;
/**
* Test testing the high level bindings.
* @author Michael Bien
*/
public class HighLevelBindingTest {
@BeforeClass
public static void setUpClass() throws Exception {
out.println("OS: " + System.getProperty("os.name"));
out.println("ARCH: " + System.getProperty("os.arch"));
out.println("VM: " + System.getProperty("java.vm.name"));
out.println("lib path: " + System.getProperty("java.library.path"));
}
@Test
public void contextlessTest() {
out.println(" - - - highLevelTest; contextless - - - ");
// enum tests
final EnumSet<SingleFPConfig> singleFPConfig = SingleFPConfig.valuesOf(CL.CL_FP_DENORM | CL.CL_FP_ROUND_TO_INF);
assertEquals(0, SingleFPConfig.valuesOf(0).size());
assertTrue(singleFPConfig.contains(SingleFPConfig.DENORM));
assertTrue(singleFPConfig.contains(SingleFPConfig.ROUND_TO_INF));
final EnumSet<Mode> queueMode = Mode.valuesOf(CL.CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL.CL_QUEUE_PROFILING_ENABLE);
assertEquals(0, Mode.valuesOf(0).size());
assertTrue(queueMode.contains(Mode.OUT_OF_ORDER_EXEC_MODE));
assertTrue(queueMode.contains(Mode.PROFILING_MODE));
// platform/device info tests
CLPlatform[] clPlatforms = CLPlatform.listCLPlatforms();
for (CLPlatform platform : clPlatforms) {
out.println("platform info:");
out.println(" name: "+platform.getName());
out.println(" id: "+platform.ID);
out.println(" profile: "+platform.getProfile());
out.println(" version: "+platform.getVersion());
out.println(" vendor: "+platform.getVendor());
out.println(" max FLOPS device: "+platform.getMaxFlopsDevice());
CLDevice[] clDevices = platform.listCLDevices();
for (CLDevice device : clDevices) {
out.println("device info:");
out.println(" name: "+device.getName());
out.println(" profile: "+device.getProfile());
out.println(" vendor: "+device.getVendor());
out.println(" type: "+device.getType());
out.println(" global mem: "+device.getGlobalMemSize()/(1024*1024)+" MB");
out.println(" local mem: "+device.getLocalMemSize()/1024+" KB");
out.println(" local mem type: "+device.getLocalMemType());
out.println(" global mem cache size: "+device.getGlobalMemCachSize());
out.println(" global mem cache type: "+device.getGlobalMemCacheType());
out.println(" constant buffer size: "+device.getMaxConstantBufferSize());
out.println(" queue properties: "+device.getQueueProperties());
out.println(" clock: "+device.getMaxClockFrequency()+" MHz");
out.println(" timer res: "+device.getProfilingTimerResolution()+" ns");
out.println(" single FP config: "+device.getSingleFPConfig());
out.println(" max work group size: "+device.getMaxWorkGroupSize());
out.println(" max compute units: "+device.getMaxComputeUnits());
out.println(" extensions: "+device.getExtensions());
}
}
}
@Test
public void vectorAddGMTest() throws IOException {
out.println(" - - - highLevelTest; global memory kernel - - - ");
CLPlatform[] clPlatforms = CLPlatform.listCLPlatforms();
CLContext context = CLContext.create(clPlatforms[0]);
CLDevice[] contextDevices = context.getCLDevices();
out.println("context devices:");
for (CLDevice device : contextDevices) {
out.println(" "+device.toString());
}
out.println("max FLOPS device: " + context.getMaxFlopsDevice());
CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl")).build();
CLDevice[] programDevices = program.getCLDevices();
assertEquals(contextDevices.length, programDevices.length);
out.println("build log:\n"+program.getBuildLog());
out.println("build status:\n"+program.getBuildStatus());
String source = program.getSource();
assertFalse(source.trim().isEmpty());
// out.println("source:\n"+source);
// Map<CLDevice, byte[]> binaries = program.getBinaries();
// assertFalse(binaries.isEmpty());
int elementCount = 11444777; // Length of float arrays to process (odd # for illustration)
int localWorkSize = 256; // set and log Global and Local work size dimensions
int globalWorkSize = roundUp(localWorkSize, elementCount); // rounded up to the nearest multiple of the LocalWorkSize
out.println("allocateing buffers of size: "+globalWorkSize);
ByteBuffer srcA = newDirectByteBuffer(globalWorkSize*SIZEOF_INT);
ByteBuffer srcB = newDirectByteBuffer(globalWorkSize*SIZEOF_INT);
ByteBuffer dest = newDirectByteBuffer(globalWorkSize*SIZEOF_INT);
fillBuffer(srcA, 23456);
fillBuffer(srcB, 46987);
CLBuffer<ByteBuffer> clBufferA = context.createBuffer(srcA, Mem.READ_ONLY);
CLBuffer<ByteBuffer> clBufferB = context.createBuffer(srcB, Mem.READ_ONLY);
CLBuffer<ByteBuffer> clBufferC = context.createBuffer(dest, Mem.WRITE_ONLY);
Map<String, CLKernel> kernels = program.getCLKernels();
for (CLKernel kernel : kernels.values()) {
out.println("kernel: "+kernel.toString());
}
assertNotNull(kernels.get("VectorAddGM"));
assertNotNull(kernels.get("Test"));
CLKernel vectorAddKernel = kernels.get("VectorAddGM");
vectorAddKernel.setArg(0, clBufferA)
.setArg(1, clBufferB)
.setArg(2, clBufferC)
.setArg(3, elementCount);
CLCommandQueue queue = programDevices[0].createCommandQueue();
// Asynchronous write of data to GPU device, blocking read later
queue.putWriteBuffer(clBufferA, false)
.putWriteBuffer(clBufferB, false)
.put1DRangeKernel(vectorAddKernel, 0, globalWorkSize, localWorkSize)
.putReadBuffer(clBufferC, true)
.finish().release();
out.println("a+b=c result snapshot: ");
for(int i = 0; i < 10; i++)
out.print(dest.getInt()+", ");
out.println("...; "+dest.remaining()/SIZEOF_INT + " more");
assertTrue(3 == context.getCLBuffers().size());
clBufferA.release();
assertTrue(2 == context.getCLBuffers().size());
assertTrue(2 == context.getCLBuffers().size());
clBufferB.release();
assertTrue(1 == context.getCLBuffers().size());
assertTrue(1 == context.getCLBuffers().size());
clBufferC.release();
assertTrue(0 == context.getCLBuffers().size());
assertTrue(1 == context.getCLPrograms().size());
program.release();
assertTrue(0 == context.getCLPrograms().size());
context.release();
}
@Test
public void rebuildProgramTest() throws IOException {
out.println(" - - - highLevelTest; rebuild program test - - - ");
CLContext context = CLContext.create();
CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl"));
try{
program.getCLKernels();
fail("expected exception but got none :(");
}catch(CLException ex) {
out.println("got expected exception:\n"+ex.getMessage());
assertEquals(ex.errorcode, CL.CL_INVALID_PROGRAM_EXECUTABLE);
}
program.build();
assertTrue(program.isExecutable());
out.println(program.getBuildStatus());
Map<String, CLKernel> kernels = program.getCLKernels();
assertNotNull(kernels);
assertTrue("kernel map is empty", kernels.size() > 0);
// rebuild
// 1. release kernels (internally)
// 2. build program
program.build();
assertTrue(program.isExecutable());
out.println(program.getBuildStatus());
// try again with rebuilt program
kernels = program.getCLKernels();
assertNotNull(kernels);
assertTrue("kernel map is empty", kernels.size() > 0);
assertTrue(kernels.size() > 0);
context.release();
}
}
|