source: XIOS/dev/dev_trunk_omp/src/policy.cpp @ 1646

Last change on this file since 1646 was 1646, checked in by yushan, 5 years ago

branch merged with trunk @1645. arch file (ep&mpi) added for ADA

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