1 | #include "spatial_transform_filter.hpp" |
---|
2 | #include "grid_transformation.hpp" |
---|
3 | #include "context.hpp" |
---|
4 | #include "context_client.hpp" |
---|
5 | #include "timer.hpp" |
---|
6 | |
---|
7 | namespace xios |
---|
8 | { |
---|
9 | CSpatialTransformFilter::CSpatialTransformFilter(CGarbageCollector& gc, CSpatialTransformFilterEngine* engine, double outputValue, size_t inputSlotsCount) |
---|
10 | : CFilter(gc, inputSlotsCount, engine), outputDefaultValue(outputValue) |
---|
11 | { /* Nothing to do */ } |
---|
12 | |
---|
13 | std::pair<boost::shared_ptr<CSpatialTransformFilter>, boost::shared_ptr<CSpatialTransformFilter> > |
---|
14 | CSpatialTransformFilter::buildFilterGraph(CGarbageCollector& gc, CGrid* srcGrid, CGrid* destGrid, bool hasMissingValue, double missingValue) |
---|
15 | { |
---|
16 | if (!srcGrid || !destGrid) |
---|
17 | ERROR("std::pair<boost::shared_ptr<CSpatialTransformFilter>, boost::shared_ptr<CSpatialTransformFilter> >" |
---|
18 | "buildFilterGraph(CGarbageCollector& gc, CGrid* srcGrid, CGrid* destGrid)", |
---|
19 | "Impossible to build the filter graph if either the source or the destination grid are null."); |
---|
20 | |
---|
21 | boost::shared_ptr<CSpatialTransformFilter> firstFilter, lastFilter; |
---|
22 | // Note that this loop goes from the last transformation to the first transformation |
---|
23 | do |
---|
24 | { |
---|
25 | CGridTransformation* gridTransformation = destGrid->getTransformations(); |
---|
26 | CSpatialTransformFilterEngine* engine = CSpatialTransformFilterEngine::get(destGrid->getTransformations()); |
---|
27 | const std::vector<StdString>& auxInputs = gridTransformation->getAuxInputs(); |
---|
28 | size_t inputCount = 1 + (auxInputs.empty() ? 0 : auxInputs.size()); |
---|
29 | double defaultValue = (hasMissingValue) ? std::numeric_limits<double>::quiet_NaN() : 0.0; |
---|
30 | |
---|
31 | |
---|
32 | const CGridTransformationSelector::ListAlgoType& algoList = gridTransformation->getAlgoList() ; |
---|
33 | CGridTransformationSelector::ListAlgoType::const_iterator it ; |
---|
34 | |
---|
35 | bool isSpatialTemporal=false ; |
---|
36 | for (it=algoList.begin();it!=algoList.end();++it) if (it->second.first == TRANS_TEMPORAL_SPLITTING) isSpatialTemporal=true ; |
---|
37 | |
---|
38 | boost::shared_ptr<CSpatialTransformFilter> filter ; |
---|
39 | if( isSpatialTemporal) filter = boost::shared_ptr<CSpatialTransformFilter>(new CSpatialTemporalFilter(gc, engine, gridTransformation, defaultValue, inputCount)); |
---|
40 | else filter = boost::shared_ptr<CSpatialTransformFilter>(new CSpatialTransformFilter(gc, engine, defaultValue, inputCount)); |
---|
41 | |
---|
42 | |
---|
43 | if (!lastFilter) |
---|
44 | lastFilter = filter; |
---|
45 | else |
---|
46 | filter->connectOutput(firstFilter, 0); |
---|
47 | |
---|
48 | firstFilter = filter; |
---|
49 | for (size_t idx = 0; idx < auxInputs.size(); ++idx) |
---|
50 | { |
---|
51 | CField* fieldAuxInput = CField::get(auxInputs[idx]); |
---|
52 | fieldAuxInput->buildFilterGraph(gc, false); |
---|
53 | fieldAuxInput->getInstantDataFilter()->connectOutput(firstFilter,idx+1); |
---|
54 | } |
---|
55 | |
---|
56 | destGrid = gridTransformation->getGridSource(); |
---|
57 | } |
---|
58 | while (destGrid != srcGrid); |
---|
59 | |
---|
60 | return std::make_pair(firstFilter, lastFilter); |
---|
61 | } |
---|
62 | |
---|
63 | void CSpatialTransformFilter::onInputReady(std::vector<CDataPacketPtr> data) |
---|
64 | { |
---|
65 | CSpatialTransformFilterEngine* spaceFilter = static_cast<CSpatialTransformFilterEngine*>(engine); |
---|
66 | CDataPacketPtr outputPacket = spaceFilter->applyFilter(data, outputDefaultValue); |
---|
67 | if (outputPacket) |
---|
68 | onOutputReady(outputPacket); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | CSpatialTemporalFilter::CSpatialTemporalFilter(CGarbageCollector& gc, CSpatialTransformFilterEngine* engine, CGridTransformation* gridTransformation, double outputValue, size_t inputSlotsCount) |
---|
76 | : CSpatialTransformFilter(gc, engine, outputValue, inputSlotsCount), record(0) |
---|
77 | { |
---|
78 | const CGridTransformationSelector::ListAlgoType& algoList = gridTransformation->getAlgoList() ; |
---|
79 | CGridTransformationSelector::ListAlgoType::const_iterator it ; |
---|
80 | |
---|
81 | int pos ; |
---|
82 | for (it=algoList.begin();it!=algoList.end();++it) |
---|
83 | if (it->second.first == TRANS_TEMPORAL_SPLITTING) |
---|
84 | { |
---|
85 | pos=it->first ; |
---|
86 | if (pos < algoList.size()-1) |
---|
87 | ERROR("SpatialTemporalFilter::CSpatialTemporalFilter(CGarbageCollector& gc, CSpatialTransformFilterEngine* engine, CGridTransformation* gridTransformation, double outputValue, size_t inputSlotsCount))", |
---|
88 | "temporal splitting operation must be the last of whole transformation on same grid") ; |
---|
89 | } |
---|
90 | |
---|
91 | CGrid* grid=gridTransformation->getGridDestination() ; |
---|
92 | |
---|
93 | CAxis* axis = grid->getAxis(gridTransformation->getElementPositionInGridDst2AxisPosition().find(pos)->second) ; |
---|
94 | |
---|
95 | nrecords = axis->index.numElements() ; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void CSpatialTemporalFilter::onInputReady(std::vector<CDataPacketPtr> data) |
---|
100 | { |
---|
101 | CSpatialTransformFilterEngine* spaceFilter = static_cast<CSpatialTransformFilterEngine*>(engine); |
---|
102 | CDataPacketPtr outputPacket = spaceFilter->applyFilter(data, outputDefaultValue); |
---|
103 | |
---|
104 | if (outputPacket) |
---|
105 | { |
---|
106 | size_t nelements=outputPacket->data.numElements() ; |
---|
107 | if (!tmpData.numElements()) |
---|
108 | { |
---|
109 | tmpData.resize(nelements); |
---|
110 | tmpData=outputDefaultValue ; |
---|
111 | } |
---|
112 | |
---|
113 | nelements/=nrecords ; |
---|
114 | size_t offset=nelements*record ; |
---|
115 | for(size_t i=0;i<nelements;++i) tmpData(i+offset) = outputPacket->data(i) ; |
---|
116 | |
---|
117 | record ++ ; |
---|
118 | if (record==nrecords) |
---|
119 | { |
---|
120 | record=0 ; |
---|
121 | CDataPacketPtr packet = CDataPacketPtr(new CDataPacket); |
---|
122 | packet->date = data[0]->date; |
---|
123 | packet->timestamp = data[0]->timestamp; |
---|
124 | packet->status = data[0]->status; |
---|
125 | packet->data.resize(tmpData.numElements()); |
---|
126 | packet->data = tmpData; |
---|
127 | onOutputReady(packet); |
---|
128 | tmpData.resize(0) ; |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | CSpatialTransformFilterEngine::CSpatialTransformFilterEngine(CGridTransformation* gridTransformation) |
---|
135 | : gridTransformation(gridTransformation) |
---|
136 | { |
---|
137 | if (!gridTransformation) |
---|
138 | ERROR("CSpatialTransformFilterEngine::CSpatialTransformFilterEngine(CGridTransformation* gridTransformation)", |
---|
139 | "Impossible to construct a spatial transform filter engine without a valid grid transformation."); |
---|
140 | } |
---|
141 | |
---|
142 | std::map<CGridTransformation*, boost::shared_ptr<CSpatialTransformFilterEngine> > CSpatialTransformFilterEngine::engines; |
---|
143 | |
---|
144 | CSpatialTransformFilterEngine* CSpatialTransformFilterEngine::get(CGridTransformation* gridTransformation) |
---|
145 | { |
---|
146 | if (!gridTransformation) |
---|
147 | ERROR("CSpatialTransformFilterEngine& CSpatialTransformFilterEngine::get(CGridTransformation* gridTransformation)", |
---|
148 | "Impossible to get the requested engine, the grid transformation is invalid."); |
---|
149 | |
---|
150 | std::map<CGridTransformation*, boost::shared_ptr<CSpatialTransformFilterEngine> >::iterator it = engines.find(gridTransformation); |
---|
151 | if (it == engines.end()) |
---|
152 | { |
---|
153 | boost::shared_ptr<CSpatialTransformFilterEngine> engine(new CSpatialTransformFilterEngine(gridTransformation)); |
---|
154 | it = engines.insert(std::make_pair(gridTransformation, engine)).first; |
---|
155 | } |
---|
156 | |
---|
157 | return it->second.get(); |
---|
158 | } |
---|
159 | |
---|
160 | CDataPacketPtr CSpatialTransformFilterEngine::apply(std::vector<CDataPacketPtr> data) |
---|
161 | { |
---|
162 | /* Nothing to do */ |
---|
163 | } |
---|
164 | |
---|
165 | CDataPacketPtr CSpatialTransformFilterEngine::applyFilter(std::vector<CDataPacketPtr> data, double defaultValue) |
---|
166 | { |
---|
167 | CDataPacketPtr packet(new CDataPacket); |
---|
168 | packet->date = data[0]->date; |
---|
169 | packet->timestamp = data[0]->timestamp; |
---|
170 | packet->status = data[0]->status; |
---|
171 | |
---|
172 | if (packet->status == CDataPacket::NO_ERROR) |
---|
173 | { |
---|
174 | if (1 < data.size()) // Dynamical transformations |
---|
175 | { |
---|
176 | std::vector<CArray<double,1>* > dataAuxInputs(data.size()-1); |
---|
177 | for (size_t idx = 0; idx < dataAuxInputs.size(); ++idx) dataAuxInputs[idx] = &(data[idx+1]->data); |
---|
178 | gridTransformation->computeAll(dataAuxInputs, packet->timestamp); |
---|
179 | } |
---|
180 | packet->data.resize(gridTransformation->getGridDestination()->storeIndex_client.numElements()); |
---|
181 | if (0 != packet->data.numElements()) |
---|
182 | (packet->data)(0) = defaultValue; |
---|
183 | apply(data[0]->data, packet->data); |
---|
184 | } |
---|
185 | |
---|
186 | return packet; |
---|
187 | } |
---|
188 | |
---|
189 | void CSpatialTransformFilterEngine::apply(const CArray<double, 1>& dataSrc, CArray<double,1>& dataDest) |
---|
190 | { |
---|
191 | CTimer::get("CSpatialTransformFilterEngine::apply").suspend(); ; |
---|
192 | CContextClient* client = CContext::getCurrent()->client; |
---|
193 | |
---|
194 | // Get default value for output data |
---|
195 | bool ignoreMissingValue = false; |
---|
196 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
197 | if (0 != dataDest.numElements()) ignoreMissingValue = NumTraits<double>::isnan(dataDest(0)); |
---|
198 | |
---|
199 | const std::list<CGridTransformation::SendingIndexGridSourceMap>& listLocalIndexSend = gridTransformation->getLocalIndexToSendFromGridSource(); |
---|
200 | const std::list<CGridTransformation::RecvIndexGridDestinationMap>& listLocalIndexToReceive = gridTransformation->getLocalIndexToReceiveOnGridDest(); |
---|
201 | const std::list<size_t>& listNbLocalIndexToReceive = gridTransformation->getNbLocalIndexToReceiveOnGridDest(); |
---|
202 | const std::list<std::vector<bool> >& listLocalIndexMaskOnDest = gridTransformation->getLocalMaskIndexOnGridDest(); |
---|
203 | const std::vector<CGenericAlgorithmTransformation*>& listAlgos = gridTransformation->getAlgos(); |
---|
204 | |
---|
205 | CArray<double,1> dataCurrentDest(dataSrc.copy()); |
---|
206 | |
---|
207 | std::list<CGridTransformation::SendingIndexGridSourceMap>::const_iterator itListSend = listLocalIndexSend.begin(), |
---|
208 | iteListSend = listLocalIndexSend.end(); |
---|
209 | std::list<CGridTransformation::RecvIndexGridDestinationMap>::const_iterator itListRecv = listLocalIndexToReceive.begin(); |
---|
210 | std::list<size_t>::const_iterator itNbListRecv = listNbLocalIndexToReceive.begin(); |
---|
211 | std::list<std::vector<bool> >::const_iterator itLocalMaskIndexOnDest = listLocalIndexMaskOnDest.begin(); |
---|
212 | std::vector<CGenericAlgorithmTransformation*>::const_iterator itAlgo = listAlgos.begin(); |
---|
213 | |
---|
214 | for (; itListSend != iteListSend; ++itListSend, ++itListRecv, ++itNbListRecv, ++itLocalMaskIndexOnDest, ++itAlgo) |
---|
215 | { |
---|
216 | CArray<double,1> dataCurrentSrc(dataCurrentDest); |
---|
217 | const CGridTransformation::SendingIndexGridSourceMap& localIndexToSend = *itListSend; |
---|
218 | |
---|
219 | // Sending data from field sources to do transformations |
---|
220 | std::map<int, CArray<int,1> >::const_iterator itbSend = localIndexToSend.begin(), itSend, |
---|
221 | iteSend = localIndexToSend.end(); |
---|
222 | int idxSendBuff = 0; |
---|
223 | std::vector<double*> sendBuff(localIndexToSend.size()); |
---|
224 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
225 | { |
---|
226 | if (0 != itSend->second.numElements()) |
---|
227 | sendBuff[idxSendBuff] = new double[itSend->second.numElements()]; |
---|
228 | } |
---|
229 | |
---|
230 | idxSendBuff = 0; |
---|
231 | std::vector<MPI_Request> sendRecvRequest; |
---|
232 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
233 | { |
---|
234 | int destRank = itSend->first; |
---|
235 | const CArray<int,1>& localIndex_p = itSend->second; |
---|
236 | int countSize = localIndex_p.numElements(); |
---|
237 | for (int idx = 0; idx < countSize; ++idx) |
---|
238 | { |
---|
239 | sendBuff[idxSendBuff][idx] = dataCurrentSrc(localIndex_p(idx)); |
---|
240 | } |
---|
241 | sendRecvRequest.push_back(MPI_Request()); |
---|
242 | MPI_Isend(sendBuff[idxSendBuff], countSize, MPI_DOUBLE, destRank, 12, client->intraComm, &sendRecvRequest.back()); |
---|
243 | } |
---|
244 | |
---|
245 | // Receiving data on destination fields |
---|
246 | const CGridTransformation::RecvIndexGridDestinationMap& localIndexToReceive = *itListRecv; |
---|
247 | CGridTransformation::RecvIndexGridDestinationMap::const_iterator itbRecv = localIndexToReceive.begin(), itRecv, |
---|
248 | iteRecv = localIndexToReceive.end(); |
---|
249 | int recvBuffSize = 0; |
---|
250 | for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) recvBuffSize += itRecv->second.size(); //(recvBuffSize < itRecv->second.size()) |
---|
251 | //? itRecv->second.size() : recvBuffSize; |
---|
252 | double* recvBuff; |
---|
253 | if (0 != recvBuffSize) recvBuff = new double[recvBuffSize]; |
---|
254 | int currentBuff = 0; |
---|
255 | for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) |
---|
256 | { |
---|
257 | int srcRank = itRecv->first; |
---|
258 | int countSize = itRecv->second.size(); |
---|
259 | sendRecvRequest.push_back(MPI_Request()); |
---|
260 | MPI_Irecv(recvBuff + currentBuff, countSize, MPI_DOUBLE, srcRank, 12, client->intraComm, &sendRecvRequest.back()); |
---|
261 | currentBuff += countSize; |
---|
262 | } |
---|
263 | std::vector<MPI_Status> status(sendRecvRequest.size()); |
---|
264 | MPI_Waitall(sendRecvRequest.size(), &sendRecvRequest[0], &status[0]); |
---|
265 | |
---|
266 | dataCurrentDest.resize(*itNbListRecv); |
---|
267 | const std::vector<bool>& localMaskDest = *itLocalMaskIndexOnDest; |
---|
268 | for (int i = 0; i < localMaskDest.size(); ++i) |
---|
269 | if (localMaskDest[i]) dataCurrentDest(i) = 0.0; |
---|
270 | else dataCurrentDest(i) = defaultValue; |
---|
271 | |
---|
272 | std::vector<bool> localInitFlag(dataCurrentDest.numElements(), true); |
---|
273 | currentBuff = 0; |
---|
274 | bool firstPass=true; |
---|
275 | for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) |
---|
276 | { |
---|
277 | int countSize = itRecv->second.size(); |
---|
278 | const std::vector<std::pair<int,double> >& localIndex_p = itRecv->second; |
---|
279 | (*itAlgo)->apply(localIndex_p, |
---|
280 | recvBuff+currentBuff, |
---|
281 | dataCurrentDest, |
---|
282 | localInitFlag, |
---|
283 | ignoreMissingValue,firstPass); |
---|
284 | |
---|
285 | currentBuff += countSize; |
---|
286 | firstPass=false ; |
---|
287 | } |
---|
288 | |
---|
289 | (*itAlgo)->updateData(dataCurrentDest); |
---|
290 | |
---|
291 | idxSendBuff = 0; |
---|
292 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
293 | { |
---|
294 | if (0 != itSend->second.numElements()) |
---|
295 | delete [] sendBuff[idxSendBuff]; |
---|
296 | } |
---|
297 | if (0 != recvBuffSize) delete [] recvBuff; |
---|
298 | } |
---|
299 | if (dataCurrentDest.numElements() != dataDest.numElements()) |
---|
300 | ERROR("CSpatialTransformFilterEngine::apply(const CArray<double, 1>& dataSrc, CArray<double,1>& dataDest)", |
---|
301 | "Incoherent between the received size and expected size. " << std::endl |
---|
302 | << "Expected size: " << dataDest.numElements() << std::endl |
---|
303 | << "Received size: " << dataCurrentDest.numElements()); |
---|
304 | |
---|
305 | dataDest = dataCurrentDest; |
---|
306 | |
---|
307 | CTimer::get("CSpatialTransformFilterEngine::apply").resume() ; |
---|
308 | } |
---|
309 | } // namespace xios |
---|