forked from plumed/plumed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Communicator.cpp
181 lines (152 loc) · 4.88 KB
/
Communicator.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2013 The plumed team
(see the PEOPLE file at the root of the distribution for a list of names)
See http://www.plumed-code.org for more information.
This file is part of plumed, version 2.0.
plumed is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
plumed is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include <cstdlib>
#include "Communicator.h"
#include "Exception.h"
using namespace std;
namespace PLMD{
Communicator::Communicator()
#ifdef __PLUMED_MPI
: communicator(MPI_COMM_SELF)
#endif
{
}
Communicator::Communicator(const Communicator&pc){
Set_comm(pc.communicator);
}
Communicator& Communicator::operator=(const Communicator&pc){
if (this != &pc){
Set_comm(pc.communicator);
}
return *this;
}
int Communicator::Get_rank()const{
int r=0;
#ifdef __PLUMED_MPI
if(initialized()) MPI_Comm_rank(communicator,&r);
#endif
return r;
}
Communicator& Communicator::Get_world(){
static Communicator c;
#ifdef __PLUMED_MPI
if(initialized()) c.communicator=MPI_COMM_WORLD;
#endif
return c;
}
int Communicator::Get_size()const{
int s=1;
#ifdef __PLUMED_MPI
if(initialized()) MPI_Comm_size(communicator,&s);
#endif
return s;
}
void Communicator::Set_comm(MPI_Comm c){
#ifdef __PLUMED_MPI
if(initialized()){
if(communicator!=MPI_COMM_SELF && communicator!=MPI_COMM_WORLD) MPI_Comm_free(&communicator);
if(c!=MPI_COMM_SELF) MPI_Comm_dup(c,&communicator);
}
#else
(void) c;
#endif
}
Communicator::~Communicator(){
#ifdef __PLUMED_MPI
if(initialized() && communicator!=MPI_COMM_SELF && communicator!=MPI_COMM_WORLD) MPI_Comm_free(&communicator);
#endif
}
void Communicator::Set_comm(void*val){
#ifdef __PLUMED_MPI
plumed_massert(initialized(),"you are trying to use an MPI function, but MPI is not initialized");
if(val) Set_comm(*(MPI_Comm*)val);
#else
(void) val;
plumed_merror("you are trying to use an MPI function, but PLUMED has been compiled without MPI support");
#endif
}
void Communicator::Set_fcomm(void*val){
#ifdef __PLUMED_MPI
plumed_massert(initialized(),"you are trying to use an MPI function, but MPI is not initialized");
if(val){
MPI_Comm comm=MPI_Comm_f2c(*(MPI_Fint*)val);
Set_comm(comm);
}
#else
(void) val;
plumed_merror("you are trying to use an MPI function, but PLUMED has been compiled without MPI support");
#endif
}
void Communicator::Abort(int errorcode){
#ifdef __PLUMED_MPI
if(initialized()){
MPI_Abort(communicator,errorcode);
}
std::exit(errorcode);
#else
std::exit(errorcode);
#endif
}
void Communicator::Barrier()const{
#ifdef __PLUMED_MPI
if(initialized()) MPI_Barrier(communicator);
#endif
}
MPI_Comm & Communicator::Get_comm(){
return communicator;
}
bool Communicator::initialized(){
int flag=false;
#if defined(__PLUMED_MPI)
MPI_Initialized(&flag);
#endif
if(flag) return true;
else return false;
}
void Communicator::Request::wait(){
#ifdef __PLUMED_MPI
plumed_massert(initialized(),"you are trying to use an MPI function, but MPI is not initialized");
MPI_Wait(&r,MPI_STATUS_IGNORE);
#else
plumed_merror("you are trying to use an MPI function, but PLUMED has been compiled without MPI support");
#endif
}
void Communicator::Request::wait(Status&s){
#ifdef __PLUMED_MPI
plumed_massert(initialized(),"you are trying to use an MPI function, but MPI is not initialized");
MPI_Wait(&r,&s.s);
#else
(void) s;
plumed_merror("you are trying to use an MPI function, but PLUMED has been compiled without MPI support");
#endif
}
#ifdef __PLUMED_MPI
template<> MPI_Datatype Communicator::getMPIType<float>(){ return MPI_FLOAT;}
template<> MPI_Datatype Communicator::getMPIType<double>(){ return MPI_DOUBLE;}
template<> MPI_Datatype Communicator::getMPIType<int>() { return MPI_INT;}
template<> MPI_Datatype Communicator::getMPIType<char>() { return MPI_CHAR;}
template<> MPI_Datatype Communicator::getMPIType<unsigned>() { return MPI_UNSIGNED;}
template<> MPI_Datatype Communicator::getMPIType<long unsigned>() { return MPI_UNSIGNED_LONG;}
#endif
void Communicator::Split(int color,int key,Communicator&pc)const{
#ifdef __PLUMED_MPI
MPI_Comm_split(communicator,color,key,&pc.communicator);
#else
plumed_merror("you are trying to use an MPI function, but PLUMED has been compiled without MPI support");
#endif
}
}