我有一个愚蠢的问题,我希望有人向我解释一下。我有一个简单的 OpenCL 内核,它只接收一个 double,在内核中将其打印出来,然后将其复制回主机。我注意到,当我在设备上 printf(显然使用 CPU 作为设备而不是 GPU)时,打印的值是浮点精度而不是双精度。OpenCL 中有关于 printf 的内容吗?(我想知道它是否对浮动进行了隐式转换?)这是一些测试代码。(这是在 Macbook Pro(视网膜)、OSX 10.8.4 上运行的)。
#include <stdio.h>
#include <OpenCL/opencl.h>
#include <math.h>
#define CL_CHECK(_expr) \
do { \
cl_int _err = _expr; \
if (_err == CL_SUCCESS) \
break; \
fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \
abort(); \
} while (0)
#define CL_CHECK_ERR(_expr) \
({ \
cl_int _err = CL_INVALID_VALUE; \
typeof(_expr) _ret = _expr; \
if (_err != CL_SUCCESS) { \
fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \
abort(); \
} \
_ret; \
})
void pfn_notify(const char *errinfo, const void *private_info, size_t cb, void *user_data{
fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo);
}
int main(int argc, const char * argv[]){
cl_platform_id platforms[100];
cl_uint platforms_n = 0;
CL_CHECK(clGetPlatformIDs(100, platforms, &platforms_n));
if (platforms_n == 0)return 1;
cl_device_id devices[100];
cl_uint devices_n = 0;
CL_CHECK(clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_CPU, 100, devices, &devices_n));
if (devices_n == 0)return 1;
cl_context context;
context = CL_CHECK_ERR(clCreateContext(NULL, 1, devices, &pfn_notify, NULL, &_err));
double num = M_PI ;
printf("number before is : %1.17e\n",num);
const char *program_source[] = {
"#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n",
"__kernel void simple_demo(__global double *src, __global double *dst)\n",
"{\n",
" int i = get_global_id(0);\n",
" printf(\"src on device is : %1.17e\\n\",src[i]);\n",
" dst[i] = src[i];\n",
"}\n"
};
cl_program program;
program = CL_CHECK_ERR(clCreateProgramWithSource(context,
sizeof(program_source)/sizeof(*program_source), program_source, NULL, &_err));
if (clBuildProgram(program, 1, devices, "", NULL, NULL) != CL_SUCCESS) {
char buffer[10240];
clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG,
sizeof(buffer), buffer, NULL);
fprintf(stderr, "CL Compilation failed:\n%s", buffer);
abort();
}
cl_mem input_buffer;
input_buffer = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(double), NULL, &_err));
cl_mem output_buffer;
output_buffer = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(double), NULL, &_err));
cl_kernel kernel;
kernel = CL_CHECK_ERR(clCreateKernel(program, "simple_demo", &_err));
CL_CHECK(clSetKernelArg(kernel, 0, sizeof(input_buffer), &input_buffer));
CL_CHECK(clSetKernelArg(kernel, 1, sizeof(output_buffer), &output_buffer));
cl_command_queue queue;
queue = CL_CHECK_ERR(clCreateCommandQueue(context, devices[0], 0, &_err));
CL_CHECK(clEnqueueWriteBuffer(queue, input_buffer, CL_TRUE, 0, sizeof(double), &num, 0, NULL, NULL));
cl_event kernel_completion;
size_t global_work_size[1] = { 1 };
CL_CHECK(clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, &kernel_completion));
CL_CHECK(clWaitForEvents(1, &kernel_completion));
CL_CHECK(clReleaseEvent(kernel_completion));
printf("number after is :");
double data;
CL_CHECK(clEnqueueReadBuffer(queue, output_buffer, CL_TRUE, 0, sizeof(double), &data, 0, NULL, NULL));
printf(" %1.17e", data);
printf("\n");
CL_CHECK(clReleaseMemObject(input_buffer));
CL_CHECK(clReleaseMemObject(output_buffer));
CL_CHECK(clReleaseKernel(kernel));
CL_CHECK(clReleaseProgram(program));
CL_CHECK(clReleaseContext(context));
return 0;
}
如果你复制它并编译并运行它,你应该得到:
number before is : 3.14159265358979312e+00
src is : 3.14159274101257324e+00
number after is : 3.14159265358979312e+00
有任何想法吗?