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

Last change on this file since 2002 was 1998, checked in by ymipsl, 4 years ago

Adapt transformation algorithm to new infrastructure (on going...)

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}
67CATCH
68
69/*!
70  Compute the index mapping between domain on grid source and one on grid destination
71*/
72void CDomainAlgorithmGenerateRectilinear::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
73{
74 /* Nothing to do */
75}
76
77/*!
78  Calculate the number of distributed parts on domain source
79*/
80void CDomainAlgorithmGenerateRectilinear::computeDistributionGridSource(CGrid* gridSrc)
81TRY
82{
83  CContext* context = CContext::getCurrent();
84  int clientSize = context->intraCommSize_ ;
85  int clientRank = context->intraCommRank_ ;
86 
87  std::vector<CDomain*> domListSrcP = gridSrc->getDomains();
88  std::vector<CAxis*> axisListSrcP = gridSrc->getAxis();
89
90  for (int i = 0; i < domListSrcP.size(); ++i) // support we have only domain, more than one, for now, dont know how to process
91  {
92    // First, find (roundly) distribution of associated axis (if any)
93    if (axisListSrcP.empty()) nbDomainDistributedPart_ = clientSize;
94    else
95    {
96      gridSrc->solveAxisRef(false);
97      int nbAxis = axisListSrcP.size();
98      std::vector<int> nbLocalAxis(nbAxis, 0);
99      for (int j = 0; j < nbAxis; ++j)
100      {
101        std::vector<int> globalAxisIndex(axisListSrcP[j]->n);
102        for (int idx = 0; idx < axisListSrcP[j]->n; ++idx)
103          globalAxisIndex[idx] = axisListSrcP[j]->begin + idx;
104        HashXIOS<int> hashFunc;
105        StdSize hashValue = hashFunc.hashVec(globalAxisIndex);
106        std::vector<StdSize> recvBuff(clientSize);
107        MPI_Gather(&hashValue, 1, MPI_UNSIGNED_LONG,
108                   &recvBuff[0], 1, MPI_UNSIGNED_LONG,
109                   0,
110                   context->intraComm_);
111        if (0 == clientRank)
112        {
113          std::set<StdSize> setTmp;
114          for (int k = 0; k < recvBuff.size(); ++k)
115          {
116            if (setTmp.end() == setTmp.find(recvBuff[k]))
117            {
118              ++nbLocalAxis[j];
119              setTmp.insert(recvBuff[k]);
120            }
121          }
122        }
123
124        MPI_Bcast(&nbLocalAxis[0], nbAxis, MPI_INT,
125                  0, context->intraComm_);
126      }
127
128      int nbAxisDistributedPart = 1;
129      for (int j = 0; j < nbAxis; ++j) nbAxisDistributedPart *= nbLocalAxis[j];
130      nbDomainDistributedPart_ = clientSize/nbAxisDistributedPart;
131    }
132  }
133}
134CATCH
135
136/*!
137  Compute the distribution of the domain destination by using available information provided by user such as n_distributed_partition of axis
138*/
139void CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)
140TRY
141{
142  // For now, just suppose that the grid contains only one domain
143  std::vector<CAxis*> axisListDestP = gridDest->getAxis();
144  int nbPartition = 1, idx = 0;
145  for (int i = 0; i < gridDest->axis_domain_order.numElements(); ++i)
146  {
147    if (false == (gridDest->axis_domain_order)(i))
148    {
149      nbPartition *= (axisListDestP[idx]->n_distributed_partition.isEmpty()) ? 1: (axisListDestP[idx]->n_distributed_partition.getValue());
150      ++idx;
151    }
152  }
153
154  CContext* context = CContext::getCurrent();
155  int modPart = (context->intraCommSize_) % nbPartition;
156  if (0 != modPart)
157    ERROR("CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)",
158       << "The grid " <<gridDest->getId() << " is not well-distributed. There is an incompatibility between distribution of axis and domain.");
159  nbDomainDistributedPart_ = context->intraCommSize_/nbPartition;
160
161}
162CATCH
163
164/*!
165  Fill in all necessary attributes of domain destination and their values
166*/
167void CDomainAlgorithmGenerateRectilinear::fillInAttributesDomainDestination()
168TRY
169{
170  if (!domainDest_->distributionAttributesHaveValue())
171    domainDest_->redistribute(nbDomainDistributedPart_);
172  domainDest_->fillInLonLat();
173}
174CATCH
175}
Note: See TracBrowser for help on using the repository browser.