衡量内核运行时间

时间:2017-03-21 19:02:58

标签: c++ opencl

我用C ++包装器制作了我的第一个OpenCL程序,它将2个向量与方格相乘。我需要看看整个OpenCL过程花了多少时间,但我还没有找到任何完整的方法来实现C ++包装器的分析。我的主要问题是,enqueueNDRangeKernel内核采用事件向量,但clgetEventProfilingInfo只需要一个cl_event。我试图找到使用enqueueMarker的另一种方式,但不再支持它。这是代码:

int main()
{
    std::vector<cl::Platform> all_platforms;
    cl::Platform::get(&all_platforms);
    if (all_platforms.size() == 0) {
        std::cout << " No platforms found. Check OpenCL installation!\n";
        exit(1);
    }
    cl::Platform default_platform = all_platforms[0];
    std::cout << "Using platform: " << default_platform.getInfo<CL_PLATFORM_NAME>() << "\n";
    //get default device of the default platform
    std::vector<cl::Device> all_devices;
    default_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);
    if (all_devices.size() == 0) 
    {
        std::cout << " No devices found. Check OpenCL installation!\n";
        exit(1);
    }
    cl::Device default_device = all_devices[0];
    std::cout << "Using device: " << default_device.getInfo<CL_DEVICE_NAME>() << "\n";
    cl::Context context({ default_device });
    cl::Program::Sources sources;

    std::string kernel_code =
        "   void kernel simple_add(global const float* A, global const float* B, global float* C){ "
"               C[get_global_id(0)]=A[get_global_id(0)]*B[get_global_id(0)];        "
        "       C[get_global_id(0)]=sqrt(C[get_global_id(0)]);               "
        "   }                                                                               ";
    sources.push_back({ kernel_code.c_str(),kernel_code.length() });
    cl::Program program(context, sources);
    if (program.build({ default_device }) != CL_SUCCESS) {
        std::cout << " Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(default_device) << "\n";
        system("pause");
        exit(1);
    }
    cl::Buffer buffer_A(context, CL_MEM_READ_WRITE, sizeof(float) * 10000);
    cl::Buffer buffer_B(context, CL_MEM_READ_WRITE, sizeof(float) * 10000);
    cl::Buffer buffer_C(context, CL_MEM_READ_WRITE, sizeof(float) * 10000);
    float A[10000];
    for (int i = 0; i < 10000; i++)
    {
        A[i] = (float)i;
    }
    float B[10000] ;

    cl::CommandQueue queue(context, default_device,CL_QUEUE_PROFILING_ENABLE);

    queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, sizeof(float) * 10000, A);
    queue.enqueueWriteBuffer(buffer_B, CL_TRUE, 0, sizeof(float) * 10000, B);

    cl::Kernel kernel_add=cl::Kernel(program,"simple_add");
    kernel_add.setArg(0,buffer_A);
    kernel_add.setArg(1,buffer_B);
    kernel_add.setArg(2,buffer_C);
    std::vector<cl::Event> gpu_event;
    queue.enqueueNDRangeKernel(kernel_add, cl::NullRange, cl::NDRange(10000), cl::NullRange,&gpu_event);
    //this takes vector 
    queue.finish();
    cl_ulong time_start, time_end;
    float C[10000];
    queue.enqueueReadBuffer(buffer_C, CL_TRUE, 0, sizeof(float) * 10000, C);
    clGetEventProfilingInfo(&gpu_event,CL_PROFILING_COMMAND_START,sizeof(time_start),&time_start,NULL);
    clGetEventProfilingInfo(&gpu_event,CL_PROFILING_COMMAND_END,sizeof(time_end),&time_end,NULL);
    //theese two take cl_event
    double time= time_end - time_start;
    std::cout<<"TIME: "<<time << "\n ";
    system("pause");
}

我可能通过从C和C ++中的其他部分中获取一些部分来搞砸了它。

1 个答案:

答案 0 :(得分:0)

huseyin tugrul buyukisik's comment中所述,enqueNDRangeKernel调用会获取要等待的事件列表,另外还有一个cl::Event*来提供自己的信息。因此,您的通话应该看起来像

cl::Event event; 
queue.enqueueNDRangeKernel(kernel_add, cl::NullRange, cl::NDRange(10000), cl::NullRange, NULL, &event);

有了这个,您可以使用cl::Event的界面进行查询:

time_start = event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
time_end = event.getProfilingInfo<CL_PROFILING_COMMAND_END>();