2

I am writing an opencl code to run on 3 devices. I create the context,program,devices and kernels in the following way:

cl_uint deviceCount;
err = clGetDeviceIDs(platform,CL_DEVICE_TYPE_GPU,0,NULL,&deviceCount);
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, deviceCount, device, NULL);
cl_context_properties context_prop[3]={(cl_context_properties)CL_CONTEXT_PLATFORM,(cl_context_properties)platform,
0};
context = clCreateContext(context_prop,deviceCount,device, NULL, NULL, &err);
program = clCreateProgramWithSource(context, 1,
                                    (const char**)&program_buffer, &program_size, &err);
err = clBuildProgram(program, deviceCount, device, NULL, NULL, NULL);
queue[0]= clCreateCommandQueue(context, device[0], 0, &err);
queue[1]= clCreateCommandQueue(context, device[1], 0, &err);
for (i=0; i<NUM_KERNELS; i++) {
    kernel[i] = clCreateKernel(program,kernel_names[i],&err);
    if (err < 0) {
        perror("Couldn't create Kernel");
        printf("%d,%d",i,err);
        exit(1);
    }
}

And all the buffers are created in the following way:

    buffer = clCreateBuffer(context,CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,size,pointer,&err);

Then I enqueue the same kernel to queue[0] and queue[1] in the following way:

 err = clSetKernelArg(kernel[6],0,sizeof(cl_mem),&grad_buf[0]);
    err |= clSetKernelArg(kernel[6],1,sizeof(num_space),&num_space);
    err = clEnqueueNDRangeKernel(queue[0],kernel[6],2,NULL,global_size3,NULL,2,&waitlist1[2],&waitlist1[4]);
    err = clSetKernelArg(kernel[6],0,sizeof(cl_mem),&grad_buf[1]);
    err |= clSetKernelArg(kernel[6],1,sizeof(num_space),&num_space);
    err = clEnqueueNDRangeKernel(queue[0],kernel[6],2,NULL,global_size3,NULL,2,&waitlist2[2],&waitlist2[4]);

I get the error -34 in the second clEnqueueNDRangeKernel function which means CL_INVALID_ CONTEXT. CL_INVALID_CONTEXT if context associated with command_queue and kernel is not the same or if the context associated with command_queue and events in event_wait_list are not the same. But in my code, I only have one context, Could someone help me find why I get this error? Thanks a lot!

Hai
  • 21
  • 3
  • As mentioned by others on [this topic](http://stackoverflow.com/questions/24761665/what-could-cause-this-pyopencl-logicerror-clenqueuereadbuffer-failed-invalid-c) you can get errors like this if kernels are not executing correctly. Try adding clFinish after each clEnqueue* operation to allow it to finish and see if the error shows up sooner, which will help you find the problem. Otherwise start removing host or device code to narrow down the cause. – Dithermaster Jul 16 '14 at 14:31

0 Answers0