| 1 | 
gezelter | 
1539 | 
/** | 
| 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]  Vardeman & Gezelter, in progress (2009).                         | 
| 47 | 
  | 
  | 
 */ | 
| 48 | 
  | 
  | 
 | 
| 49 | 
gezelter | 
1544 | 
#ifndef PARALLEL_COMMUNICATOR_HPP | 
| 50 | 
  | 
  | 
#define PARALLEL_COMMUNICATOR_HPP | 
| 51 | 
gezelter | 
1539 | 
 | 
| 52 | 
  | 
  | 
#include <config.h> | 
| 53 | 
  | 
  | 
#include <mpi.h> | 
| 54 | 
  | 
  | 
#include "math/SquareMatrix3.hpp" | 
| 55 | 
  | 
  | 
 | 
| 56 | 
gezelter | 
1551 | 
using namespace std; | 
| 57 | 
gezelter | 
1539 | 
namespace OpenMD{ | 
| 58 | 
  | 
  | 
   | 
| 59 | 
  | 
  | 
#ifdef IS_MPI | 
| 60 | 
  | 
  | 
 | 
| 61 | 
gezelter | 
1549 | 
  enum communicatorType { | 
| 62 | 
  | 
  | 
    Global = 0, | 
| 63 | 
  | 
  | 
    Row = 1, | 
| 64 | 
  | 
  | 
    Column = 2 | 
| 65 | 
gezelter | 
1539 | 
  }; | 
| 66 | 
  | 
  | 
     | 
| 67 | 
gezelter | 
1551 | 
  template<class T> | 
| 68 | 
  | 
  | 
  class MPITraits { | 
| 69 | 
  | 
  | 
  public: | 
| 70 | 
  | 
  | 
    static MPI::Datatype Type(); | 
| 71 | 
  | 
  | 
    static int Length() { return 1; }; | 
| 72 | 
gezelter | 
1539 | 
  }; | 
| 73 | 
  | 
  | 
   | 
| 74 | 
gezelter | 
1551 | 
  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> > { | 
| 79 | 
  | 
  | 
  public: | 
| 80 | 
  | 
  | 
    static MPI::Datatype Type() { return MPITraits<T>::Type(); } | 
| 81 | 
  | 
  | 
    static int Length() {return Dim;} | 
| 82 | 
  | 
  | 
  }; | 
| 83 | 
  | 
  | 
 | 
| 84 | 
  | 
  | 
  template<class T> | 
| 85 | 
  | 
  | 
  class MPITraits< Vector3<T> > { | 
| 86 | 
  | 
  | 
  public: | 
| 87 | 
  | 
  | 
    static MPI::Datatype Type() { return MPITraits<T>::Type(); } | 
| 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> > { | 
| 93 | 
  | 
  | 
  public: | 
| 94 | 
  | 
  | 
    static MPI::Datatype Type() { return MPITraits<T>::Type(); } | 
| 95 | 
  | 
  | 
    static int Length() {return Row * Col;} | 
| 96 | 
  | 
  | 
  }; | 
| 97 | 
  | 
  | 
 | 
| 98 | 
  | 
  | 
  template<class T> | 
| 99 | 
  | 
  | 
  class MPITraits< SquareMatrix3<T> > { | 
| 100 | 
  | 
  | 
  public: | 
| 101 | 
  | 
  | 
    static MPI::Datatype Type() { return MPITraits<T>::Type(); } | 
| 102 | 
  | 
  | 
    static int Length() {return 9;} | 
| 103 | 
  | 
  | 
  }; | 
| 104 | 
gezelter | 
1539 | 
   | 
| 105 | 
gezelter | 
1551 | 
   | 
| 106 | 
gezelter | 
1593 | 
  template<communicatorType D> | 
| 107 | 
gezelter | 
1540 | 
  class Communicator {  | 
| 108 | 
gezelter | 
1539 | 
  public:  | 
| 109 | 
  | 
  | 
     | 
| 110 | 
gezelter | 
1593 | 
    Communicator<D>() { | 
| 111 | 
gezelter | 
1539 | 
       | 
| 112 | 
  | 
  | 
      int nProc = MPI::COMM_WORLD.Get_size(); | 
| 113 | 
  | 
  | 
      int myRank = MPI::COMM_WORLD.Get_rank(); | 
| 114 | 
gezelter | 
1593 | 
       | 
| 115 | 
gezelter | 
1539 | 
      int nColumnsMax = (int) sqrt(RealType(nProc)); | 
| 116 | 
  | 
  | 
 | 
| 117 | 
  | 
  | 
      int nColumns; | 
| 118 | 
  | 
  | 
      for (int i = 1; i < nColumnsMax + 1; i++) { | 
| 119 | 
  | 
  | 
        if (nProc % i == 0) nColumns = i;         | 
| 120 | 
  | 
  | 
      } | 
| 121 | 
  | 
  | 
         | 
| 122 | 
  | 
  | 
      int nRows = nProc / nColumns; | 
| 123 | 
  | 
  | 
      rowIndex_ = myRank / nColumns;       | 
| 124 | 
  | 
  | 
      columnIndex_ = myRank % nColumns; | 
| 125 | 
  | 
  | 
 | 
| 126 | 
gezelter | 
1549 | 
      switch(D) { | 
| 127 | 
  | 
  | 
      case Row : | 
| 128 | 
gezelter | 
1539 | 
        myComm = MPI::COMM_WORLD.Split(rowIndex_, 0); | 
| 129 | 
gezelter | 
1549 | 
        break; | 
| 130 | 
  | 
  | 
      case Column: | 
| 131 | 
gezelter | 
1539 | 
        myComm = MPI::COMM_WORLD.Split(columnIndex_, 0); | 
| 132 | 
gezelter | 
1549 | 
        break; | 
| 133 | 
  | 
  | 
      case Global: | 
| 134 | 
  | 
  | 
        myComm = MPI::COMM_WORLD.Split(myRank, 0); | 
| 135 | 
gezelter | 
1539 | 
      } | 
| 136 | 
gezelter | 
1593 | 
 | 
| 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) { | 
| 153 | 
  | 
  | 
      myComm = comm; | 
| 154 | 
gezelter | 
1539 | 
      int nCommProcs = myComm.Get_size(); | 
| 155 | 
gezelter | 
1593 | 
       | 
| 156 | 
gezelter | 
1589 | 
      counts.resize(nCommProcs, 0); | 
| 157 | 
  | 
  | 
      displacements.resize(nCommProcs, 0); | 
| 158 | 
gezelter | 
1593 | 
       | 
| 159 | 
gezelter | 
1551 | 
      planSize_ = MPITraits<T>::Length() * nObjects;  | 
| 160 | 
gezelter | 
1593 | 
       | 
| 161 | 
gezelter | 
1539 | 
      myComm.Allgather(&planSize_, 1, MPI::INT, &counts[0], 1, MPI::INT); | 
| 162 | 
gezelter | 
1593 | 
       | 
| 163 | 
gezelter | 
1539 | 
      displacements[0] = 0; | 
| 164 | 
  | 
  | 
      for (int i = 1; i < nCommProcs; i++) { | 
| 165 | 
  | 
  | 
        displacements[i] = displacements[i-1] + counts[i-1]; | 
| 166 | 
gezelter | 
1541 | 
      } | 
| 167 | 
  | 
  | 
 | 
| 168 | 
  | 
  | 
      size_ = 0; | 
| 169 | 
  | 
  | 
      for (int i = 0; i < nCommProcs; i++) { | 
| 170 | 
  | 
  | 
        size_ += counts[i]; | 
| 171 | 
  | 
  | 
      } | 
| 172 | 
gezelter | 
1539 | 
    } | 
| 173 | 
  | 
  | 
 | 
| 174 | 
gezelter | 
1551 | 
     | 
| 175 | 
  | 
  | 
    void gather(vector<T>& v1, vector<T>& v2) { | 
| 176 | 
gezelter | 
1593 | 
       | 
| 177 | 
gezelter | 
1589 | 
      // an assert would be helpful here to make sure the vectors are the | 
| 178 | 
  | 
  | 
      // correct geometry | 
| 179 | 
  | 
  | 
       | 
| 180 | 
gezelter | 
1539 | 
      myComm.Allgatherv(&v1[0],  | 
| 181 | 
  | 
  | 
                        planSize_,  | 
| 182 | 
gezelter | 
1551 | 
                        MPITraits<T>::Type(),  | 
| 183 | 
gezelter | 
1539 | 
                        &v2[0],  | 
| 184 | 
  | 
  | 
                        &counts[0],  | 
| 185 | 
  | 
  | 
                        &displacements[0],  | 
| 186 | 
gezelter | 
1551 | 
                        MPITraits<T>::Type());       | 
| 187 | 
gezelter | 
1593 | 
    }        | 
| 188 | 
gezelter | 
1539 | 
     | 
| 189 | 
gezelter | 
1551 | 
    void scatter(vector<T>& v1, vector<T>& v2) { | 
| 190 | 
gezelter | 
1589 | 
      // an assert would be helpful here to make sure the vectors are the | 
| 191 | 
  | 
  | 
      // correct geometry | 
| 192 | 
gezelter | 
1593 | 
             | 
| 193 | 
gezelter | 
1539 | 
      myComm.Reduce_scatter(&v1[0], &v2[0], &counts[0],  | 
| 194 | 
gezelter | 
1551 | 
                            MPITraits<T>::Type(), MPI::SUM); | 
| 195 | 
gezelter | 
1539 | 
    } | 
| 196 | 
gezelter | 
1551 | 
     | 
| 197 | 
gezelter | 
1541 | 
    int getSize() { | 
| 198 | 
  | 
  | 
      return size_; | 
| 199 | 
  | 
  | 
    } | 
| 200 | 
gezelter | 
1539 | 
     | 
| 201 | 
  | 
  | 
  private: | 
| 202 | 
  | 
  | 
    int planSize_;     ///< how many are on local proc | 
| 203 | 
gezelter | 
1541 | 
    int size_; | 
| 204 | 
gezelter | 
1551 | 
    vector<int> counts; | 
| 205 | 
  | 
  | 
    vector<int> displacements; | 
| 206 | 
gezelter | 
1539 | 
    MPI::Intracomm myComm; | 
| 207 | 
  | 
  | 
  };  | 
| 208 | 
  | 
  | 
 | 
| 209 | 
  | 
  | 
#endif | 
| 210 | 
  | 
  | 
} | 
| 211 | 
  | 
  | 
#endif | 
| 212 | 
  | 
  | 
 | 
| 213 | 
  | 
  | 
 |