|
| 1 | +# Distributed-ranges tutorial |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +The distributed-ranges (dr) library is a C++20 library for multi-CPU and multi-GPU computing environments. It provides algorithms, data structures and views tailored to use in multi-node HPC systems and servers with many CPUs and/or GPUs. It takes advantage of parallel processing and MPI communication in distributed memory model as well as parallel processing in shared memory model with many GPUs. |
| 6 | +The library is designed as replacement for chosen data structures, containers, and algorithms of the C++20 Standard Template Library. If you are familiar with the C++ Template Libraries, and in particular std::ranges (C++20) or ranges-v3 (C++11 -- C++17), switching to dr will be straightforward, but this tutorial will help you get started even if you have never used them. However, we assume that you are familiar with C++, at least in the C++11 standard (C++20 is recommended). |
| 7 | + |
| 8 | +## Getting started |
| 9 | + |
| 10 | +### Prerequisites |
| 11 | + |
| 12 | +The distributed-ranges library can be used on any system with a working SYCL or g++ compiler. _Intel's DPC++ is recommended, and it is required by this tutorial_. g++ v. 10, 11 or 12 is also supported, but GPU usage is not possible. |
| 13 | +Distributed-ranges depends on MPI and oneDPL libraries. DPC++, oneDPL and oneMPI are part of the [oneAPI](whttps://www.oneapi.io/) - open-standards based industry initiative. OneAPI and the associated [Intel® oneAPI Toolkits and products](https://www.intel.com/content/www/us/en/developer/tools/oneapi/overview.html), help to provide a unified approach to mixed-architecture offload computing. Its approach also ensures interoperability with existing distributed computing standards. It is recommended to install oneAPI components before downloading distributed-ranges. |
| 14 | + |
| 15 | +### First steps |
| 16 | + |
| 17 | +Currently, there are two ways to start work with distributed-ranges. |
| 18 | + |
| 19 | +#### Users |
| 20 | + |
| 21 | +If you want to use dr in your application, and your development environment is connected to the Internet, we encourage you to clone the [distributed-ranges-tutorial repository](https://github.com/intel/distributed-ranges-tutorial) and modify examples provided. The cmake files provided in the skeleton repo will download the dr library as a source code and build the examples, there is no need for separate install. |
| 22 | + |
| 23 | +In Linux system (bash shell) download distributed-ranges-tutorial from GitHub and build with the following commands |
| 24 | + |
| 25 | + ```shell |
| 26 | + git clone https://github.com/mateuszpn/distributed-ranges-tutorial |
| 27 | + cd distributed-ranges-tutorial |
| 28 | + CXX=icpx CC=icx cmake -B build |
| 29 | + cmake --build build |
| 30 | + mpirun -n N ./build/src/example_name |
| 31 | + ``` |
| 32 | + |
| 33 | +If you have a compiler different than DPC++, change CXX and CC values respectively. |
| 34 | +Modify the call of mpirun, replacing N with number of mpi processes you want to start, and _example_name_ with an actual example name. |
| 35 | + |
| 36 | +Now you can: |
| 37 | + |
| 38 | +- modify provided examples |
| 39 | +- add new source files, modifying src/CMakeList.txt accordingly |
| 40 | +- start a new project, using the tutorial as a template |
| 41 | + |
| 42 | +In case your environment is not configured properly or you just prefer a hassle-free code exploration you can use Docker. |
| 43 | + |
| 44 | + ```shell |
| 45 | + git clone https://github.com/mateuszpn/distributed-ranges-tutorial |
| 46 | + cd distributed-ranges-tutorial |
| 47 | + docker run -it -v $(pwd):/custom-directory-name -u root docker.io/intel/oneapi:latest /bin/bash |
| 48 | + cd custom-directory-name |
| 49 | + CXX=icpx CC=icx cmake -B build -DENABLE_SYCL=ON |
| 50 | + cmake --build build -j |
| 51 | + ``` |
| 52 | + |
| 53 | +where 'custom-directory-name' stands for the name of a directory containing local repo data on a docker volume |
| 54 | + |
| 55 | +#### Contributors |
| 56 | + |
| 57 | +If you want to contribute to distributed-ranges or go through more advanced examples, please go to original [distributed-ranges GitHub repository](https://github.com/oneapi-src/distributed-ranges/) |
| 58 | + |
| 59 | + ```shell |
| 60 | + git clone https://github.com/oneapi-src/distributed-ranges |
| 61 | + cd distributed-ranges |
| 62 | + CXX=icpx CC=icx cmake -B build -DENABLE_SYCL=ON |
| 63 | + cmake --build build -j |
| 64 | + ``` |
| 65 | + |
| 66 | +## Distributed-ranges library |
| 67 | + |
| 68 | +The distributed-ranges library provides data-structures, algorithms and views designed to be used in two memory models - distributed memory and shared (common) memory. For distributed memory model, MPI is used as communication library between processes. Both model are able to use SYCL devices (GPUs and multi-core CPUs) for calculations. |
| 69 | + |
| 70 | +Algorithms and data structures are designed to take the user off the need to worry about the technical details of their parallelism. An example would be the definition of a distributed vector in memory of multiple nodes connected using MPI. |
| 71 | + |
| 72 | +```cpp |
| 73 | +dr::mhp::distributed_vector<double> dv(N); |
| 74 | +``` |
| 75 | +
|
| 76 | +Such a vector, containing N elements, is automatically distributed among all the nodes involved in the calculation, with individual nodes storing an equal (if possible) amount of data. |
| 77 | +Then again, functions such as `for_each()` or `transform()` allow you to perform in parallel operations on each element of a data structure conforming to dr. |
| 78 | +
|
| 79 | +In this way, many of the technical details related to the parallel execution of calculations can remain hidden from the user. On the other hand, a programmer aware of the capabilities of the environment in which the application is run has access to the necessary information. |
| 80 | +
|
| 81 | +### Namespaces |
| 82 | +
|
| 83 | +General namespace used in the library is `dr::` |
| 84 | +For program using a single node with shared memory available for multiple CPUs and one or more GPUs, data structures and algoritms from `dr::shp::` namespace are provided. |
| 85 | +For distributed memory model, use the `dr::mhp::` namespace. |
| 86 | +
|
| 87 | +### Data structures |
| 88 | +
|
| 89 | +Content of distributes-ranges' data structures is distributed over available nodes. For example, segments of `dr::mhp::distributed_vector` are located in memory of different nodes (mpi processes). Still, global view of the `distributed_vector` is uniform, with contigous indices. |
| 90 | +<!-- TODO: some pictures here --> |
| 91 | +
|
| 92 | +#### Halo concept |
| 93 | +
|
| 94 | +When implementing an algorithm using a distributed data structure such as `distributed_vector`, its segmented internal structure must be kept in mind. The issue comes up when the algorithm references cells adjacent to the current one, and the local loop reaches the beginning or end of the segment. At this point, the neighboring cells are in the physical memory of another node! |
| 95 | +To support this situation, the concept of halo was introduced. A halo is an area into which the contents of the edge elements of a neighboring segment are copied. Also, changes in the halo are copied to cells in the corresponding segment to maintain the consistency of the entire vector. |
| 96 | +<!-- TODO: picture here --> |
| 97 | +
|
| 98 | +### Algorithms |
| 99 | +
|
| 100 | +Follwing algorithms are included in distributed-ranges, both in mhp and shp versions: |
| 101 | +
|
| 102 | +```cpp |
| 103 | + copy() |
| 104 | + exclusive_scan() |
| 105 | + fill() |
| 106 | + for_each() |
| 107 | + inclusive_scan() |
| 108 | + iota() |
| 109 | + reduce() |
| 110 | + sort() |
| 111 | + transform() |
| 112 | +``` |
| 113 | + |
| 114 | +Refer to C++20 documentation for detailed description of how the above functions work. |
| 115 | + |
| 116 | +## Examples |
| 117 | + |
| 118 | +The examples should be compiled with SYCL compiler and run with. |
| 119 | + |
| 120 | +```shell |
| 121 | +mpirun -n N ./build/src/example_name |
| 122 | +``` |
| 123 | + |
| 124 | +where `N` - number of MPI processes. Replace _example_name_ with appropiate name of a file tu run. |
| 125 | + |
| 126 | +### Example 1 |
| 127 | + |
| 128 | +[./src/example1.cpp](src/example1.cpp) |
| 129 | + |
| 130 | +The example, performing very simple decoding of encoded string, presents copying data between local and distributed data structures, and a `for_each()` loop performing a lambda on each element of the `distributed_vector<>`. Please note, that the copy operation affects only local vector on the node 0 (the _root_ argument of `copy()` function is 0), and only the node prints the decoded message. |
| 131 | + |
| 132 | +### Example 2 |
| 133 | + |
| 134 | +[./src/example2.cpp](src/example2.cpp) |
| 135 | + |
| 136 | +The example shows the distributed nature of dr data structures. The distributed_vector has segments located in each of the nodes performing the example. The nodes introduce themselves at the beginning. You can try different numbers on MPI processes when calling `mpirun`. |
| 137 | +`iota()` function is aware what distributed_vector is, and fills the segments accordingly. Then node 0 prints out the general information about the vector, and every node presents size and content of its local part. |
| 138 | + |
| 139 | +### Example 3 |
| 140 | + |
| 141 | +[./src/example3.cpp](src/example3.cpp) |
| 142 | + |
| 143 | + The example simulates the elementary 1-d cellular automaton (ECA). Description of what the automaton is and how it works can be found in [wikipedia](https://en.wikipedia.org/wiki/Elementary_cellular_automaton). Visulisation of the automaton work is available in [ASU team webpage](https://elife-asu.github.io/wss-modules/modules/1-1d-cellular-automata). |
| 144 | + |
| 145 | + The ECA calculates the new value of a cell using old value of the cell and old values of the cell's neighbors. Therefore a halo of 1-cell width is used, to get access to neighboring cells' values when the loop eaches end of local segment of a vector. |
| 146 | + Additionally, a use of a subrange is presented, and `transform()` function, which puts transformed values of input structure to the output structure, element by element. The transforming function is given as lambda `newvalue`. |
| 147 | + _Please note: after each loop the vector content is printed with `fmt::print()`. The formatter function for `distributed_vector` is rather slow, as it gets the vector element by element, both from local node and remote nodes. You can think about customised, more effective way of results presentation._ |
| 148 | + |
| 149 | +<!-- |
| 150 | +Consider adding one more example: |
| 151 | +*Simple 2-D operation - Find a pattern in the randomly filled array* |
| 152 | +--> |
0 commit comments