Improve async build callback testing (#797)

* Improve async build callback testing

Check that the program build status can be queried from inside the
program completion callback, and also add a test to ensure that the
completion callback is called when the build fails.

* Address review comments

* Fix formatting
This commit is contained in:
James Price
2020-06-01 09:31:50 -04:00
committed by GitHub
parent 58cf793fdb
commit 8579e61401

View File

@@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 The Khronos Group Inc. // Copyright (c) 2017-2020 The Khronos Group Inc.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@@ -19,75 +19,142 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <atomic>
#include <string>
namespace {
const char *sample_async_kernel[] = { const char *sample_async_kernel[] = {
"__kernel void sample_test(__global float *src, __global int *dst)\n" "__kernel void sample_test(__global float *src, __global int *dst)\n"
"{\n" "{\n"
" int tid = get_global_id(0);\n" " size_t tid = get_global_id(0);\n"
"\n" "\n"
" dst[tid] = (int)src[tid];\n" " dst[tid] = (int)src[tid];\n"
"\n" "\n"
"}\n" }; "}\n"
};
volatile int buildNotificationSent; const char *sample_async_kernel_error[] = {
"__kernel void sample_test(__global float *src, __global int *dst)\n"
"{\n"
" size_t tid = get_global_id(0);\n"
"\n"
" dst[tid] = badcodehere;\n"
"\n"
"}\n"
};
// Data passed to a program completion callback
struct TestData
{
cl_device_id device;
cl_build_status expectedStatus;
};
std::atomic<int> callbackResult;
}
void CL_CALLBACK test_notify_build_complete(cl_program program, void *userData) void CL_CALLBACK test_notify_build_complete(cl_program program, void *userData)
{ {
if( userData == NULL || strcmp( (char *)userData, "userData" ) != 0 ) TestData *data = reinterpret_cast<TestData *>(userData);
// Check user data is valid
if (data == nullptr)
{ {
log_error( "ERROR: User data passed in to build notify function was not correct!\n" ); log_error("ERROR: User data passed to callback was not valid!\n");
buildNotificationSent = -1; callbackResult = -1;
return;
}
// Get program build status
cl_build_status status;
cl_int err =
clGetProgramBuildInfo(program, data->device, CL_PROGRAM_BUILD_STATUS,
sizeof(cl_build_status), &status, NULL);
if (err != CL_SUCCESS)
{
log_info("ERROR: failed to get build status from callback\n");
callbackResult = -1;
return;
}
log_info("Program completion callback received build status %d\n", status);
// Check program build status matches expectation
if (status != data->expectedStatus)
{
log_info("ERROR: build status %d != expected status %d\n", status,
data->expectedStatus);
callbackResult = -1;
} }
else else
buildNotificationSent = 1; {
log_info( "\n <-- program successfully built\n" ); callbackResult = 1;
}
} }
int test_async_build(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements) int test_async_build(cl_device_id deviceID, cl_context context,
cl_command_queue queue, int num_elements)
{ {
int error; cl_int error;
cl_program program;
cl_build_status status;
struct TestDef
{
const char **source;
cl_build_status expectedStatus;
};
buildNotificationSent = 0; TestDef testDefs[] = { { sample_async_kernel, CL_BUILD_SUCCESS },
{ sample_async_kernel_error, CL_BUILD_ERROR } };
for (TestDef &testDef : testDefs)
{
log_info("\nTesting program that should produce status %d\n",
testDef.expectedStatus);
/* First, test by doing the slow method of the individual calls */ // Create the program
error = create_single_kernel_helper_create_program(context, &program, 1, sample_async_kernel); clProgramWrapper program;
error = create_single_kernel_helper_create_program(context, &program, 1,
testDef.source);
test_error(error, "Unable to create program from source"); test_error(error, "Unable to create program from source");
/* Compile the program */ // Start an asynchronous build, registering the completion callback
error = clBuildProgram( program, 1, &deviceID, NULL, test_notify_build_complete, (void *)"userData" ); TestData testData = { deviceID, testDef.expectedStatus };
test_error( error, "Unable to build program source" ); callbackResult = 0;
error = clBuildProgram(program, 1, &deviceID, NULL,
test_notify_build_complete, (void *)&testData);
// Allow implementations to return synchronous build failures.
// They still need to call the callback.
if (!(error == CL_BUILD_PROGRAM_FAILURE
&& testDef.expectedStatus == CL_BUILD_ERROR))
test_error(error, "Unable to start build");
/* Wait for build to complete (just keep polling, since we're just a test */ // Wait for callback to fire
if( ( error = clGetProgramBuildInfo( program, deviceID, CL_PROGRAM_BUILD_STATUS, sizeof( status ), &status, NULL ) ) != CL_SUCCESS ) int timeout = 20;
while (callbackResult == 0)
{ {
print_error( error, "Unable to get program build status" ); if (timeout < 0)
{
log_error("Timeout while waiting for callback to fire.\n\n");
return -1; return -1;
} }
while( (int)status == CL_BUILD_IN_PROGRESS )
{ log_info(" -- still waiting for callback...\n");
log_info( "\n -- still waiting for build... (status is %d)", status );
sleep(1); sleep(1);
error = clGetProgramBuildInfo( program, deviceID, CL_PROGRAM_BUILD_STATUS, sizeof( status ), &status, NULL ); timeout--;
test_error( error, "Unable to get program build status" );
} }
if( status != CL_BUILD_SUCCESS ) // Check the callback result
if (callbackResult == 1)
{ {
log_error( "ERROR: build failed! (status: %d)\n", (int)status ); log_error("Test passed.\n\n");
}
else
{
log_error("Async build callback indicated test failure.\n\n");
return -1; return -1;
} }
if( buildNotificationSent == 0 )
{
log_error( "ERROR: Async build completed, but build notification was not sent!\n" );
return -1;
} }
error = clReleaseProgram( program );
test_error( error, "Unable to release program object" );
return 0; return 0;
} }