source: XIOS/dev/dev_ym/XIOS_COUPLING/src/transformation/domain_algorithm/domain_algorithm_generate_rectilinear.cpp @ 2016

Last change on this file since 2016 was 2016, checked in by ymipsl, 3 years ago

Transformation : fix problem xhen chaining transformation that generate the element (for now only generate_rectilinear_domain)
YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.7 KB
Line 
1/*!
2   \file domain_algorithm_generate_rectilinear.cpp
3   \author Ha NGUYEN
4   \since 31 Aug 2015
5   \date 31 Aug 2015
6
7   \brief Algorithm for automatic generation of rectilinear domain.
8 */
9#include "domain_algorithm_generate_rectilinear.hpp"
10#include "grid.hpp"
11#include "domain.hpp"
12#include "context.hpp"
13#include "context_client.hpp"
14#include "generate_rectilinear_domain.hpp"
15#include "grid_transformation_factory_impl.hpp"
16
17namespace xios {
18
19
20CGenericAlgorithmTransformation* CDomainAlgorithmGenerateRectilinear::create(bool isSource, CGrid* gridDst, CGrid* gridSrc,
21                                                                     CTransformation<CDomain>* transformation,
22                                                                     int elementPositionInGrid,
23                                                                     std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
24                                                                     std::map<int, int>& elementPositionInGridSrc2AxisPosition,
25                                                                     std::map<int, int>& elementPositionInGridSrc2DomainPosition,
26                                                                     std::map<int, int>& elementPositionInGridDst2ScalarPosition,
27                                                                     std::map<int, int>& elementPositionInGridDst2AxisPosition,
28                                                                     std::map<int, int>& elementPositionInGridDst2DomainPosition)
29TRY
30{
31  std::vector<CDomain*> domainListDestP = gridDst->getDomains();
32  std::vector<CDomain*> domainListSrcP  = gridSrc->getDomains();
33
34  CGenerateRectilinearDomain* transform = dynamic_cast<CGenerateRectilinearDomain*> (transformation);
35  int domainDstIndex = elementPositionInGridDst2DomainPosition[elementPositionInGrid];
36  int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid];
37
38  return (new CDomainAlgorithmGenerateRectilinear(isSource, domainListDestP[domainDstIndex], domainListSrcP[domainSrcIndex], gridDst, gridSrc, transform));
39}
40CATCH
41
42bool CDomainAlgorithmGenerateRectilinear::dummyRegistered_ = CDomainAlgorithmGenerateRectilinear::registerTrans();
43
44bool CDomainAlgorithmGenerateRectilinear::registerTrans()
45TRY
46{
47  return CGridTransformationFactory<CDomain>::registerTransformation(TRANS_GENERATE_RECTILINEAR_DOMAIN, create);
48}
49CATCH
50
51
52
53CDomainAlgorithmGenerateRectilinear::CDomainAlgorithmGenerateRectilinear(bool isSource, CDomain* domainDestination, CDomain* domainSource,
54                                                                         CGrid* gridDest, CGrid* gridSource,
55                                                                         CGenerateRectilinearDomain* genRectDomain)
56: CAlgorithmTransformationNoDataModification(isSource), nbDomainDistributedPart_(0), domainDest_(domainDestination)
57TRY
58{
59  genRectDomain->checkValid(domainDestination);
60  if (0 != gridSource) computeDistributionGridSource(gridSource);
61  else
62  {
63    computeDistributionGridDestination(gridDest);
64  }
65  fillInAttributesDomainDestination();
66  domainDestination->checkAttributes() ;
67}
68CATCH
69
70/*!
71  Compute the index mapping between domain on grid source and one on grid destination
72*/
73void CDomainAlgorithmGenerateRectilinear::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
74{
75 /* Nothing to do */
76}
77
78/*!
79  Calculate the number of distributed parts on domain source
80*/
81void CDomainAlgorithmGenerateRectilinear::computeDistributionGridSource(CGrid* gridSrc)
82TRY
83{
84  CContext* context = CContext::getCurrent();
85  int clientSize = context->intraCommSize_ ;
86  int clientRank = context->intraCommRank_ ;
87 
88  std::vector<CDomain*> domListSrcP = gridSrc->getDomains();
89  std::vector<CAxis*> axisListSrcP = gridSrc->getAxis();
90
91  for (int i = 0; i < domListSrcP.size(); ++i) // support we have only domain, more than one, for now, dont know how to process
92  {
93    // First, find (roundly) distribution of associated axis (if any)
94    if (axisListSrcP.empty()) nbDomainDistributedPart_ = clientSize;
95    else
96    {
97      gridSrc->solveAxisRef(false);
98      int nbAxis = axisListSrcP.size();
99      std::vector<int> nbLocalAxis(nbAxis, 0);
100      for (int j = 0; j < nbAxis; ++j)
101      {
102        std::vector<int> globalAxisIndex(axisListSrcP[j]->n);
103        for (int idx = 0; idx < axisListSrcP[j]->n; ++idx)
104          globalAxisIndex[idx] = axisListSrcP[j]->begin + idx;
105        HashXIOS<int> hashFunc;
106        StdSize hashValue = hashFunc.hashVec(globalAxisIndex);
107        std::vector<StdSize> recvBuff(clientSize);
108        MPI_Gather(&hashValue, 1, MPI_UNSIGNED_LONG,
109                   &recvBuff[0], 1, MPI_UNSIGNED_LONG,
110                   0,
111                   context->intraComm_);
112        if (0 == clientRank)
113        {
114          std::set<StdSize> setTmp;
115          for (int k = 0; k < recvBuff.size(); ++k)
116          {
117            if (setTmp.end() == setTmp.find(recvBuff[k]))
118            {
119              ++nbLocalAxis[j];
120              setTmp.insert(recvBuff[k]);
121            }
122          }
123        }
124
125        MPI_Bcast(&nbLocalAxis[0], nbAxis, MPI_INT,
126                  0, context->intraComm_);
127      }
128
129      int nbAxisDistributedPart = 1;
130      for (int j = 0; j < nbAxis; ++j) nbAxisDistributedPart *= nbLocalAxis[j];
131      nbDomainDistributedPart_ = clientSize/nbAxisDistributedPart;
132    }
133  }
134}
135CATCH
136
137/*!
138  Compute the distribution of the domain destination by using available information provided by user such as n_distributed_partition of axis
139*/
140void CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)
141TRY
142{
143  // For now, just suppose that the grid contains only one domain
144  std::vector<CAxis*> axisListDestP = gridDest->getAxis();
145  int nbPartition = 1, idx = 0;
146  for (int i = 0; i < gridDest->axis_domain_order.numElements(); ++i)
147  {
148    if (false == (gridDest->axis_domain_order)(i))
149    {
150      nbPartition *= (axisListDestP[idx]->n_distributed_partition.isEmpty()) ? 1: (axisListDestP[idx]->n_distributed_partition.getValue());
151      ++idx;
152    }
153  }
154
155  CContext* context = CContext::getCurrent();
156  int modPart = (context->intraCommSize_) % nbPartition;
157  if (0 != modPart)
158    ERROR("CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)",
159       << "The grid " <<gridDest->getId() << " is not well-distributed. There is an incompatibility between distribution of axis and domain.");
160  nbDomainDistributedPart_ = context->intraCommSize_/nbPartition;
161
162}
163CATCH
164
165/*!
166  Fill in all necessary attributes of domain destination and their values
167*/
168void CDomainAlgorithmGenerateRectilinear::fillInAttributesDomainDestination()
169TRY
170{
171  if (!domainDest_->distributionAttributesHaveValue())
172    domainDest_->redistribute(nbDomainDistributedPart_);
173  domainDest_->fillInLonLat();
174}
175CATCH
176}
Note: See TracBrowser for help on using the repository browser.