OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
Communicator.hpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#ifndef PARALLEL_COMMUNICATOR_HPP
46#define PARALLEL_COMMUNICATOR_HPP
47
48#include <config.h>
49#include <mpi.h>
50
52
53using namespace std;
54namespace OpenMD {
55
56#ifdef IS_MPI
57
58 enum communicatorType { Global = 0, Row = 1, Column = 2 };
59
60 template<class T>
61 class MPITraits {
62 public:
63 static MPI_Datatype Type();
64 static int Length() { return 1; };
65 };
66
67 template<>
68 inline MPI_Datatype MPITraits<int>::Type() {
69 return MPI_INT;
70 }
71 template<>
72 inline MPI_Datatype MPITraits<RealType>::Type() {
73 return MPI_REALTYPE;
74 }
75
76 template<class T, unsigned int Dim>
77 class MPITraits<Vector<T, Dim>> {
78 public:
79 static MPI_Datatype Type() { return MPITraits<T>::Type(); }
80 static int Length() { return Dim; }
81 };
82
83 template<class T>
84 class MPITraits<Vector3<T>> {
85 public:
86 static MPI_Datatype Type() { return MPITraits<T>::Type(); }
87 static int Length() { return 3; }
88 };
89
90 template<class T, unsigned int R, unsigned int C>
91 class MPITraits<RectMatrix<T, R, C>> {
92 public:
93 static MPI_Datatype Type() { return MPITraits<T>::Type(); }
94 static int Length() { return R * C; }
95 };
96
97 template<class T>
99 public:
100 static MPI_Datatype Type() { return MPITraits<T>::Type(); }
101 static int Length() { return 9; }
102 };
103
104 template<communicatorType D>
106 public:
107 Communicator<D>() {
108 int nProc;
109 int myRank;
110
111 MPI_Comm_size(MPI_COMM_WORLD, &nProc);
112 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
113
114 int nColumnsMax = (int)sqrt(RealType(nProc));
115
116 int nColumns(0);
117 for (int i = 1; i < nColumnsMax + 1; i++) {
118 if (nProc % i == 0) nColumns = i;
119 }
120
121 // int nRows = nProc / nColumns;
122 rowIndex_ = myRank / nColumns;
123 columnIndex_ = myRank % nColumns;
124
125 switch (D) {
126 case Row:
127 MPI_Comm_split(MPI_COMM_WORLD, rowIndex_, 0, &myComm);
128 break;
129 case Column:
130 MPI_Comm_split(MPI_COMM_WORLD, columnIndex_, 0, &myComm);
131 break;
132 case Global:
133 MPI_Comm_split(MPI_COMM_WORLD, myRank, 0, &myComm);
134 }
135 }
136
137 MPI_Comm getComm() { return myComm; }
138
139 private:
140 int rowIndex_;
141 int columnIndex_;
142 MPI_Comm myComm;
143 };
144
145 template<typename T>
146 class Plan {
147 public:
148 Plan<T>(MPI_Comm comm, int nObjects) : myComm(comm) {
149 int nCommProcs;
150 MPI_Comm_size(myComm, &nCommProcs);
151
152 counts.resize(nCommProcs, 0);
153 displacements.resize(nCommProcs, 0);
154
155 planSize_ = MPITraits<T>::Length() * nObjects;
156
157 MPI_Allgather(&planSize_, 1, MPI_INT, &counts[0], 1, MPI_INT, myComm);
158
159 displacements[0] = 0;
160 for (int i = 1; i < nCommProcs; i++) {
161 displacements[i] = displacements[i - 1] + counts[i - 1];
162 }
163
164 size_ = 0;
165 for (int i = 0; i < nCommProcs; i++) {
166 size_ += counts[i];
167 }
168 }
169
170 void gather(vector<T>& v1, vector<T>& v2) {
171 // an assert would be helpful here to make sure the vectors are the
172 // correct geometry
173
174 MPI_Allgatherv(&v1[0], planSize_, MPITraits<T>::Type(), &v2[0],
175 &counts[0], &displacements[0], MPITraits<T>::Type(),
176 myComm);
177 }
178
179 void scatter(vector<T>& v1, vector<T>& v2) {
180 // an assert would be helpful here to make sure the vectors are the
181 // correct geometry
182
183 MPI_Reduce_scatter(&v1[0], &v2[0], &counts[0], MPITraits<T>::Type(),
184 MPI_SUM, myComm);
185 }
186
187 int getSize() { return size_; }
188
189 private:
190 int planSize_; ///< how many are on local proc
191 int size_;
192 vector<int> counts;
193 vector<int> displacements;
194 MPI_Comm myComm;
195 };
196
197#endif
198} // namespace OpenMD
199
200#endif
rectangular matrix class
Fix length vector class.
Definition Vector.hpp:78
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.