1 | #ifndef __XIOS_CGrid__ |
---|
2 | #define __XIOS_CGrid__ |
---|
3 | |
---|
4 | /// XIOS headers /// |
---|
5 | #include "xios_spl.hpp" |
---|
6 | #include "group_factory.hpp" |
---|
7 | |
---|
8 | #include "declare_group.hpp" |
---|
9 | #include "domain.hpp" |
---|
10 | #include "axis.hpp" |
---|
11 | #include "scalar.hpp" |
---|
12 | #include "array_new.hpp" |
---|
13 | #include "attribute_array.hpp" |
---|
14 | #include "distribution_server.hpp" |
---|
15 | #include "client_server_mapping.hpp" |
---|
16 | #include "utils.hpp" |
---|
17 | #include "transformation_enum.hpp" |
---|
18 | |
---|
19 | namespace xios { |
---|
20 | |
---|
21 | /// ////////////////////// Déclarations ////////////////////// /// |
---|
22 | |
---|
23 | class CGridGroup; |
---|
24 | class CGridAttributes; |
---|
25 | class CDomainGroup; |
---|
26 | class CAxisGroup; |
---|
27 | class CScalarGroup; |
---|
28 | class CGrid; |
---|
29 | class CDistributionClient; |
---|
30 | class CDistributionServer; |
---|
31 | class CServerDistributionDescription; |
---|
32 | class CClientServerMapping; |
---|
33 | class CGridTransformation; |
---|
34 | |
---|
35 | ///-------------------------------------------------------------- |
---|
36 | |
---|
37 | // Declare/Define CGridAttribute |
---|
38 | BEGIN_DECLARE_ATTRIBUTE_MAP(CGrid) |
---|
39 | # include "grid_attribute.conf" |
---|
40 | END_DECLARE_ATTRIBUTE_MAP(CGrid) |
---|
41 | |
---|
42 | ///-------------------------------------------------------------- |
---|
43 | |
---|
44 | class CGrid |
---|
45 | : public CObjectTemplate<CGrid> |
---|
46 | , public CGridAttributes |
---|
47 | { |
---|
48 | /// typedef /// |
---|
49 | typedef CObjectTemplate<CGrid> SuperClass; |
---|
50 | typedef CGridAttributes SuperClassAttribute; |
---|
51 | |
---|
52 | public: |
---|
53 | |
---|
54 | typedef CGridAttributes RelAttributes; |
---|
55 | typedef CGridGroup RelGroup; |
---|
56 | |
---|
57 | enum EEventId |
---|
58 | { |
---|
59 | EVENT_ID_INDEX, EVENT_ID_ADD_DOMAIN, EVENT_ID_ADD_AXIS, EVENT_ID_ADD_SCALAR |
---|
60 | }; |
---|
61 | |
---|
62 | /// Constructeurs /// |
---|
63 | CGrid(void); |
---|
64 | explicit CGrid(const StdString& id); |
---|
65 | CGrid(const CGrid& grid); // Not implemented yet. |
---|
66 | CGrid(const CGrid* const grid); // Not implemented yet. |
---|
67 | |
---|
68 | /// Traitements /// |
---|
69 | // void solveReference(void); |
---|
70 | |
---|
71 | void checkEligibilityForCompressedOutput(); |
---|
72 | |
---|
73 | void solveDomainAxisRef(bool areAttributesChecked); |
---|
74 | |
---|
75 | void checkMaskIndex(bool doCalculateIndex); |
---|
76 | |
---|
77 | // virtual void toBinary (StdOStream& os) const; |
---|
78 | // virtual void fromBinary(StdIStream& is); |
---|
79 | |
---|
80 | void addRelFileCompressed(const StdString& filename); |
---|
81 | |
---|
82 | /// Tests /// |
---|
83 | bool isCompressible(void) const; |
---|
84 | bool isWrittenCompressed(const StdString& filename) const; |
---|
85 | |
---|
86 | public: |
---|
87 | |
---|
88 | /// Accesseurs /// |
---|
89 | StdSize getDimension(void) const; |
---|
90 | |
---|
91 | StdSize getDataSize(void) const; |
---|
92 | |
---|
93 | /// Entrées-sorties de champs /// |
---|
94 | template <int n> |
---|
95 | void inputField(const CArray<double,n>& field, CArray<double,1>& stored) const; |
---|
96 | template <int n> |
---|
97 | void outputField(const CArray<double,1>& stored, CArray<double,n>& field) const; |
---|
98 | |
---|
99 | void outputField(int rank, const CArray<double,1>& stored, double* field); |
---|
100 | void inputField(int rank, const double* const field, CArray<double,1>& stored); |
---|
101 | |
---|
102 | void outputCompressedField(int rank, const CArray<double,1>& stored, double* field); |
---|
103 | |
---|
104 | virtual void parse(xml::CXMLNode& node); |
---|
105 | |
---|
106 | /// Destructeur /// |
---|
107 | virtual ~CGrid(void); |
---|
108 | |
---|
109 | public: |
---|
110 | |
---|
111 | /// Accesseurs statiques /// |
---|
112 | static StdString GetName(void); |
---|
113 | static StdString GetDefName(void); |
---|
114 | |
---|
115 | static ENodeType GetType(void); |
---|
116 | |
---|
117 | /// Instanciateurs Statiques /// |
---|
118 | static CGrid* createGrid(CDomain* domain); |
---|
119 | static CGrid* createGrid(CDomain* domain, CAxis* axis); |
---|
120 | static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis, |
---|
121 | const CArray<int,1>& axisDomainOrder = CArray<int,1>()); |
---|
122 | static CGrid* createGrid(StdString id, const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis, |
---|
123 | const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>()); |
---|
124 | static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis, |
---|
125 | const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder); |
---|
126 | static StdString generateId(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis, |
---|
127 | const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>()); |
---|
128 | static StdString generateId(const CGrid* gridSrc, const CGrid* gridDest); |
---|
129 | static CGrid* cloneGrid(const StdString& idNewGrid, CGrid* gridSrc); |
---|
130 | |
---|
131 | public: |
---|
132 | |
---|
133 | /// Entrées-sorties de champs (interne) /// |
---|
134 | void storeField_arr(const double* const data, CArray<double,1>& stored) const; |
---|
135 | void restoreField_arr(const CArray<double,1>& stored, double* const data) const; |
---|
136 | |
---|
137 | /// Traitements protégés /// |
---|
138 | void computeIndexServer(void); |
---|
139 | void computeIndex(void); |
---|
140 | void computeIndexScalarGrid(); |
---|
141 | void computeCompressedIndex(); |
---|
142 | |
---|
143 | void solveDomainRef(bool checkAtt); |
---|
144 | void solveAxisRef(bool checkAtt); |
---|
145 | void solveScalarRef(bool checkAtt); |
---|
146 | void solveDomainAxisRefInheritance(bool apply = true); |
---|
147 | void solveTransformations(); |
---|
148 | void solveDomainAxisBaseRef(); |
---|
149 | |
---|
150 | CDomain* addDomain(const std::string& id=StdString()); |
---|
151 | CAxis* addAxis(const std::string& id=StdString()); |
---|
152 | CScalar* addScalar(const std::string& id=StdString()); |
---|
153 | void sendAddDomain(const std::string& id=""); |
---|
154 | void sendAddAxis(const std::string& id=""); |
---|
155 | void sendAddScalar(const std::string& id=""); |
---|
156 | void sendAllDomains(); |
---|
157 | void sendAllAxis(); |
---|
158 | void sendAllScalars(); |
---|
159 | |
---|
160 | static void recvAddDomain(CEventServer& event); |
---|
161 | void recvAddDomain(CBufferIn& buffer); |
---|
162 | static void recvAddAxis(CEventServer& event); |
---|
163 | void recvAddAxis(CBufferIn& buffer); |
---|
164 | static void recvAddScalar(CEventServer& event); |
---|
165 | void recvAddScalar(CBufferIn& buffer); |
---|
166 | |
---|
167 | static bool dispatchEvent(CEventServer& event); |
---|
168 | static void recvIndex(CEventServer& event); |
---|
169 | void recvIndex(vector<int> ranks, vector<CBufferIn*> buffers); |
---|
170 | void sendIndex(void); |
---|
171 | void sendIndexScalarGrid(); |
---|
172 | |
---|
173 | void computeDomConServer(); |
---|
174 | std::map<int, int> getDomConServerSide(); |
---|
175 | std::map<int, StdSize> getAttributesBufferSize(); |
---|
176 | std::map<int, StdSize> getDataBufferSize(const std::string& id = ""); |
---|
177 | std::vector<StdString> getDomainList(); |
---|
178 | std::vector<StdString> getAxisList(); |
---|
179 | std::vector<StdString> getScalarList(); |
---|
180 | std::vector<CDomain*> getDomains(); |
---|
181 | std::vector<CAxis*> getAxis(); |
---|
182 | std::vector<CScalar*> getScalars(); |
---|
183 | CDomain* getDomain(int domainIndex); |
---|
184 | CAxis* getAxis(int axisIndex); |
---|
185 | CScalar* getScalar(int scalarIndex); |
---|
186 | std::vector<int> getAxisOrder(); |
---|
187 | std::vector<int> getGlobalDimension(); |
---|
188 | bool isScalarGrid() const; |
---|
189 | std::vector<int> getAxisPositionInGrid() const; |
---|
190 | |
---|
191 | bool doGridHaveDataToWrite(); |
---|
192 | bool doGridHaveDataDistributed(); |
---|
193 | size_t getWrittenDataSize() const; |
---|
194 | int getNumberWrittenIndexes() const; |
---|
195 | int getTotalNumberWrittenIndexes() const; |
---|
196 | int getOffsetWrittenIndexes() const; |
---|
197 | |
---|
198 | CDistributionServer* getDistributionServer(); |
---|
199 | CDistributionClient* getDistributionClient(); |
---|
200 | CGridTransformation* getTransformations(); |
---|
201 | |
---|
202 | void transformGrid(CGrid* transformGridSrc); |
---|
203 | void completeGrid(CGrid* transformGridSrc = 0); |
---|
204 | void doAutoDistribution(CGrid* transformGridSrc); |
---|
205 | bool isTransformed(); |
---|
206 | void setTransformed(); |
---|
207 | bool isGenerated(); |
---|
208 | void setGenerated(); |
---|
209 | void addTransGridSource(CGrid* gridSrc); |
---|
210 | std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource(); |
---|
211 | bool hasTransform(); |
---|
212 | |
---|
213 | public: |
---|
214 | |
---|
215 | /// Propriétés privées /// |
---|
216 | bool isChecked; |
---|
217 | bool isDomainAxisChecked; |
---|
218 | bool isIndexSent; |
---|
219 | |
---|
220 | CArray<int, 1> storeIndex_client; |
---|
221 | |
---|
222 | map<int, CArray<int, 1> > storeIndex_toSrv; |
---|
223 | map<int,int> nbSenders; |
---|
224 | |
---|
225 | map<int, CArray<size_t, 1> > outIndexFromClient, compressedOutIndexFromClient; |
---|
226 | void checkMask(void); |
---|
227 | void createMask(void); |
---|
228 | void modifyMask(const CArray<int,1>& indexToModify); |
---|
229 | |
---|
230 | void computeGridGlobalDimension(const std::vector<CDomain*>& domains, |
---|
231 | const std::vector<CAxis*>& axis, |
---|
232 | const std::vector<CScalar*>& scalars, |
---|
233 | const CArray<int,1>& axisDomainOrder); |
---|
234 | |
---|
235 | private: |
---|
236 | template<int N> |
---|
237 | void checkGridMask(CArray<bool,N>& gridMask, |
---|
238 | const std::vector<CArray<bool,1>* >& domainMasks, |
---|
239 | const std::vector<CArray<bool,1>* >& axisMasks, |
---|
240 | const CArray<int,1>& axisDomainOrder, |
---|
241 | bool createMask = false); |
---|
242 | template<int N> |
---|
243 | void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify); |
---|
244 | |
---|
245 | void setVirtualDomainGroup(CDomainGroup* newVDomainGroup); |
---|
246 | void setVirtualAxisGroup(CAxisGroup* newVAxisGroup); |
---|
247 | void setVirtualScalarGroup(CScalarGroup* newVScalarGroup); |
---|
248 | |
---|
249 | void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>()); |
---|
250 | void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>()); |
---|
251 | void setScalarList(const std::vector<CScalar*> scalars = std::vector<CScalar*>()); |
---|
252 | |
---|
253 | CDomainGroup* getVirtualDomainGroup() const; |
---|
254 | CAxisGroup* getVirtualAxisGroup() const; |
---|
255 | CScalarGroup* getVirtualScalarGroup() const; |
---|
256 | |
---|
257 | void checkAttributesAfterTransformation(); |
---|
258 | void setTransformationAlgorithms(); |
---|
259 | void computeIndexByElement(const std::vector<boost::unordered_map<size_t,std::vector<int> > >& indexServerOnElement, |
---|
260 | CClientServerMapping::GlobalIndexMap& globalIndexOnServer); |
---|
261 | |
---|
262 | private: |
---|
263 | CDomainGroup* vDomainGroup_; |
---|
264 | CAxisGroup* vAxisGroup_; |
---|
265 | CScalarGroup* vScalarGroup_; |
---|
266 | std::vector<std::string> axisList_, domList_, scalarList_; |
---|
267 | bool isAxisListSet, isDomListSet, isScalarListSet; |
---|
268 | |
---|
269 | CDistributionClient* clientDistribution_; |
---|
270 | CDistributionServer* serverDistribution_; |
---|
271 | CClientServerMapping* clientServerMap_; |
---|
272 | size_t writtenDataSize_; |
---|
273 | int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_; |
---|
274 | std::map<int,size_t> connectedDataSize_; |
---|
275 | std::vector<int> connectedServerRank_; |
---|
276 | bool isDataDistributed_; |
---|
277 | int positionDimensionDistributed_; |
---|
278 | //! True if and only if the data defined on the grid can be outputted in a compressed way |
---|
279 | bool isCompressible_; |
---|
280 | std::set<std::string> relFilesCompressed; |
---|
281 | |
---|
282 | bool isTransformed_; |
---|
283 | bool isGenerated_; |
---|
284 | std::vector<int> axisPositionInGrid_; |
---|
285 | CGridTransformation* transformations_; |
---|
286 | bool hasDomainAxisBaseRef_; |
---|
287 | std::vector<int> globalDim_; |
---|
288 | std::map<CGrid*, std::pair<bool,StdString> > gridSrc_; |
---|
289 | bool hasTransform_; |
---|
290 | CClientServerMapping::GlobalIndexMap globalIndexOnServer_; |
---|
291 | // List order of axis and domain in a grid, if there is a domain, it will take value 1 (true), axis 0 (false) |
---|
292 | std::vector<int> order_; |
---|
293 | }; // class CGrid |
---|
294 | |
---|
295 | ///-------------------------------------------------------------- |
---|
296 | |
---|
297 | template <int n> |
---|
298 | void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored) const |
---|
299 | { |
---|
300 | if (this->getDataSize() != field.numElements()) |
---|
301 | ERROR("void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored) const", |
---|
302 | << "[ Awaiting data of size = " << this->getDataSize() << ", " |
---|
303 | << "Received data size = " << field.numElements() << " ] " |
---|
304 | << "The data array does not have the right size! " |
---|
305 | << "Grid = " << this->GetName()) |
---|
306 | this->storeField_arr(field.dataFirst(), stored); |
---|
307 | } |
---|
308 | |
---|
309 | template <int n> |
---|
310 | void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const |
---|
311 | { |
---|
312 | if (this->getDataSize() != field.numElements()) |
---|
313 | ERROR("void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const", |
---|
314 | << "[ Size of the data = " << this->getDataSize() << ", " |
---|
315 | << "Output data size = " << field.numElements() << " ] " |
---|
316 | << "The ouput array does not have the right size! " |
---|
317 | << "Grid = " << this->GetName()) |
---|
318 | this->restoreField_arr(stored, field.dataFirst()); |
---|
319 | } |
---|
320 | |
---|
321 | template<int N> |
---|
322 | void CGrid::checkGridMask(CArray<bool,N>& gridMask, |
---|
323 | const std::vector<CArray<bool,1>* >& domainMasks, |
---|
324 | const std::vector<CArray<bool,1>* >& axisMasks, |
---|
325 | const CArray<int,1>& axisDomainOrder, |
---|
326 | bool createMask) |
---|
327 | { |
---|
328 | int idx = 0; |
---|
329 | int numElement = axisDomainOrder.numElements(); |
---|
330 | int dim = domainMasks.size() * 2 + axisMasks.size(); |
---|
331 | std::vector<CDomain*> domainP = this->getDomains(); |
---|
332 | |
---|
333 | std::vector<int> idxLoop(dim,0), indexMap(numElement), eachDimSize(dim); |
---|
334 | std::vector<int> currentIndex(dim); |
---|
335 | int idxDomain = 0, idxAxis = 0; |
---|
336 | for (int i = 0; i < numElement; ++i) |
---|
337 | { |
---|
338 | indexMap[i] = idx; |
---|
339 | if (2 == axisDomainOrder(i)) { |
---|
340 | eachDimSize[indexMap[i]] = domainP[idxDomain]->ni; |
---|
341 | eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj; |
---|
342 | idx += 2; ++idxDomain; |
---|
343 | } |
---|
344 | else if (1 == axisDomainOrder(i)) { |
---|
345 | eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements(); |
---|
346 | ++idx; ++idxAxis; |
---|
347 | } |
---|
348 | else {}; |
---|
349 | } |
---|
350 | |
---|
351 | if (!gridMask.isEmpty() && !createMask) |
---|
352 | { |
---|
353 | for (int i = 0; i < dim; ++i) |
---|
354 | { |
---|
355 | if (gridMask.extent(i) != eachDimSize[i]) |
---|
356 | ERROR("CGrid::checkMask(void)", |
---|
357 | << "The mask has one dimension whose size is different from the one of the local grid." << std::endl |
---|
358 | << "Local size of dimension " << i << " is " << eachDimSize[i] << "." << std::endl |
---|
359 | << "Mask size for dimension " << i << " is " << gridMask.extent(i) << "." << std::endl |
---|
360 | << "Grid = " << this->GetName()) |
---|
361 | } |
---|
362 | } |
---|
363 | else { |
---|
364 | CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize); |
---|
365 | gridMask = true; |
---|
366 | } |
---|
367 | |
---|
368 | int ssize = gridMask.numElements(); |
---|
369 | idx = 0; |
---|
370 | while (idx < ssize) |
---|
371 | { |
---|
372 | for (int i = 0; i < dim-1; ++i) |
---|
373 | { |
---|
374 | if (idxLoop[i] == eachDimSize[i]) |
---|
375 | { |
---|
376 | idxLoop[i] = 0; |
---|
377 | ++idxLoop[i+1]; |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | // Find out outer index |
---|
382 | idxDomain = idxAxis = 0; |
---|
383 | bool maskValue = true; |
---|
384 | for (int i = 0; i < numElement; ++i) |
---|
385 | { |
---|
386 | if (2 == axisDomainOrder(i)) |
---|
387 | { |
---|
388 | maskValue = maskValue && (*domainMasks[idxDomain])(idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]]); |
---|
389 | ++idxDomain; |
---|
390 | } |
---|
391 | else if (1 == axisDomainOrder(i)) |
---|
392 | { |
---|
393 | maskValue = maskValue && (*axisMasks[idxAxis])(idxLoop[indexMap[i]]); |
---|
394 | ++idxAxis; |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | int maskIndex = idxLoop[0]; |
---|
399 | int mulDim = 1; |
---|
400 | for (int k = 1; k < dim; ++k) |
---|
401 | { |
---|
402 | mulDim *= eachDimSize[k-1]; |
---|
403 | maskIndex += idxLoop[k]*mulDim; |
---|
404 | } |
---|
405 | *(gridMask.dataFirst()+maskIndex) &= maskValue; |
---|
406 | |
---|
407 | ++idxLoop[0]; |
---|
408 | ++idx; |
---|
409 | } |
---|
410 | |
---|
411 | } |
---|
412 | |
---|
413 | /*! |
---|
414 | Modify the current mask of grid, the local index to be modified will take value false |
---|
415 | \param [in/out] gridMask current mask of grid |
---|
416 | \param [in] indexToModify local index to modify |
---|
417 | */ |
---|
418 | template<int N> |
---|
419 | void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify) |
---|
420 | { |
---|
421 | bool valueToModify = false; |
---|
422 | int num = indexToModify.numElements(); |
---|
423 | for (int idx = 0; idx < num; ++idx) |
---|
424 | { |
---|
425 | *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify; |
---|
426 | } |
---|
427 | } |
---|
428 | ///-------------------------------------------------------------- |
---|
429 | |
---|
430 | // Declare/Define CGridGroup and CGridDefinition |
---|
431 | DECLARE_GROUP(CGrid); |
---|
432 | |
---|
433 | ///-------------------------------------------------------------- |
---|
434 | |
---|
435 | } // namespace xios |
---|
436 | |
---|
437 | #endif // __XIOS_CGrid__ |
---|