Programowanie w systemie UNIX/OpenCL

Z Wikibooks, biblioteki wolnych podręczników.

Wymagania[edytuj]

nvidia: cuda-installation-guide-linux


Skrócona wersja

  • Wybieramy sterownik odpowiedni dla naszej karty graficznej ( GPU) nvidia: Version: 470.199.02 dla Nvidia Geoforce GTX 770
  • na podstawie GPU - Compute compatybility: GeoForce GTX 770 = 3.0


Sprzęt[edytuj]

Możemy użyć sysinfo

sysinfo

lub lshw:

sudo lshw

CPU[edytuj]

Sprawdzamy czy mamy odpowiedni CPU procesor (x86 CPUs z SSE3):[1]

cat /proc/cpuinfo | grep sse3


GPU[edytuj]

Sprawdzamy czy mamy odpowiedni GPU:[2]

Z konsoli ( CLI):[3]

  • lspci | grep VGA
  • sudo lshw -C display
  • nvidia-smi
  • grep "X Driver" /var/log/Xorg.0.log
  • modinfo /usr/lib/modules/$(uname -r)/kernel/drivers/video/nvidia.ko | grep ^version

GUI

  • NVIDIA X server settings/ System information/NVIDIA Driver Version: 470.199.02

Wybieramy sterownik odpowiedni dla naszej karty graficznej ( GPU)

  • nvidia: Version: 470.199.02 dla Nvidia Geoforce GTX 770

Oprogramowanie[edytuj]

Z punktu widzenia programisty oprogramowanie OpenCL składa się z:[4]


Pakiety :

  • icd-loader : nvidia-libopencl1
  • icd : nvidia-opencl-icd
  • headers: opencl-headers


You need to have a SDK which provides interface to CPU. nVidia does not, AMD and Intel's SDKs do; in my case the one from Intel is significantly (something like 10x) faster, which might due to bad programming on my part however.

You don't need the SDK for programs to run, just the runtime. In Linux, each vendor installs a file in /etc/OpenCL/vendors/*.icd, which contains path of the runtime library to use. That is scanned by the OpenCL runtime you link to (libOpenCL.so), which then calls each of the vendor's libs when querying for devices on that particular platform.

In Linux, the GPU drivers install OpenCL runtime automatically, the Intel runtime is likely to be downloadable separately from the SDK, but is part of the SDK as well, of course.

answered Mar 28 '12 at 19:55  eudoxos

[5]


SDK[edytuj]

Wszystkie te składniki powinien zawierać OpenCl SDK. Instalacje SDK powinna rozwiązywać problem z instalacją OpenCl. W praktyce jest to bardziej skomplikowane.

Podział[6]

  • wg platformy sprzętowej (Intel, Nvidia, AMD)
  • wg licencji
    • własnościowe: Intel SDK
    • OpenSource
      • Beignet[7]
      • Portable Computing Language
      • Clover [8]
      • Neo[9]


Istnieją SDK:

  • dla GPU firmy AMD/ATI wybieramy AMD APP SDK (wcześniej znany jako AMD Stream SDK)[10], interfejs to CPU
  • dla GPU firmy Intel (np. GPU Intel HD Graphics 4000 wbudowany w procesor CPU i7-3770K)[11], interfejs to CPU
  • dla GPU firmy Nvidia wybieramy CUDA toolkit [12], który obsługuje również OpenCL[13], nie ma interfejsu do CPU[14]
  • Nie ma SDK Khronos[15]

OS[edytuj]

Wybieramy taką wersję OS dla której jest dostępny CUDA Toolkit[12]. Aktualnie np. 64-bit Ubuntu 12.04 LTS[16]


Sterowniki GPU[edytuj]

Instalujemy sterowniki odpowiednie do posiadanego sprzętu

Nvidia[edytuj]

Opis: [16]

Blacklist the open source driver (IMPORTANT)
sudo nano /etc/modprobe.d/blacklist.conf
Add the line: blacklist nouveau
Install some dependencies
sudo apt-get install build-essential linux-header-generic opencl-headers
Download the NVIDIA driver from their website and unpack the download
In a virtual terminal (Ctrl+Alt+F1), stop the windows manager
sudo service lightdm stop
Give the script run permissions then run it
chmod +x *.run
sudo ./*.run
The pre-install test will fail – this is OK!
Say yes to DKMS, 32-bit GL libraries and to update your X config
Reboot!

Opis [17][18][19][20]


Problemy:

  • /var/log/cuda-installer.log
  • /var/log/nvidia-installer.log

Pliki nagłówkowe[edytuj]

Instalacja plików nagłówkowych w Ubuntu 13.04:

 sudo apt-get install ocl-icd-opencl-dev

Pliki nagłówkowe Khronos (wspólne dla wszystkich SDK):

Wszystkie pliki nagłówkowe powinny być w katalogu CL/

Plik opencl.h dołącza (ang. includes) pozostałe pliki nagłówkowe.


Pakiety :

  • opencl-headers

Biblioteki[edytuj]

podstawowe[edytuj]

OpenCL.lib


Dla Nvidia i 64 bitowego systemu:

echo "/usr/local/cuda/lib64" > /etc/ld.so.conf.d/opencl-vendor-nvidia.conf
echo "/usr/local/cuda/lib" >> /etc/ld.so.conf.d/opencl-vendor-nvidia.conf

i

ldconfig

dodatkowe[edytuj]

  • easyCL [23]
  • dynamic scheduling (gpu cpu) [24]
    • StarPU: A Unified Runtime System for Heterogeneous Multicore Architectures[25]

libOpenCL.so[edytuj]

Biblioteka współdzielona (rozszerzenie so) libOpenCL.so = ICD loader

Lokalizacja biblioteki zależy od rodzaju zainstalowanego SDK [26]


locate libOpenCL.so

Przykładowy wynik:

/usr/lib/libOpenCL.so
/usr/lib/nvidia-current/libOpenCL.so
/usr/lib/nvidia-current/libOpenCL.so.1
/usr/lib/nvidia-current/libOpenCL.so.1.0
/usr/lib/nvidia-current/libOpenCL.so.1.0.0
/usr/lib32/libOpenCL.so
/usr/lib32/nvidia-current/libOpenCL.so
/usr/lib32/nvidia-current/libOpenCL.so.1
/usr/lib32/nvidia-current/libOpenCL.so.1.0
/usr/lib32/nvidia-current/libOpenCL.so.1.0.0

Używamy tylko jednego loadera (najlepiej Khronos, nawet jeśli mamy Nvidia!):[4]

sudo ln -s /usr/lib64/libOpenCL.so /usr/lib64/libOpenCL.so.1.2
sudo ln -s /usr/lib/libOpenCL.so /usr/lib/libOpenCL.so.1.2


Linki:

  • OpenCL ICD Loader [27]

icd[edytuj]

(do przetłumaczenia) "ICD" to plik w systemach Linux ze ścieżką i nazwą pliku biblioteki .so.


"OpenCL ICD: Installable Client Driver. Vendor-provided component that interfaces between the OpenCL library and the actual hardware. Example:

  • NVIDIA CUDA also offers an OpenCL ICD, allowing OpenCL access to its GPUs.
  • AMD offers an OpenCL ICD as part of its Accelerated Parallel Processing (APP) (formerly known as ATI Stream) solution, allowing OpenCL access to AMD and ATI video cards as well as x86 and x86-64 CPUs
  • Intel offers an OpenCL ICD for its CPUs and for the Sandy Bridge (integrated CPU/GPU)
  • Khronos [28]

Multiple ICDs can coexist on the same machine, the programmer can choose which one(s) to use."[29]

Pliki icd powinny być w katalogu: [4]

/etc/OpenCL/vendor/


Lista zainstalowanych platform OpenCL:[30]

ls /etc/OpenCL/vendors

przykładowy wynik:

nvidia.icd

dodatkowe programy[edytuj]

  • przykłady OpenCl wykorzystują Glut dla tworzenia GUI. Zainstaluj glut3-dev.[31]
  • gcc w wersji >= 4.7
  • pyopencl, numpy

IDE[edytuj]


toolkit[edytuj]

nvcc -V


Command 'nvcc' not found, but can be installed with:
sudo apt install nvidia-cuda-toolkit

Test[edytuj]

 apt-cache search opencl

clinfo[edytuj]

 sudo apt-get install clinfo
 sudo clinfo


Przykładowy wynik:

Number of platforms                               1
  Platform Name                                   NVIDIA CUDA
  Platform Vendor                                 NVIDIA Corporation
  Platform Version                                OpenCL 1.2 CUDA 8.0.20
  Platform Profile                                FULL_PROFILE
  Platform Extensions                             cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options        cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts
  Platform Extensions function suffix             NV

  Platform Name                                   NVIDIA CUDA
Number of devices                                 1
  Device Name                                     GeForce GTX 770
  Device Vendor                                   NVIDIA Corporation
  Device Vendor ID                                0x10de
  Device Version                                  OpenCL 1.2 CUDA
  Driver Version                                  361.42
  Device OpenCL C Version                         OpenCL C 1.2 
  Device Type                                     GPU
  Device Profile                                  FULL_PROFILE
  Device Topology (NV)                            PCI-E, 01:00.0
  Max compute units                               8
  Max clock frequency                             1189MHz
  Compute Capability (NV)                         3.0
  Device Partition                                (core)
    Max number of sub-devices                     1
    Supported partition types                     None
  Max work item dimensions                        3
  Max work item sizes                             1024x1024x64
  Max work group size                             1024
  Preferred work group size multiple              32
  Warp size (NV)                                  32
  Preferred / native vector sizes                 
    char                                                 1 / 1       
    short                                                1 / 1       
    int                                                  1 / 1       
    long                                                 1 / 1       
    half                                                 0 / 0        (n/a)
    float                                                1 / 1       
    double                                               1 / 1        (cl_khr_fp64)
  Half-precision Floating-point support           (n/a)
  Single-precision Floating-point support         (core)
    Denormals                                     Yes
    Infinity and NANs                             Yes
    Round to nearest                              Yes
    Round to zero                                 Yes
    Round to infinity                             Yes
    IEEE754-2008 fused multiply-add               Yes
    Support is emulated in software               No
    Correctly-rounded divide and sqrt operations  Yes
  Double-precision Floating-point support         (cl_khr_fp64)
    Denormals                                     Yes
    Infinity and NANs                             Yes
    Round to nearest                              Yes
    Round to zero                                 Yes
    Round to infinity                             Yes
    IEEE754-2008 fused multiply-add               Yes
    Support is emulated in software               No
    Correctly-rounded divide and sqrt operations  No
  Address bits                                    64, Little-Endian
  Global memory size                              2146762752 (1.999GiB)
  Error Correction support                        No
  Max memory allocation                           536690688 (511.8MiB)
  Unified memory for Host and Device              No
  Integrated memory (NV)                          No
  Minimum alignment for any data type             128 bytes
  Alignment of base address                       4096 bits (512 bytes)
  Global Memory cache type                        Read/Write
  Global Memory cache size                        131072
  Global Memory cache line                        128 bytes
  Image support                                   Yes
    Max number of samplers per kernel             32
    Max size for 1D images from buffer            134217728 pixels
    Max 1D or 2D image array size                 2048 images
    Max 2D image size                             16384x16384 pixels
    Max 3D image size                             4096x4096x4096 pixels
    Max number of read image args                 256
    Max number of write image args                16
  Local memory type                               Local
  Local memory size                               49152 (48KiB)
  Registers per block (NV)                        65536
  Max constant buffer size                        65536 (64KiB)
  Max number of constant args                     9
  Max size of kernel argument                     4352 (4.25KiB)
  Queue properties                                
    Out-of-order execution                        Yes
    Profiling                                     Yes
  Prefer user sync for interop                    No
  Profiling timer resolution                      1000ns
  Execution capabilities                          
    Run OpenCL kernels                            Yes
    Run native kernels                            No
    Kernel execution timeout (NV)                 Yes
  Concurrent copy and kernel execution (NV)       Yes
    Number of async copy engines                  1
  printf() buffer size                            1048576 (1024KiB)
  Built-in kernels                                
  Device Available                                Yes
  Compiler Available                              Yes
  Linker Available                                Yes
  Device Extensions                               cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts

NULL platform behavior
  clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...)  NVIDIA CUDA
  clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...)   Success [NV]
  clCreateContext(NULL, ...) [default]            Success [NV]
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU)  No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU)  No platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR)  No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM)  No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL)  No platform

ICD loader properties
  ICD loader Name                                 OpenCL ICD Loader
  ICD loader Vendor                               OCL Icd free software
  ICD loader Version                              2.2.8
  ICD loader Profile                              OpenCL 1.2
	NOTE:	your OpenCL library declares to support OpenCL 1.2,
		but it seems to support up to OpenCL 2.1 too.

benchmark-all.py[edytuj]

Po zainstalowaniu pakietu dla Pythona: PyOpenCL ( python-pyopencl) możemy przetestować system:[36]

python /usr/share/doc/python-pyopencl/examples/benchmark-all.py

clDeviceQuery.cpp[edytuj]

Jest to mały program, który raportuje dostępne platformy OpenCL na Twojej maszynie oraz wszystkie ich urządzenia.[37]

  1. Skompiluj clDeviceQuery.cpp za pomocą g++
  2. Uruchom program
 g++ -o clDeviceQuery -I/opt/intel/opencl-1.2-3.0.67279/include clDeviceQuery.cpp -lOpenCL
 ./clDeviceQuery

Musisz mieć:

  • pliki nagłówkowe OpenCL w ścieżce include path
  • libOpenCL.so w LD_LIBRARY_PATH (Po załadowaniu libOpenCL.so wykorzystywany jest rejestr ICD do znalezienia wszystkich implementacji dla producentów, które nie muszą się już znajdować w LD_LIBRARY_PATH.)

Run[edytuj]

Jak uruchomić zestaw narzędzi NVidia-CUDA-Toolkit? [38]

  1. zainstaluj aktualny toolkit
  2. wyłącz X-server
  3. zaloguj się do swojego konta
  4. przejdź do folderu toolkit
  5. uruchom toolkit
  6. reboot
 sudo service gdm stop
 # Ctrl + Alt + F1
 # log in 
 # cd'd to the folder where the toolkit was and ran it:
 sudo sh "name of toolkit".run
 sudo reboot

Kompilacja[edytuj]

W pliku e.c dodajemy:

#include "CL/cl.h"

Kompilacja z użyciem standardowego kompilatora:

gcc e.c -lOpenCL


wersja[edytuj]

//https://stackoverflow.com/questions/54148144/checking-opencl-version-in-c

 #include <CL/cl.h>
 #include <stdio.h>

 int main(int argc, char *argv[])
 {
     char *driver_version;
     clGetDeviceInfo(0, CL_DRIVER_VERSION, sizeof(char*), &driver_version, NULL);
     printf("%s\n", driver_version);
     return 0;

Przykłady[edytuj]

Odnośniki[edytuj]

  1. OpenCL Hardware-support by Vincent HindriksenDecember 29, 2011
  2. OpenCl Conformant Products
  3. linuxconfig : how-to-check-nvidia-driver-version-on-your-linux-system
  4. 4,0 4,1 4,2 Install OpenCL on Debian, Ubuntu and Mint orderly
  5. Stackoverflow.com: Need to install opencl for CPU and GPU platforms?
  6. stackoverflow questions: hat-is-a-example-of-a-target-platform-in-opencl
  7. Beignet
  8. clover
  9. Neo
  10. AMD APP SDK
  11. Intel® SDK for OpenCL
  12. 12,0 12,1 CUDA Toolkit
  13. Unable to find Nvidia OpenCL SDK
  14. Stackoverflow: Need to install opencl for CPU and GPU platforms?
  15. How to obtain OpenCL SDK?
  16. 16,0 16,1 Hands On OpenCL, by Simon McIntosh-Smith and Tom Deakin
  17. Opis instalacji na Ubuntu
  18. Instalacja OpenCl na Ubuntu
  19. NVIDIA CUDA Getting Started Guide for Linux
  20. A Tutorial for using OpenCL in ImageJ
  21. Khronos OpenCL Registry
  22. “khronos-opencl-headers” package in Ubuntu
  23. EasyCL w Github
  24. OpenCL: Running CPU/GPU multiple devices
  25. StarPU: A Unified Runtime System for Heterogeneous Multicore Architectures
  26. The OpenCL Programming Book - pierwszy program
  27. OpenCL ICD Loader
  28. cl_khr_icd.txt
  29. OpenCL by Giuseppe Bilotta, Eugenio Rustico, Alexis Hérault
  30. PClab : Akceleracja GPU w praktyce – OpenCL. Drzemiące megaflopy. Mateusz Brzostek, Poniedziałek, 21 stycznia 2013
  31. OpenCL Ubuntu Install Guide Oct24 by Brandon A. Mayer
  32. OpenCL studio
  33. User Manual for Intel® SDK for OpenCL™ Applications
  34. OpenCL on Linux
  35. OpenCL experiments
  36. PyOpenCL dla Ubuntu >= 10.10
  37. Installing Intel CPU OpenCL on Ubuntu (12.04)
  38. Ask ubuntu: OpenCL on Ubuntu 14.04
  39. OpenCL programming book
  40. MandelCPU vs MandelGPU Written by David Bucciarelli
  41. MandelGPU (OpenCL) - wideo
  42. OpenCl examples by Eric Bainville.
  43. materiały Krzysztofa Rządcy
  44. Przykłady programów OpenCL Krzysztofa Rządcy
  45. Wstęp do OpenCL - Marek Zając