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