[721] | 1 | /*! |
---|
| 2 | \file policy.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 06 Oct 2015 |
---|
| 5 | \date 06 Oct 2015 |
---|
| 6 | |
---|
| 7 | \brief Some useful policies for templated classes |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include "policy.hpp" |
---|
| 11 | #include <cmath> |
---|
[1601] | 12 | using namespace ep_lib; |
---|
[721] | 13 | |
---|
| 14 | namespace xios |
---|
| 15 | { |
---|
[855] | 16 | ///*! |
---|
| 17 | // Calculate MPI communicator for each level of hierarchy. |
---|
| 18 | // \param[in] mpiCommRoot MPI communicator of the level 0 (usually communicator of all clients) |
---|
| 19 | // \param[in] levels number of level in hierarchy |
---|
| 20 | //*/ |
---|
| 21 | //void DivideCommByTwo::computeMPICommLevel(const MPI_Comm& mpiCommRoot, int levels) |
---|
| 22 | //{ |
---|
| 23 | // int nbProc; |
---|
| 24 | // MPI_Comm_size(mpiCommRoot,&nbProc); |
---|
| 25 | // if (levels > nbProc) levels = std::log10(nbProc) * 3.3219; // log2(x) = log2(10) * log10(x); stupid C++98 |
---|
| 26 | // else if (1 > levels) levels = 1; |
---|
| 27 | // |
---|
| 28 | // commLevel_.push_back(mpiCommRoot); |
---|
| 29 | // divideMPICommLevel(mpiCommRoot, levels); |
---|
| 30 | //} |
---|
| 31 | // |
---|
| 32 | ///*! |
---|
| 33 | // Divide each MPI communicator into sub-communicator. Recursive function |
---|
| 34 | // \param [in] mpiCommLevel MPI communicator of current level |
---|
| 35 | // \param [in] level current level |
---|
| 36 | //*/ |
---|
| 37 | //void DivideCommByTwo::divideMPICommLevel(const MPI_Comm& mpiCommLevel, int level) |
---|
| 38 | //{ |
---|
| 39 | // int clientRank; |
---|
| 40 | // MPI_Comm_rank(mpiCommLevel,&clientRank); |
---|
| 41 | // |
---|
| 42 | // --level; |
---|
| 43 | // if (0 < level) |
---|
| 44 | // { |
---|
| 45 | // int color = clientRank % 2; |
---|
| 46 | // commLevel_.push_back(MPI_Comm()); |
---|
| 47 | // MPI_Comm_split(mpiCommLevel, color, 0, &(commLevel_.back())); |
---|
| 48 | // divideMPICommLevel(commLevel_.back(), level); |
---|
| 49 | // } |
---|
| 50 | //} |
---|
[721] | 51 | |
---|
[833] | 52 | DivideAdaptiveComm::DivideAdaptiveComm(const MPI_Comm& mpiComm) |
---|
| 53 | : internalComm_(mpiComm), level_(0), groupBegin_(), nbInGroup_(), computed_(false) |
---|
| 54 | { |
---|
| 55 | |
---|
[721] | 56 | } |
---|
| 57 | |
---|
[833] | 58 | void DivideAdaptiveComm::computeMPICommLevel() |
---|
| 59 | { |
---|
| 60 | if (computed_) return; |
---|
| 61 | computed_ = true; |
---|
| 62 | |
---|
| 63 | int mpiSize, mpiRank; |
---|
| 64 | MPI_Comm_size(internalComm_,&mpiSize); |
---|
| 65 | MPI_Comm_rank(internalComm_,&mpiRank); |
---|
| 66 | |
---|
| 67 | int maxChild=1; |
---|
| 68 | int m; |
---|
| 69 | do |
---|
| 70 | { |
---|
| 71 | m=1; |
---|
| 72 | ++maxChild; |
---|
| 73 | for(int i=0;i<maxChild;++i) m *= maxChild; |
---|
| 74 | } while(m<mpiSize); |
---|
| 75 | |
---|
| 76 | int maxLevel=0; |
---|
| 77 | for(int size=1; size<=mpiSize; size*=maxChild) ++maxLevel; |
---|
| 78 | |
---|
| 79 | int pos, n, idx; |
---|
| 80 | level_=0; |
---|
| 81 | int begin=0; |
---|
| 82 | int end=mpiSize-1; |
---|
| 83 | int nb=end-begin+1; |
---|
| 84 | |
---|
| 85 | nbInGroup_ = groupBegin_= std::vector<int>(maxLevel); |
---|
| 86 | nbInGroupParents_ = groupParentsBegin_= std::vector<std::vector<int> >(maxLevel,std::vector<int>(maxChild)); |
---|
| 87 | |
---|
| 88 | groupBegin_[level_] = begin; |
---|
| 89 | nbInGroup_[level_] = nb; |
---|
| 90 | ++level_; |
---|
| 91 | while (nb>2 && (level_<maxLevel)) |
---|
| 92 | { |
---|
| 93 | n = 0; idx = 0; |
---|
| 94 | pos = begin; |
---|
| 95 | for(int i=0;i<maxChild && i<nb;i++) |
---|
| 96 | { |
---|
| 97 | if (i<nb%maxChild) n = nb/maxChild + 1; |
---|
| 98 | else n = nb/maxChild; |
---|
| 99 | |
---|
| 100 | if (mpiRank>=pos && mpiRank<pos+n) |
---|
| 101 | { |
---|
| 102 | begin=pos; |
---|
| 103 | end=pos+n-1; |
---|
| 104 | } |
---|
| 105 | groupParentsBegin_[level_-1][idx] = pos; |
---|
| 106 | nbInGroupParents_[level_-1][idx] = n; |
---|
| 107 | ++idx; |
---|
| 108 | pos=pos+n; |
---|
| 109 | } |
---|
| 110 | groupBegin_[level_] = begin; |
---|
| 111 | nbInGroup_[level_] = nb = end-begin+1; |
---|
| 112 | |
---|
| 113 | ++level_; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | for (int i = 0; i < nbInGroup_[level_-1];++i) |
---|
| 117 | { |
---|
| 118 | groupParentsBegin_[level_-1][i] = groupBegin_[level_-1]+i; |
---|
| 119 | nbInGroupParents_[level_-1][i] = 1; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | // parent=vector<int>(maxLevel+1); |
---|
| 125 | // child=vector<vector<int> >(maxLevel+1,vector<int>(maxChild)); |
---|
| 126 | // nbChild=vector<int> (maxLevel+1); |
---|
| 127 | |
---|
| 128 | // do |
---|
| 129 | // { |
---|
| 130 | // n=0; |
---|
| 131 | // pos=begin; |
---|
| 132 | // nbChild[level_]=0; |
---|
| 133 | // parent[level_+1]=begin; |
---|
| 134 | // for(int i=0;i<maxChild && i<nb;i++) |
---|
| 135 | // { |
---|
| 136 | // if (i<nb%maxChild) n = nb/maxChild + 1; |
---|
| 137 | // else n = nb/maxChild; |
---|
| 138 | // |
---|
| 139 | // if (mpiRank>=pos && mpiRank<pos+n) |
---|
| 140 | // { |
---|
| 141 | // begin=pos; |
---|
| 142 | // end=pos+n-1; |
---|
| 143 | // } |
---|
| 144 | // child[level_][i]=pos; |
---|
| 145 | // pos=pos+n; |
---|
| 146 | // nbChild[level_]++; |
---|
| 147 | // } |
---|
| 148 | // nb=end-begin+1; |
---|
| 149 | // level_=level_+1; |
---|
| 150 | // } while (nb>1); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | //void DivideAdaptiveComm::divideMPICommLevel(const MPI_Comm& mpiCommLevel, int color, int level) |
---|
| 154 | //{ |
---|
| 155 | //// int clientRank; |
---|
| 156 | //// MPI_Comm_rank(mpiCommLevel,&clientRank); |
---|
| 157 | // |
---|
| 158 | // --level; |
---|
| 159 | // if (0 < level) |
---|
| 160 | // { |
---|
| 161 | // int color = clientRank % 2; |
---|
| 162 | // commLevel_.push_back(MPI_Comm()); |
---|
| 163 | // MPI_Comm_split(mpiCommLevel, color, 0, &(commLevel_.back())); |
---|
| 164 | // divideMPICommLevel(commLevel_.back(), level); |
---|
| 165 | // } |
---|
| 166 | //} |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | } |
---|
| 170 | |
---|