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