gpt4 book ai didi

c - 将函数从 C 移植到 OPENCL

转载 作者:行者123 更新时间:2023-11-30 19:28:04 24 4
gpt4 key购买 nike

我用 C 编写了这个函数:

void SimpleGraphDrawing(unsigned char *image, int dim[2],double range[2][2])
{
for (int j=0;j<dim[1];j++)
for (int i=0;i<dim[0];i++)
{
float x=range[0][0]+(i+0.5)*(range[0][1]-range[0][0])/dim[0];
float y=range[1][0]+(j+0.5)*(range[1][1]-range[1][0])/dim[1];
float val=(x*x+y*y-1);
val=val*val*val-x*x*y*y*y;
image[j*dim[0]+i]=(val>0)*255;
}
}

我想将其移植到 OPENCL。

这是我的新功能:

void SimpleGraphDrawingGPU(OpenCLContext &context, unsigned char *image, int dim[2],double range[2][2])
{
cl_int error;
int blocking = true;
SetConsoleColor(15);
cl_int myKernel = GRAPH_DRAWING;

cl_mem imageKernel = clCreateBuffer(context.GetContext(), CL_MEM_READ_WRITE, dim[0]*dim[1] * sizeof(char), NULL, &error);
cl_mem dimKernel = clCreateBuffer(context.GetContext(), CL_MEM_READ_WRITE, 2*sizeof(int), NULL, &error);
cl_mem rangeKernel = clCreateBuffer(context.GetContext(), CL_MEM_READ_WRITE, 4 * sizeof(double), NULL, &error);

error = clEnqueueWriteBuffer(context.GetCommandQueue(0), imageKernel, blocking, 0, dim[0] * dim[1]* sizeof(int), image, 0, NULL, NULL);
error = clEnqueueWriteBuffer(context.GetCommandQueue(0), dimKernel, blocking, 0, 2*sizeof(int), &dim, 0, NULL, NULL);
error = clEnqueueWriteBuffer(context.GetCommandQueue(0), rangeKernel, blocking, 0, 4 * sizeof(double), &range, 0, NULL, NULL);

error = clSetKernelArg(context.GetKernel(myKernel), 0, sizeof(cl_mem), &imageKernel);
error = clSetKernelArg(context.GetKernel(myKernel), 1, sizeof(cl_mem), &dimKernel);
error = clSetKernelArg(context.GetKernel(myKernel), 2, sizeof(cl_mem), &rangeKernel);

size_t globalWorkSize[1] = { 1024 };
error = clEnqueueNDRangeKernel(context.GetCommandQueue(0), context.GetKernel(myKernel), 1, NULL, globalWorkSize, NULL, 0, NULL, NULL);

error = clReleaseMemObject(imageKernel);
error = clReleaseMemObject(dimKernel);
error = clReleaseMemObject(rangeKernel);
;
}

这也是 MyKernels 中的函数:

__kernel void GRAPH_DRAWING ()
{

}

输出真的很奇怪,我不知道如何修复它:“PROGRAM -33”

最佳答案

-33 是无效设备。您应该观看此示例以了解他们如何管理和处理 OCL 设备:https://software.intel.com/en-us/articles/gemm

关于c - 将函数从 C 移植到 OPENCL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54859344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com