OpenCL Vector添加程序

时间:2015-11-18 07:04:37

标签: opencl

我是OpenCL编程的新手。我有一个OpenCL库和驱动程序的工作安装。但是我试图运行的程序没有产生预期的输出(输出全为零)。它只是一个简单的vector_add程序。 提前感谢您的建议。

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 个答案:

答案 0 :(得分:2)

正如评论中所指出的,在使用OpenCL API函数时,您应该始终检查错误代码。这可以通过在C ++包装器中启用异常处理来实现:

clCreateKernel

如果您这样做,您将收到有关您的代码的几个问题的有用信息。

CL_INVALID_NAME函数返回simple_add。在内核中,使用名称vector_add定义内核函数,然后尝试使用名称Context context(default_device); // ... if (program.build(all_devices)==CL_SUCCESS) cout << "Built Successfully" << endl; 创建内核对象。

如果您的OpenCL平台包含多个设备,则在构建内核程序时也可能会收到错误消息。这是因为您使用单个设备创建OpenCL上下文,然后尝试为设备列表构建程序:

build

最简单的解决方法是从if (program.build()==CL_SUCCESS) cout << "Built Successfully" << endl; 函数中删除参数,因为默认情况下它会为上下文中的所有设备构建程序(这几乎总是你真正想要的):

{{1}}