1 | /*! |
---|
2 | \file axis_algorithm_redistribute.cpp |
---|
3 | \brief Algorithm for redistribute an axis. |
---|
4 | */ |
---|
5 | #include "axis_algorithm_redistribute.hpp" |
---|
6 | #include "redistribute_axis.hpp" |
---|
7 | #include "axis.hpp" |
---|
8 | #include "grid.hpp" |
---|
9 | #include "grid_transformation_factory_impl.hpp" |
---|
10 | #include "context.hpp" |
---|
11 | |
---|
12 | namespace xios |
---|
13 | { |
---|
14 | |
---|
15 | shared_ptr<CGenericAlgorithmTransformation> CAxisAlgorithmRedistribute::create(bool isSource, CGrid* gridDst, CGrid* gridSrc, |
---|
16 | CTransformation<CAxis>* transformation, |
---|
17 | int elementPositionInGrid, |
---|
18 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
19 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
20 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
21 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
22 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
23 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
24 | TRY |
---|
25 | { |
---|
26 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
27 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
28 | |
---|
29 | CRedistributeAxis* redistributeAxis = dynamic_cast<CRedistributeAxis*> (transformation); |
---|
30 | int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid]; |
---|
31 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
32 | |
---|
33 | return make_shared<CAxisAlgorithmRedistribute>(isSource, axisListDestP[axisDstIndex], axisListSrcP[axisSrcIndex], redistributeAxis); |
---|
34 | } |
---|
35 | CATCH |
---|
36 | |
---|
37 | bool CAxisAlgorithmRedistribute::dummyRegistered_ = CAxisAlgorithmRedistribute::registerTrans(); |
---|
38 | bool CAxisAlgorithmRedistribute::registerTrans() |
---|
39 | TRY |
---|
40 | { |
---|
41 | return CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDISTRIBUTE_AXIS, create); |
---|
42 | } |
---|
43 | CATCH |
---|
44 | |
---|
45 | CAxisAlgorithmRedistribute::CAxisAlgorithmRedistribute(bool isSource, CAxis* axisDestination, CAxis* axisSource, CRedistributeAxis* redistributeAxis) |
---|
46 | : CAlgorithmTransformationTransfer(isSource) |
---|
47 | TRY |
---|
48 | { |
---|
49 | CContext* context = CContext::getCurrent(); |
---|
50 | |
---|
51 | axisDestination->n_glo.reset(); |
---|
52 | axisDestination->index.reset(); |
---|
53 | axisDestination->n.reset(); |
---|
54 | axisDestination->begin.reset(); |
---|
55 | |
---|
56 | axisDestination->mask.reset(); |
---|
57 | axisDestination->data_index.reset(); |
---|
58 | axisDestination->data_n.reset(); |
---|
59 | axisDestination->data_begin.reset(); |
---|
60 | |
---|
61 | axisDestination->value.reset(); |
---|
62 | axisDestination->label.reset(); |
---|
63 | axisDestination->bounds.reset(); |
---|
64 | |
---|
65 | redistributeAxis->checkValid(axisSource); |
---|
66 | |
---|
67 | auto& index = redistributeAxis-> index ; |
---|
68 | int n_glo = axisSource -> n_glo ; |
---|
69 | axisDestination->n_glo = n_glo ; |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | // define the global index of the new domain |
---|
74 | |
---|
75 | CArray<size_t,1> globalIndex ; |
---|
76 | |
---|
77 | if (redistributeAxis->type == CRedistributeAxis::type_attr::index) |
---|
78 | { |
---|
79 | globalIndex.resize(index.numElements()) ; |
---|
80 | globalIndex=index ; |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | |
---|
85 | auto distType = CRedistributeAxis::type_attr::column ; |
---|
86 | |
---|
87 | if (redistributeAxis->type == CRedistributeAxis::type_attr::bands) distType = CRedistributeAxis::type_attr::column ; |
---|
88 | else distType=redistributeAxis->type; |
---|
89 | |
---|
90 | int size, start ; |
---|
91 | if (distType==CRedistributeAxis::type_attr::column) // Bands distribution to send to file server |
---|
92 | { |
---|
93 | int nbClient = context->getIntraCommSize() ; |
---|
94 | int rankClient = context->getIntraCommRank() ; |
---|
95 | |
---|
96 | size = n_glo/nbClient ; |
---|
97 | if (rankClient < n_glo % nbClient) |
---|
98 | { |
---|
99 | size++ ; |
---|
100 | start = rankClient*size ; |
---|
101 | } |
---|
102 | else start = (n_glo % nbClient)*(size+1) + (rankClient-(n_glo % nbClient)) * size ; |
---|
103 | |
---|
104 | } |
---|
105 | else if (distType==CRedistributeAxis::type_attr::full) // domain is not distributed ie all servers get the same local domain |
---|
106 | { |
---|
107 | size=n_glo ; start=0 ; |
---|
108 | } |
---|
109 | else if (distType==CRedistributeAxis::type_attr::root) // domain is not distributed ie all servers get the same local domain |
---|
110 | { |
---|
111 | if (context->getIntraCommRank()==0) { size=n_glo ; start=0 ; } |
---|
112 | else { size=0 ; start=0 ; } |
---|
113 | } |
---|
114 | |
---|
115 | globalIndex.resize(size) ; |
---|
116 | size_t ind ; |
---|
117 | ind=0 ; |
---|
118 | for(int i=start; i<start+size; i++) |
---|
119 | { |
---|
120 | globalIndex(ind) = i ; |
---|
121 | ind++ ; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | axisDestination->index.resize(globalIndex.numElements()) ; |
---|
126 | axisDestination->index = globalIndex ; |
---|
127 | // apply the transformation to attributes |
---|
128 | |
---|
129 | auto& transMap = this->transformationMapping_; |
---|
130 | for(int i=0; i<globalIndex.numElements(); i++) transMap[globalIndex(i)]=globalIndex(i) ; |
---|
131 | |
---|
132 | auto elementDst=shared_ptr<CLocalElement>(new CLocalElement(context->getIntraCommRank(), n_glo, globalIndex)) ; |
---|
133 | elementDst->addFullView() ; |
---|
134 | |
---|
135 | auto transformConnector = make_shared<CTransformConnector>(axisSource->getLocalView(CElementView::FULL), elementDst->getView(CElementView::FULL),context->getIntraComm()) ; |
---|
136 | transformConnector->computeConnector() ; |
---|
137 | |
---|
138 | if (axisSource->hasValue()) transformConnector->transfer(axisSource->value, axisDestination->value) ; |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | if (axisSource->hasBounds()) |
---|
143 | { |
---|
144 | CArray<double,1> boundsSrc(axisSource->bounds.dataFirst(),shape(axisSource->bounds.numElements()),neverDeleteData) ; |
---|
145 | CArray<double,1> boundsDst ; |
---|
146 | transformConnector->transfer(2, boundsSrc, boundsDst) ; |
---|
147 | axisDestination->bounds.resize(2,globalIndex.numElements()) ; |
---|
148 | axisDestination->bounds = CArray<double,2>(boundsDst.dataFirst(), shape(2,globalIndex.numElements()),neverDeleteData) ; |
---|
149 | } |
---|
150 | |
---|
151 | if (axisSource->hasLabel()) |
---|
152 | { |
---|
153 | //ym transferTransformConnector.transfer(axisSrc->label, axisDestination->label) ; -> probably not supported now, see later |
---|
154 | } |
---|
155 | |
---|
156 | // transfer the mask |
---|
157 | auto transformMask = make_shared<CTransformConnector>(axisSource->getLocalView(CElementView::WORKFLOW), elementDst->getView(CElementView::FULL),context->getIntraComm()) ; |
---|
158 | transformMask->computeConnector() ; |
---|
159 | |
---|
160 | CArray<bool,1> workflow(axisSource->getLocalView(CElementView::WORKFLOW)->getSize()) ; |
---|
161 | axisDestination->mask.resize(axisSource->getLocalView(CElementView::FULL)->getSize()) ; |
---|
162 | workflow=true ; |
---|
163 | |
---|
164 | transformMask->transfer(workflow,axisDestination->mask,false) ; |
---|
165 | |
---|
166 | axisDestination->checkAttributes() ; |
---|
167 | this->computeAlgorithm(axisSource->getLocalView(CElementView::WORKFLOW), axisDestination->getLocalView(CElementView::WORKFLOW)) ; |
---|
168 | } |
---|
169 | CATCH |
---|
170 | |
---|
171 | |
---|
172 | } |
---|