From daeeeb86760f5571e283e579e2cac66919a2e15d Mon Sep 17 00:00:00 2001 From: Hatef Date: Mon, 11 Jul 2022 10:48:45 +0430 Subject: [PATCH] Successfully running kernel first time --- main.cpp | 98 ++++++++++++++++++-------------------------------------- test1.cl | 5 +++ 2 files changed, 37 insertions(+), 66 deletions(-) create mode 100644 test1.cl diff --git a/main.cpp b/main.cpp index 52b16ed..f6f5198 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,8 @@ #define CL_HPP_TARGET_OPENCL_VERSION 120 #include +#include +#include #include #include @@ -9,84 +11,48 @@ int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); -//std::vector all_platforms; -////std::vector all_dev; -//cl::Platform::get(&all_platforms); -////cl::Device::get(&all_dev); + std::vector all_platforms; + cl::Platform::get(&all_platforms); -//qDebug() << "Number of platforms found:" << all_platforms.size(); + qDebug() << "Number of platforms found:" << all_platforms.size(); -//foreach(auto platform, all_platforms) -//{ -//qDebug() << platform.getInfo().data(); -//} - -//std::vector all_devices; -//all_platforms.front().getDevices(CL_DEVICE_TYPE_GPU, &all_devices); - -//foreach(auto device, all_devices) -//{ -//qDebug() << device.getInfo().data(); -//} - -////cl::Context main_context(all_devices.front()); + foreach(auto platform, all_platforms) + { + qDebug() << platform.getInfo().data(); + } -////cl::Program program(main_context) -/// -/// -/// - std::vector platforms; - cl::Platform::get(&platforms); + std::vector all_devices; + all_platforms.front().getDevices(CL_DEVICE_TYPE_GPU, &all_devices); - int platform_id = 0; - int device_id = 0; + foreach(auto device, all_devices) + { + qDebug() << device.getInfo().data(); + } - qDebug() << "Number of Platforms: " << platforms.size(); + cl::Context main_context(all_devices.front()); - for(std::vector::iterator it = platforms.begin(); it != platforms.end(); ++it) + QFile cl_file("../OpenCL/test1.cl"); + if(!cl_file.open(QIODevice::ReadOnly)) { - cl::Platform platform(*it); - - qDebug() << "Platform ID: " << platform_id++; - qDebug() << "Platform Name: " << platform.getInfo().data(); - qDebug() << "Platform Vendor: " << platform.getInfo().data(); + qDebug() << cl_file.errorString(); + } + auto script = cl_file.readAll(); + qDebug() << script; + cl_file.close(); - std::vector devices; - platform.getDevices(CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_CPU, &devices); + cl_int error; + cl::Program program(main_context, script.toStdString(), CL_TRUE, &error); + qDebug() << error; - for(std::vector::iterator it2 = devices.begin(); it2 != devices.end(); ++it2) - { - cl::Device device(*it2); + cl::Kernel kernel(program, "myVeryFirstFinction", &error); + qDebug() << error; - qDebug() << "\tDevice " << device_id++ << ": "; - qDebug() << "\t\tDevice Name: " << device.getInfo().data(); - qDebug() << "\t\tDevice Vendor: " << device.getInfo().data(); - qDebug() << "\t\tDevice Version: " << device.getInfo().data(); - switch(device.getInfo()) - { - case 4: - qDebug() << "\t\tDevice Type: GPU"; - break; + cl::KernelFunctor<> Functor(kernel); - case 2: - qDebug() << "\t\tDevice Type: CPU"; - break; + cl::CommandQueue queue(main_context, all_devices.front()); + cl::EnqueueArgs enargs(queue, cl::NDRange(5)); - default: - qDebug() << "\t\tDevice Type: unknown"; - } - qDebug() << "\t\tDevice Max Compute Units: " << - device.getInfo(); - qDebug() << "\t\tDevice Global Memory: " << device.getInfo(); - qDebug() << "\t\tDevice Max Clock Frequency: " << - device.getInfo(); - qDebug() << "\t\tDevice Max Memory Allocation: " << - device.getInfo(); - qDebug() << "\t\tDevice Local Memory: " << device.getInfo(); - qDebug() << "\t\tDevice Available: " << device.getInfo(); - } - qDebug(); - } + Functor(enargs); return a.exec(); } diff --git a/test1.cl b/test1.cl new file mode 100644 index 0000000..d5f92e3 --- /dev/null +++ b/test1.cl @@ -0,0 +1,5 @@ +void __kernel myVeryFirstFinction() +{ + if(get_global_id(0)) + printf("Success!"); +}