Results 1 to 10 of 10

Thread: Unable to pass structure to a opencl kernel

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    Unable to pass structure to a opencl kernel

    Hello,

    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 :

    Code :
    // 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

    Code :
    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
    Code :
    ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&c_mem_obj);
     
    	if(ret != CL_SUCCESS)  { 
    		printf("Error: Setting kernel argument. (c_mem)\n");	
    	}
       	ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&st_mem_obj);
    	if(ret != CL_SUCCESS)  { 
    		printf("Error: Setting kernel argument. (struct c)\n");	
    	}

    and defined the kernel as :

    Code :
    __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.

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Unable to pass structure to a opencl kernel

    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.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    Re: Unable to pass structure to a opencl kernel

    Error returned

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

    Structure definition :

    Code :
    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.

  4. #4
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Unable to pass structure to a opencl kernel

    typedef long long int bitboard;
    I wonder whether it would make a difference if you declared it like this:

    Code :
    typedef long bitboard;

    Notice that OpenCL already requires long variables to be 64 bits.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    Re: Unable to pass structure to a opencl kernel

    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?

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    Re: Unable to pass structure to a opencl kernel

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

    The code before setting kernel parametes
    Code :
     // Build the program
     
    	ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
    	if( ret != CL_SUCCESS ) {
    		printf( "Error : Building program\n" );		
    		if( ret == CL_OUT_OF_HOST_MEMORY ) printf( "CL_OUT_OF_HOST_MEMORY\n");
    		if( ret == CL_BUILD_PROGRAM_FAILURE ) printf("CL_BUILD_PROGRAM_FAILURE \n");
     
    	}

    returns
    Code :
    Error : Building program.
    CL_BUILD_PROGRAM_FAILURE.

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

  7. #7
    Senior Member
    Join Date
    Aug 2011
    Posts
    271

    Re: Unable to pass structure to a opencl kernel

    Quote Originally Posted by whitepearl
    Debugging through I found that the code has error in the statement before setting kernel parameters.

    The code before setting kernel parametes
    Code :
     // Build the program
     
    	ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
    	if( ret != CL_SUCCESS ) {
    		printf( "Error : Building program\n" );		
    		if( ret == CL_OUT_OF_HOST_MEMORY ) printf( "CL_OUT_OF_HOST_MEMORY\n");
    		if( ret == CL_BUILD_PROGRAM_FAILURE ) printf("CL_BUILD_PROGRAM_FAILURE \n");
     
    	}

    returns
    Code :
    Error : Building program.
    CL_BUILD_PROGRAM_FAILURE.

    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?"

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    Re: Unable to pass structure to a opencl kernel

    Quote Originally Posted by notzed

    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
    Code :
    Error : Building program
    CL_BUILD_PROGRAM_FAILURE

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    Re: Unable to pass structure to a opencl kernel

    Hey, found bug and fixed it.

    Thanx all,

  10. #10
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Unable to pass structure to a opencl kernel

    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.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

Similar Threads

  1. How to pass complex structures to kernel?
    By chanakya.sun in forum OpenCL
    Replies: 2
    Last Post: 07-22-2011, 04:21 AM
  2. Replies: 2
    Last Post: 01-28-2010, 04:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •