我尝试在我的OpenCL程序中实现内存映射技术,但它不起作用!这是我的内核代码:
__kernel void update(__global char *in, __global char *out)
{
size_t i;
for (i = 0; i < 10; i++);
out[i] += 'A' - 'a';
}
这是主持人代码:
cl_platform_id platformId = NULL;
cl_device_id deviceId = NULL;
cl_context context = NULL;
cl_command_queue commandQueue = NULL;
cl_mem cmPinnedBufIn = NULL;
cl_mem cmPinnedBufOut = NULL;
cl_mem cmDevBufIn = NULL;
cl_mem cmDevBufOut = NULL;
unsigned char *cDataIn = NULL;
unsigned char *cDataOut = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_uint retNumDevices;
cl_uint retNumPlatforms;
cl_int ret;
cl_event event;
cl_ulong start;
cl_ulong end;
size_t group_size = GLOBAL_ITEM_SIZE / LOCAL_ITEM_SIZE;
FILE *fp;
const char fileName[] = "./update.cl";
size_t sourceSize;
char *sourceStr;
unsigned char tt[10];
/* Load kernel source file */
if ( !(fp = fopen(fileName, "r")) )
quitErr("Failed to load kernel.", EXIT_FAILURE);
sourceStr = (char *)malloc(MAX_SOURCE_SIZE);
sourceSize = fread(sourceStr, 1, MAX_SOURCE_SIZE, fp);
fclose(fp);
/* Get Platform/Device Information */
ret = clGetPlatformIDs(1, &platformId, &retNumPlatforms);
assert(ret == CL_SUCCESS);
ret = clGetDeviceIDs(platformId, CL_DEVICE_TYPE_GPU, 1, &deviceId, &retNumDevices);
assert(ret == CL_SUCCESS);
/* Create OpenCL Context */
context = clCreateContext( NULL, retNumDevices, &deviceId, NULL, NULL, &ret);
/* Create command queue with measurment of preformance */
commandQueue = clCreateCommandQueue(context, deviceId, CL_QUEUE_PROFILING_ENABLE, &ret);
/* Create buffer objects */
size_t memSize = 10 * sizeof(unsigned char);
cmPinnedBufIn = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, memSize, NULL, &ret);
assert(ret == CL_SUCCESS);
cmPinnedBufOut = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, memSize, NULL, &ret);
assert(ret == CL_SUCCESS);
/* Mapp pinned memmory */
cDataIn = (unsigned char *)clEnqueueMapBuffer(commandQueue, cmPinnedBufIn, CL_TRUE, CL_MAP_WRITE, 0, memSize, 0, NULL, NULL, &ret);
assert(ret == CL_SUCCESS);
/* Initialize data */
for (size_t w = 0; w < memSize; w++)
cDataIn[w] = 'a' + w;
/* Create kernel program from source file */
program = clCreateProgramWithSource(context, 1, (const char **)&sourceStr, (const size_t *)&sourceSize, &ret);
assert(ret == CL_SUCCESS);
ret = clBuildProgram(program, 1, &deviceId, NULL, NULL, NULL);
if (ret != CL_SUCCESS) {
error("\nFail to build the program\n");
char buffer[10240];
clGetProgramBuildInfo(program, deviceId, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL);
quitErr(buffer, EXIT_FAILURE);
}
/* Create data parallel OpenCL kernel */
kernel = clCreateKernel(program, "update", &ret);
assert(ret == CL_SUCCESS);
/* Set OpenCL kernel arguments */
ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&cmPinnedBufIn);
assert(ret == CL_SUCCESS);
ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&cmPinnedBufOut);
assert(ret == CL_SUCCESS);
size_t global_item_size = GLOBAL_ITEM_SIZE;
size_t local_item_size = LOCAL_ITEM_SIZE;
/* Execute OpenCL kernel as data parallel */
ret = clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, &event);
if (ret == CL_INVALID_WORK_GROUP_SIZE)
quitErr("Invalid work group size: error when compute group size.", EXIT_FAILURE);
assert(ret == CL_SUCCESS);
/* Execute measurment issue */
if (preformanceMeas) {
clWaitForEvents(1, &event);
clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
printf("Kernels execution time: %10.6f [ms]\n", (end - start) * 1.0e-6f);
}
cDataOut = (unsigned char *)clEnqueueMapBuffer(commandQueue, cmPinnedBufOut, CL_TRUE, CL_MAP_READ, 0, memSize, 0, NULL, NULL, &ret);
assert(ret == CL_SUCCESS);
/* Transfer result to host */
memcpy(tt, cDataOut, memSize);
/* Transfer measurment issue */
if (preformanceMeas) {
//clWaitForEvents(1, &event);
clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
printf("Memory x buffer read: %10.6f [ms]\n", (end - start) * 1.0e-6f);
}
/* Display Results */
int i;
for (i = 0; i < group_size; i++)
for (size_t x = 0; x < memSize; x++)
printf("%c", tt[x]);
printf("\n");
/* Finalization */
clFlush(commandQueue);
clFinish(commandQueue);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseMemObject(cmPinnedBufIn);
clReleaseMemObject(cmPinnedBufOut);
clReleaseMemObject(cmDevBufIn);
clReleaseMemObject(cmDevBufOut);
clReleaseCommandQueue(commandQueue);
clReleaseContext(context);
free(sourceStr);
return EXIT_SUCCESS;
内核将小写字符更改为大写,但我的输出为空。当我静静地在内核中分配字符时,如下所示:
__kernel void update(__global char *in, __global char *out)
{
size_t i;
for (i = 0; i < 10; i++)
out[i] = 'A' + i;
}
比结果还可以。因此,我从输入数据得出的结论并未正确传输到内存,但为什么呢?有人可以帮帮我吗?
答案 0 :(得分:4)
内核的for循环有一个#34 ;;&#34;,所以它是一个空循环。
for (i = 0; i < 10; i++);
答案 1 :(得分:1)
在映射缓冲区中写入输入后,必须调用clEnqueueUnmapMemObject
。参见OpenCL 1.1规范,5.4.2.1。
您的内核不访问in
,并且不依赖于线程索引get_global_id(0)
。你可能想要这样的东西:
size_t i = get_global_id(0)
char c = in[i];
out[i] = (c>='a' && c<='z')?(c + 'A' - 'a'):c;
要在OpenCL 1.0中编写char数组,您需要启用byte_addressable_store
extension.r