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