OpenCL Vector add program

Deadly 提交于 2019-12-11 20:04:12

问题


I'm absolutely new to OpenCL programming. I have a working installation of OpenCL library and drivers. But the program I'm trying to run is not producing expected output (Output is all zeros). It is just a simple vector_add program. Thanks in advance for suggestions.

int main(int argc, char** argv)
{
cout << "Hello OpenCL" << endl;

vector<Platform> all_platforms;
int err = Platform::get(&all_platforms);
cout << "Getting Platform ... Error code " << err << endl;
if (all_platforms.size()==0)
    (cout << "No platforms" << endl, exit(0));
cout << "Platform info : " << all_platforms[0].getInfo<CL_PLATFORM_NAME>() << endl;
Platform default_platform = all_platforms[0];
cout << "Defaulting to it ..." << endl;

vector<Device> all_devices;
err = default_platform.getDevices(CL_DEVICE_TYPE_GPU, &all_devices);
cout << "Getting devices ... Error code : " << err << endl;
if (all_devices.size()==0)
    (cout << "No devices" << endl, exit(0));
Device default_device = all_devices[0];
cout << all_devices.size() << " devices & " << "Device info : " << all_devices[0].getInfo<CL_DEVICE_NAME>() << endl;
cout << "Defaulting to it ..." << endl;

Context context(default_device);
Program::Sources sources;

std::string kernel_code=
        "   void kernel simple_add(global const int* A, global const int* B, global int* C){"
        "   unsigned int i = get_global_id(0);  "
        "       C[i]=A[i]+B[i];                 "
        "   }                                                                               ";

sources.push_back(make_pair(kernel_code.c_str(), kernel_code.length()+1));
Program program(context, sources);

if (program.build(all_devices)==CL_SUCCESS)
    cout << "Built Successfully" << endl;

Buffer buffer_A(context,CL_MEM_READ_WRITE,sizeof(int)*10);
Buffer buffer_B(context,CL_MEM_READ_WRITE,sizeof(int)*10);
Buffer buffer_C(context,CL_MEM_READ_WRITE,sizeof(int)*10);

int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int B[] = {0, 1, 2, 0, 1, 2, 0, 1, 2, 0};

CommandQueue queue(context,default_device);
queue.enqueueWriteBuffer(buffer_A,CL_TRUE,0,sizeof(int)*10,A); // load data from host to device
queue.enqueueWriteBuffer(buffer_B,CL_TRUE,0,sizeof(int)*10,B);

Kernel kernel(program, "vector_add");
kernel.setArg(0, buffer_A);
kernel.setArg(1, buffer_B);
kernel.setArg(2, buffer_C);

queue.enqueueNDRangeKernel(kernel,cl::NullRange,cl::NDRange(10),cl::NullRange);
queue.finish();

int *C = new int[10];
queue.enqueueReadBuffer(buffer_C, CL_TRUE, 0, 10 * sizeof(int), C);

for (int i=0;i<10;i++)
    std::cout << A[i] << " + " << B[i] << " = " << C[i] << std::endl;

return 0;
}

回答1:


As pointed out in the comments, you should always check the error codes when using OpenCL API functions. This can be achieved by enabling exception handling in the C++ wrapper:

#define __CL_ENABLE_EXCEPTIONS      // with cl.hpp
//#define CL_HPP_ENABLE_EXCEPTIONS  // with cl2.hpp

#include <CL/cl.hpp>

int main(int argc, char *argv[])
{
  try
  {
    // OpenCL code here
  }
  catch (cl::Error& err)
  {
    cout << err.what() << " failed with error code " << err.err() << endl;
  }
}

If you do this, you will receive useful information about a couple of issues with your code.

The clCreateKernel function returns CL_INVALID_NAME. In your kernel, you define the kernel function with the name simple_add, but then you try and create a kernel object using the name vector_add.

If you have an OpenCL platform with multiple devices, you may also receive an error when building your kernel program. This is because you are creating an OpenCL context with a single device, but then trying to build the program for a list of devices:

Context context(default_device);
// ...
if (program.build(all_devices)==CL_SUCCESS)
  cout << "Built Successfully" << endl;

The simplest fix is just to remove the argument from the build function, since by default it will build the program for all devices in the context (which is almost always what you actually want):

if (program.build()==CL_SUCCESS)
  cout << "Built Successfully" << endl;


来源:https://stackoverflow.com/questions/33773749/opencl-vector-add-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!