1 | /*! |
---|
2 | \file generic_algorithm_transformation.hpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 14 May 2015 |
---|
5 | \date 29 June 2015 |
---|
6 | |
---|
7 | \brief Interface for all transformation algorithms. |
---|
8 | */ |
---|
9 | #ifndef __XIOS_GENERIC_ALGORITHM_TRANSFORMATION_HPP__ |
---|
10 | #define __XIOS_GENERIC_ALGORITHM_TRANSFORMATION_HPP__ |
---|
11 | |
---|
12 | #include <map> |
---|
13 | #include <set> |
---|
14 | #include "array_new.hpp" |
---|
15 | #include "client_client_dht_template.hpp" |
---|
16 | #include "local_view.hpp" |
---|
17 | #include "transform_connector.hpp" |
---|
18 | #include "weight_transform_connector.hpp" |
---|
19 | |
---|
20 | namespace xios |
---|
21 | { |
---|
22 | class CGrid; |
---|
23 | class CDomain; |
---|
24 | class CAxis; |
---|
25 | class CScalar; |
---|
26 | class CGridAlgorithm ; |
---|
27 | |
---|
28 | /*! |
---|
29 | \class CGenericAlgorithmTransformation |
---|
30 | This class defines the interface for all other inherited algorithms class |
---|
31 | */ |
---|
32 | class CGenericAlgorithmTransformation |
---|
33 | { |
---|
34 | public: |
---|
35 | enum AlgoTransType { |
---|
36 | ELEMENT_GENERATION = 0, |
---|
37 | ELEMENT_MODIFICATION_WITHOUT_DATA = 1, |
---|
38 | ELEMENT_MODIFICATION_WITH_DATA = 2, |
---|
39 | ELEMENT_NO_MODIFICATION_WITH_DATA = 3, |
---|
40 | ELEMENT_NO_MODIFICATION_WITHOUT_DATA = 4 |
---|
41 | } ; |
---|
42 | |
---|
43 | public: |
---|
44 | // Mapping between global index map of DESTINATION and its local index with pair of global index of SOURCE and weights |
---|
45 | typedef std::unordered_map<int, std::unordered_map<size_t, std::vector<std::pair<size_t,double> > > > SourceDestinationIndexMap; |
---|
46 | |
---|
47 | protected: |
---|
48 | typedef std::unordered_map<size_t,int> GlobalLocalMap; |
---|
49 | protected: |
---|
50 | typedef std::unordered_map<int, std::vector<int> > TransformationIndexMap; |
---|
51 | typedef std::unordered_map<int, std::vector<double> > TransformationWeightMap; |
---|
52 | typedef std::unordered_map<int, std::vector<int> > TransformationPositionMap; |
---|
53 | |
---|
54 | public: |
---|
55 | //CGenericAlgorithmTransformation(); |
---|
56 | CGenericAlgorithmTransformation(bool isSource); |
---|
57 | |
---|
58 | virtual ~CGenericAlgorithmTransformation() {} |
---|
59 | |
---|
60 | bool isDistributedTransformation(int elementPositionInGrid, CGrid* gridSrc, CGrid* gridDst) ; |
---|
61 | |
---|
62 | void computeGlobalSourceIndex(int elementPositionInGrid, |
---|
63 | CGrid* gridSrc, |
---|
64 | CGrid* gridDst, |
---|
65 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst); |
---|
66 | |
---|
67 | /*! |
---|
68 | Apply a operation on local data. |
---|
69 | \param [in] localIndex vector contains local index of local data output and the corresponding weight |
---|
70 | \param [in] dataInput Pointer to the first element of data input array (in form of buffer) |
---|
71 | \param [in/out] dataOut Array contains local data |
---|
72 | \param [in/out] flagInitial vector of boolean to mark the local index already initialized. True means there is a need for initalization |
---|
73 | \param [in] ignoreMissingValue don't count missing value in operation if this flag is true |
---|
74 | \param [in] firstPass indicate if it is the first time the apply funtion is called for a same transformation, in order to make a clean initialization |
---|
75 | */ |
---|
76 | virtual void apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
77 | const double* dataInput, |
---|
78 | CArray<double,1>& dataOut, |
---|
79 | std::vector<bool>& flagInitial, |
---|
80 | bool ignoreMissingValue, bool firstPass); |
---|
81 | |
---|
82 | /*! |
---|
83 | * Update whole dataOut (on necessary). |
---|
84 | * (Example: Impose a weight, whose value is only known after being applied an operation, on dataOut) |
---|
85 | * \param [in/out] dataOut dataOut |
---|
86 | */ |
---|
87 | virtual void updateData(CArray<double,1>& dataOut); |
---|
88 | |
---|
89 | std::vector<StdString> getIdAuxInputs(); |
---|
90 | AlgoTransType type(); |
---|
91 | /*! |
---|
92 | Compute global index mapping from one element of destination grid to the corresponding element of source grid |
---|
93 | */ |
---|
94 | void computeIndexSourceMapping(const std::vector<CArray<double,1>* >& dataAuxInputs = std::vector<CArray<double,1>* >()); |
---|
95 | void computeTransformationMappingNonDistributed(int elementPositionInGrid, CGrid* gridSrc, CGrid* gridDst, |
---|
96 | vector<int>& localSrc, vector<int>& localDst, vector<double>& weight, int& nbLocalIndexOnGridDest); |
---|
97 | void nonDistributedrecursiveFunct(int currentPos, bool masked, int elementPositionInGrid, vector< CArray<bool,1>* >& maskSrc, vector< CArray<bool,1>* >& maskDst, |
---|
98 | int& srcInd, int& srcIndCompressed, vector<int>& nIndexSrc, |
---|
99 | int& t, vector<vector<vector<pair<int,double> > > >& dstIndWeight, int currentInd, |
---|
100 | vector<int>& localSrc, vector<int>& localDst, vector<double>& weight) ; |
---|
101 | |
---|
102 | protected: |
---|
103 | virtual void computeIndexSourceMapping_(const std::vector<CArray<double,1>* >&) {}; |
---|
104 | |
---|
105 | /*! |
---|
106 | Compute proc which contains global index of an element |
---|
107 | \param[in] globalElementIndex demanding global index of an element of source grid |
---|
108 | \param[in] elementType type of source element, 2: domain, 1: axis and 0: scalar |
---|
109 | \param[out] globalElementIndexOnProc Proc contains the demanding global index |
---|
110 | */ |
---|
111 | virtual void computeExchangeGlobalIndex(const CArray<size_t,1>& globalElementIndex, |
---|
112 | int elementType, |
---|
113 | CClientClientDHTInt::Index2VectorInfoTypeMap& globalElementIndexOnProc){}; |
---|
114 | |
---|
115 | protected: |
---|
116 | void computeGlobalGridIndexMapping(int elementPositionInGrid, |
---|
117 | const std::vector<int>& srcRank, |
---|
118 | std::unordered_map<int, std::vector<std::pair<int,double> > >& src2DstMap, |
---|
119 | CGrid* gridDst, |
---|
120 | CGrid* gridSrc, |
---|
121 | std::vector<std::unordered_map<int,std::vector<size_t> > >& globalElementIndexOnProc, |
---|
122 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst); |
---|
123 | |
---|
124 | void computeExchangeDomainIndex(CDomain* domainDst, |
---|
125 | CDomain* domainSrc, |
---|
126 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
127 | std::unordered_map<int,std::vector<size_t> >& globalDomainIndexOnProc); |
---|
128 | |
---|
129 | void computeExchangeAxisIndex(CAxis* axisDst, |
---|
130 | CAxis* axisSrc, |
---|
131 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
132 | std::unordered_map<int,std::vector<size_t> >& globalAxisIndexOnProc); |
---|
133 | |
---|
134 | void computeExchangeScalarIndex(CScalar* scalarDst, |
---|
135 | CScalar* scalarSrc, |
---|
136 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
137 | std::unordered_map<int,std::vector<size_t> >& globalScalarIndexOnProc); |
---|
138 | |
---|
139 | void computePositionElements(CGrid* dst, CGrid* src); |
---|
140 | |
---|
141 | protected: |
---|
142 | //! indicate if the transformation is performed on a distributed element |
---|
143 | bool isDistributed_ ; |
---|
144 | //! indicate if the method isDistributedTransformation has been called before |
---|
145 | bool isDistributedComputed_ ; |
---|
146 | |
---|
147 | //! Map between global index of destination element and source element |
---|
148 | std::vector<TransformationIndexMap> transformationMapping_; |
---|
149 | //! Weight corresponding of source to destination |
---|
150 | std::vector<TransformationWeightMap> transformationWeight_; |
---|
151 | //! Map of global index of destination element and corresponding global index of other elements in the same grid |
---|
152 | //! By default, one index of an element corresponds to all index of remaining element in the grid. So it's empty |
---|
153 | std::vector<TransformationPositionMap> transformationPosition_; |
---|
154 | |
---|
155 | //! Id of auxillary inputs which helps doing transformation dynamically |
---|
156 | std::vector<StdString> idAuxInputs_; |
---|
157 | AlgoTransType type_; |
---|
158 | |
---|
159 | std::set<StdSize> indexElementSrc_; |
---|
160 | |
---|
161 | std::vector<std::unordered_map<int,std::vector<size_t> > > globalElementIndexOnProc_; |
---|
162 | |
---|
163 | std::vector<int> procContainSrcElementIdx_; // List of processes containing source index of transformed elements |
---|
164 | // std::set<int> procOfNonTransformedElements_; // Processes contain the source index of non-transformed elements |
---|
165 | std::set<int> commonProc_; |
---|
166 | std::vector< set<int> > procElementList_ ; // List of processes containing source index of elements |
---|
167 | |
---|
168 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndexOfTransformedElementOnProc_; |
---|
169 | |
---|
170 | bool computedProcSrcNonTransformedElement_; // Flag to indicate whether we computed proc containing non transformed elements |
---|
171 | |
---|
172 | std::map<int, int> elementPositionInGridSrc2AxisPosition_, elementPositionInGridSrc2DomainPosition_, elementPositionInGridSrc2ScalarPosition_; |
---|
173 | std::map<int, int> elementPositionInGridDst2AxisPosition_, elementPositionInGridDst2DomainPosition_, elementPositionInGridDst2ScalarPosition_; |
---|
174 | |
---|
175 | bool eliminateRedondantSrc_ ; // flag to indicate if the transformation must select only one global source point for all proc. |
---|
176 | // In this case it will choose preferentially the current process |
---|
177 | bool isSource_ ; //flag to indicate that the algorithm is a source algorithm, that mean the grid is modified or generated but |
---|
178 | // no fluxes are tranformed |
---|
179 | |
---|
180 | // new methods for new algorithm |
---|
181 | |
---|
182 | public : |
---|
183 | |
---|
184 | virtual void computeAlgorithm(CLocalView* srcView, CLocalView* dstView) ; |
---|
185 | virtual void apply(int dimBefore, int dimAfter, const CArray<double,1>& dataIn, CArray<double,1>& dataOut); |
---|
186 | virtual CGridAlgorithm* createGridAlgorithm(CGrid* gridSrc, CGrid* newGrid, int pos) ; |
---|
187 | virtual void computeRecvElement(CLocalView* srcView, CLocalView* dstView) ; |
---|
188 | protected : |
---|
189 | |
---|
190 | CLocalElement* recvElement_=nullptr ; |
---|
191 | public: |
---|
192 | CLocalElement* getRecvElement(void) { return recvElement_ ;} |
---|
193 | |
---|
194 | protected: |
---|
195 | CTransformConnector* transformConnector_ ; |
---|
196 | CWeightTransformConnector* weightTransformConnector_ ; |
---|
197 | |
---|
198 | }; |
---|
199 | |
---|
200 | } |
---|
201 | #endif // __XIOS_GENERIC_ALGORITHM_TRANSFORMATION_HPP__ |
---|