All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GREX.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "GREX.h"
23 #include "PlumedMain.h"
24 #include "Atoms.h"
25 #include "tools/Tools.h"
26 #include "tools/Communicator.h"
27 #include <sstream>
28 
29 using namespace std;
30 namespace PLMD{
31 
32 GREX::GREX(PlumedMain&p):
33  initialized(false),
34  intracomm(*new Communicator),
35  intercomm(*new Communicator),
36  plumedMain(p),
37  atoms(p.getAtoms()),
38  partner(-1), // = unset
39  localDeltaBias(0),
40  foreignDeltaBias(0),
41  localUNow(0),
42  localUSwap(0),
43  myreplica(-1) // = unset
44 {
45  p.setSuffix(".NA");
46 }
47 
49  delete &intercomm;
50  delete &intracomm;
51 }
52 
53 #define CHECK_INIT(ini,word) plumed_massert(ini,"cmd(\"" + word +"\") should be only used after GREX initialization")
54 #define CHECK_NOTINIT(ini,word) plumed_massert(!(ini),"cmd(\"" + word +"\") should be only used before GREX initialization")
55 #define CHECK_NULL(val,word) plumed_massert(val,"NULL pointer received in cmd(\"GREX " + word + "\")");
56 
57 void GREX::cmd(const string&key,void*val){
58  if(false){
59  }else if(key=="initialized"){
60  CHECK_NULL(val,key);
61  *static_cast<int*>(val)=initialized;
62  }else if(key=="setMPIIntracomm"){
64  intracomm.Set_comm(val);
65  }else if(key=="setMPIIntercomm"){
67  intercomm.Set_comm(val);
69  }else if(key=="setMPIFIntracomm"){
71  intracomm.Set_fcomm(val);
72  }else if(key=="setMPIFIntercomm"){
74  intercomm.Set_fcomm(val);
76  }else if(key=="init"){
78  initialized=true;
79  std::string s;
80 // note that for PEs!=root this is automatically 0 (comm defaults to MPI_COMM_SELF)
84  plumedMain.setSuffix("."+s);
85  }else if(key=="prepare"){
87  if(intracomm.Get_rank()==0) return;
88  intracomm.Bcast(&partner,1,0);
89  calculate();
90  }else if(key=="setPartner"){
92  partner=*static_cast<int*>(val);
93  }else if(key=="savePositions"){
95  savePositions();
96  }else if(key=="calculate"){
98  if(intracomm.Get_rank()!=0) return;
99  intracomm.Bcast(&partner,1,0);
100  calculate();
101  }else if(key=="getLocalDeltaBias"){
102  CHECK_INIT(initialized,key);
103  CHECK_NULL(val,key);
105  atoms.double2MD(x,val);
106  }else if(key=="cacheLocalUNow"){
107  CHECK_INIT(initialized,key);
108  CHECK_NULL(val,key);
109  double x;
110  atoms.MD2double(val,x);
112  intracomm.Sum(&localUNow,1);
113  }else if(key=="cacheLocalUSwap"){
114  CHECK_INIT(initialized,key);
115  CHECK_NULL(val,key);
116  double x;
117  atoms.MD2double(val,x);
120  }else if(key=="getForeignDeltaBias"){
121  CHECK_INIT(initialized,key);
122  CHECK_NULL(val,key);
124  atoms.double2MD(x,val);
125  }else if(key=="shareAllDeltaBias"){
126  CHECK_INIT(initialized,key);
127  if(intracomm.Get_rank()!=0) return;
128  allDeltaBias.assign(intercomm.Get_size(),0.0);
131  }else{
132 // multi word commands
133  std::vector<std::string> words=Tools::getWords(key);
134  int nw=words.size();
135  if(false){
136  } else if(nw==2 && words[0]=="getDeltaBias"){
137  CHECK_INIT(initialized,key);
138  CHECK_NULL(val,key);
139  plumed_massert(allDeltaBias.size()==static_cast<unsigned>(intercomm.Get_size()),
140  "to retrieve bias with cmd(\"GREX getDeltaBias\"), first share it with cmd(\"GREX shareAllDeltaBias\")");
141  unsigned rep;
142  Tools::convert(words[1],rep);
143  plumed_massert(rep<allDeltaBias.size(),"replica index passed to cmd(\"GREX getDeltaBias\") is out of range");
145  atoms.double2MD(d,val);
146  } else{
147  plumed_merror("cannot interpret cmd(\"GREX " + key + "\"). check plumed developers manual to see the available commands.");
148  };
149  };
150 }
151 
154  plumedMain.resetActive(true);
155  atoms.shareAll();
157  ostringstream o;
158  atoms.writeBinary(o);
159  buffer=o.str();
160 }
161 
163 //fprintf(stderr,"CALCULATE %d %d\n",intercomm.Get_rank(),partner);
164  unsigned nn=buffer.size();
165  vector<char> rbuf(nn);
167  if(intracomm.Get_rank()==0){
168  Communicator::Request req=intercomm.Isend(&buffer.c_str()[0],nn,partner,1066);
169  intercomm.Recv(&rbuf[0],rbuf.size(),partner,1066);
170  req.wait();
171  }
172  intracomm.Bcast(&rbuf[0],nn,0);
173  istringstream i(string(&rbuf[0],rbuf.size()));
174  atoms.readBinary(i);
181  if(intracomm.Get_rank()==0){
184  req.wait();
185 //fprintf(stderr,">>> %d %d %20.12f %20.12f %20.12f %20.12f\n",intercomm.Get_rank(),partner,localDeltaBias,foreignDeltaBias,localUSwap,localUNow);
186  }
188 }
189 
190 }
Communicator & intracomm
Definition: GREX.h:39
#define CHECK_NOTINIT(ini, word)
Definition: GREX.cpp:54
void calculate()
Definition: GREX.cpp:162
Atoms & atoms
Definition: GREX.h:42
#define CHECK_INIT(ini, word)
Definition: GREX.cpp:53
void setExchangeStep(bool f)
Set exchangeStep flag.
Definition: PlumedMain.h:308
Communicator & intercomm
Definition: GREX.h:40
void Bcast(T *, int, int)
Definition: Communicator.h:134
double foreignDeltaBias
Definition: GREX.h:45
void waitData()
Scatters the needed atoms.
Definition: PlumedMain.cpp:500
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
Communicator & multi_sim_comm
Definition: PlumedMain.h:78
void writeBinary(std::ostream &) const
Definition: Atoms.cpp:394
Class containing wrappers to MPI.
Definition: Communicator.h:44
STL namespace.
void savePositions()
Definition: GREX.cpp:152
void justCalculate()
Perform the forward loop on active actions.
Definition: PlumedMain.cpp:508
void shareAll()
Definition: Atoms.cpp:138
double localDeltaBias
Definition: GREX.h:44
void double2MD(const double &d, void *m) const
Definition: Atoms.cpp:314
std::vector< double > allDeltaBias
Definition: GREX.h:48
#define CHECK_NULL(val, word)
Definition: GREX.cpp:55
std::string buffer
Definition: GREX.h:49
int Get_rank() const
Obtain the rank of the present process.
void readBinary(std::istream &)
Definition: Atoms.cpp:400
void prepareDependencies()
Prepare the list of active Actions and needed atoms.
Definition: PlumedMain.cpp:449
static std::vector< std::string > getWords(const std::string &line, const char *sep=NULL, int *parlevel=NULL, const char *parenthesis="{")
Split the line in words using separators.
Definition: Tools.cpp:112
const double & getEnergy() const
Get energy units as double.
Definition: Units.h:90
void setSuffix(const std::string &)
Set the suffix string.
Definition: PlumedMain.h:298
void Recv(T *, int, int, int, Status &)
Definition: Communicator.h:200
double getBias() const
get the value of the bias
Definition: PlumedMain.cpp:633
const Units & getMDUnits()
Definition: Atoms.h:179
const Units & getUnits()
Definition: Atoms.h:181
bool initialized
Definition: GREX.h:38
Main plumed object.
Definition: PlumedMain.h:71
void Set_comm(MPI_Comm)
Set from a real MPI communicator.
void resetActive(bool active)
Enforce active flag.
Definition: PlumedMain.h:318
int Get_size() const
Obtain the number of processes.
void Set_fcomm(void *comm)
Set from a pointer to a real MPI communicator (FORTRAN)
int partner
Definition: GREX.h:43
void cmd(const std::string &key, void *val=NULL)
This has to be implemented in daughter classes.
Definition: GREX.cpp:57
~GREX()
Definition: GREX.cpp:48
void Sum(T *, int)
Wrapper for MPI_Allreduce with MPI_SUM.
Definition: Communicator.h:124
double localUNow
Definition: GREX.h:46
int myreplica
Definition: GREX.h:50
Request Isend(const T *, int, int, int)
Definition: Communicator.h:183
void MD2double(const void *m, double &d) const
Definition: Atoms.cpp:311
double localUSwap
Definition: GREX.h:47
PlumedMain & plumedMain
Definition: GREX.h:41