43 |
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
44 |
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
45 |
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
46 |
< |
* [4] Vardeman & Gezelter, in progress (2009). |
46 |
> |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
47 |
> |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
48 |
|
*/ |
49 |
|
|
50 |
< |
#ifndef FORCEDECOMPOSITION_COMMUNICATOR_HPP |
51 |
< |
#define FORCEDECOMPOSITION_COMMUNICATOR_HPP |
50 |
> |
#ifndef PARALLEL_COMMUNICATOR_HPP |
51 |
> |
#define PARALLEL_COMMUNICATOR_HPP |
52 |
|
|
53 |
|
#include <config.h> |
54 |
|
#include <mpi.h> |
55 |
|
#include "math/SquareMatrix3.hpp" |
56 |
|
|
57 |
+ |
using namespace std; |
58 |
|
namespace OpenMD{ |
59 |
|
|
60 |
|
#ifdef IS_MPI |
61 |
|
|
62 |
< |
enum direction { |
63 |
< |
I = 0, |
64 |
< |
J = 1 |
62 |
> |
enum communicatorType { |
63 |
> |
Global = 0, |
64 |
> |
Row = 1, |
65 |
> |
Column = 2 |
66 |
|
}; |
67 |
|
|
68 |
< |
template<typename T> |
69 |
< |
struct MPITraits |
70 |
< |
{ |
71 |
< |
static const MPI::Datatype datatype; |
72 |
< |
static const int dim; |
68 |
> |
template<class T> |
69 |
> |
class MPITraits { |
70 |
> |
public: |
71 |
> |
static MPI::Datatype Type(); |
72 |
> |
static int Length() { return 1; }; |
73 |
|
}; |
74 |
|
|
75 |
< |
template<> const MPI::Datatype MPITraits<int>::datatype = MPI_INT; |
76 |
< |
template<> const int MPITraits<int>::dim = 1; |
77 |
< |
template<> const MPI::Datatype MPITraits<RealType>::datatype = MPI_REALTYPE; |
78 |
< |
template<> const int MPITraits<RealType>::dim = 1; |
79 |
< |
template<> const MPI::Datatype MPITraits<Vector3d>::datatype = MPI_REALTYPE; |
80 |
< |
template<> const int MPITraits<Vector3d>::dim = 3; |
81 |
< |
template<> const MPI::Datatype MPITraits<Mat3x3d>::datatype = MPI_REALTYPE; |
82 |
< |
template<> const int MPITraits<Mat3x3d>::dim = 9; |
75 |
> |
template<> inline MPI::Datatype MPITraits<int>::Type() { return MPI::INT; } |
76 |
> |
template<> inline MPI::Datatype MPITraits<RealType>::Type() { return MPI::REALTYPE; } |
77 |
> |
|
78 |
> |
template<class T, unsigned int Dim> |
79 |
> |
class MPITraits< Vector<T, Dim> > { |
80 |
> |
public: |
81 |
> |
static MPI::Datatype Type() { return MPITraits<T>::Type(); } |
82 |
> |
static int Length() {return Dim;} |
83 |
> |
}; |
84 |
> |
|
85 |
> |
template<class T> |
86 |
> |
class MPITraits< Vector3<T> > { |
87 |
> |
public: |
88 |
> |
static MPI::Datatype Type() { return MPITraits<T>::Type(); } |
89 |
> |
static int Length() {return 3;} |
90 |
> |
}; |
91 |
> |
|
92 |
> |
template<class T, unsigned int Row, unsigned int Col> |
93 |
> |
class MPITraits< RectMatrix<T, Row, Col> > { |
94 |
> |
public: |
95 |
> |
static MPI::Datatype Type() { return MPITraits<T>::Type(); } |
96 |
> |
static int Length() {return Row * Col;} |
97 |
> |
}; |
98 |
> |
|
99 |
> |
template<class T> |
100 |
> |
class MPITraits< SquareMatrix3<T> > { |
101 |
> |
public: |
102 |
> |
static MPI::Datatype Type() { return MPITraits<T>::Type(); } |
103 |
> |
static int Length() {return 9;} |
104 |
> |
}; |
105 |
|
|
106 |
< |
template<direction D, typename T> |
107 |
< |
class Comm { |
106 |
> |
|
107 |
> |
template<communicatorType D> |
108 |
> |
class Communicator { |
109 |
|
public: |
110 |
|
|
111 |
< |
Comm<D, T>(int nObjects) { |
111 |
> |
Communicator<D>() { |
112 |
|
|
113 |
|
int nProc = MPI::COMM_WORLD.Get_size(); |
114 |
|
int myRank = MPI::COMM_WORLD.Get_rank(); |
115 |
< |
|
115 |
> |
|
116 |
|
int nColumnsMax = (int) sqrt(RealType(nProc)); |
117 |
|
|
118 |
|
int nColumns; |
120 |
|
if (nProc % i == 0) nColumns = i; |
121 |
|
} |
122 |
|
|
97 |
– |
int nRows = nProc / nColumns; |
123 |
|
rowIndex_ = myRank / nColumns; |
124 |
|
columnIndex_ = myRank % nColumns; |
125 |
|
|
126 |
< |
if (D == I) { |
126 |
> |
switch(D) { |
127 |
> |
case Row : |
128 |
|
myComm = MPI::COMM_WORLD.Split(rowIndex_, 0); |
129 |
< |
} else { |
129 |
> |
break; |
130 |
> |
case Column: |
131 |
|
myComm = MPI::COMM_WORLD.Split(columnIndex_, 0); |
132 |
+ |
break; |
133 |
+ |
case Global: |
134 |
+ |
myComm = MPI::COMM_WORLD.Split(myRank, 0); |
135 |
|
} |
106 |
– |
|
107 |
– |
int nCommProcs = myComm.Get_size(); |
136 |
|
|
137 |
< |
counts.reserve(nCommProcs); |
138 |
< |
displacements.reserve(nCommProcs); |
137 |
> |
} |
138 |
> |
|
139 |
> |
MPI::Intracomm getComm() { return myComm; } |
140 |
> |
|
141 |
> |
private: |
142 |
> |
int rowIndex_; |
143 |
> |
int columnIndex_; |
144 |
> |
MPI::Intracomm myComm; |
145 |
> |
}; |
146 |
> |
|
147 |
|
|
148 |
< |
planSize_ = MPITraits<T>::dim * nObjects; |
149 |
< |
|
148 |
> |
template<typename T> |
149 |
> |
class Plan { |
150 |
> |
public: |
151 |
> |
|
152 |
> |
Plan<T>(MPI::Intracomm comm, int nObjects) { |
153 |
> |
myComm = comm; |
154 |
> |
int nCommProcs = myComm.Get_size(); |
155 |
> |
|
156 |
> |
counts.resize(nCommProcs, 0); |
157 |
> |
displacements.resize(nCommProcs, 0); |
158 |
> |
|
159 |
> |
planSize_ = MPITraits<T>::Length() * nObjects; |
160 |
> |
|
161 |
|
myComm.Allgather(&planSize_, 1, MPI::INT, &counts[0], 1, MPI::INT); |
162 |
< |
|
162 |
> |
|
163 |
|
displacements[0] = 0; |
164 |
|
for (int i = 1; i < nCommProcs; i++) { |
165 |
|
displacements[i] = displacements[i-1] + counts[i-1]; |
166 |
< |
} |
166 |
> |
} |
167 |
> |
|
168 |
> |
size_ = 0; |
169 |
> |
for (int i = 0; i < nCommProcs; i++) { |
170 |
> |
size_ += counts[i]; |
171 |
> |
} |
172 |
|
} |
173 |
|
|
174 |
< |
|
175 |
< |
void gather(std::vector<T>& v1, std::vector<T>& v2) { |
174 |
> |
|
175 |
> |
void gather(vector<T>& v1, vector<T>& v2) { |
176 |
|
|
177 |
+ |
// an assert would be helpful here to make sure the vectors are the |
178 |
+ |
// correct geometry |
179 |
+ |
|
180 |
|
myComm.Allgatherv(&v1[0], |
181 |
|
planSize_, |
182 |
< |
MPITraits<T>::datatype, |
182 |
> |
MPITraits<T>::Type(), |
183 |
|
&v2[0], |
184 |
|
&counts[0], |
185 |
|
&displacements[0], |
186 |
< |
MPITraits<T>::datatype); |
187 |
< |
} |
133 |
< |
|
186 |
> |
MPITraits<T>::Type()); |
187 |
> |
} |
188 |
|
|
189 |
< |
|
190 |
< |
void scatter(std::vector<T>& v1, std::vector<T>& v2) { |
191 |
< |
|
189 |
> |
void scatter(vector<T>& v1, vector<T>& v2) { |
190 |
> |
// an assert would be helpful here to make sure the vectors are the |
191 |
> |
// correct geometry |
192 |
> |
|
193 |
|
myComm.Reduce_scatter(&v1[0], &v2[0], &counts[0], |
194 |
< |
MPITraits<T>::datatype, MPI::SUM); |
194 |
> |
MPITraits<T>::Type(), MPI::SUM); |
195 |
|
} |
196 |
|
|
197 |
+ |
int getSize() { |
198 |
+ |
return size_; |
199 |
+ |
} |
200 |
+ |
|
201 |
|
private: |
202 |
|
int planSize_; ///< how many are on local proc |
203 |
< |
int rowIndex_; |
204 |
< |
int columnIndex_; |
205 |
< |
std::vector<int> counts; |
147 |
< |
std::vector<int> displacements; |
203 |
> |
int size_; |
204 |
> |
vector<int> counts; |
205 |
> |
vector<int> displacements; |
206 |
|
MPI::Intracomm myComm; |
207 |
|
}; |
208 |
|
|