Wednesday 23 April 2014

Intel CPU OpenCL on Ubuntu (12.04)

Intel CPU OpenCL on Ubuntu (12.04)

Installing Intel CPU OpenCL on Ubuntu (12.04)

1. Download the IntelĀ® SDK for OpenCL* Applications XE 2013 from the Intel website, here http://software.intel.com/en-us/vcsource/tools/opencl-sdk-xe.

2. Unpack the tarball and cd into the new directory
  $ tar -xzvf intel_sdk_for_ocl_applications_2013_xe_sdk_3.0.67279_x64.tgz
  $ cd  intel_sdk_for_ocl_applications_2013_xe_sdk_3.0.67279_x64

3. There are a bunch of .rpm files. These are the default packages for redhat linux. We can install them on Ubuntu (a debian based distro) by converting them to .deb files.
a) If you don't have these packages alreafy, you'll need them for dealing with rpm files.
  $ sudo apt-get install -y rpm alien libnuma1
b) Convert all of the rpm files into deb format, and then install them with dpkg. You can do this by pasting the following commands into bash, or copying this as a script and then running it.
 
 #/bin/bash
 for f in *.rpm; do
   fakeroot alien --to-deb $f
 done
 for f in *.deb; do
   sudo dpkg -i $f
 done

4. You need to install the so-called icd-file, which registers this OpenCL implementation, so that it's available in paralell to any other.
  sudo ln -s /opt/intel/opencl-1.2-3.0.67279/etc/intel64.icd /etc/OpenCL/vendors/intel64.icd

Hint: if your OpenCL version number is more recent (I'm writing this as of August, 2013, then the installed path of the OpenCL implementation in /opt/intel/opencl-* might be different than the "1.2-3.0.67279" that I'm using.

5.If this is the only OpenCL implementation on your machine, you should install a symlink to libOpenCL.so into /usr/lib, so that things can be linked up easily. If you already have the NVIDIA OpenCL platform (for your GPU) then this is not necessary -- installing the icd file into the registry is enough to tell the system about your new OpenCL platform.

   $ sudo ln -s /opt/intel/opencl-1.2-3.0.67279/lib64/libOpenCL.so /usr/lib/libOpenCL.so
   $ sudo ldconfig 
 
 

Checking your OpenCL Installation

  1. Download the file clDeviceQuery.cpp from this gist. Its a small progam that reports all of the available OpenCL platforms on your machine, and all of their devices.
  2. CompileclDeviceQuery.cpp with g++, and run it. You'll need to have the OpenCL header files in your include path, and libOpenCL.so in your LD_LIBRARY_PATH. Note that you dont need the vendor-specific OpenCL implementation in your LD_LIBRARY_PATH necessarily. When libOpenCL.so is loaded, it uses the ICD registry to find all of the vendor implementations.
      $ g++ -o clDeviceQuery -I/opt/intel/opencl-1.2-3.0.67279/include clDeviceQuery.cpp -lOpenCL
      $ ./clDeviceQuery
     
     

    Programming using OpenCL

    In file hello.cu:

    #include "stdio.h"

    int main()

    {

    printf("Hello, world\n");

    return 0;

    }

    ----------------------------------------------------

    We should write the header as follows for linux

    #ifdef _LINUX_
    #include <OpenCL/opencl.h>
    #else
    #include <CL/cl.h>
    #endif
    To run "Hello.c"
    > gcc -I /path-to-NVIDIA/OpenCL/common/inc -L /path-to-NVIDIA/OpenCL/common/lib/Linux32 -o hello hello.c -lOpenCL (32-bit Linux)
    > gcc -I /path-to-NVIDIA/OpenCL/common/inc -L /path-to-NVIDIA/OpenCL/common/lib/Linux64 -o hello hello.c -lOpenCL (64-bit Linux)