如何在Haskell OpenCLRaw中修复clSetKernelArg错误CL_INVALID_MEM_OBJECT?

时间:2011-07-01 09:50:05

标签: pointers haskell opencl

我尝试使用HASKELL使用OpenCL。我编写了一个简单的程序来转换C中的工作程序。它似乎运行良好,但是当我将内存对象分配给内核参数时,它会因CL_INVALID_MEM_OBJECT错误而失败。我不知道要修复谁,因为我使用相同的调用而不是C程序,并且它有效:

programSource是OpenCL代码

programSource :: String
programSource = "__kernel void duparray(__global float *in, __global float *out ){  int id = get_global_id(0); out[id] = 2*in[id]; }"

初始化工作一直有效,直到clSetKernelArg的调用因Just (ErrorCode (-38))

而失败
-- test openCL
input  <- newArray ([0,1,2,3,4] :: [CFloat])

Right mem_in <- clCreateBuffer myContext (memFlagsJoin [clMemReadOnly,clMemCopyHostPtr]) (4*5) (castPtr input)
Right mem_out <- clCreateBuffer myContext clMemWriteOnly (4*5) nullPtr
print (mem_in,mem_out)
Right program <- clCreateProgramWithSource myContext programSource
print program

err <- clBuildProgram program [myDeviceId] "" buildProgramCallback nullPtr
print err

Right kernel <- clCreateKernel program "duparray"
print kernel

kaErr0 <- clSetKernelArg kernel 0 (fromIntegral.sizeOf $ mem_in) (castPtr mem_in)
kaErr1 <- clSetKernelArg kernel 1 (fromIntegral.sizeOf $ mem_out) (castPtr mem_out)
print (kaErr0,kaErr1)

我正在使用OpenCLRaw,我对https://github.com/zhensydow/OpenCLRaw

进行了一些修改

1 个答案:

答案 0 :(得分:1)

我发现我需要传递内存缓冲区指针的方向,而不是指针本身。这是调用clSetKernelArg的正确方法:

dir_mem_in <- (malloc :: IO (Ptr Mem)) 
poke dir_mem_in mem_in
kaErr0 <- clSetKernelArg kernel 0 (fromIntegral.sizeOf $ mem_in) (castPtr dir_mem_in)
dir_mem_out <- (malloc :: IO (Ptr Mem)) 
poke dir_mem_out mem_out
kaErr1 <- clSetKernelArg kernel 1 (fromIntegral.sizeOf $ mem_out) (castPtr dir_mem_out)
print (kaErr0, kaErr1)