source: XIOS/trunk/src/node/distribution_client.hpp @ 572

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

Correct some bugs on discovering server index and do some code cleanings

+) Add some checking functions to make sure mpi_isend and mpi_irecv work correctly
+) Add comments to code
+) Remove some redundant code and comments

Test
+) On Curie
+) The new functions are tested in test_new_features.f90. Test_client and test_complete work like before
+) Test cases:

  • 3 dimension grid with: 1 domain, 1 axis
  • 3 dimension grid with: 3 axis
  • Attached and connected

+) All pass and results are correct

TODO:
+) Fix zoom bug with grid composed of only one axis

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