ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/parallel/Communicator.hpp
Revision: 1798
Committed: Thu Sep 13 14:10:11 2012 UTC (12 years, 7 months ago) by gezelter
File size: 6270 byte(s)
Log Message:
Merged trunk changes into the development branch

File Contents

# Content
1 /**
2 * @file Communicator.hpp
3 * @author Charles Vardeman <cvardema.at.nd.edu>
4 * @date 08/18/2010
5 * @time 11:56am
6 * @version 1.0
7 *
8 * @section LICENSE
9 * Copyright (c) 2010 The University of Notre Dame. All Rights Reserved.
10 *
11 * The University of Notre Dame grants you ("Licensee") a
12 * non-exclusive, royalty free, license to use, modify and
13 * redistribute this software in source and binary code form, provided
14 * that the following conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the
22 * distribution.
23 *
24 * This software is provided "AS IS," without a warranty of any
25 * kind. All express or implied conditions, representations and
26 * warranties, including any implied warranty of merchantability,
27 * fitness for a particular purpose or non-infringement, are hereby
28 * excluded. The University of Notre Dame and its licensors shall not
29 * be liable for any damages suffered by licensee as a result of
30 * using, modifying or distributing the software or its
31 * derivatives. In no event will the University of Notre Dame or its
32 * licensors be liable for any lost revenue, profit or data, or for
33 * direct, indirect, special, consequential, incidental or punitive
34 * damages, however caused and regardless of the theory of liability,
35 * arising out of the use of or inability to use software, even if the
36 * University of Notre Dame has been advised of the possibility of
37 * such damages.
38 *
39 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
40 * research, please cite the appropriate papers when you publish your
41 * work. Good starting points are:
42 *
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] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
47 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
48 */
49
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 communicatorType {
63 Global = 0,
64 Row = 1,
65 Column = 2
66 };
67
68 template<class T>
69 class MPITraits {
70 public:
71 static MPI::Datatype Type();
72 static int Length() { return 1; };
73 };
74
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
107 template<communicatorType D>
108 class Communicator {
109 public:
110
111 Communicator<D>() {
112
113 int nProc = MPI::COMM_WORLD.Get_size();
114 int myRank = MPI::COMM_WORLD.Get_rank();
115
116 int nColumnsMax = (int) sqrt(RealType(nProc));
117
118 int nColumns;
119 for (int i = 1; i < nColumnsMax + 1; i++) {
120 if (nProc % i == 0) nColumns = i;
121 }
122
123 int nRows = nProc / nColumns;
124 rowIndex_ = myRank / nColumns;
125 columnIndex_ = myRank % nColumns;
126
127 switch(D) {
128 case Row :
129 myComm = MPI::COMM_WORLD.Split(rowIndex_, 0);
130 break;
131 case Column:
132 myComm = MPI::COMM_WORLD.Split(columnIndex_, 0);
133 break;
134 case Global:
135 myComm = MPI::COMM_WORLD.Split(myRank, 0);
136 }
137
138 }
139
140 MPI::Intracomm getComm() { return myComm; }
141
142 private:
143 int rowIndex_;
144 int columnIndex_;
145 MPI::Intracomm myComm;
146 };
147
148
149 template<typename T>
150 class Plan {
151 public:
152
153 Plan<T>(MPI::Intracomm comm, int nObjects) {
154 myComm = comm;
155 int nCommProcs = myComm.Get_size();
156
157 counts.resize(nCommProcs, 0);
158 displacements.resize(nCommProcs, 0);
159
160 planSize_ = MPITraits<T>::Length() * nObjects;
161
162 myComm.Allgather(&planSize_, 1, MPI::INT, &counts[0], 1, MPI::INT);
163
164 displacements[0] = 0;
165 for (int i = 1; i < nCommProcs; i++) {
166 displacements[i] = displacements[i-1] + counts[i-1];
167 }
168
169 size_ = 0;
170 for (int i = 0; i < nCommProcs; i++) {
171 size_ += counts[i];
172 }
173 }
174
175
176 void gather(vector<T>& v1, vector<T>& v2) {
177
178 // an assert would be helpful here to make sure the vectors are the
179 // correct geometry
180
181 myComm.Allgatherv(&v1[0],
182 planSize_,
183 MPITraits<T>::Type(),
184 &v2[0],
185 &counts[0],
186 &displacements[0],
187 MPITraits<T>::Type());
188 }
189
190 void scatter(vector<T>& v1, vector<T>& v2) {
191 // an assert would be helpful here to make sure the vectors are the
192 // correct geometry
193
194 myComm.Reduce_scatter(&v1[0], &v2[0], &counts[0],
195 MPITraits<T>::Type(), MPI::SUM);
196 }
197
198 int getSize() {
199 return size_;
200 }
201
202 private:
203 int planSize_; ///< how many are on local proc
204 int size_;
205 vector<int> counts;
206 vector<int> displacements;
207 MPI::Intracomm myComm;
208 };
209
210 #endif
211 }
212 #endif
213
214

Properties

Name Value
svn:eol-style native