#include "Common.h" #include "OpenCLHandler.h" #include <iostream> void OpenCLHandler::printTimeStats(cl::Event& event) { cl_int err = CL_SUCCESS; event.wait(); cl_ulong execStart, execEnd; execStart = event.getProfilingInfo<CL_PROFILING_COMMAND_START>(&err); if (err != CL_SUCCESS) { std::cerr << "Error during profile query: CL_PROFILING_COMMAND_START [" << err << "]." << std::endl; } execEnd = event.getProfilingInfo<CL_PROFILING_COMMAND_END>(&err); if (err != CL_SUCCESS) { std::cerr << "Error during profile query: CL_PROFILING_COMMAND_END [" << err << "]." << std::endl; } //std::cout << "[start] " << execStart << " [end] " << execEnd // << " [time] " << (execEnd - execStart) / 1e+06 << "ms." << std::endl; std::cout << "GPU [time] " << (execEnd - execStart) / 1e+06 << " ms" << std::endl; } OpenCLHandler::OpenCLHandler() { cl_int err = CL_SUCCESS; // Get a platform ID std::vector<cl::Platform> platforms; cl::Platform::get(&platforms); if (platforms.size() == 0) { std::cout << "Unable to find suitable platform." << std::endl; exit(-1); } std::cout << "Running on: " << platforms[0].getInfo<CL_PLATFORM_NAME>() << std::endl; // Create a context cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0 }; context = cl::Context(CL_DEVICE_TYPE_GPU, properties); // Enumerate the devices std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>(); std::cout << "Global memory: " << devices[0].getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() << std::endl; max_workgroup_size = devices[0].getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>(); std::cout << "Max workgroup: " << max_workgroup_size << std::endl << std::endl; // Create the command queue cl::Event event; queue = cl::CommandQueue(context, devices[0], CL_QUEUE_PROFILING_ENABLE, &err); // Create the OpenCL program std::string programSource = FileToString("../kernels/programs.cl"); program = cl::Program(context, programSource); program.build(devices); } bool OpenCLHandler::run_test(TestCase* test) { cl::Event gpuEvent; test->gpu_compute(&context, &queue, &program, &gpuEvent); Timer::measure([&]() { test->cpu_compute(); }, 5); printTimeStats(gpuEvent); test->collect_results(&queue); return test->validate_results(); } size_t OpenCLHandler::get_max_size() { return max_workgroup_size; }