2 |
|
* @file Communicator.hpp |
3 |
|
* @author Charles Vardeman <cvardema.at.nd.edu> |
4 |
|
* @date 08/18/2010 |
5 |
– |
* @time 11:56am |
5 |
|
* @version 1.0 |
6 |
|
* |
7 |
|
* @section LICENSE |
41 |
|
* |
42 |
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
43 |
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
44 |
< |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
45 |
< |
* [4] Vardeman & Gezelter, in progress (2009). |
44 |
> |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
45 |
> |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
46 |
> |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
47 |
|
*/ |
48 |
|
|
49 |
|
#ifndef PARALLEL_COMMUNICATOR_HPP |
71 |
|
static int Length() { return 1; }; |
72 |
|
}; |
73 |
|
|
74 |
< |
template<> inline MPI::Datatype MPITraits<int>::Type() { return MPI_INT; } |
75 |
< |
template<> inline MPI::Datatype MPITraits<RealType>::Type() { return MPI_REALTYPE; } |
74 |
> |
template<> inline MPI::Datatype MPITraits<int>::Type() { return MPI::INT; } |
75 |
> |
template<> inline MPI::Datatype MPITraits<RealType>::Type() { return MPI::REALTYPE; } |
76 |
|
|
77 |
|
template<class T, unsigned int Dim> |
78 |
|
class MPITraits< Vector<T, Dim> > { |
88 |
|
static int Length() {return 3;} |
89 |
|
}; |
90 |
|
|
91 |
< |
template<class T, unsigned int Row, unsigned int Col> |
92 |
< |
class MPITraits< RectMatrix<T, Row, Col> > { |
91 |
> |
template<class T, unsigned int R, unsigned int C> |
92 |
> |
class MPITraits< RectMatrix<T, R, C> > { |
93 |
|
public: |
94 |
|
static MPI::Datatype Type() { return MPITraits<T>::Type(); } |
95 |
< |
static int Length() {return Row * Col;} |
95 |
> |
static int Length() {return R * C;} |
96 |
|
}; |
97 |
|
|
98 |
|
template<class T> |
103 |
|
}; |
104 |
|
|
105 |
|
|
106 |
< |
template<communicatorType D, typename T> |
106 |
> |
template<communicatorType D> |
107 |
|
class Communicator { |
108 |
|
public: |
109 |
|
|
110 |
< |
Communicator<D, T>(int nObjects) { |
110 |
> |
Communicator<D>() { |
111 |
|
|
112 |
|
int nProc = MPI::COMM_WORLD.Get_size(); |
113 |
|
int myRank = MPI::COMM_WORLD.Get_rank(); |
114 |
< |
|
114 |
> |
|
115 |
|
int nColumnsMax = (int) sqrt(RealType(nProc)); |
116 |
|
|
117 |
|
int nColumns; |
119 |
|
if (nProc % i == 0) nColumns = i; |
120 |
|
} |
121 |
|
|
122 |
< |
int nRows = nProc / nColumns; |
122 |
> |
// int nRows = nProc / nColumns; |
123 |
|
rowIndex_ = myRank / nColumns; |
124 |
|
columnIndex_ = myRank % nColumns; |
125 |
|
|
133 |
|
case Global: |
134 |
|
myComm = MPI::COMM_WORLD.Split(myRank, 0); |
135 |
|
} |
136 |
– |
|
137 |
– |
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 |
+ |
template<typename T> |
149 |
+ |
class Plan { |
150 |
+ |
public: |
151 |
+ |
|
152 |
+ |
Plan<T>(MPI::Intracomm comm, int nObjects) : myComm(comm) { |
153 |
+ |
int nCommProcs = myComm.Get_size(); |
154 |
+ |
|
155 |
+ |
counts.resize(nCommProcs, 0); |
156 |
+ |
displacements.resize(nCommProcs, 0); |
157 |
+ |
|
158 |
|
planSize_ = MPITraits<T>::Length() * nObjects; |
159 |
< |
|
159 |
> |
|
160 |
|
myComm.Allgather(&planSize_, 1, MPI::INT, &counts[0], 1, MPI::INT); |
161 |
< |
|
161 |
> |
|
162 |
|
displacements[0] = 0; |
163 |
|
for (int i = 1; i < nCommProcs; i++) { |
164 |
|
displacements[i] = displacements[i-1] + counts[i-1]; |
149 |
– |
size_ += counts[i-1]; |
165 |
|
} |
166 |
|
|
167 |
|
size_ = 0; |
173 |
|
|
174 |
|
void gather(vector<T>& v1, vector<T>& v2) { |
175 |
|
|
176 |
+ |
// an assert would be helpful here to make sure the vectors are the |
177 |
+ |
// correct geometry |
178 |
+ |
|
179 |
|
myComm.Allgatherv(&v1[0], |
180 |
|
planSize_, |
181 |
|
MPITraits<T>::Type(), |
183 |
|
&counts[0], |
184 |
|
&displacements[0], |
185 |
|
MPITraits<T>::Type()); |
186 |
< |
} |
186 |
> |
} |
187 |
|
|
170 |
– |
|
171 |
– |
|
188 |
|
void scatter(vector<T>& v1, vector<T>& v2) { |
189 |
< |
|
189 |
> |
// an assert would be helpful here to make sure the vectors are the |
190 |
> |
// correct geometry |
191 |
> |
|
192 |
|
myComm.Reduce_scatter(&v1[0], &v2[0], &counts[0], |
193 |
|
MPITraits<T>::Type(), MPI::SUM); |
194 |
|
} |
199 |
|
|
200 |
|
private: |
201 |
|
int planSize_; ///< how many are on local proc |
184 |
– |
int rowIndex_; |
185 |
– |
int columnIndex_; |
202 |
|
int size_; |
203 |
|
vector<int> counts; |
204 |
|
vector<int> displacements; |