forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_common.cpp
More file actions
111 lines (93 loc) · 2.03 KB
/
Copy pathparallel_common.cpp
File metadata and controls
111 lines (93 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "parallel_common.h"
#include "source_base/parallel_reduce.h"
#ifdef __MPI
#include <mpi.h>
#endif
namespace Parallel_Common
{
#ifdef __MPI
/// Broadcast a trivially-copyable buffer of type T on MPI_COMM_WORLD from
/// rank 0. This is the single implementation behind all bcast_* wrappers.
template <typename T>
static void bcast_world_impl(T* object, const int n)
{
MPI_Bcast(object, n, Parallel_Reduce::MPI_Type<T>::value, 0, MPI_COMM_WORLD);
}
#endif
void bcast_string(std::string& object) // Peize Lin fix bug 2019-03-18
{
#ifdef __MPI
int size = object.size();
MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (0 != my_rank)
{
object.resize(size);
}
MPI_Bcast(&object[0], size, MPI_CHAR, 0, MPI_COMM_WORLD);
#endif
return;
}
void bcast_string(std::string* object, const int n) // Peize Lin fix bug 2019-03-18
{
#ifdef __MPI
for (int i = 0; i < n; i++)
bcast_string(object[i]);
#endif
return;
}
void bcast_complex_double(std::complex<double>& object)
{
#ifdef __MPI
bcast_world_impl(&object, 1);
#endif
}
void bcast_complex_double(std::complex<double>* object, const int n)
{
#ifdef __MPI
bcast_world_impl(object, n);
#endif
}
void bcast_double(double& object)
{
#ifdef __MPI
bcast_world_impl(&object, 1);
#endif
}
void bcast_double(double* object, const int n)
{
#ifdef __MPI
bcast_world_impl(object, n);
#endif
}
void bcast_int(int& object)
{
#ifdef __MPI
bcast_world_impl(&object, 1);
#endif
}
void bcast_int(int* object, const int n)
{
#ifdef __MPI
bcast_world_impl(object, n);
#endif
}
void bcast_bool(bool& object)
{
#ifdef __MPI
int swap = object;
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Bcast(&swap, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (my_rank != 0)
object = static_cast<bool>(swap);
#endif
}
void bcast_char(char* object, const int n)
{
#ifdef __MPI
MPI_Bcast(object, n, MPI_CHAR, 0, MPI_COMM_WORLD);
#endif
}
} // namespace Parallel_Common