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

Last change on this file since 829 was 829, checked in by mhnguyen, 8 years ago

Refactoring transformation code

+) On exchanging information during transformation, not only global index are sent but also local index
+) Correct a bug in distributed hash table (dht)
+) Add new type for dht
+) Clean up some redundant codes

Test
+) On Curie
+) Every test passes
+) Code runs faster in some cases (up to 30%)

File size: 6.5 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
14namespace xios {
15
16CAxisAlgorithmTransformation::CAxisAlgorithmTransformation(CAxis* axisDestination, CAxis* axisSource)
17 : CGenericAlgorithmTransformation(), axisDest_(axisDestination), axisSrc_(axisSource)
18{
19  axisDestGlobalSize_ = axisDestination->n_glo.getValue();
20  int niDest = axisDestination->n.getValue();
21  int ibeginDest = axisDestination->begin.getValue();
22
23  for (int idx = 0; idx < niDest; ++idx)
24    if ((axisDestination->mask)(idx)) axisDestGlobalIndex_.push_back(ibeginDest+idx);
25}
26
27CAxisAlgorithmTransformation::~CAxisAlgorithmTransformation()
28{
29}
30
31void CAxisAlgorithmTransformation::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
32{
33}
34
35/*!
36  Compute an array of global index from a global index on a axis
37  \param[in] axisDestGlobalIndex global index on an axis of destination grid
38  \param[in] axisSrcGlobalIndex global index on an axis of source grid (which are needed by one index on axis destination)
39  \param[in] axisPositionInGrid position of the axis in the grid
40  \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)
41  \param[in] globalIndexGridDestSendToServer global index of destination grid which are to be sent to server(s), this array is already acsending sorted
42  \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
43  \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
44*/
45void CAxisAlgorithmTransformation::computeGlobalGridIndexFromGlobalIndexElement(int axisDestGlobalIndex,
46                                                                          const std::vector<int>& axisSrcGlobalIndex,
47                                                                          const std::vector<int>& destGlobalIndexPositionInGrid,
48                                                                          int axisPositionInGrid,
49                                                                          const std::vector<int>& gridDestGlobalDim,
50                                                                          const std::vector<int>& gridSrcGlobalDim,
51                                                                          const GlobalLocalMap& globalLocalIndexDestSendToServerMap,
52                                                                          std::vector<std::pair<size_t,int> >& globalLocalIndexDestMap,
53                                                                          std::vector<std::vector<size_t> >& globalIndexSrcGrid)
54{
55  bool hasDestGlobalIndexPos = !destGlobalIndexPositionInGrid.empty();
56  int globalDim = gridDestGlobalDim.size();
57
58  std::vector<size_t> currentIndex(globalDim);
59  std::vector<int> gridAxisGlobalDim(globalDim);
60  std::vector<int> idxLoop(globalDim, 0);
61
62  size_t ssize = 1, idx = 0, realGlobalIndexSize = 0;
63  for (int i = 0; i< globalDim; ++i)
64  {
65    if (axisPositionInGrid == i) gridAxisGlobalDim[i] = 1;
66    else
67    {
68      if (!hasDestGlobalIndexPos) gridAxisGlobalDim[i] = gridDestGlobalDim[i];
69      else gridAxisGlobalDim[i] = 1;
70    }
71    ssize *= gridAxisGlobalDim[i];
72  }
73
74  GlobalLocalMap::const_iterator iteArr = globalLocalIndexDestSendToServerMap.end(), it;
75
76  while (idx < ssize)
77  {
78    for (int i = 0; i < globalDim-1; ++i)
79    {
80      if (idxLoop[i] == gridAxisGlobalDim[i])
81      {
82        idxLoop[i] = 0;
83        ++idxLoop[i+1];
84      }
85    }
86
87    int j = 0;
88    for (int i = 0; i < globalDim; ++i)
89    {
90      if (!hasDestGlobalIndexPos) currentIndex[i] = idxLoop[i];
91      else
92      {
93        if (axisPositionInGrid == i) currentIndex[i] = axisDestGlobalIndex;
94        else
95        {
96          currentIndex[i] = destGlobalIndexPositionInGrid[j];
97          ++j;
98        }
99      }
100    }
101
102    currentIndex[axisPositionInGrid] = axisDestGlobalIndex;
103
104    size_t globIndex = currentIndex[0];
105    size_t mulDim = 1;
106    for (int k = 1; k < globalDim; ++k)
107    {
108      mulDim *= gridDestGlobalDim[k-1];
109      globIndex += (currentIndex[k])*mulDim;
110    }
111
112    if (iteArr != globalLocalIndexDestSendToServerMap.find(globIndex)) ++realGlobalIndexSize;
113    ++idxLoop[0];
114    ++idx;
115  }
116
117  if (globalLocalIndexDestMap.size() != realGlobalIndexSize)
118    globalLocalIndexDestMap.resize(realGlobalIndexSize);
119
120  if (realGlobalIndexSize != globalIndexSrcGrid.size()) globalIndexSrcGrid.resize(realGlobalIndexSize);
121  for (int i = 0; i < globalIndexSrcGrid.size(); ++i)
122    if (globalIndexSrcGrid[i].size() != axisSrcGlobalIndex.size())
123      globalIndexSrcGrid[i].resize(axisSrcGlobalIndex.size());
124
125  size_t realGlobalIndex = 0;
126  idx = 0;
127  idxLoop.assign(globalDim, 0);
128  while (idx < ssize)
129  {
130    for (int i = 0; i < globalDim-1; ++i)
131    {
132      if (idxLoop[i] == gridAxisGlobalDim[i])
133      {
134        idxLoop[i] = 0;
135        ++idxLoop[i+1];
136      }
137    }
138
139    int j = 0;
140    for (int i = 0; i < globalDim; ++i)
141    {
142      if (!hasDestGlobalIndexPos) currentIndex[i] = idxLoop[i];
143      else
144      {
145        if (axisPositionInGrid == i) currentIndex[i] = axisDestGlobalIndex;
146        else
147        {
148          currentIndex[i] = destGlobalIndexPositionInGrid[j];
149          ++j;
150        }
151      }
152    }
153    currentIndex[axisPositionInGrid] = axisDestGlobalIndex;
154
155    size_t globIndex = currentIndex[0];
156    size_t mulDim = 1;
157    for (int k = 1; k < globalDim; ++k)
158    {
159      mulDim *= gridDestGlobalDim[k-1];
160      globIndex += (currentIndex[k])*mulDim;
161    }
162
163    it = globalLocalIndexDestSendToServerMap.find(globIndex);
164    if (iteArr != it)
165    {
166      globalLocalIndexDestMap[realGlobalIndex] = (std::make_pair(it->first,it->second));
167      for (int i = 0; i < globalIndexSrcGrid[realGlobalIndex].size(); ++i)
168      {
169        currentIndex[axisPositionInGrid] = axisSrcGlobalIndex[i];
170        globIndex = currentIndex[0];
171        mulDim = 1;
172        for (int k = 1; k < globalDim; ++k)
173        {
174          mulDim *= gridDestGlobalDim[k-1];
175          globIndex += (currentIndex[k])*mulDim;
176        }
177        (globalIndexSrcGrid[realGlobalIndex])[i] = globIndex;
178      }
179      ++realGlobalIndex;
180    }
181
182    ++idxLoop[0];
183    ++idx;
184  }
185}
186}
Note: See TracBrowser for help on using the repository browser.