OpenCL - 内核在第二次运行时崩溃

时间:2013-09-11 23:32:33

标签: crash kernel opencl

我正在尝试运行第一次运行但第二次运行时崩溃的代码。导致崩溃的函数是Octree_GPU类的一部分,是:

int Octree_GPU::runCreateNodeKernel(int length)
{
      cl_uint nodeLength;
      if(nodeNumsArray[length-1] == 0)
            nodeLength = nodeAddArray[length-1];
      else
            nodeLength = nodeAddArray[length-1]+8;
      nodeArray = (cl_uint*)malloc(sizeof(cl_uint)*nodeLength);
      nodePointsArray = (cl_int*)malloc(sizeof(cl_uint)*nodeLength);
      startIndexArray = (cl_int*)malloc(sizeof(cl_int)*nodeLength);

      d_nodeAdd = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_uint)*length, NULL, &err);
      d_nodeArray = clCreateBuffer(context,CL_MEM_READ_WRITE, sizeof(cl_uint)*temp_length, NULL, &err);
      d_numPoints = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_uint)*length, NULL, &err);
      d_pointIndex = clCreateBuffer(context, CL_MEM_READ_WRITE,sizeof(cl_uint)*length,NULL, &err);
      d_nodePointsArray = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int)*temp_length, NULL, &err);
      d_nodeIndexArray = clCreateBuffer(context,CL_MEM_READ_WRITE, sizeof(cl_int)*temp_length, NULL, &err);

      err |= clEnqueueWriteBuffer(commands, d_nodeAdd, CL_TRUE, 0, sizeof(cl_uint)*length, nodeAddArray, 0, NULL,NULL);
      err |= clEnqueueWriteBuffer(commands, d_numPoints,CL_TRUE, 0, sizeof(cl_uint)*length,numPointsArray,0,NULL,NULL);
      err |= clEnqueueWriteBuffer(commands, d_pointIndex, CL_TRUE, 0, sizeof(cl_uint)*length,pointStartIndexArray,0, NULL, NULL);
      clFinish(commands);

      err  = clSetKernelArg(createNodeKernel, 0, sizeof(cl_mem), &d_odata);
      err |= clSetKernelArg(createNodeKernel, 1, sizeof(cl_mem), &d_nodeNums);
      err |= clSetKernelArg(createNodeKernel, 2, sizeof(cl_mem), &d_nodeAdd);
      err |= clSetKernelArg(createNodeKernel, 3, sizeof(cl_mem), &d_numPoints);
      err |= clSetKernelArg(createNodeKernel, 4, sizeof(cl_mem), &d_pointIndex);
      err |= clSetKernelArg(createNodeKernel, 5, sizeof(cl_mem), &d_nodeArray);
      err |= clSetKernelArg(createNodeKernel, 6, sizeof(cl_mem), &d_nodePointsArray);
      err |= clSetKernelArg(createNodeKernel, 7, sizeof(cl_mem), &d_nodeIndexArray);
      clFinish(commands);

      if(err != CL_SUCCESS) {
            printf("Cannot set Kernel Arg \n");
            exit(1);
      }
      size_t global_size[1] = {limit-1};
      err = clEnqueueNDRangeKernel(commands, createNodeKernel, 1, NULL, global_size, NULL, 0, NULL, NULL);
      if(err != CL_SUCCESS) {
            printf(" Kernel does not work \n");
            exit(1);
      }
      clFinish(commands);

      err = clEnqueueReadBuffer(commands, d_nodeArray, CL_TRUE, 0, sizeof(cl_uint)*temp_length, nodeArray, 0, NULL, NULL);
      err|= clEnqueueReadBuffer(commands, d_nodePointsArray, CL_TRUE, 0, sizeof(cl_int)*nodeLength, nodePointsArray, 0, NULL, NULL);
      err|= clEnqueueReadBuffer(commands, d_nodeIndexArray, CL_TRUE, 0, sizeof(cl_int)*nodeLength, startIndexArray, 0, NULL, NULL);
      clFinish(commands);

      clReleaseMemObject(d_nodeAdd);
      clReleaseMemObject(d_numPoints);
      clReleaseMemObject(d_nodeArray);
      clReleaseMemObject(d_nodePointsArray);
      clFinish(commands);
      return 0;
}

请注意,d_odata和d_nodeNums已在前面的函数中声明。内核代码如下所示:

__kernel void createNode(__global int* uniqueCode, __global int* nodeNums,__global int* nodeAdd, __global int* numPoints, __global int* pointIndex,__global int* nodeArray, __global int* nodePoints,__global int* nodeIndex)
{
    int ig = get_global_id(0);
    int add;
    int num = uniqueCode[ig];
    int pt = numPoints[ig];
    int ind = pointIndex[ig];
    int temp,j;
    if(nodeNums[ig] == 8) 
    {
           for(int i=0;i<8;i++)
           {
                    temp = ((int)num/10)*10+i;
                    add = nodeAdd[ig] + i;
                    nodeArray[add] = temp;
                    nodePoints[add] = select(0, pt, temp==num);
                    nodeIndex[add] = select(-1, ind, temp==num);
                    barrier(CLK_LOCAL_MEM_FENCE);
           }
    }
    else
    {
           j = num % 10;
           nodeAdd[ig] = nodeAdd[ig-1];
           add = nodeAdd[ig]+j;
           nodePoints[add] = pt;
           nodeIndex[add] = ind;
           barrier(CLK_LOCAL_MEM_FENCE);
    }

}

我试图找出原因,但没有成功。我可能会忽略一些非常简单的事情。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

我不是百分之百确定这是导致崩溃的原因,而是你写的地方

if(nodeNums[ig] == 8) 
{
       for(int i=0;i<8;i++)
       {
                barrier(CLK_LOCAL_MEM_FENCE);
       }
}
else
{
       barrier(CLK_LOCAL_MEM_FENCE);
}

这意味着工作组中的不同线程将执行不同数量的障碍,这可能导致挂起/崩溃。屏障(使用CLK_LOCAL_MEM_FENCE)用于同步对本地内存的访问,因此组中的所有工作项必须在继续之前执行此操作

在非崩溃说明中,看起来您正在使用CLK_LOCAL_MEM_FENCE(确保跨线程可以看到本地内存访问),这意味着CLK_GLOBAL_MEM_FENCE(确保跨线程可以看到全局内存访问)

另外

nodeAdd[ig] = nodeAdd[ig-1];

对于ig == 0是不正确的。这可能不会导致实际崩溃(因为我发现OpenCL可能很不宽容),但它值得修复