Using OpenCL with Eclipse on Mac OS

It’s very easy to execute an openCL code in Mac, by simply using the flag ‘-framework OpenCL’ during compilation.

But some times when dealing with large projects, it becomes necessary to have a visual structure of the code tree, just the command line won’t do the trick. In this post, I will show how to integrate an OpenCL project with Eclipse IDE.

Eclipse is an open-source, cross-platform IDE for developing applications using many languages. Firstly, make sure that you have OpenCL installed on your machine. Latest versions of Xcode are shipped with OpenCL, so no extra installation is required.

  1. Install Eclipse IDE for C/C++ from here
  2. Once installed, open the application
  3. From File menu, select New -> C++ Project
  4. One the project is created, add a C++ source file, name it as test.cpp and write the following code into it:
    #include <stdio.h>
    #include <stdlib.h>
    #ifdef __APPLE__
    #include <OpenCL/opencl.h>
    #else
    #include <CL/cl.h>
    #endif
    
    using namespace std;
    
    int main(int argc, char* const argv[])
    {
    
        //Number of devices
        cl_uint num_devices, i;
    
        //Getting device ids
        clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
    
        cl_device_id* devices = (cl_device_id*)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);
    
                //Printing device info
                fprintf(stdout, "%s\n", buf);
            }
        free(devices);
    }
    

    The above code basically prints out the details of the devices available on your machine.

  5. Right click on the project and click settings. In C/C++ build, choose settings. Click MacOS X C++ Linker. In command field, it should be g++ -framework OpenCL
    openCL1
  6. Click on GCC C++ compiler, in inlcudes field paste this
    /System/Library/Frameworks/OpenCL.framework/Versions/A/Headers
    

    openCL2

    Choose other flags as needed

  7. Then, right click on project and select build. Then run your program.
    This is the output from the console

    Device GeForce 320M supports OpenCL 1.0
    Device Intel(R) Core(TM)2 Duo CPU     U9600  @ 1.60GHz supports OpenCL 1.0
    

 

Update: To use the same settings again for a different project, try the following

  1. Start an Empty C++ project
  2. Add a cpp source file and write some openCL code into it
  3. In the Project Explorer, right click on the project and click Import…. You will see the following dialog box
    1
  4. In C/C++, select C/C++ Project Settings, then click Next
  5. Create the file ‘OpenCL_properties.xml‘ and write the following into it
    </pre>
    <?xml version="1.0" encoding="UTF-8"?>
    <cdtprojectproperties>
    <section name="org.eclipse.cdt.internal.ui.wizards.settingswizards.IncludePaths">
    <language name="Object File">
    
    </language>
    <language name="Assembly Source File">
    
    </language>
    <language name="C++ Source File">
    <includepath>/System/Library/Frameworks/OpenCL.framework/Versions/A/Headers</includepath>
    
    </language>
    <language name="C Source File">
    
    </language>
    </section>
    <section name="org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros">
    <language name="Object File">
    
    </language>
    <language name="Assembly Source File">
    
    </language>
    <language name="C++ Source File">
    
    </language>
    <language name="C Source File">
    
    </language>
    </section>
    </cdtprojectproperties>
    <pre>
    
  6. For the Settings File, use the above xml file
  7. One more thing left to do. Right click on the Project and select Properties
  8. Under MacOS X C++ Linker, make sure you have g++ -framework OpenCL

    2

  9. Done and done! You can build and run the project now. You can use the same configuration file for other projects
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s