Unable to pass structure to a opencl kernel


I am a novice in OpenCl and I am facing problem with passing structure to a opencl kernel.

I have a structure :

struct c {
Bitboard a;
Bitboard m;

I need to pass a pointer to this structure to my opencl kernel. The first argument of opencl kernel is an array of interger.

I have created memory buffers for the two arguments :

// Create memory buffers on the device 

	cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_READ_WRITE, LIST_SIZE * sizeof(int), NULL, &ret);
	cl_mem st_mem_obj = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(struct c), NULL, &ret);

Then : // Copy array C and struct to their respective memory buffers

ret = clEnqueueWriteBuffer(command_queue, c_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), C, 0, NULL, NULL);
	ret = clEnqueueWriteBuffer(command_queue, st_mem_obj, CL_TRUE, 0, sizeof(struct c), &c, 0, NULL, NULL);

For Setting the arguments of the kernel

ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&c_mem_obj);
	if(ret != CL_SUCCESS)  { 
		printf("Error: Setting kernel argument. (c_mem)
   	ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&st_mem_obj);
	if(ret != CL_SUCCESS)  { 
		printf("Error: Setting kernel argument. (struct c)

and defined the kernel as :

__kernel void eval( __global int *a,  __global struct chess* cc)

Can any body point out the mistake/mistakes from the procedure mentioned? The program works perfectly fine if I pass only the integer array to the kernel whereas on passing both it shows error with setting kernel parameters.

The program works perfectly fine if I pass only the integer array to the kernel whereas on passing both it shows error with setting kernel parameters.

What error is returned?

Also, can you show us the definition of that struct? Notice from section 6.8 that structs cannot contain pointers.

Error returned

Error: Setting kernel argument. (c_mem)
Error: Setting kernel argument. (struct c)

Structure definition :

typedef long long int bitboard;

struct c {
	int side;			//To represent whose's turn it is?
	bitboard white_pawns;		//To represent White Pawns
	bitboard white_knights;		//To represent White knights
	bitboard white_rooks;		//To represent White Rooks
	bitboard white_bishops;		//To represent White Bishops
	bitboard white_king;		//To represent White King
	bitboard white_queen;		//To represent White Queen
	bitboard black_pawns;		//To represent Black Pawns
	bitboard black_knights;		//To represent Black Knights
	bitboard black_rooks;		//To represent Black Rooks
	bitboard black_bishops;		//To represent Black Bishops
	bitboard black_king;		//To represent Black King
	bitboard black_queen;		//To represent Black Queen
	bitboard white_pieces;		//To represent all White Pieces
	bitboard black_pieces;		//To represent all Black Pieces

The structure does not contain any pointers.

typedef long long int bitboard;

I wonder whether it would make a difference if you declared it like this:

typedef long bitboard;

Notice that OpenCL already requires long variables to be 64 bits.

No, changing ‘typedef long long int bitboard’ to ‘typedef long int bitboard’ does not make a difference.

While passing a structure do we need to write the structure definition in host and kernel both. Or are there any other measures that need to be taken care of while passing structure?

Debugging through I found that the code has error in the statement before setting kernel parameters.

The code before setting kernel parametes

 // Build the program

	ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
	if( ret != CL_SUCCESS ) {
		printf( "Error : Building program
" );		


Error : Building program.

But this happens only when I pass a structure to a kernel otherwise no such error pops up.

That could mean anything: get the error from the compiler output.

I mean it’s like asking “when i compile one programme it works, but when i compile a completely different program it doesn’t work: why?”

This error comes only when I pass a structure to the kernel. For rest other datatypes the same code works fine, so it is not a completely different program. Can any one help urgently?

What can I presume by

Error : Building program

Hey, found bug and fixed it.

Thanx all, :slight_smile:

whitepearl, I think other people who come to this forum with a similar problem would appreciate if you explained what was the problem and how you fixed it.