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

Last change on this file since 1853 was 1784, checked in by ymipsl, 4 years ago
  • Preparing coupling functionalities.
  • Make some cleaner things

YM

File size: 4.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
16namespace xios {
17
18CDomainAlgorithmGenerateRectilinear::CDomainAlgorithmGenerateRectilinear(CDomain* domainDestination, CDomain* domainSource,
19                                                                         CGrid* gridDest, CGrid* gridSource,
20                                                                         CGenerateRectilinearDomain* genRectDomain)
21: CDomainAlgorithmTransformation(domainDestination, domainSource), nbDomainDistributedPart_(0)
22TRY
23{
24  type_ = ELEMENT_GENERATION;
25  genRectDomain->checkValid(domainDestination);
26  if (0 != gridSource) computeDistributionGridSource(gridSource);
27  else
28  {
29    computeDistributionGridDestination(gridDest);
30  }
31  fillInAttributesDomainDestination();
32}
33CATCH
34
35/*!
36  Compute the index mapping between domain on grid source and one on grid destination
37*/
38void CDomainAlgorithmGenerateRectilinear::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
39{
40 /* Nothing to do */
41}
42
43/*!
44  Calculate the number of distributed parts on domain source
45*/
46void CDomainAlgorithmGenerateRectilinear::computeDistributionGridSource(CGrid* gridSrc)
47TRY
48{
49  CContext* context = CContext::getCurrent();
50  int clientSize = context->intraCommSize_ ;
51  int clientRank = context->intraCommRank_ ;
52 
53  std::vector<CDomain*> domListSrcP = gridSrc->getDomains();
54  std::vector<CAxis*> axisListSrcP = gridSrc->getAxis();
55
56  for (int i = 0; i < domListSrcP.size(); ++i) // support we have only domain, more than one, for now, dont know how to process
57  {
58    // First, find (roundly) distribution of associated axis (if any)
59    if (axisListSrcP.empty()) nbDomainDistributedPart_ = clientSize;
60    else
61    {
62      gridSrc->solveAxisRef(false);
63      int nbAxis = axisListSrcP.size();
64      std::vector<int> nbLocalAxis(nbAxis, 0);
65      for (int j = 0; j < nbAxis; ++j)
66      {
67        std::vector<int> globalAxisIndex(axisListSrcP[j]->n);
68        for (int idx = 0; idx < axisListSrcP[j]->n; ++idx)
69          globalAxisIndex[idx] = axisListSrcP[j]->begin + idx;
70        HashXIOS<int> hashFunc;
71        StdSize hashValue = hashFunc.hashVec(globalAxisIndex);
72        std::vector<StdSize> recvBuff(clientSize);
73        MPI_Gather(&hashValue, 1, MPI_UNSIGNED_LONG,
74                   &recvBuff[0], 1, MPI_UNSIGNED_LONG,
75                   0,
76                   context->intraComm_);
77        if (0 == clientRank)
78        {
79          std::set<StdSize> setTmp;
80          for (int k = 0; k < recvBuff.size(); ++k)
81          {
82            if (setTmp.end() == setTmp.find(recvBuff[k]))
83            {
84              ++nbLocalAxis[j];
85              setTmp.insert(recvBuff[k]);
86            }
87          }
88        }
89
90        MPI_Bcast(&nbLocalAxis[0], nbAxis, MPI_INT,
91                  0, context->intraComm_);
92      }
93
94      int nbAxisDistributedPart = 1;
95      for (int j = 0; j < nbAxis; ++j) nbAxisDistributedPart *= nbLocalAxis[j];
96      nbDomainDistributedPart_ = clientSize/nbAxisDistributedPart;
97    }
98  }
99}
100CATCH
101
102/*!
103  Compute the distribution of the domain destination by using available information provided by user such as n_distributed_partition of axis
104*/
105void CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)
106TRY
107{
108  // For now, just suppose that the grid contains only one domain
109  std::vector<CAxis*> axisListDestP = gridDest->getAxis();
110  int nbPartition = 1, idx = 0;
111  for (int i = 0; i < gridDest->axis_domain_order.numElements(); ++i)
112  {
113    if (false == (gridDest->axis_domain_order)(i))
114    {
115      nbPartition *= (axisListDestP[idx]->n_distributed_partition.isEmpty()) ? 1: (axisListDestP[idx]->n_distributed_partition.getValue());
116      ++idx;
117    }
118  }
119
120  CContext* context = CContext::getCurrent();
121  int modPart = (context->intraCommSize_) % nbPartition;
122  if (0 != modPart)
123    ERROR("CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)",
124       << "The grid " <<gridDest->getId() << " is not well-distributed. There is an incompatibility between distribution of axis and domain.");
125  nbDomainDistributedPart_ = context->intraCommSize_/nbPartition;
126
127}
128CATCH
129
130/*!
131  Fill in all necessary attributes of domain destination and their values
132*/
133void CDomainAlgorithmGenerateRectilinear::fillInAttributesDomainDestination()
134TRY
135{
136  if (!domainDest_->distributionAttributesHaveValue())
137    domainDest_->redistribute(nbDomainDistributedPart_);
138  domainDest_->fillInLonLat();
139}
140CATCH
141}
Note: See TracBrowser for help on using the repository browser.