source: XIOS/trunk/src/transformation/axis_algorithm_transformation.cpp @ 866

Last change on this file since 866 was 866, checked in by mhnguyen, 9 years ago

Reorganizing codes

+) Reorganize some structures to make them more performant

Test:
+) On Curie
+) All tests pass

File size: 8.8 KB
Line 
1/*!
2   \file axis_algorithm_transformation.hpp
3   \author Ha NGUYEN
4   \since 14 May 2015
5   \date 29 June 2015
6
7   \brief Interface for all axis transformation algorithms.
8 */
9
10#include "axis_algorithm_transformation.hpp"
11#include "axis_algorithm_inverse.hpp"
12#include "axis_algorithm_zoom.hpp"
13#include "context.hpp"
14#include "context_client.hpp"
15#include "client_client_dht_template.hpp"
16
17namespace xios {
18
19CAxisAlgorithmTransformation::CAxisAlgorithmTransformation(CAxis* axisDestination, CAxis* axisSource)
20 : CGenericAlgorithmTransformation(), axisDest_(axisDestination), axisSrc_(axisSource)
21{
22  axisDestGlobalSize_ = axisDestination->n_glo.getValue();
23  int niDest = axisDestination->n.getValue();
24  int ibeginDest = axisDestination->begin.getValue();
25
26  for (int idx = 0; idx < niDest; ++idx)
27    if ((axisDestination->mask)(idx)) axisDestGlobalIndex_.push_back(ibeginDest+idx);
28}
29
30CAxisAlgorithmTransformation::~CAxisAlgorithmTransformation()
31{
32}
33
34void CAxisAlgorithmTransformation::computeExchangeGlobalIndex(const CArray<size_t,1>& globalAxisIndex,
35                                                              CClientClientDHTInt::Index2VectorInfoTypeMap& globalAxisIndexOnProc)
36{
37  CContext* context = CContext::getCurrent();
38  CContextClient* client=context->client;
39  int clientRank = client->clientRank;
40  int clientSize = client->clientSize;
41
42  size_t globalIndex;
43  int nIndexSize = axisSrc_->index.numElements();
44  CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank;
45  globalIndex2ProcRank.rehash(std::ceil(nIndexSize/globalIndex2ProcRank.max_load_factor()));
46  for (int idx = 0; idx < nIndexSize; ++idx)
47  {
48    globalIndex = axisSrc_->index(idx);
49    globalIndex2ProcRank[globalIndex].resize(1);
50    globalIndex2ProcRank[globalIndex][0] = clientRank;
51//    globalIndex2ProcRank[globalIndex].push_back(clientRank);
52  }
53
54  CClientClientDHTInt dhtIndexProcRank(globalIndex2ProcRank, client->intraComm);
55  dhtIndexProcRank.computeIndexInfoMapping(globalAxisIndex);
56
57//  std::vector<int> countIndex(clientSize,0);
58//  const CClientClientDHTInt::Index2VectorInfoTypeMap& computedGlobalIndexOnProc = dhtIndexProcRank.getInfoIndexMap();
59  globalAxisIndexOnProc = dhtIndexProcRank.getInfoIndexMap();
60//
61//  CClientClientDHTInt::Index2VectorInfoTypeMap::const_iterator itb = computedGlobalIndexOnProc.begin(), it,
62//                                                               ite = computedGlobalIndexOnProc.end();
63//  for (it = itb; it != ite; ++it)
64//  {
65//    const std::vector<int>& procList = it->second;
66//    for (int idx = 0; idx < procList.size(); ++idx) ++countIndex[procList[idx]];
67//  }
68//
69//  globalAxisIndexOnProc.rehash(std::ceil(clientSize/globalAxisIndexOnProc.max_load_factor()));
70//  for (int idx = 0; idx < clientSize; ++idx)
71//  {
72//    if (0 != countIndex[idx])
73//    {
74//      globalAxisIndexOnProc[idx].resize(countIndex[idx]);
75//      countIndex[idx] = 0;
76//    }
77//  }
78//
79//  for (it = itb; it != ite; ++it)
80//  {
81//    const std::vector<int>& procList = it->second;
82//    for (int idx = 0; idx < procList.size(); ++idx)
83//    {
84//      globalAxisIndexOnProc[procList[idx]][countIndex[procList[idx]]] = it->first;
85//      ++countIndex[procList[idx]];
86//    }
87//  }
88}
89
90void CAxisAlgorithmTransformation::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
91{
92}
93
94/*!
95  Compute an array of global index from a global index on a axis
96  \param[in] axisDestGlobalIndex global index on an axis of destination grid
97  \param[in] axisSrcGlobalIndex global index on an axis of source grid (which are needed by one index on axis destination)
98  \param[in] axisPositionInGrid position of the axis in the grid
99  \param[in] gridDestGlobalDim dimension size of destination grid (it should share the same size for all dimension, maybe except the axis on which transformation is performed)
100  \param[in] globalIndexGridDestSendToServer global index of destination grid which are to be sent to server(s), this array is already acsending sorted
101  \param[in/out] globalIndexDestGrid array of global index (for 2d grid, this array is a line, for 3d, this array represents a plan). It should be preallocated
102  \param[in/out] globalIndexSrcGrid array of global index of source grid (for 2d grid, this array is a line, for 3d, this array represents a plan). It should be preallocated
103*/
104void CAxisAlgorithmTransformation::computeGlobalGridIndexFromGlobalIndexElement(int axisDestGlobalIndex,
105                                                                          const std::vector<int>& axisSrcGlobalIndex,
106                                                                          const std::vector<int>& destGlobalIndexPositionInGrid,
107                                                                          int axisPositionInGrid,
108                                                                          const std::vector<int>& gridDestGlobalDim,
109                                                                          const std::vector<int>& gridSrcGlobalDim,
110                                                                          const GlobalLocalMap& globalLocalIndexDestSendToServerMap,
111                                                                          std::vector<std::pair<size_t,int> >& globalLocalIndexDestMap,
112                                                                          std::vector<std::vector<size_t> >& globalIndexSrcGrid)
113{
114  bool hasDestGlobalIndexPos = !destGlobalIndexPositionInGrid.empty();
115  int globalDim = gridDestGlobalDim.size();
116
117  std::vector<size_t> currentIndex(globalDim);
118  std::vector<int> gridAxisGlobalDim(globalDim);
119  std::vector<int> idxLoop(globalDim, 0);
120
121  size_t ssize = 1, idx = 0, realGlobalIndexSize = 0;
122  for (int i = 0; i< globalDim; ++i)
123  {
124    if (axisPositionInGrid == i) gridAxisGlobalDim[i] = 1;
125    else
126    {
127      if (!hasDestGlobalIndexPos) gridAxisGlobalDim[i] = gridDestGlobalDim[i];
128      else gridAxisGlobalDim[i] = 1;
129    }
130    ssize *= gridAxisGlobalDim[i];
131  }
132
133  GlobalLocalMap::const_iterator iteArr = globalLocalIndexDestSendToServerMap.end(), it;
134
135  while (idx < ssize)
136  {
137    for (int i = 0; i < globalDim-1; ++i)
138    {
139      if (idxLoop[i] == gridAxisGlobalDim[i])
140      {
141        idxLoop[i] = 0;
142        ++idxLoop[i+1];
143      }
144    }
145
146    int j = 0;
147    for (int i = 0; i < globalDim; ++i)
148    {
149      if (!hasDestGlobalIndexPos) currentIndex[i] = idxLoop[i];
150      else
151      {
152        if (axisPositionInGrid == i) currentIndex[i] = axisDestGlobalIndex;
153        else
154        {
155          currentIndex[i] = destGlobalIndexPositionInGrid[j];
156          ++j;
157        }
158      }
159    }
160
161    currentIndex[axisPositionInGrid] = axisDestGlobalIndex;
162
163    size_t globIndex = currentIndex[0];
164    size_t mulDim = 1;
165    for (int k = 1; k < globalDim; ++k)
166    {
167      mulDim *= gridDestGlobalDim[k-1];
168      globIndex += (currentIndex[k])*mulDim;
169    }
170
171    if (iteArr != globalLocalIndexDestSendToServerMap.find(globIndex)) ++realGlobalIndexSize;
172    ++idxLoop[0];
173    ++idx;
174  }
175
176  if (globalLocalIndexDestMap.size() != realGlobalIndexSize)
177    globalLocalIndexDestMap.resize(realGlobalIndexSize);
178
179  if (realGlobalIndexSize != globalIndexSrcGrid.size()) globalIndexSrcGrid.resize(realGlobalIndexSize);
180  for (int i = 0; i < globalIndexSrcGrid.size(); ++i)
181    if (globalIndexSrcGrid[i].size() != axisSrcGlobalIndex.size())
182      globalIndexSrcGrid[i].resize(axisSrcGlobalIndex.size());
183
184  size_t realGlobalIndex = 0;
185  idx = 0;
186  idxLoop.assign(globalDim, 0);
187  while (idx < ssize)
188  {
189    for (int i = 0; i < globalDim-1; ++i)
190    {
191      if (idxLoop[i] == gridAxisGlobalDim[i])
192      {
193        idxLoop[i] = 0;
194        ++idxLoop[i+1];
195      }
196    }
197
198    int j = 0;
199    for (int i = 0; i < globalDim; ++i)
200    {
201      if (!hasDestGlobalIndexPos) currentIndex[i] = idxLoop[i];
202      else
203      {
204        if (axisPositionInGrid == i) currentIndex[i] = axisDestGlobalIndex;
205        else
206        {
207          currentIndex[i] = destGlobalIndexPositionInGrid[j];
208          ++j;
209        }
210      }
211    }
212    currentIndex[axisPositionInGrid] = axisDestGlobalIndex;
213
214    size_t globIndex = currentIndex[0];
215    size_t mulDim = 1;
216    for (int k = 1; k < globalDim; ++k)
217    {
218      mulDim *= gridDestGlobalDim[k-1];
219      globIndex += (currentIndex[k])*mulDim;
220    }
221
222    it = globalLocalIndexDestSendToServerMap.find(globIndex);
223    if (iteArr != it)
224    {
225      globalLocalIndexDestMap[realGlobalIndex] = (std::make_pair(it->first,it->second));
226      for (int i = 0; i < globalIndexSrcGrid[realGlobalIndex].size(); ++i)
227      {
228        currentIndex[axisPositionInGrid] = axisSrcGlobalIndex[i];
229        globIndex = currentIndex[0];
230        mulDim = 1;
231        for (int k = 1; k < globalDim; ++k)
232        {
233          mulDim *= gridDestGlobalDim[k-1];
234          globIndex += (currentIndex[k])*mulDim;
235        }
236        (globalIndexSrcGrid[realGlobalIndex])[i] = globIndex;
237      }
238      ++realGlobalIndex;
239    }
240
241    ++idxLoop[0];
242    ++idx;
243  }
244}
245}
Note: See TracBrowser for help on using the repository browser.