include headers to .cl file

Hi,

I wrote a kernel in a .cl file, but when I include a .h file, and run code,

I get ““file.h” not found”.

so I read the opencl spec, and found that I have to add the path to the headers to my buildprogram API (5.4.3 of 1.0spec), so this is my code line:

[codebox]

ciErrNum = clBuildProgram(cpProgram, 0, NULL, “-I C:/Users/localcontrol/internship/OpenCV/cv/include”, NULL, NULL);[/codebox]

but it doesn’t work, the program crashes at this line,

is anyone seeing what could be the problem,

I really need help with this.

thanks

maybe a bug in drivers. In any case, it might be easier to create your OpenCL Program from multiple source files (where the source files are also the header files)

I created a “source file” for my header file, this is my code

[codebox]const char* clSourcefile = “oclfind_obj.cl”;// kernel file

const char* cvincludefile = “cv.h”; //include file

// Program Setup

const char* strings[2];

size_t program_length;

const char* source_path = shrFindFilePath(clSourcefile, argv[0]);

char *source = oclLoadProgSource(source_path, "", &program_length);

shrCheckErrorEX(source != NULL, shrTRUE, pCleanup);

size_t include_length;

const char* include_path = shrFindFilePath(cvincludefile, argv[0]);

char *include = oclLoadProgSource(include_path, "", &include_length);

shrCheckErrorEX(include != NULL, shrTRUE, pCleanup);



strings[0] = source;

strings[1] = include;

// create the program

cpProgram = clCreateProgramWithSource(cxGPUContext, 1,(const char **) &strings, &program_length, &ciErrNum);

shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);

free(source);

// build the program

ciErrNum = clBuildProgram(cpProgram, 0, NULL, NULL, NULL, NULL);

[/codebox]

so I have 2 source files, I also add include <cv.h> in .cl file,

but I still get error : ‘cv.h’ file not found, and it doesn’t recognise my variables.

also clBuildProgram returns an error, may be I have to change parameters in :

ciErrNum = clBuildProgram(cpProgram, 0, NULL, NULL, NULL, NULL);

If you pass all your files into:

clCreateProgramWithSource()

then you shouldn’t use the #include command.

What ‘#include’ does is that it basically copy and paste all code that is in the given ‘.h’ file into the source file but it is not longer necessary when you manually include the header files into clCreateProgramWithSource() call.

(clCreateProgramWithSource() basically merges all input files into one big source code which is essentially the same thing what #include does …)

I removed the #include from .cl file, and kept .h file in clcreateProgramWithSource, but when I build it I got nothing,

before adding it I was getting errors that variables are not defined, here my code, can you see if I maid errors in it:

[codebox]const char* clSourcefile = “oclfind_obj.cl”;// source file

const char* cvincludefile = “cv.h”; //include file

// Program Setup

const char* strings[2];

size_t program_length;

const char* source_path = shrFindFilePath(clSourcefile, argv[0]);

char *source = oclLoadProgSource(source_path, "", &program_length);

shrCheckErrorEX(source != NULL, shrTRUE, pCleanup);

size_t include_length;

const char* include_path = shrFindFilePath(cvincludefile, argv[0]);

char *cvinclude = oclLoadProgSource(include_path, "", &include_length);

shrCheckErrorEX(cvinclude != NULL, shrTRUE, pCleanup);

strings[0] = source;

strings[1] = cvinclude;

size_t lenghts[2];

lenghts[0] = program_length;

lenghts[1] = include_length;

// create the program

cpProgram = clCreateProgramWithSource(cxGPUContext, 2,(const char **) &strings, (const size_t *) &lenghts, &ciErrNum);

shrCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);

// build the program

ciErrNum = clBuildProgram(cpProgram, 0, NULL, NULL, NULL, NULL);

if (ciErrNum != CL_SUCCESS)

{[/codebox]

Thanks.

The order of the source files matters (they are basically expanded to one source file). So you have to use your include file as the first source file.

(e.g. like this:

strings[0] = cvinclude;
strings[1] = source;
size_t lenghts[2];
lenghts[0] = include_length;
lenghts[1] = program_length;

)

I did what you said like this

[codebox]const char* files[2];

size_t include_length;

const char* include_path = "C:/Users/localcontrol/Desktop/work/nvidia3.0/cv.h";

char *cvinclude = oclLoadProgSource(include_path, "", &include_length);

printf( "cv path %s\n", include_path);

//printf( "%s\n", cvinclude);

printf( "include_length %d\n", include_length);

shrCheckErrorEX(cvinclude != NULL, shrTRUE, pCleanup);

size_t program_length;

const char* source_path = shrFindFilePath(clSourcefile, argv[0]);

char *source = oclLoadProgSource(source_path, "", &program_length);

printf( "cl path %s\n", source_path);

//printf( "%s\n", source);

printf( "program_length %d\n", program_length);

shrCheckErrorEX(source != NULL, shrTRUE, pCleanup);

files[0] = cvinclude;

files[1] = source;

size_t lenghts[2];

lenghts[0] = include_length;

lenghts[1] = program_length;

printf( "%s\n", files[0]);

printf( "%s\n", files[1]);

 // create the program

cpProgram = clCreateProgramWithSource(cxGPUContext, 2, (const char **) &files, (const size_t *) lenghts, &ciErrNum);

oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);

 // build the program

ciErrNum = clBuildProgram(cpProgram, 0, NULL, "-cl-mad-enable", NULL, NULL);[/codebox] 

I printf cl and .h files to be sure, they’re correctly loaded,

I printf files two, but it doesn’t print out all .h file, although when I print include_length I get the right number.

I attached the errors I get when I build my program,

both with header file and without it.


The second image it’s without header file,
so it doesn’t recognize cvArr defined in .h

The first image it’s when I add the .h file, so I get weard things

Well, if you can’t print your loaded “.h” file then it seems that it was loaded incorrectly… but this has nothing to do with OpenCL. I use clCreateProgramWithSource() with multiple different source files all the time and it works just fine

ciErrNum = clBuildProgram(cpProgram, 0, NULL, "-I C:/Users/localcontrol/internship/OpenCV/cv/include", NULL, NULL);[/codebox]

It might not help you much, but I have used clBuildProgram just as you write above with #include in my .cl file to include cross c++/opencl information. It worked well.

If you are under Windows (Im using LInux so don’t take this too seriously…), don’t you have to use \ instead of / in paths and file names?