Skip to content

Host and device backends in Hydra

Antonio Augusto Alves Junior edited this page Mar 24, 2018 · 2 revisions

Introduction

Here we demonstrate how to use Hydra's "backend systems" which control how Hydra's algorithms get mapped to and executed on the parallel processors available to the application. Hydra inherit and overload this functionally from Thrust. The following lines reproduce most of the contents of the Thrust's wiki page on the subject updated to reflect Hydra's behaviour.

There are two basic ways to access Hydra's systems: by specifying the global "device" system associated with types like hydra::device::vector, or by selecting a specific container associated with a particular system, such as hydra::cuda::vector. These two approaches are complementary and may be used together within the same program.

Selecting a global device system

Here, we demonstrate how to switch between the CPP (standard sequential C++), CUDA, OpenMP and TBB "device" backend systems. This is a global setting which applies to all types associated with the device system. For brevity, in the following we'll consider the phsp_averaging_functor example, which does not depends on any external non-standard library but any of the example programs would also do the same, just requiring to add more compiler and linker commands. Note that absolutely no changes to the source code are required to switch the device system.

First of all, download the files phsp_averaging_functor.inl, phsp_averaging_functor.cu and phsp_averaging_functor.cpp to a local folder, lets say "phsp".

mkdir phsp 
cd phsp
wget  https://raw.githubusercontent.com/MultithreadCorner/Hydra/master/examples/phase_space/phsp_averaging_functor.inl
wget  https://raw.githubusercontent.com/MultithreadCorner/Hydra/master/examples/phase_space/phsp_averaging_functor.cpp
wget  https://raw.githubusercontent.com/MultithreadCorner/Hydra/master/examples/phase_space/phsp_averaging_functor.cu

As for all examples, the actual code is placed in the .inl file. The .cpp and .cu files does not implements any functionality, these files only includes the .inl file.
This program calculates the average value and corresponding error of the cosine helicity angle of the K* in the the decay $B^0 \to J/\psi K \pi$ over a sample of n simulated phase-space decays. The analytically calculated value is 0.
The executable is invoked setting the command line argument -n to the number of required samples and will produce output like this

|< cos(theta_K) >(B0 -> J/psi K pi): -0.0460407 +- 0.034863
| Number of events :1000
| Time (ms)        :2.86434

Set the environment variables CC and CXX to point to compatible host compiler to process the source files using NVCC. To compile and run the source files with NVCC, issue the commands below:

CUDA

nvcc -O2 -ccbin=$CC -std=c++11  --expt-extended-lambda -I/path/to/hydra_directory/ -o phsp_averaging_functor  phsp_averaging_functor.cu -Xcompiler -fopenmp -DHYDRA_DEVICE_SYSTEM=CUDA -lgomp

OpenMP

nvcc -O2 -ccbin=$CC -std=c++11  --expt-extended-lambda -I/path/to/hydra_directory/ -o phsp_averaging_functor  phsp_averaging_functor.cu -Xcompiler -fopenmp -DHYDRA_DEVICE_SYSTEM=OMP -lgom

TBB

nvcc -O2 -ccbin=$CC -std=c++11  --expt-extended-lambda -I/path/to/hydra_directory/ -o phsp_averaging_functor  phsp_averaging_functor.cu -Xcompiler -DHYDRA_DEVICE_SYSTEM=TBB -ltbb

CPP

nvcc -O2 -ccbin=$CC -std=c++11  --expt-extended-lambda -I/path/to/hydra_directory/ -o phsp_averaging_functor  phsp_averaging_functor.cu -Xcompiler -DHYDRA_DEVICE_SYSTEM=CPP

Additional details

Using NVCC is not required to compile targets for OpenMP, TBB and CPP:

OpenMP

g++ -O2  -std=c++11  -I/path/to/hydra_directory/ -o phsp_averaging_functor  phsp_averaging_functor.cpp  -fopenmp -DHYDRA_DEVICE_SYSTEM=OMP -lgomp

TBB

g++ -O2  -std=c++11  -I/path/to/hydra_directory/ -o phsp_averaging_functor  phsp_averaging_functor.cpp -DHYDRA_DEVICE_SYSTEM=TBB -ltbb

CPP

g++ -O2  -std=c++11  -I/path/to/hydra_directory/ -o phsp_averaging_functor  phsp_averaging_functor.cpp -DHYDRA_DEVICE_SYSTEM=CPP