People‎ > ‎John Stavrakakis‎ > ‎John's blog‎ > ‎

Cross platform issues for OpenCL

posted 30 Jan 2012, 16:35 by John Stavrakakis
A while back I had to reinstall windows after an SSD drive failed. I was surprised to find that tried and tested OpenCL code was not working. There are three reasons why:

1) A newer version of the OpenCL driver was released by AMD in that time. It now follows the specification strictly for accessing vector components:

// old code style

float4 vertex;
vertex[0] = x;
vertex[1] = y;
vertex[2] = z;
vertex[3] = 1.0;

// OpenCL strict code style

float4 vertex;
vertex.x = x;
vertex.y = y;
vertex.z = z;
vertex.w = 1.0;

This is referred in a thread on intel forums:

2) One standard way of getting file size is to use stat. But this seems to cause problem with newline on windows, even after duplicating the text in windows format.

Reading the file with stat

    struct stat statbuf;
    FILE *fh;
    char *source;
    fh = fopen(filename, "r");
    if (fh == 0)
        return 0;
    stat(filename, &statbuf);
    source = (char *) malloc(statbuf.st_size + 1);
    fread(source, statbuf.st_size, 1, fh);
    source[statbuf.st_size] = '\0';
    printf("File %s read as string -->%s<--\n", filename, source);

CL file

void newline_creates_garbage()

CMD output

File read as string
void newline_creates_garbage()

3) Build log was not big enough to display all the above errors, for each wrongly placed index, for each unidentified character. The build log is too big.

Obtaining error log

    char build[4096];
    clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, 4096, build, NULL);   CLERR(clGetProgramBuildInfo, __LINE__);
    lulog("Build Log:\n%s\n",build);

4096 is not enough space for errors!
There is no query for how large the build log will be. The return value does specify the written amount. One could check to see the limit, resize the buffer, then rebuild. Then again, this is only needed for Debug, so leave it out for Release version.

There is nothing wrong with OpenCL C bindings as I had feared!