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 file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 /*
30  * File: parallel.h
31  * Author: jturney, jjwilke
32  *
33  * Created on December 11, 2009, 3:34 PM
34  */
35 
36 #ifndef _psi_src_lib_libparallel_parallel_h_
37 #define _psi_src_lib_libparallel_parallel_h_
38 
39 #include <vector>
40 #include <string>
42 #include "libparallel.h"
43 
44 // Use this to silence "warning: unused parameter" messages from the compiler.
45 // Sometimes you have to define the parameter for Doxygen regardless if you
46 // use it or not.
47 #ifndef UNUSED
48 #define UNUSED(expr) (void)(expr)
49 #endif
50 
51 namespace psi {
52 
53 template <typename DerivedType>
54 class Parallel {
55  public:
58  protected:
60  std::vector<std::string> CurrentComm;
61  public:
64  CurrentComm.push_back("COMM_WORLD");
65  }
66 
68  virtual ~Parallel() {
69 
70  }
71 
73  virtual void sync(const std::string& CommName="NONE") const {
74  UNUSED(CommName);
75  }
76 
94  template<typename T>
95  void all_reduce(const T* localdata, const int nelem,T* target,
96  const MPIOperation& op,
97  const std::string& Name)const{
98  static_cast<const DerivedType*>(this)->AllReduceImpl(localdata,
99  nelem,target,op, Name);
100  }
101 
112  template <class T>
113  void all_gather(const T* localdata, const int nelem, T* target,
114  const std::string& CommName="NONE") const {
115  static_cast<const DerivedType*>(this)->all_gatherImpl(localdata, nelem,
116  target, CommName);
117  }
118 
120  template <class T>
121  void gather(const T* localdata, const int nelem, T* target,const int Root,
122  const std::string& CommName="NONE") const {
123  static_cast<const DerivedType*>(this)->gatherImpl(localdata, nelem,
124  target, Root,CommName);
125  }
126 
138  template <class T>
139  void bcast(T* data, const int nelem, const int broadcaster,
140  const std::string& CommName="NONE") const {
141  static_cast<const DerivedType*>(this)->bcastImpl(data, nelem,
142  broadcaster, CommName);
143  }
144 
147  template <class T>
148  void bcast(T& data, const int broadcaster,
149  const std::string& CommName="NONE") const {
150  static_cast<const DerivedType*>(this)->bcastImpl(data,
151  broadcaster, CommName);
152  }
153 
163  virtual int Iprobe(const int Sender,const int MessageTag,
164  const std::string& Comm="NONE") const {
165  UNUSED(Sender);
166  UNUSED(MessageTag);
167  UNUSED(Comm);
168  return true;
169  }
170 
172  virtual int probe(const int Sender,const int MessageTag,
173  const std::string& Comm="NONE")const{
174  UNUSED(Sender);
175  UNUSED(MessageTag);
176  UNUSED(Comm);
177  return true;
178  }
179 
180 
192  template<typename T>
193  void Isend(const int destination, const int tag, T* message=NULL,
194  const int length=0,const std::string& Comm="NONE")const{
195  static_cast<const DerivedType*>(this)->
196  IsendImpl(destination,tag,message,length,Comm);
197  }
198 
210  template<typename T>
211  void send(const int destination, const int tag, T* message=NULL,
212  const int length=0,const std::string& Comm="NONE")const{
213  static_cast<const DerivedType*>(this)->
214  sendImpl(destination,tag,message,length,Comm);
215  }
216 
229  template<typename T>
230  void Irecv(const int source, const int tag, T* message=NULL,
231  const int length=0,const std::string& Comm="NONE")const{
232  static_cast<const DerivedType*>(this)->
233  IrecvImpl(source,tag,message,length,Comm);
234  }
235 
247  template<typename T>
248  void recv(const int source, const int tag, T* message=NULL,
249  const int length=0,const std::string& Comm="NONE")const{
250  static_cast<const DerivedType*>(this)->
251  recvImpl(source,tag,message,length,Comm);
252  }
253 
255  virtual int me(const std::string& CommName="NONE") const {
256  UNUSED(CommName);
257  return 0;
258  }
259 
261  virtual int nproc(const std::string& CommName="NONE") const {
262  UNUSED(CommName);
263  return 1;
264  }
265 
267  virtual int nthread() const {
269  }
270 
272  std::string communicator() const {
273  return CurrentComm.back();
274  }
275 
276  int thread_id(const pthread_t&) {
277  return 0;
278  }
279 
280  virtual void MakeComm(const std::string& /*Name*/, const int /*Color*/,
281  const std::string& /*Comm2Split*/="NONE"){}
282 
283  virtual void FreeComm(const std::string& /*Name*/="NONE"){}
284 };// End Parallel base class
285 }//End namespace psi
286 
287 #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:230
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:95
int thread_id(const pthread_t &)
Definition: parallel.h:276
virtual int nthread() const
Needed for legacy compatibility. Don&#39;t use.
Definition: parallel.h:267
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:113
std::vector< std::string > CurrentComm
Array of our communicator names, in the order they are derived.
Definition: parallel.h:60
MPIOperation
The supported operations for MPI reduce-like fxns.
Definition: libparallel.h:33
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:193
void bcast(T &data, const int broadcaster, const std::string &CommName="NONE") const
Definition: parallel.h:148
virtual ~Parallel()
No memory to free-up, does nothing.
Definition: parallel.h:68
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:261
virtual int probe(const int Sender, const int MessageTag, const std::string &Comm="NONE") const
Same as Iprobe, except blocking.
Definition: parallel.h:172
std::string communicator() const
Returns the current communicator.
Definition: parallel.h:272
virtual void FreeComm(const std::string &="NONE")
Definition: parallel.h:283
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:121
Definition: parallel.h:54
virtual void MakeComm(const std::string &, const int, const std::string &="NONE")
Definition: parallel.h:280
Parallel()
Sets current comm to COMM_WORLD.
Definition: parallel.h:63
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:211
Parallel< DerivedType > ThisType
This typedef is the type of this class.
Definition: parallel.h:57
#define UNUSED(expr)
Definition: parallel.h:48
int get_n_threads() const
Number of threads per process.
Definition: process.cc:186
virtual int Iprobe(const int Sender, const int MessageTag, const std::string &Comm="NONE") const
Sees if a message is available.
Definition: parallel.h:163
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:139
virtual void sync(const std::string &CommName="NONE") const
Provides MPI barrier functionality.
Definition: parallel.h:73
Definition: PsiFileImpl.h:40
virtual int me(const std::string &CommName="NONE") const
Returns the current MPI process number.
Definition: parallel.h:255
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:248