Difference between revisions of "OpenCL"

From NaplesPU Documentation
Jump to: navigation, search
Line 33: Line 33:
 
See [http://portablecl.org/docs/html/using.html] and [https://github.com/pocl/pocl/issues/282] for further informations.
 
See [http://portablecl.org/docs/html/using.html] and [https://github.com/pocl/pocl/issues/282] for further informations.
  
== Adding a new device class in pocl ==
+
== Modify pocl ==
 +
 
 +
=== Adding a new device class in pocl ===
  
 
* Create a directory for the new device class in "lib/CL/devices". In this case the "nuplus" folder is created.
 
* Create a directory for the new device class in "lib/CL/devices". In this case the "nuplus" folder is created.
 
* Create at least the files newdevice.c and newdevice.h. In this case "nuplus.c" and "nuplus.h"
 
* Create at least the files newdevice.c and newdevice.h. In this case "nuplus.c" and "nuplus.h"
 
* Create the CMakeList.txt file in the device folder, specifying the files created before and the device name.
 
* Create the CMakeList.txt file in the device folder, specifying the files created before and the device name.
 
+
*: <syntaxhighlight lang="C">
<syntaxhighlight lang="C">
+
if(MSVC)
if(MSVC)
+
set_source_files_properties( nuplus.h nuplus.c PROPERTIES LANGUAGE CXX )
  set_source_files_properties( nuplus.h nuplus.c PROPERTIES LANGUAGE CXX )
+
endif(MSVC)
endif(MSVC)
+
add_library("pocl-devices-nuplus" OBJECT nuplus.h nuplus.c)
add_library("pocl-devices-nuplus" OBJECT nuplus.h nuplus.c)
+
</syntaxhighlight>
</syntaxhighlight>
 
  
 
* Modify the "lib/CL/devices.c" file including the new header file for the device and adding the init fucntion in the vector '''pocl_devices_init_ops'''
 
* Modify the "lib/CL/devices.c" file including the new header file for the device and adding the init fucntion in the vector '''pocl_devices_init_ops'''
 +
*: <syntaxhighlight lang="C">
  
 +
# include "nuplus/nuplus.h"
  
<syntaxhighlight lang="C">
+
...
 
 
#include "nuplus/nuplus.h"
 
 
 
...
 
  
static init_device_ops pocl_devices_init_ops[] = {
+
static init_device_ops pocl_devices_init_ops[] = {
 
  pocl_pthread_init_device_ops,
 
  pocl_pthread_init_device_ops,
 
  pocl_basic_init_device_ops,
 
  pocl_basic_init_device_ops,
 
  pocl_nuplus_init_device_ops,
 
  pocl_nuplus_init_device_ops,
#if defined(TCE_AVAILABLE)
+
# if defined(TCE_AVAILABLE)
  pocl_ttasim_init_device_ops,
+
pocl_ttasim_init_device_ops,
#endif
+
# endif
#if defined(BUILD_HSA)
+
# if defined(BUILD_HSA)
  pocl_hsa_init_device_ops,
+
pocl_hsa_init_device_ops,
#endif
+
# endif
};
+
};
  
...
+
...
</syntaxhighlight>
+
</syntaxhighlight>
  
  
 
* Modify the "lib/CL/devices/CMakeLists.txt" adding the new device subdirectory name.
 
* Modify the "lib/CL/devices/CMakeLists.txt" adding the new device subdirectory name.
 +
*: <syntaxhighlight lang="C">
 +
...
  
 +
add_subdirectory("nuplus")
  
<syntaxhighlight lang="C">
+
...
...
+
</syntaxhighlight>
  
add_subdirectory("nuplus")
+
* Modify the "CMakeLists.txt" in the pocl root directory adding the new device name to the "OCL_DRIVERS".
 +
*:<syntaxhighlight lang="C">
 +
...
  
...
+
set(OCL_DRIVERS "basic pthreads nuplus")
</syntaxhighlight>
 
  
* Modify the "CMakeLists.txt" in the pocl root directory adding the new device name to the "OCL_DRIVERS".
+
...
 +
</syntaxhighlight>
  
<syntaxhighlight lang="C">
+
=== Build pocl with a custom LLVM ===
...
 
  
set(OCL_DRIVERS "basic pthreads nuplus")
+
==== Prerequisites ====
 +
Be sure that the LLVM compiler is built with all the targets and the default target is set as the host.
 +
For the nu+ toolchain the CMakeLists.txt in the LLVM root folder must be modified as reported below:
  
...
+
# Delete the code
</syntaxhighlight>
+
#: <syntaxhighlight lang="C">
 +
set(LLVM_TARGETS_TO_BUILD "NuPlus"
 +
    CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
 +
</syntaxhighlight>
 +
# modify
 +
#: <syntaxhighlight lang="C">
 +
set(LLVM_DEFAULT_TARGET_TRIPLE "nuplus-none-none" CACHE STRING
 +
</syntaxhighlight>
 +
#: in
 +
#: <syntaxhighlight lang="C">
 +
set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
 +
</syntaxhighlight>

Revision as of 12:25, 3 October 2017

The OpenCL support for the nu+ architecture is made through pocl.

How to install vanilla pocl

  1. Download following the link.
  2. In order to build pocl, you need the following support libraries and tools:
    • Latest released version of LLVM & Clang
    • GNU make
    • libtool dlopen wrapper files (e.g. libltdl3-dev in Debian)
    • pthread (should be installed by default)
    • hwloc v1.0 or newer (e.g. libhwloc-dev)
    • pkg-config
    • cmake
    • libclang-3.8-dev if you are using Ubuntu 16.04 LTS
    On Ubuntu 16.04 LTS you can run the following code on a terminal
    sudo apt-get install llvm & clang & libltdl3-dev & libhwloc-dev & libhwloc-dev & libclang-3.8-dev & make & cmake
  3. Build and install
    cd <directory-with-pocl-sources>
    mkdir build
    cd build
    cmake [-D<option>=<value> ...] ..
    make && make install

Using pocl

To compile with pocl you have to execute:

gcc example1.c -o example `pkg-config --libs --cflags pocl`

See [1] and [2] for further informations.

Modify pocl

Adding a new device class in pocl

  • Create a directory for the new device class in "lib/CL/devices". In this case the "nuplus" folder is created.
  • Create at least the files newdevice.c and newdevice.h. In this case "nuplus.c" and "nuplus.h"
  • Create the CMakeList.txt file in the device folder, specifying the files created before and the device name.
     if(MSVC)
     set_source_files_properties( nuplus.h nuplus.c PROPERTIES LANGUAGE CXX )
     endif(MSVC)
     add_library("pocl-devices-nuplus" OBJECT nuplus.h nuplus.c)
  • Modify the "lib/CL/devices.c" file including the new header file for the device and adding the init fucntion in the vector pocl_devices_init_ops
    # include "nuplus/nuplus.h"
    
     ...
    
     static init_device_ops pocl_devices_init_ops[] = {
     pocl_pthread_init_device_ops,
     pocl_basic_init_device_ops,
     pocl_nuplus_init_device_ops,
    # if defined(TCE_AVAILABLE)
     pocl_ttasim_init_device_ops,
    # endif
    # if defined(BUILD_HSA)
     pocl_hsa_init_device_ops,
    # endif
     };
    
     ...


  • Modify the "lib/CL/devices/CMakeLists.txt" adding the new device subdirectory name.
     ...
    
     add_subdirectory("nuplus")
    
     ...
  • Modify the "CMakeLists.txt" in the pocl root directory adding the new device name to the "OCL_DRIVERS".
     ...
    
     set(OCL_DRIVERS "basic pthreads nuplus")
    
     ...

Build pocl with a custom LLVM

Prerequisites

Be sure that the LLVM compiler is built with all the targets and the default target is set as the host. For the nu+ toolchain the CMakeLists.txt in the LLVM root folder must be modified as reported below:

  1. Delete the code
    set(LLVM_TARGETS_TO_BUILD "NuPlus"
        CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
  2. modify
    set(LLVM_DEFAULT_TARGET_TRIPLE "nuplus-none-none" CACHE STRING
    in
    set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING