CL_INVALID_COMMAND_QUEUE when setting an element in an array

I have a for loop inside a kernel that processes part of an array in global memory and sets an array element to 0 if it meets a certain condition

for (unsigned int i = start; i < start + loopSize && i < neighbourDegree; i++) 

	{

		//if neighbour is vertex being removed

		if (ioEdgeArray[neighbourNeighbourIdx + i] - 1 == vertex ) 

		{

			ioEdgeArray[neighbourNeighbourIdx + i] = NULL;

		}

	}

After executing the kernel that contains the above I get the error CL_INVALID_COMMAND_QUEUE after calling clFinish on the commandQueue used to run the kernel.

This is confusing in itself but if I replace NULL with a value from 1-8, the program completes successfully.

  1. Do you #define NULL to any value earlier in OpenCL source code?

  2. Do you check that clBuildProgram returns CL_SUCCESS?

I’ve got #define NULL 0 at the top of my .cl program and clBuildProgram completes successfully.

I did some more reading on the forum and saw comments suggesting using __global. Modifying my kernel to the following gives me no error:

__global unsigned int * neighbourNeighbourIdx = &ioVertexArray[neighbour - 1];

...

	// Partial loop of neighbours

	for (unsigned int i = start; i < start + loopSize && i < neighbourDegree; i++) 

	{

		//if neighbour is vertex being removed

		if (*(neighbourNeighbourIdx + i) - 1 == vertex )

		{

			*(neighbourNeighbourIdx + i) = NULL;

		}

	}

I still don’t understand the original error though. I could replace the NULL with any value between 1-8 and the program would run. Replacing NULL with 0 or a value > 8 would give me the error.

I’m generally not a c programmer so the error explanation may be obvious…

If you don’t specify __global then the compiler assumes that it is a pointer to private memory. Most probably compiler will treat access to that memory as access to scratch registers (as far as real register ar not dynamically accessible in NVidia chips). Scratch registers are in global memory (at some specific part of it). Thus the compiler generates the code which writes to global memory at some weird address. I don’t know what is locate there. And you don’t know too.

Thanks that makes sense. I don’t think the __global was the cause of my problem. It was an earlier process modifying one of my arrays, I fixed it with a quick if check.