KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
mpi_utils.cpp
Go to the documentation of this file.
1#include "mpi_utils.h"
2
3#include <tuple>
4
5namespace PROJECT_NS {
6MPI_Guard::MPI_Guard(std::size_t TOTAL) : TOTAL{TOTAL} {
7 MPI_Init(NULL, NULL);
8 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
9 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
11 isroot = (rank == 0);
12}
13
14int MPI_Guard::range(const size_t& idx1, const size_t& idx2, size_t& ista, size_t& iend) {
15 int num = (idx2 - idx1) / nprocs;
16 ista = idx1 + rank * num;
17 iend = (rank == nprocs - 1) ? idx2 : ista + num;
18 return 0;
19}
20
21/*
22void collect(std::vector<std::tuple<void*, void*, kids_dtype, kids_option, size_t, size_t>>& list, size_t ncalls) {
23 for (auto& term : list) {
24 auto [fromdata_raw, todata_raw, dataType, ruleOption, startidx, datasize] = term; //
25 switch (dataType) {
26 case kids_real_type: {
27 kids_real* fromdata = (kids_real*) fromdata_raw;
28 kids_real* todata = (kids_real*) todata_raw;
29 switch (ruleOption) {
30 case kids_copy: {
31 for (int i = startidx, cnt = 0; cnt < datasize; ++cnt, ++i) todata[i] = fromdata[i];
32 break;
33 }
34 case kids_sum: {
35 for (int i = startidx, cnt = 0; cnt < datasize; ++cnt, ++i) todata[i] += fromdata[i];
36 break;
37 }
38 case kids_mean: {
39 kids_real k1 = ncalls / (kids_real)(ncalls + 1);
40 kids_real k2 = 1.0e0 - k1;
41 for (int i = startidx, cnt = 0; cnt < datasize; ++cnt, ++i)
42 todata[i] = k1 * todata[i] + k2 * fromdata[i];
43 break;
44 }
45 }
46 break;
47 }
48 case kids_complex_type: {
49 kids_complex* fromdata = (kids_real*) fromdata_raw;
50 kids_complex* todata = (kids_real*) todata_raw;
51 switch (ruleOption) {
52 case kids_copy: {
53 for (int i = startidx, cnt = 0; cnt < datasize; ++cnt, ++i) todata[i] = fromdata[i];
54 break;
55 }
56 case kids_sum: {
57 for (int i = startidx, cnt = 0; cnt < datasize; ++cnt, ++i) todata[i] += fromdata[i];
58 break;
59 }
60 case kids_mean: {
61 kids_real k1 = ncalls / (kids_real)(ncalls + 1);
62 kids_real k2 = 1.0e0 - k1;
63 for (int i = startidx, cnt = 0; cnt < datasize; ++cnt, ++i)
64 todata[i] = k1 * todata[i] + k2 * fromdata[i];
65 break;
66 }
67 }
68 break;
69 }
70 }
71 }
72}
73*/
74
75int MPI_Guard::reduce(const std::vector<std::tuple<kids_dtype, void*, void*, std::size_t>>& info_list) {
76 kids_dtype dtype;
77 void * from_data, *to_data;
78 std::size_t ndata;
79 for (auto&& info : info_list) {
80 std::tie(dtype, from_data, to_data, ndata) = info;
81 switch (dtype) {
82 case kids_int_type: {
83 MPI_Reduce((kids_int*) from_data, (kids_int*) to_data, //
84 ndata, MPI::INT, MPI_SUM, 0, MPI_COMM_WORLD);
85 break;
86 }
87 case kids_real_type: {
88 MPI_Reduce((kids_real*) from_data, (kids_real*) to_data, //
89 ndata, MPI::DOUBLE_PRECISION, MPI_SUM, 0, MPI_COMM_WORLD);
90 break;
91 }
92 case kids_complex_type: {
93 MPI_Reduce((kids_complex*) from_data, (kids_complex*) to_data, //
94 ndata, MPI::DOUBLE_COMPLEX, MPI_SUM, 0, MPI_COMM_WORLD);
95 break;
96 }
97 }
98 }
99 return 0;
100}
101
102int MPI_Guard::reduce(const std::tuple<kids_dtype, void*, void*, std::size_t>& info) {
103 // kids_dtype dtype;
104 // void * from_data, *to_data;
105 // std::size_t ndata;
106 // std::tie(dtype, from_data, to_data, ndata) = info;
107 auto [dtype, from_data, to_data, ndata] = info;
108 switch (dtype) {
109 case kids_int_type: {
110 MPI_Reduce((kids_int*) from_data, (kids_int*) to_data, //
111 ndata, MPI::INT, MPI_SUM, 0, MPI_COMM_WORLD);
112 break;
113 }
114 case kids_real_type: {
115 MPI_Reduce((kids_real*) from_data, (kids_real*) to_data, //
116 ndata, MPI::DOUBLE_PRECISION, MPI_SUM, 0, MPI_COMM_WORLD);
117 break;
118 }
119 case kids_complex_type: {
120 MPI_Reduce((kids_complex*) from_data, (kids_complex*) to_data, //
121 ndata, MPI::DOUBLE_COMPLEX, MPI_SUM, 0, MPI_COMM_WORLD);
122 break;
123 }
124 }
125
126 return 0;
127}
128
129
130MPI_Guard::~MPI_Guard() { MPI_Finalize(); }
131
132
133int MPI_Guard::rank = 0;
134int MPI_Guard::nprocs = 1;
135bool MPI_Guard::isroot = true;
136
137}; // namespace PROJECT_NS
static int reduce(const std::tuple< kids_dtype, void *, void *, std::size_t > &info)
static int range(const size_t &idx1, const size_t &idx2, size_t &ista, size_t &iend)
Definition mpi_utils.cpp:14
MPI_Guard(std::size_t TOTAL)
Definition mpi_utils.cpp:6
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
@ kids_real_type
Represents real number type.
Definition Types.h:47
@ kids_complex_type
Represents complex number type.
Definition Types.h:48
@ kids_int_type
Represents integer type.
Definition Types.h:46
double kids_real
Alias for real number type.
Definition Types.h:59
std::complex< double > kids_complex
Alias for complex number type.
Definition Types.h:60
int kids_int
Alias for integer type.
Definition Types.h:57