TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ex6.cpp
1#include <nda/nda.hpp>
2#include <nda/mpi.hpp>
3#include <mpi/mpi.hpp>
4#include <iostream>
5
6template <typename A>
7void print(const A &arr, const mpi::communicator &comm) {
8 std::cout << "Rank " << comm.rank() << ": " << arr << std::endl;
9}
10
11int main(int argc, char *argv[]) {
12 // initialize MPI environment and communicator
13 mpi::environment env(argc, argv);
14 mpi::communicator comm;
15 const int root = 0;
16
17 // create an array and initialize it on root
19 if (comm.rank() == root) {
20 A.resize(2, comm.size());
21 for (int i = 0; auto &x : A) x = i++;
22 }
23 print(A, comm);
24 comm.barrier();
25
26 // broadcast the array from root to all other ranks
27 mpi::broadcast(A, comm);
28 print(A, comm);
29 comm.barrier();
30
31 // prepare the array
32 A = 0;
33 if (comm.rank() == root) {
34 A(nda::range::all, root) = 1;
35 }
36 print(A, comm);
37 comm.barrier();
38
39 // broadcast the first column from root to all other ranks
40 auto A_v = A(nda::range::all, comm.rank());
41 mpi::broadcast(A_v, comm);
42 print(A, comm);
43 comm.barrier();
44
45 // prepare the array to be gathered
46 auto B = nda::array<int, 1>(comm.rank() + 1);
47 B() = comm.rank();
48 print(B, comm);
49 comm.barrier();
50
51 // gather the arrays on root
52 auto B_g = mpi::gather(B, comm, root);
53 print(B_g, comm);
54 comm.barrier();
55
56 // all-gather the arrays
57 auto B_g_all = mpi::all_gather(B, comm);
58 (void)B_g_all;
59
60 // resize and reshape the arrays and gather the resulting views
61 B.resize(4);
62 B = comm.rank();
63 auto B_r = nda::reshape(B, 2, 2);
64 auto B_rg = mpi::gather(B_r, comm, root);
65 print(B_rg, comm);
66 comm.barrier();
67
68 // gather Fortran-layout arrays
69 auto B_f = nda::array<int, 2, nda::F_layout>(2, 2);
70 B_f = comm.rank();
71 auto B_fg = mpi::gather(nda::transpose(B_f), comm, root);
72 print(B_fg, comm);
73 comm.barrier();
74
75 // transpose the result
76 print(nda::transpose(B_fg), comm);
77 comm.barrier();
78
79 // scatter an array from root to all other ranks
80 nda::array<int, 2> C = mpi::scatter(B_rg, comm, root);
81 print(C, comm);
82 comm.barrier();
83
84 // scatter an array with extents not divisible by the number of ranks
85 auto C_s = mpi::scatter(C, comm, 2);
86 print(C_s, comm);
87 comm.barrier();
88
89 // reduce an array on root using MPI_SUM
90 auto D = mpi::reduce(C, comm, root);
91 print(D, comm);
92 comm.barrier();
93
94 // all-reduce an array in-place
95 mpi::all_reduce_in_place(C, comm);
96 print(C, comm);
97 comm.barrier();
98
99 // scatter a view into an existing array
100 auto C_into = nda::array<int, 2>(2, 2);
101 mpi::scatter_into(B_rg, C_into, comm, root);
102 print(C_into, comm);
103 comm.barrier();
104}
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
auto reshape(A &&a, std::array< Int, R > const &new_shape)
Reshape an nda::basic_array or nda::basic_array_view.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
Includes all MPI relevant headers.
Includes all relevant headers for the core nda library.