我正在尝试深入研究openCL。目前我在问自己为什么在传输到 GPU 时标量与数组不同。
下面有两个输入。一个标量和一个数组。为什么将它们传输到 GPU 会有如此大的差异?
提前致谢!
// input size
int input_size = 4;
err = clSetKernelArg(kernel, 0, sizeof(unsigned int), &_input_size);
if (err != CL_SUCCESS) {
throw std::runtime_error("Failed to set kernel arguments!");
}
// input
int input[input_size];
input[0] = 1;
input[1] = 2;
input[2] = 3;
input[3] = 4;
cl_mem cl_input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * _input_size, NULL, NULL);
if (!cl_input) {
throw std::runtime_error("Failed to allocate device memory!");
}
err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &cl_input);
if (err != CL_SUCCESS) {
throw std::runtime_error("Failed to set kernel arguments!");
}
err = clEnqueueWriteBuffer(commands, cl_input, CL_TRUE, 0, sizeof(int) * _input_size, _input, 0, NULL, NULL);
if (err != CL_SUCCESS) {
throw std::runtime_error("Failed to write to source array!");
}
// output
int output_size = 4;
int output[output_size];
cl_mem cl_output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) * _output_size, NULL, NULL);
if (!cl_output) {
throw std::runtime_error("Failed to allocate device memory!");
}
err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &cl_output);
if (err != CL_SUCCESS) {
throw std::runtime_error("Failed to set kernel arguments!" );
}