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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
package com.jogamp.opencl;
import com.jogamp.opencl.util.CLBuildConfiguration;
import com.jogamp.opencl.util.CLProgramConfiguration;
import com.jogamp.opencl.CLProgram.Status;
import com.jogamp.opencl.util.CLBuildListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.*;
import static java.lang.System.*;
import static com.jogamp.opencl.CLProgram.CompilerOptions.*;
/**
*
* @author Michael Bien
*/
public class CLProgramTest {
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
@Test
public void enumsTest() {
// CLProgram enums
for (Status e : Status.values()) {
assertEquals(e, Status.valueOf(e.STATUS));
}
}
@Test
public void rebuildProgramTest() throws IOException {
out.println(" - - - CLProgramTest; rebuild program test - - - ");
CLContext context = CLContext.create();
CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl"));
try{
program.createCLKernels();
fail("expected exception but got none :(");
}catch(CLException ex) {
out.println("got expected exception: "+ex.getCLErrorString());
assertEquals(ex.errorcode, CL.CL_INVALID_PROGRAM_EXECUTABLE);
}
out.println(program.getBuildStatus());
program.build();
out.println(program.getBuildStatus());
assertTrue(program.isExecutable());
Map<String, CLKernel> kernels = program.createCLKernels();
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.createCLKernels();
assertNotNull(kernels);
assertTrue("kernel map is empty", kernels.size() > 0);
assertTrue(kernels.size() > 0);
context.release();
}
@Test
public void programBinariesTest() throws IOException {
out.println(" - - - CLProgramTest; down-/upload binaries test - - - ");
CLContext context = CLContext.create();
CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl"))
.build(ENABLE_MAD, WARNINGS_ARE_ERRORS);
// optain binaries
Map<CLDevice, byte[]> binaries = program.getBinaries();
assertFalse(binaries.isEmpty());
CLDevice[] devices = program.getCLDevices();
for (CLDevice device : devices) {
assertTrue(binaries.containsKey(device));
}
// 1. release program
// 2. re-create program with old binaries
program.release();
assertFalse(program.isExecutable());
assertNotNull(program.getBinaries());
assertEquals(program.getBinaries().size(), 0);
assertNotNull(program.getBuildLog());
assertEquals(program.getBuildLog().length(), 0);
assertNotNull(program.getSource());
assertEquals(program.getSource().length(), 0);
assertNotNull(program.getCLDevices());
assertEquals(program.getCLDevices().length, 0);
{
Map<String, CLKernel> kernels = program.createCLKernels();
assertNotNull(kernels);
assertEquals(kernels.size(), 0);
}
assertNull(program.createCLKernel("foo"));
program = context.createProgram(binaries);
assertFalse(program.isExecutable());
assertNotNull(program.getCLDevices());
assertTrue(program.getCLDevices().length != 0);
assertNotNull(program.getBinaries());
assertEquals(program.getBinaries().size(), 0);
assertNotNull(program.getBuildLog());
assertTrue(program.getBuildLog().length() != 0);
assertNotNull(program.getSource());
assertEquals(program.getSource().length(), 0);
try{
Map<String, CLKernel> kernels = program.createCLKernels();
fail("expected an exception from createCLKernels but got: "+kernels);
}catch(CLException ex) {
// expected, not build yet
}
out.println(program.getBuildStatus());
program.build();
out.println(program.getBuildStatus());
assertNotNull(program.createCLKernel("Test"));
assertTrue(program.isExecutable());
context.release();
}
@Test
public void builderTest() throws IOException, ClassNotFoundException, InterruptedException {
out.println(" - - - CLProgramTest; program builder test - - - ");
CLContext context = CLContext.create();
CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl"));
// same as program.build()
program.prepare().build();
assertTrue(program.isExecutable());
// complex build
program.prepare().withOption(ENABLE_MAD)
.forDevice(context.getMaxFlopsDevice())
.withDefine("RADIUS", 5)
.withDefine("ENABLE_FOOBAR")
.build();
assertTrue(program.isExecutable());
// reusable builder
CLBuildConfiguration builder = CLProgramBuilder.createConfiguration()
.withOption(ENABLE_MAD)
.forDevices(context.getDevices())
.withDefine("RADIUS", 5)
.withDefine("ENABLE_FOOBAR");
out.println(builder);
// async build test
{
final CountDownLatch countdown = new CountDownLatch(1);
final CLProgram outerProgram = program;
CLBuildListener buildCallback = new CLBuildListener() {
public void buildFinished(CLProgram program) {
assertEquals(outerProgram, program);
countdown.countDown();
}
};
builder.setProgram(program).build(buildCallback);
countdown.countDown(); // TODO remove if callbacks are enabled again
countdown.await();
}
assertTrue(program.isExecutable());
// serialization test
File file = tmpFolder.newFile("foobar.builder");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
builder.save(oos);
oos.close();
// build configuration
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
CLBuildConfiguration buildConfig = CLProgramBuilder.loadConfiguration(ois);
ois.close();
assertEquals(builder, buildConfig);
buildConfig.build(program);
assertTrue(program.isExecutable());
// program configuration
ois = new ObjectInputStream(new FileInputStream(file));
CLProgramConfiguration programConfig = CLProgramBuilder.loadConfiguration(ois, context);
assertNotNull(programConfig.getProgram());
ois.close();
program = programConfig.build();
assertTrue(program.isExecutable());
// cloneing
assertEquals(builder, builder.clone());
context.release();
}
@Test
public void kernelTest() {
String source = "__attribute__((reqd_work_group_size(256, 256, 256))) kernel void foo(void) { }\n";
// to workaround "Internal error: Link failed." on AMD platform + GPU device
final CLPlatform platform = CLPlatform.getDefault();
CLDevice.Type type = CLDevice.Type.DEFAULT;
if(platform.getVendor().toLowerCase().contains("amd")) {
type = CLDevice.Type.CPU;
}
CLContext context = CLContext.create(platform, type);
try{
CLProgram program = context.createProgram(source).build();
assertTrue(program.isExecutable());
CLKernel kernel = program.createCLKernel("foo");
assertNotNull(kernel);
long[] wgs = kernel.getCompileWorkGroupSize(context.getDevices()[0]);
out.println("compile workgroup size: " + wgs[0]+" "+wgs[1]+" "+wgs[2]);
assertEquals(256, wgs[0]);
assertEquals(256, wgs[1]);
assertEquals(256, wgs[2]);
}finally{
context.release();
}
}
}
|