Psi4
parallel.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  * @END LICENSE
26  */
27 
28 /*
29  * File: parallel.h
30  * Author: jturney, jjwilke
31  *
32  * Created on December 11, 2009, 3:34 PM
33  */
34 
35 #ifndef _psi_src_lib_libparallel_parallel_h_
36 #define _psi_src_lib_libparallel_parallel_h_
37 
38 #include <vector>
39 #include <string>
41 #include "libparallel.h"
42 
43 // Use this to silence "warning: unused parameter" messages from the compiler.
44 // Sometimes you have to define the parameter for Doxygen regardless if you
45 // use it or not.
46 #ifndef UNUSED
47 #define UNUSED(expr) (void)(expr)
48 #endif
49 
50 namespace psi {
51 
52 template <typename DerivedType>
53 class Parallel {
54  public:
57  protected:
59  std::vector<std::string> CurrentComm;
60  public:
63  CurrentComm.push_back("COMM_WORLD");
64  }
65 
67  virtual ~Parallel() {
68 
69  }
70 
72  virtual void sync(const std::string& CommName="NONE") const {
73  UNUSED(CommName);
74  }
75 
93  template<typename T>
94  void all_reduce(const T* localdata, const int nelem,T* target,
95  const MPIOperation& op,
96  const std::string& Name)const{
97  static_cast<const DerivedType*>(this)->AllReduceImpl(localdata,
98  nelem,target,op, Name);
99  }
100 
111  template <class T>
112  void all_gather(const T* localdata, const int nelem, T* target,
113  const std::string& CommName="NONE") const {
114  static_cast<const DerivedType*>(this)->all_gatherImpl(localdata, nelem,
115  target, CommName);
116  }
117 
119  template <class T>
120  void gather(const T* localdata, const int nelem, T* target,const int Root,
121  const std::string& CommName="NONE") const {
122  static_cast<const DerivedType*>(this)->gatherImpl(localdata, nelem,
123  target, Root,CommName);
124  }
125 
137  template <class T>
138  void bcast(T* data, const int nelem, const int broadcaster,
139  const std::string& CommName="NONE") const {
140  static_cast<const DerivedType*>(this)->bcastImpl(data, nelem,
141  broadcaster, CommName);
142  }
143 
146  template <class T>
147  void bcast(T& data, const int broadcaster,
148  const std::string& CommName="NONE") const {
149  static_cast<const DerivedType*>(this)->bcastImpl(data,
150  broadcaster, CommName);
151  }
152 
162  virtual int Iprobe(const int Sender,const int MessageTag,
163  const std::string& Comm="NONE") const {
164  UNUSED(Sender);
165  UNUSED(MessageTag);
166  UNUSED(Comm);
167  return true;
168  }
169 
171  virtual int probe(const int Sender,const int MessageTag,
172  const std::string& Comm="NONE")const{
173  UNUSED(Sender);
174  UNUSED(MessageTag);
175  UNUSED(Comm);
176  return true;
177  }
178 
179 
191  template<typename T>
192  void Isend(const int destination, const int tag, T* message=NULL,
193  const int length=0,const std::string& Comm="NONE")const{
194  static_cast<const DerivedType*>(this)->
195  IsendImpl(destination,tag,message,length,Comm);
196  }
197 
209  template<typename T>
210  void send(const int destination, const int tag, T* message=NULL,
211  const int length=0,const std::string& Comm="NONE")const{
212  static_cast<const DerivedType*>(this)->
213  sendImpl(destination,tag,message,length,Comm);
214  }
215 
228  template<typename T>
229  void Irecv(const int source, const int tag, T* message=NULL,
230  const int length=0,const std::string& Comm="NONE")const{
231  static_cast<const DerivedType*>(this)->
232  IrecvImpl(source,tag,message,length,Comm);
233  }
234 
246  template<typename T>
247  void recv(const int source, const int tag, T* message=NULL,
248  const int length=0,const std::string& Comm="NONE")const{
249  static_cast<const DerivedType*>(this)->
250  recvImpl(source,tag,message,length,Comm);
251  }
252 
254  virtual int me(const std::string& CommName="NONE") const {
255  UNUSED(CommName);
256  return 0;
257  }
258 
260  virtual int nproc(const std::string& CommName="NONE") const {
261  UNUSED(CommName);
262  return 1;
263  }
264 
266  virtual int nthread() const {
268  }
269 
271  std::string communicator() const {
272  return CurrentComm.back();
273  }
274 
275  int thread_id(const pthread_t&) {
276  return 0;
277  }
278 
279  virtual void MakeComm(const std::string& /*Name*/, const int /*Color*/,
280  const std::string& /*Comm2Split*/="NONE"){}
281 
282  virtual void FreeComm(const std::string& /*Name*/="NONE"){}
283 };// End Parallel base class
284 }//End namespace psi
285 
286 #endif /* _psi_src_lib_libparallel_parallel_h_ */
void Irecv(const int source, const int tag, T *message=NULL, const int length=0, const std::string &Comm="NONE") const
Non-blocking receive.
Definition: parallel.h:229
void all_reduce(const T *localdata, const int nelem, T *target, const MPIOperation &op, const std::string &Name) const
Performs and all reduce.
Definition: parallel.h:94
int thread_id(const pthread_t &)
Definition: parallel.h:275
virtual int nthread() const
Needed for legacy compatibility. Don&#39;t use.
Definition: parallel.h:266
void all_gather(const T *localdata, const int nelem, T *target, const std::string &CommName="NONE") const
Function for gathering a vector whose elements lie on different MPI processes.
Definition: parallel.h:112
std::vector< std::string > CurrentComm
Array of our communicator names, in the order they are derived.
Definition: parallel.h:59
MPIOperation
The supported operations for MPI reduce-like fxns.
Definition: libparallel.h:32
void Isend(const int destination, const int tag, T *message=NULL, const int length=0, const std::string &Comm="NONE") const
Non-blocking send.
Definition: parallel.h:192
void bcast(T &data, const int broadcaster, const std::string &CommName="NONE") const
Definition: parallel.h:147
virtual ~Parallel()
No memory to free-up, does nothing.
Definition: parallel.h:67
static Environment environment
Definition: process.h:138
virtual int nproc(const std::string &CommName="NONE") const
Returns the current number of MPI processes.
Definition: parallel.h:260
virtual int probe(const int Sender, const int MessageTag, const std::string &Comm="NONE") const
Same as Iprobe, except blocking.
Definition: parallel.h:171
std::string communicator() const
Returns the current communicator.
Definition: parallel.h:271
virtual void FreeComm(const std::string &="NONE")
Definition: parallel.h:282
void gather(const T *localdata, const int nelem, T *target, const int Root, const std::string &CommName="NONE") const
Same as all_gather, except only root has copy.
Definition: parallel.h:120
Definition: parallel.h:53
virtual void MakeComm(const std::string &, const int, const std::string &="NONE")
Definition: parallel.h:279
Parallel()
Sets current comm to COMM_WORLD.
Definition: parallel.h:62
void send(const int destination, const int tag, T *message=NULL, const int length=0, const std::string &Comm="NONE") const
blocking send
Definition: parallel.h:210
Parallel< DerivedType > ThisType
This typedef is the type of this class.
Definition: parallel.h:56
#define UNUSED(expr)
Definition: parallel.h:47
int get_n_threads() const
Number of threads per process.
Definition: process.cc:185
virtual int Iprobe(const int Sender, const int MessageTag, const std::string &Comm="NONE") const
Sees if a message is available.
Definition: parallel.h:162
void bcast(T *data, const int nelem, const int broadcaster, const std::string &CommName="NONE") const
Function for broadcasting data held by one process to all other processes.
Definition: parallel.h:138
virtual void sync(const std::string &CommName="NONE") const
Provides MPI barrier functionality.
Definition: parallel.h:72
Definition: PsiFileImpl.h:39
virtual int me(const std::string &CommName="NONE") const
Returns the current MPI process number.
Definition: parallel.h:254
void recv(const int source, const int tag, T *message=NULL, const int length=0, const std::string &Comm="NONE") const
Blocking receive.
Definition: parallel.h:247