简体   繁体   中英

Measure kernel run time

I have made my first OpenCL program with the C++ wrapper which multiplies 2 vectors and squares the result. I need to see how much time the whole OpenCL process takes but I have not found any complete way to implement profiling with C++ wrapper. My main problem is, that enqueueNDRangeKernel kernel takes an vector of events, but clgetEventProfilingInfo takes a single cl_event . I have tried to find another way using enqueueMarker , but it is no longer supported. Here is the code :

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

I might have messed it all up by taking some parts from C and others from C++.

As stated in huseyin tugrul buyukisik's comment the enqueNDRangeKernel call takes a list of events to wait for and additionally a single cl::Event* to offer its own information. Hence, your call should look like

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

With that in place you can use cl::Event 's interface for the querying:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM