https://i.stack.imgur.com/TA9v6.png
我一直在尝试编译一个内核,该内核使用 OpenCL 通过 clEnqueueReadBuffer 函数将某些索引分配给 std::vector,但它似乎无法正常工作,因为第一个结果是 std::vector 中唯一分配的
c++中主机的源代码如下:
cl_mem originalPixelsBuffer = clCreateBuffer(p1.context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(Color) * imageObj->SourceLength(), source, &p1.status); CheckErrorCode(p1.status, p1.program, p1.devices[0], "未能创建缓冲区 0"); cl_mem targetBuffer = clCreateBuffer(p1.context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(Color) * imageObj->OutputLength(), target, &p1.status); CheckErrorCode(p1.status, p1.program, p1.devices[0], "未能创建缓冲区 1"); //写缓冲区 p1.status = clEnqueueWriteBuffer(p1.commandQueue, originalPixelsBuffer, CL_FALSE, 0, sizeof(Color) * imageObj->SourceLength(), source, 0, NULL, NULL); CheckErrorCode(p1.status, p1.program, p1.devices[0], "写入缓冲区 0 失败"); p1.status = clEnqueueWriteBuffer(p1.commandQueue, targetBuffer, CL_TRUE, 0, sizeof(Color) * imageObj->OutputLength(), target, 0, NULL, NULL); CheckErrorCode(p1.status, p1.program, p1.devices[0], "写入缓冲区 1 失败"); size_t globalWorkSize[2] = { imageObj->originalWidth * 4, imageObj->originalHeight * 4 }; 尺寸_t localWorkSize[2]{ 64,64 }; SetLocalWorkSize(IsDivisibleBy64(localWorkSize[0]), localWorkSize); //执行内核 p1.status = clEnqueueNDRangeKernel(p1.commandQueue, Kernel, 1, NULL, globalWorkSize, IsDisibibleByLocalWorkSize(globalWorkSize, localWorkSize) ? localWorkSize : NULL, 0, NULL, NULL); CheckErrorCode(p1.status, p1.program, p1.devices[0], "clEnqueueDRangeKernel 失败"); //读取缓冲区 p1.status = clEnqueueReadBuffer(p1.commandQueue, targetBuffer, CL_TRUE, 0, sizeof(Color) * imageObj->OutputLength(), target, 0, NULL, NULL); CheckErrorCode(p1.status, p1.program, p1.devices[0], "写入缓冲区 1 失败");
内核代码:
__kernel void interp(__global struct Color* source,__global struct Color* target,uint64 width,uint64 height,uint64 ratio,uint64 limit, uint64 originalHeight)
{
__private fp32 wIndex = (int64)get_global_id(0);
__private fp32 hIndex = (int64)get_global_id(1);
if(((int64)wIndex)%ratio==MATCH && ((int64)hIndex)%ratio ==MATCH)
{
__private int64 Index = (wIndex/ratio) * (originalHeight/ratio) + (hIndex/ratio);
if(Index < limit)
{
__private int64 tIndex = wIndex * height + hIndex;
target[tIndex].R = source[Index].R;
target[tIndex].G = source[Index].G;
target[tIndex].B = source[Index].B;
target[tIndex].A = source[Index].A;
}
}
}```