source: XIOS/trunk/src/distribution_client.hpp @ 895

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

Adding a new type of element into grid: Scalar

+) Add a new node Scalar for xml
+) Make some change on writing scalar value
+) Reorganize some codes
+) Remove some redundant codes

Test
+) On Curie
+) All tests pass

File size: 6.0 KB
Line 
1/*!
2   \file distribution_client.hpp
3   \author Ha NGUYEN
4   \since 13 Jan 2015
5   \date 09 Mars 2015
6
7   \brief Index distribution on client side.
8 */
9#ifndef __XIOS_DISTRIBUTIONCLIENT_HPP__
10#define __XIOS_DISTRIBUTIONCLIENT_HPP__
11
12#include <distribution.hpp>
13#include "axis.hpp"
14#include "domain.hpp"
15#include "grid.hpp"
16#include <boost/unordered_map.hpp>
17
18namespace xios {
19
20class CGrid;
21class CDomain;
22class CAxis;
23class CScalar;
24
25/*!
26  \class CDistributionClient
27  This class bases on the knowledge of distribution on client side (decided by users)
28to calculate the global index of its local data.
29*/
30class CDistributionClient : public CDistribution
31{
32public:
33  typedef boost::unordered_map<size_t,int> GlobalLocalDataMap;
34
35  public:
36    /** Default constructor */
37    CDistributionClient(int rank, int dims, const CArray<size_t,1>& globalIndex = CArray<size_t,1>());
38    CDistributionClient(int rank, CGrid* grid);
39
40    void createGlobalIndexSendToServer();
41
42    /** Default destructor */
43    virtual ~CDistributionClient();
44
45    virtual const std::vector<int>& getLocalDataIndexOnClient();
46    GlobalLocalDataMap& getGlobalLocalDataSendToServer();
47    const std::vector<int>& getLocalMaskIndexOnClient();
48
49    std::vector<int> getNGlob() { return nGlob_; }
50    std::vector<int> getDataNIndex() { return dataNIndex_; }
51
52    bool isDataDistributed() { return isDataDistributed_; }
53
54    static int getDomainIndex(const int& dataIIndex, const int& dataJIndex,
55                                     const int& dataIBegin, const int& dataJBegin,
56                                     const int& dataDim, const int& ni, int& j);
57    static int getAxisIndex(const int& dataIndex, const int& dataBegin, const int& ni);
58
59  protected:
60    void createGlobalIndex();
61    void readDistributionInfo(CGrid* grid);
62    void readDistributionInfo(const std::vector<CDomain*>& domList,
63                              const std::vector<CAxis*>& axisList,
64                              const std::vector<CScalar*>& scalarList,
65                              const CArray<int,1>& axisDomainOrder);
66  private:
67    //! Create local index of a domain
68    void createLocalDomainDataIndex();
69
70    //! Create local index of an axis
71    void createLocalAxisDataIndex();
72
73    //! Create local index of a scalar
74    void createLocalScalarDataIndex();
75
76    template<int N>
77    void readGridMaskInfo(const CArray<bool,N>& gridMask);
78
79  private:
80    //!< LocalData index on client
81    GlobalLocalDataMap globalLocalDataSendToServerMap_;
82    std::vector<int> localDataIndex_;
83    std::vector<int> localMaskIndex_;
84
85  private:
86    /*! Domains and axis are considered elements.
87     * A grid composed of 1 domain and 1 axis has 2 elements */
88    int numElement_;
89    CArray<int,1> axisDomainOrder_; //!< Order of axis and domain of a grid
90
91    std::vector<int> nLocal_; //!< Local size of each dimension (ni, nj, etc, ...)
92    std::vector<int> nGlob_; //!< Global size of each dimension (e.x: ni_glo, nj_glo, etc, ...)
93    std::vector<int> nBeginLocal_;//!< Begin index of each dimension (e.x: for domain, it's always 0, for axis, it's zoom_begin, ...)
94    std::vector<int> nBeginGlobal_; //!< Begin index of each dimension (e.x: ibegin, jbegin, ...)
95    std::vector<int> nZoomBegin_; //!< Begin index of zoom of each dimension
96    std::vector<int> nZoomEnd_; //!< End index of zoom of each dimension
97
98    // Data_n_index of domain or axis (For now, axis uses its size as data_n_index
99    std::vector<int> dataNIndex_; //!< Data_n_index in case of domain
100    std::vector<int> dataDims_; //!< Data_dim, domain can have data_dim == 1 or 2
101    std::vector<int> dataBegin_; //!< Data begin (data_ibegin, data_jbegin, etc)
102    std::vector<CArray<int,1> > dataIndex_; //!< Data index
103    std::vector<CArray<int,1> > infoIndex_; //!< i_index, j_index
104
105    std::vector<CArray<bool,1> > domainMasks_; //!< Domain mask
106    std::vector<CArray<bool,1> > axisMasks_; //!< Axis mask
107
108    std::vector<int> indexMap_; //!< Mapping element index to dimension index
109    std::vector<CArray<int,1> > elementLocalIndex_;  //!< Local index of each element
110    std::vector<CArray<size_t,1> > elementGlobalIndex_; //!< Global index of each element
111    std::vector<CArray<bool,1> > elementIndexData_; //!< // The correct index of a domain has true value, the ghost one has false value
112    std::vector<CArray<bool,1> > elementZoomMask_; //!< Only zoomed region are true
113    std::vector<size_t> elementNLocal_;
114    std::vector<size_t> elementNGlobal_;
115
116    //!< (Only for grid with one axis or scalar)Flag to determine whether data is distributed or not
117    bool isDataDistributed_;
118    int axisNum_;
119    int domainNum_;
120    bool isComputed_;
121
122  private:
123    // Just suppose that grid mask has 3 dimension. Need change
124    CArray<bool,1> gridMask_; //!< Mask of grid
125
126  private:
127    CDistributionClient(const CDistributionClient& distClient); //! Not implement
128};
129
130/*!
131  A grid can have multiple dimension, so can its mask in the form of multi-dimension array.
132It's not a good idea to store all multi-dimension arrays corresponding to each mask.
133One of the ways is to convert this array into 1-dimension one and every process is taken place on it.
134  \param [in] multi-dimension array grid mask
135*/
136template<int N>
137void CDistributionClient::readGridMaskInfo(const CArray<bool,N>& gridMask)
138{
139  int dim = gridMask.dimensions();
140  std::vector<int> dimensionSizes(dim);
141  for (int i = 0; i < dim; ++i) dimensionSizes[i] = gridMask.extent(i);
142
143  std::vector<int> idxLoop(dim,0);
144  int ssize = gridMask.numElements(), idx = 0;
145  gridMask_.resize(ssize);
146  while (idx < ssize)
147  {
148    for (int i = 0; i < dim-1; ++i)
149    {
150      if (idxLoop[i] == dimensionSizes[i])
151      {
152        idxLoop[i] = 0;
153        ++idxLoop[i+1];
154      }
155    }
156
157    int maskIndex = idxLoop[0];
158    int mulDim = 1;
159    for (int k = 1; k < dim; ++k)
160    {
161      mulDim *= dimensionSizes[k-1];
162      maskIndex += idxLoop[k]*mulDim;
163    }
164    gridMask_(maskIndex) = *(gridMask.dataFirst()+maskIndex);
165
166    ++idxLoop[0];
167    ++idx;
168  }
169}
170
171} // namespace xios
172#endif // __XIOS_DISTRIBUTIONCLIENT_HPP__
Note: See TracBrowser for help on using the repository browser.