Correct way to setup and tear down an openCL unit test in a test suite?
Quick note: I am using JOCL and Java for my openCL development. I think the calls to openCL that I need will be the same as if I was just using C or C++.
My problem is that I want to be able to run each of my tests as if it were the first thing the GPU runs after being initialised. Here is my code:
protected cl_context clContext;
protected cl_command_queue commandQueue;
@Before
public void setU开发者_如何学Gop() {
clContext = createContext();
cl_device_id devices[] = getGPUDevices(clContext);
commandQueue = clCreateCommandQueue(clContext, devices[0], 0, null);
CL.setExceptionsEnabled(true);
}
@After
public void tearDown() {
clReleaseCommandQueue(commandQueue);
clReleaseContext(clContext);
}
private cl_device_id[] getGPUDevices(cl_context clContext) {
cl_device_id devices[];
// Get the list of GPU devices associated with the context
long numBytes[] = new long[1];
clGetContextInfo(clContext, CL.CL_CONTEXT_DEVICES, 0, null, numBytes);
// Obtain the cl_device_id for the first device
int numDevices = (int) numBytes[0] / Sizeof.cl_device_id;
devices = new cl_device_id[numDevices];
clGetContextInfo(clContext, CL_CONTEXT_DEVICES, numBytes[0],
Pointer.to(devices), null);
return devices;
}
private cl_context createContext() {
cl_context clContext;
//System.out.println("Obtaining platform...");
cl_platform_id platforms[] = new cl_platform_id[1];
clGetPlatformIDs(platforms.length, platforms, null);
cl_context_properties contextProperties = new cl_context_properties();
contextProperties.addProperty(CL_CONTEXT_PLATFORM, platforms[0]);
// Create an OpenCL context on a GPU device
clContext = clCreateContextFromType(
contextProperties, CL_DEVICE_TYPE_GPU, null, null, null);
return clContext;
}
This code causes problem after 20+ tests are run. For some reason openCL will barf out a CL_MEM_OBJECT_ALLOCATION_FAILURE. I modified the code above so that teardown was fully commented out and so that setup wouldn't recreate any new clContexts or commandQueues, and now I don't get any CL_MEM_OBJECT_ALLOCATION_FAILURE errors, no matter how many tests I run. I am not sure how to successfully reset the state of my graphics card at this point, am I missing something or doing something wrong? Please let me know, thanks.
maybe its a bug in JOCL.org? I just run a few load tests on some machines with http://jocl.jogamp.org and couldn't reproduce the issue.
@org.junit.Test
public void test(){
for (int i = 0; i < 100000; i++) {
CLContext context = CLContext.create();
try{
CLCommandQueue queue = context.getDevices()[0].createCommandQueue();
queue.release();
}finally{
context.release();
}
}
}
精彩评论