Results 1 to 2 of 2

Thread: VLA : workaround to get Variable Length Arrays

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    1

    VLA : workaround to get Variable Length Arrays

    to compensate the lack of VLAs in OpenCL, this tiny trick offers the ability to create them though:

    add some extra space characters at the very top of your kernel file (in this case 20)
    after reading the kernel file into char* str, but before building the program, do the following:
    Code :
    	const char * define = "#define M 0x00000008";
     
    	memcpy(str, define, 20);
    then inside your kernel, the line
    Code :
    	char message[M];
    allocates an array of the size you entered in the host application. the hex value can be created dynamically, so you really have a VLA in OpenCL

  2. #2
    Senior Member
    Join Date
    Jul 2009
    Location
    Northern Europe
    Posts
    311

    Re: VLA : workaround to get Variable Length Arrays

    Another approach is to pass in a compiler flag to effectively insert #defines. E.g., "-define M=8" passed in as the options string to clBuildProgram will build the program as if you had #define M 8 in the code.

    I personally think using string manipulation on code is one of the cooler parts of OpenCL. Since kernel programs start out as strings you can modify and compose them all you want before building them. This makes it easy to change a kernel to use float vs. int, or change the size of an array or anything else like that. It's really very powerful. I've written a lot of kernels where I write them as three or four strings and then compose them the way I need them before building the program.

Similar Threads

  1. Arrays of Variable Length
    By toneburst in forum OpenCL
    Replies: 1
    Last Post: 01-11-2010, 06:05 AM
  2. Passing variable length structure to kernel
    By kungen85 in forum OpenCL
    Replies: 4
    Last Post: 10-08-2009, 12:47 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
  •