Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCL header inclusion with relative path issue in C++

I am trying to run an OpenCL C++ sample on Eclipse CTD that (on Mac) includes the OpenCL header as follows:

#include <OpenCL/cl.h>

The file exists on my system (OpenCL sdk is installed by default on Mac) but not in a OpenCL directory (actual path: /System/Library/Frameworks/OpenCL.framework/Versions/A/Headers), so if I add that path as an included directory in the properties of the project and remove the relative OpenCL directory from the #include statement the linking is obviously resolved but I notice that in that cl.h file other header files are referenced with the same relative path (ex. OpenCL/cl_platform.h) but you can see from the path above this OpenCL directory does not actually exist so I am wondering how this thing is supposed to work in the first place.

My question:

In my example above, is the 'OpenCL' directory in the relative path supposed to exist physically somewhere or is it supposed to be some kind of environment variable or similar that points to the actual path the sdk is installed in?

Sorry for the confusion, any help appreciated!

Note: from this article on the khronos website it seems that the OpenCL directory is supposed to physically exist.

like image 679
JohnIdol Avatar asked Nov 09 '11 02:11

JohnIdol


2 Answers

I recommend you read Apple's documentation on Frameworks.

The basic story, however, is that OS X resolves library and header search paths based on the frameworks you compile against. For example, to compile a program using the OpenCL SDK on a Macintosh, you'd compile like this:

clang -framework OpenCL main.c

This tells clang (or gcc, or llvm-gcc, depending on your choice of compiler) to search the System OpenCL SDK for both headers (for compilation) and libraries (for linking). Give it a try:

// compile me with: clang -framework OpenCL -o test test.c

#include <stdio.h>
#include <stdlib.h>
#include <OpenCL/opencl.h>

int main(int argc, char* const argv[]) {
    cl_uint num_devices, i;
    clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);

    cl_device_id* devices = calloc(sizeof(cl_device_id), num_devices);
    clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);

    char buf[128];
    for (i = 0; i < num_devices; i++) {
        clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL);
        fprintf(stdout, "Device %s supports ", buf);

        clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, 128, buf, NULL);
        fprintf(stdout, "%s\n", buf);
    }

    free(devices);
}

Note that I've include OpenCL/opencl.h here. This is the preferred header to include to get access to CL on the Mac. If you look at the header, you will see that it includes cl.h for you, as well as cl_platform.h. You can just include OpenCL/cl.h if you'd like; it will still work fine.

like image 108
James Avatar answered Nov 16 '22 23:11

James


According to Khronos specifications link to K there is a separate file that act as a wrapper for the C++, also the ".h" at the end of the file means that probably this header is only for the C and "maybe" for the C++. Probably you can solve this reading that page.

Anyway i found the Eclipse CDT really buggy and maybe it's worth trying to compile from the command line because the MAC OS environment really lacks of a good IDE and generally speaking it's not a good OS for developers.

Keep in mind that the only "native" language for the MAC is the Objective-C and all the others, if present, are porting; if you plan to develop some important projects i would suggest to consider a switch to Windows or Linux because even Linux offer a better environment for the developers compared to MAC OSX.

Edit 1: here is an example that i found, it use the binding described in the Khronos page that i just linked link 2 example

Edit 2: you can try 2 things - re-installing the VGA drivers that usually contain the proper OpenCL SDK for you machine ( Intel also offers an SDK but it runs only on iCore CPU and some C2D ) - to compile from the command line

But at this point your best companions could be Xcode and the online documentation for OpenCL under MAC link ( note that MAC include by default the 1.0 version of the OpenCL library ) simply because Xcode offers some better support for this kind of project under MAC and you can find a lot of tutorials on internet using this IDE.

like image 43
Micro Avatar answered Nov 17 '22 01:11

Micro