Logo ROOT   6.10/09
Reference Guide
MPIProcess.h
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Author: A. Lazzaro 2009
3 /***************************************************************************
4  * Package: Minuit2 *
5  * File: $Id$ *
6  * Author: Alfio Lazzaro, alfio.lazzaro@mi.infn.it *
7  * *
8  * Copyright: (C) 2008 by Universita' and INFN, Milan *
9  ***************************************************************************/
10 
11 #ifndef ROOT_Minuit2_MPIProcess
12 #define ROOT_Minuit2_MPIProcess
13 
14 // disable MPI calls
15 //#define MPIPROC
16 
17 #include "Minuit2/MnMatrix.h"
18 
19 #ifdef MPIPROC
20 #include "mpi.h"
21 #endif
22 
23 
24 namespace ROOT {
25 
26 namespace Minuit2 {
27 
28  class MPITerminate {
29  public:
31 #ifdef MPIPROC
32  if (MPI::Is_initialized() && !(MPI::Is_finalized())) {
33  std::cout << "Info --> MPITerminate:: End MPI on #"
34  << MPI::COMM_WORLD.Get_rank() << " processor"
35  << std::endl;
36 
37  MPI::Finalize();
38  }
39 #endif
40  }
41 
42  };
43 
44 
45  class MPIProcess {
46  public:
47  MPIProcess(unsigned int nelements, unsigned int indexComm);
48  ~MPIProcess();
49 
50  inline unsigned int NumElements4JobIn() const { return fNumElements4JobIn; }
51  inline unsigned int NumElements4JobOut() const { return fNumElements4JobOut; }
52 
53  inline unsigned int NumElements4Job(unsigned int rank) const
54  { return NumElements4JobIn()+((rank<NumElements4JobOut()) ? 1 : 0); }
55 
56  inline unsigned int StartElementIndex() const
57  { return ((fRank<NumElements4JobOut()) ? (fRank*NumElements4Job(fRank)) :
58  (fNelements-(fSize-fRank)*NumElements4Job(fRank))); }
59 
60  inline unsigned int EndElementIndex() const
61  { return StartElementIndex()+NumElements4Job(fRank); }
62 
63  inline unsigned int GetMPISize() const { return fSize; }
64  inline unsigned int GetMPIRank() const { return fRank; }
65 
66  bool SyncVector(ROOT::Minuit2::MnAlgebraicVector &mnvector);
67  bool SyncSymMatrixOffDiagonal(ROOT::Minuit2::MnAlgebraicSymMatrix &mnmatrix);
68 
69  static unsigned int GetMPIGlobalRank() { StartMPI(); return fgGlobalRank; }
70  static unsigned int GetMPIGlobalSize() { StartMPI(); return fgGlobalSize; }
71  static inline void StartMPI() {
72 #ifdef MPIPROC
73  if (!(MPI::Is_initialized())) {
74  MPI::Init();
75  std::cout << "Info --> MPIProcess::StartMPI: Start MPI on #"
76  << MPI::COMM_WORLD.Get_rank() << " processor"
77  << std::endl;
78  }
79  fgGlobalSize = MPI::COMM_WORLD.Get_size();
80  fgGlobalRank = MPI::COMM_WORLD.Get_rank();
81 #endif
82  }
83 
84  static void TerminateMPI() {
85 #ifdef MPIPROC
86  if (fgCommunicators[0]!=0 && fgCommunicators[1]!=0) {
87  delete fgCommunicators[0]; fgCommunicators[0] = 0; fgIndecesComm[0] = 0;
88  delete fgCommunicators[1]; fgCommunicators[1] = 0; fgIndecesComm[1] = 0;
89  }
90 
91  MPITerminate();
92 
93 #endif
94  }
95 
96  static bool SetCartDimension(unsigned int dimX, unsigned int dimY);
97  static bool SetDoFirstMPICall(bool doFirstMPICall = true);
98 
99  inline void SumReduce(const double& sub, double& total) {
100  total = sub;
101 
102 #ifdef MPIPROC
103  if (fSize>1) {
104  fgCommunicator->Allreduce(&sub,&total,1,MPI::DOUBLE,MPI::SUM);
105  }
106 #endif
107  }
108 
109  private:
110 
111 #ifdef MPIPROC
112  void MPISyncVector(double *ivector, int svector, double *ovector);
113 #endif
114 
115  private:
116  unsigned int fNelements;
117  unsigned int fSize;
118  unsigned int fRank;
119 
120  static unsigned int fgGlobalSize;
121  static unsigned int fgGlobalRank;
122 
123  static unsigned int fgCartSizeX;
124  static unsigned int fgCartSizeY;
125  static unsigned int fgCartDimension;
126  static bool fgNewCart;
127 
128  unsigned int fNumElements4JobIn;
129  unsigned int fNumElements4JobOut;
130 
131 #ifdef MPIPROC
132  static MPI::Intracomm *fgCommunicator;
133  static int fgIndexComm; // maximum 2 communicators, so index can be 0 and 1
134  static MPI::Intracomm *fgCommunicators[2]; // maximum 2 communicators
135  static unsigned int fgIndecesComm[2];
136 #endif
137 
138  };
139 
140 } // namespace Minuit2
141 } // namespace ROOT
142 
143 #endif
144 
145 
unsigned int fNumElements4JobOut
Definition: MPIProcess.h:129
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
static unsigned int fgGlobalSize
Definition: MPIProcess.h:120
void SumReduce(const double &sub, double &total)
Definition: MPIProcess.h:99
static void StartMPI()
Definition: MPIProcess.h:71
Class describing a symmetric matrix of size n.
Definition: LASymMatrix.h:51
static unsigned int GetMPIGlobalSize()
Definition: MPIProcess.h:70
static void TerminateMPI()
Definition: MPIProcess.h:84
static unsigned int fgCartDimension
Definition: MPIProcess.h:125
unsigned int fNumElements4JobIn
Definition: MPIProcess.h:128
unsigned int EndElementIndex() const
Definition: MPIProcess.h:60
unsigned int NumElements4JobOut() const
Definition: MPIProcess.h:51
static unsigned int GetMPIGlobalRank()
Definition: MPIProcess.h:69
unsigned int StartElementIndex() const
Definition: MPIProcess.h:56
static unsigned int fgCartSizeX
Definition: MPIProcess.h:123
unsigned int GetMPISize() const
Definition: MPIProcess.h:63
static unsigned int fgGlobalRank
Definition: MPIProcess.h:121
unsigned int NumElements4Job(unsigned int rank) const
Definition: MPIProcess.h:53
static unsigned int fgCartSizeY
Definition: MPIProcess.h:124
unsigned int GetMPIRank() const
Definition: MPIProcess.h:64
static unsigned int total
unsigned int NumElements4JobIn() const
Definition: MPIProcess.h:50