@@ -218,91 +218,140 @@ const unsigned char *acl_test_get_example_binary(size_t *binary_len) {
218218}
219219
220220static void l_load_example_binary (void ) {
221- const char *envvar_offline_device = " CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA" ;
222- const char *envvar_program_lib =
223- " CL_CONTEXT_PROGRAM_EXE_LIBRARY_ROOT_INTELFPGA" ;
224- const char *offline_old_value = acl_getenv (envvar_offline_device);
225- const char *program_lib_old_value = acl_getenv (envvar_program_lib);
226- int system_ret = -1 ;
227- enum { MAX_DEVICES = 100 };
228- cl_platform_id platform;
229- cl_device_id device[MAX_DEVICES];
230- cl_context context;
231- cl_program program;
232- cl_int status;
233-
234- acl_test_setenv (envvar_offline_device, ACLTEST_DEFAULT_BOARD);
235- acl_test_setenv (envvar_program_lib, " .acltest_builtin_prog" );
236- system_ret = system (" rm -rf .acltest_builtin_prog" );
237- assert (system_ret != -1 );
238-
239- ACL_LOCKED (acl_test_setup_generic_system ());
240-
241221 // Since this runs before the CppUTest runner is set up, we can't use
242222 // the CHECK* macros.
243223 // Just use asserts.
244224
245- assert (CL_SUCCESS == clGetPlatformIDs (1 , &platform, 0 ));
246- assert (CL_SUCCESS == clGetDeviceIDs (platform, CL_DEVICE_TYPE_ACCELERATOR,
247- MAX_DEVICES, device, 0 ));
225+ const char *envvar_example_binary = " ACL_TEST_EXAMPLE_BINARY" ;
226+ const char *example_binary_root = acl_getenv (envvar_example_binary);
227+ if (example_binary_root) {
228+ // Precompiled binaries exist, just read its content
229+ ACL_LOCKED (acl_test_setup_generic_system ());
230+ #ifdef _WIN32
231+ std::string bin_file =
232+ std::string (example_binary_root) + " /windows/example.aocr" ;
233+ #else
234+ std::string bin_file =
235+ std::string (example_binary_root) + " /linux/example.aocr" ;
236+ #endif
237+ FILE *infile = fopen (bin_file.c_str (), " rb" );
238+ assert (infile && " Cannot open example binary example.aocr, make sure "
239+ " ACL_TEST_EXAMPLE_BINARY is set to the correct path" );
240+
241+ // Get binary length
242+ assert (fseek (infile, 0 , SEEK_END) == 0 );
243+ long int position = ftell (infile);
244+ assert (position != -1L );
245+ acl_test_example_binary_len = (size_t )position;
246+ // Return to beginning of file
247+ assert (fseek (infile, 0 , SEEK_SET) == 0 );
248+
249+ // Read binary
250+ acl_test_example_binary =
251+ (unsigned char *)acl_malloc (acl_test_example_binary_len);
252+ assert (acl_test_example_binary);
253+ assert (fread (acl_test_example_binary, sizeof (char ),
254+ acl_test_example_binary_len,
255+ infile) == acl_test_example_binary_len);
256+ assert (fclose (infile) == 0 );
257+ } else {
258+ // Precompiled binaries don't exist, do an actual compile with the
259+ // aoc compiler and store the binary as test example binary.
260+ const char *envvar_offline_device = " CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA" ;
261+ const char *envvar_program_lib =
262+ " CL_CONTEXT_PROGRAM_EXE_LIBRARY_ROOT_INTELFPGA" ;
263+ const char *offline_old_value = acl_getenv (envvar_offline_device);
264+ const char *program_lib_old_value = acl_getenv (envvar_program_lib);
265+ int system_ret = -1 ;
266+ enum { MAX_DEVICES = 100 };
267+ cl_platform_id platform;
268+ cl_device_id device[MAX_DEVICES];
269+ cl_context context;
270+ cl_program program;
271+ cl_int status;
272+
273+ acl_test_setenv (envvar_offline_device, ACLTEST_DEFAULT_BOARD);
274+ acl_test_setenv (envvar_program_lib, " .acltest_builtin_prog" );
275+ system_ret = system (" rm -rf .acltest_builtin_prog" );
276+ assert (system_ret != -1 );
248277
249- cl_context_properties props[] = {
250- CL_CONTEXT_COMPILER_MODE_INTELFPGA,
251- CL_CONTEXT_COMPILER_MODE_OFFLINE_CREATE_EXE_LIBRARY_INTELFPGA, 0 };
252- context = clCreateContext (props, 1 , device, acl_test_notify_print, 0 , 0 );
253- assert (context);
254-
255- const char *src =
256- " kernel void vecaccum(global int*A, global int*B) {\n "
257- " size_t gid = get_global_id(0);\n "
258- " A[gid] += B[gid];\n "
259- " };\n "
260- // This one has two constant arguments.
261- " kernel void vecsum(global int*A, constant int*B, constant int*C) {\n "
262- " size_t gid = get_global_id(0);\n "
263- " A[gid] = B[gid] + C[gid];\n "
264- " };\n "
265-
266- // This has a printf.
267- " kernel void printit(global int*A) {\n "
268- " printf(\" Hello world! %d\\ n\" , A[0]);\n "
269- " };\n " ;
270-
271- program = clCreateProgramWithSource (context, 1 , &src, 0 , 0 );
272- assert (program);
273-
274- status = clBuildProgram (program, 1 , device, " -cl-kernel-arg-info" , 0 , 0 );
275- if (status != CL_SUCCESS) {
276- printf (" Compilation failed. Kernel source is:\n -----\n %s\n ----\n " , src);
278+ ACL_LOCKED (acl_test_setup_generic_system ());
279+
280+ assert (CL_SUCCESS == clGetPlatformIDs (1 , &platform, 0 ));
281+ assert (CL_SUCCESS == clGetDeviceIDs (platform, CL_DEVICE_TYPE_ACCELERATOR,
282+ MAX_DEVICES, device, 0 ));
283+
284+ cl_context_properties props[] = {
285+ CL_CONTEXT_COMPILER_MODE_INTELFPGA,
286+ CL_CONTEXT_COMPILER_MODE_OFFLINE_CREATE_EXE_LIBRARY_INTELFPGA, 0 };
287+ context = clCreateContext (props, 1 , device, acl_test_notify_print, 0 , 0 );
288+ assert (context);
289+
290+ const char *src =
291+ " kernel void vecaccum(global int*A, global int*B) {\n "
292+ " size_t gid = get_global_id(0);\n "
293+ " A[gid] += B[gid];\n "
294+ " };\n "
295+ // This one has two constant arguments.
296+ " kernel void vecsum(global int*A, constant int*B, constant int*C) {\n "
297+ " size_t gid = get_global_id(0);\n "
298+ " A[gid] = B[gid] + C[gid];\n "
299+ " };\n "
300+
301+ // This has a printf.
302+ " kernel void printit(global int*A) {\n "
303+ " printf(\" Hello world! %d\\ n\" , A[0]);\n "
304+ " };\n " ;
305+
306+ program = clCreateProgramWithSource (context, 1 , &src, 0 , 0 );
307+ assert (program);
308+
309+ status = clBuildProgram (program, 1 , device, " -cl-kernel-arg-info" , 0 , 0 );
310+ if (status != CL_SUCCESS) {
311+ printf (" Compilation failed. Kernel source is:\n -----\n %s\n ----\n " , src);
312+ size_t log_size = 0 ;
313+ clGetProgramBuildInfo (program, device[0 ], CL_PROGRAM_BUILD_LOG, 0 , 0 ,
314+ &log_size);
315+ char *log = (char *)acl_malloc (log_size);
316+ clGetProgramBuildInfo (program, device[0 ], CL_PROGRAM_BUILD_LOG, log_size,
317+ log, 0 );
318+ if (log)
319+ printf (" Build log is:\n -----\n %s\n ----\n " , log);
320+ exit (1 );
321+ }
322+
323+ // The build log should not be empty
277324 size_t log_size = 0 ;
325+ size_t empty_log_size = 1 ;
278326 clGetProgramBuildInfo (program, device[0 ], CL_PROGRAM_BUILD_LOG, 0 , 0 ,
279327 &log_size);
280- char *log = (char *)acl_malloc (log_size);
281- clGetProgramBuildInfo (program, device[0 ], CL_PROGRAM_BUILD_LOG, log_size,
282- log, 0 );
283- if (log)
284- printf (" Build log is:\n -----\n %s\n ----\n " , log);
285- exit (1 );
328+ assert (log_size > empty_log_size);
329+
330+ acl_test_example_binary_len = 0 ;
331+ assert (CL_SUCCESS == clGetProgramInfo (program, CL_PROGRAM_BINARY_SIZES,
332+ sizeof (size_t ),
333+ &acl_test_example_binary_len, 0 ));
334+ acl_test_example_binary =
335+ (unsigned char *)acl_malloc (acl_test_example_binary_len);
336+ assert (acl_test_example_binary);
337+ assert (CL_SUCCESS == clGetProgramInfo (program, CL_PROGRAM_BINARIES,
338+ sizeof (acl_test_example_binary),
339+ &acl_test_example_binary, 0 ));
340+
341+ // Don't leak
342+ clReleaseProgram (program);
343+ clReleaseContext (context);
344+
345+ acl_test_unsetenv (envvar_offline_device);
346+ if (offline_old_value) {
347+ acl_test_setenv (envvar_offline_device, offline_old_value);
348+ }
349+ acl_test_unsetenv (envvar_program_lib);
350+ if (program_lib_old_value) {
351+ acl_test_setenv (envvar_program_lib, program_lib_old_value);
352+ }
286353 }
287354
288- // The build log should not be empty
289- size_t log_size = 0 ;
290- size_t empty_log_size = 1 ;
291- clGetProgramBuildInfo (program, device[0 ], CL_PROGRAM_BUILD_LOG, 0 , 0 ,
292- &log_size);
293- assert (log_size > empty_log_size);
294-
295- acl_test_example_binary_len = 0 ;
296- assert (CL_SUCCESS == clGetProgramInfo (program, CL_PROGRAM_BINARY_SIZES,
297- sizeof (size_t ),
298- &acl_test_example_binary_len, 0 ));
299- acl_test_example_binary =
300- (unsigned char *)acl_malloc (acl_test_example_binary_len);
301- assert (acl_test_example_binary);
302- assert (CL_SUCCESS == clGetProgramInfo (program, CL_PROGRAM_BINARIES,
303- sizeof (acl_test_example_binary),
304- &acl_test_example_binary, 0 ));
305-
306355 // Save the derived sysdef for later tests.
307356 {
308357 acl_pkg_file_t pkg;
@@ -334,19 +383,6 @@ static void l_load_example_binary(void) {
334383 acl_pkg_close_file (pkg);
335384 }
336385
337- // Don't leak
338- clReleaseProgram (program);
339- clReleaseContext (context);
340-
341- acl_test_unsetenv (envvar_offline_device);
342- if (offline_old_value) {
343- acl_test_setenv (envvar_offline_device, offline_old_value);
344- }
345- acl_test_unsetenv (envvar_program_lib);
346- if (program_lib_old_value) {
347- acl_test_setenv (envvar_program_lib, program_lib_old_value);
348- }
349-
350386 ACL_LOCKED (acl_test_teardown_generic_system ());
351387}
352388
0 commit comments