在测试套件中设置和拆除openCL单元测试的正确方法? [英] Correct way to setup and tear down an openCL unit test in a test suite?

查看:87
本文介绍了在测试套件中设置和拆除openCL单元测试的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速说明:我正在使用JOCL和Java进行openCL开发.我认为我需要的对openCL的调用将与仅使用C或C ++的调用相同.

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++.

我的问题是我希望能够运行我的每个测试,就好像这是GPU初始化后首先运行的.这是我的代码:

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 setUp() {
            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;
}

此代码在运行20多个测试后会导致问题.出于某种原因,openCL会剔除CL_MEM_OBJECT_ALLOCATION_FAILURE.我修改了上面的代码,以便完全删除掉拆机,并且安装程序不会重新创建任何新的clContexts或commandQueues,现在无论我运行了多少测试,我都不会遇到任何CL_MEM_OBJECT_ALLOCATION_FAILURE错误.我现在不确定如何成功重置图形卡的状态,我是否丢失了某些东西或做错了什么?请让我知道,谢谢.

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.

推荐答案

也许是JOCL.org中的错误?我只是使用 http://jocl.jogamp.org 在某些机器上运行了一些负载测试,但无法重现问题.

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();
        }
    }
}

这篇关于在测试套件中设置和拆除openCL单元测试的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆