Mostrando entradas con la etiqueta python. Mostrar todas las entradas
Mostrando entradas con la etiqueta python. Mostrar todas las entradas

viernes, 18 de mayo de 2012

Installing OpenCL in Ubuntu 12.04

OpenCL is the new standard for "heterogeneous computing". This is a fancy way of saying that it enables us to write code that will seamlessly run both in CPU and GPU without any changes, unlike for example CUDA which only runs on GPUs.

Now, my laptop doesn't have any GPU (who has the money for that??), but I want to use OpenCL for some computations. Luckily, Intel provides an implementation of the OpenCL standard which runs on any CPU, not just the Intel ones, without the need of installing additional and proprietary Linux drivers.

The SDK provided by Intel comes in an RPM format so it is not Ubuntu-friendly. Thankfully, it is possible to convert the RPM to a .deb package and, with some additional tweaking, having a working OpenCL installation. Most of the following instructions were extracted from the ~mhr3 blog so credit goes for them.

OK, now, for the actual install:
  1. Download the Intel SDK for OpenCL Applications from Intel's web site (http://software.intel.com/en-us/articles/vcsource-tools-opencl-sdk/). The download options are not easy to spot in the messy page, they are on the top right bo. After downloading, you will end with a .tgz file with an RPM inside (crazy, I know). In my case, the file is named intel_sdk_for_ocl_applications_2012_x64.tgz.
  2. Extract the RPM from the .tgz file:
    $ tar zxvf intel_sdk_for_ocl_applications_2012_x64.tgz

    This will extract the intel_ocl_sdk_2012_x64.rpm file.
  3. Convert the RPM file to .deb format and install:
    $ sudo apt-get install -y rpm alien libnuma1    # In case you don't have these packages
    $ fakeroot alien --to-deb intel_ocl_sdk_2012_x64.rpm
    $ sudo dpkg -i intel-ocl-sdk_2.0-31361_amd64.deb
  4. Now the SDK and libraries will be installed to /usr/lib64, while Ubuntu expects them to be in /usr/lib. No problem, just make a symlink and update the library cache:
    $ sudo ln -s /usr/lib64/libOpenCL.so /usr/lib/libOpenCL.so
    $ sudo ldconfig
That's it! OpenCL should be installed now. Let's try with a test program to see the device capabilities.

 #include <iostream>  
 #include <CL/cl.hpp>  
 #include <boost/foreach.hpp>
  
 int main(int, char**) {  
     std::vector<cl::Platform> platforms;  
     cl::Platform::get(&platforms);  
     BOOST_FOREACH(cl::Platform platform, platforms) {  
         std::cout << "Platform: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;  
         std::vector<cl::Device> devices;  
         platform.getDevices(CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_CPU, &devices);  
         BOOST_FOREACH(cl::Device device, devices) {  
             std::cout << "Device: " << device.getInfo<CL_DEVICE_TYPE>();  
             std::cout << " (" << CL_DEVICE_TYPE_GPU << " means GPU, " << CL_DEVICE_TYPE_CPU << " means CPU)" << std::endl;  
         }  
     } 
 }  

Save this snippet into opencl.cpp, compile and run:
$ g++ opencl.cpp -lOpenCL -o opencl && ./opencl
Platform: Intel(R) OpenCL
Device: 2 (4 means GPU, 2 means CPU)
This shows that this particular machine has one OpenCL-capable Intel CPU.

Coming up: Making your life easier and using PyOpenCL for your computations.