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 | |
---|
17 | namespace xios { |
---|
18 | |
---|
19 | |
---|
20 | CGenericAlgorithmTransformation* 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) |
---|
29 | TRY |
---|
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 | } |
---|
40 | CATCH |
---|
41 | |
---|
42 | bool CDomainAlgorithmGenerateRectilinear::dummyRegistered_ = CDomainAlgorithmGenerateRectilinear::registerTrans(); |
---|
43 | |
---|
44 | bool CDomainAlgorithmGenerateRectilinear::registerTrans() |
---|
45 | TRY |
---|
46 | { |
---|
47 | return CGridTransformationFactory<CDomain>::registerTransformation(TRANS_GENERATE_RECTILINEAR_DOMAIN, create); |
---|
48 | } |
---|
49 | CATCH |
---|
50 | |
---|
51 | |
---|
52 | |
---|
53 | CDomainAlgorithmGenerateRectilinear::CDomainAlgorithmGenerateRectilinear(bool isSource, CDomain* domainDestination, CDomain* domainSource, |
---|
54 | CGrid* gridDest, CGrid* gridSource, |
---|
55 | CGenerateRectilinearDomain* genRectDomain) |
---|
56 | : CAlgorithmTransformationNoDataModification(isSource), nbDomainDistributedPart_(0), domainDest_(domainDestination) |
---|
57 | TRY |
---|
58 | { |
---|
59 | genRectDomain->checkValid(domainDestination); |
---|
60 | if (0 != gridSource) computeDistributionGridSource(gridSource); |
---|
61 | else |
---|
62 | { |
---|
63 | computeDistributionGridDestination(gridDest); |
---|
64 | } |
---|
65 | fillInAttributesDomainDestination(); |
---|
66 | domainDestination->checkAttributes() ; |
---|
67 | } |
---|
68 | CATCH |
---|
69 | |
---|
70 | /*! |
---|
71 | Compute the index mapping between domain on grid source and one on grid destination |
---|
72 | */ |
---|
73 | void 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 | */ |
---|
81 | void CDomainAlgorithmGenerateRectilinear::computeDistributionGridSource(CGrid* gridSrc) |
---|
82 | TRY |
---|
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 | } |
---|
135 | CATCH |
---|
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 | */ |
---|
140 | void CDomainAlgorithmGenerateRectilinear::computeDistributionGridDestination(CGrid* gridDest) |
---|
141 | TRY |
---|
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 | } |
---|
163 | CATCH |
---|
164 | |
---|
165 | /*! |
---|
166 | Fill in all necessary attributes of domain destination and their values |
---|
167 | */ |
---|
168 | void CDomainAlgorithmGenerateRectilinear::fillInAttributesDomainDestination() |
---|
169 | TRY |
---|
170 | { |
---|
171 | if (!domainDest_->distributionAttributesHaveValue()) |
---|
172 | domainDest_->redistribute(nbDomainDistributedPart_); |
---|
173 | domainDest_->fillInLonLat(); |
---|
174 | } |
---|
175 | CATCH |
---|
176 | } |
---|