source: XIOS/dev/dev_olga/src/transformation/domain_algorithm_generate_rectilinear.cpp @ 1620

Last change on this file since 1620 was 1612, checked in by oabramkina, 5 years ago

Dev: adding exception handling.

To activate it, compilation flag -DXIOS_EXCEPTION should be added.

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  CContextClient* client = context->client;
51
52  std::vector<CDomain*> domListSrcP = gridSrc->getDomains();
53  std::vector<CAxis*> axisListSrcP = gridSrc->getAxis();
54
55  for (int i = 0; i < domListSrcP.size(); ++i) // support we have only domain, more than one, for now, dont know how to process
56  {
57    // First, find (roundly) distribution of associated axis (if any)
58    if (axisListSrcP.empty()) nbDomainDistributedPart_ = client->clientSize;
59    else
60    {
61      gridSrc->solveAxisRef(false);
62      int nbAxis = axisListSrcP.size();
63      std::vector<int> nbLocalAxis(nbAxis, 0);
64      for (int j = 0; j < nbAxis; ++j)
65      {
66        std::vector<int> globalAxisIndex(axisListSrcP[j]->n);
67        for (int idx = 0; idx < axisListSrcP[j]->n; ++idx)
68          globalAxisIndex[idx] = axisListSrcP[j]->begin + idx;
69        HashXIOS<int> hashFunc;
70        StdSize hashValue = hashFunc.hashVec(globalAxisIndex);
71        std::vector<StdSize> recvBuff(client->clientSize);
72        MPI_Gather(&hashValue, 1, MPI_UNSIGNED_LONG,
73                   &recvBuff[0], 1, MPI_UNSIGNED_LONG,
74                   0,
75                   client->intraComm);
76        if (0 == client->clientRank)
77        {
78          std::set<StdSize> setTmp;
79          for (int k = 0; k < recvBuff.size(); ++k)
80          {
81            if (setTmp.end() == setTmp.find(recvBuff[k]))
82            {
83              ++nbLocalAxis[j];
84              setTmp.insert(recvBuff[k]);
85            }
86          }
87        }
88
89        MPI_Bcast(&nbLocalAxis[0], nbAxis, MPI_INT,
90                  0, client->intraComm);
91      }
92
93      int nbAxisDistributedPart = 1;
94      for (int j = 0; j < nbAxis; ++j) nbAxisDistributedPart *= nbLocalAxis[j];
95      nbDomainDistributedPart_ = client->clientSize/nbAxisDistributedPart;
96    }
97  }
98}
99CATCH
100
101/*!
102  Compute the distribution of the domain destination by using available information provided by user such as n_distributed_partition of axis
103*/
104void CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest)
105TRY
106{
107  // For now, just suppose that the grid contains only one domain
108  std::vector<CAxis*> axisListDestP = gridDest->getAxis();
109  int nbPartition = 1, idx = 0;
110  for (int i = 0; i < gridDest->axis_domain_order.numElements(); ++i)
111  {
112    if (false == (gridDest->axis_domain_order)(i))
113    {
114      nbPartition *= (axisListDestP[idx]->n_distributed_partition.isEmpty()) ? 1: (axisListDestP[idx]->n_distributed_partition.getValue());
115      ++idx;
116    }
117  }
118
119  CContext* context = CContext::getCurrent();
120  CContextClient* client = context->client;
121  int modPart = (client->clientSize) % 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_ = client->clientSize/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.