[644] | 1 | #include "spatial_transform_filter.hpp" |
---|
| 2 | #include "grid_transformation.hpp" |
---|
| 3 | #include "context.hpp" |
---|
| 4 | #include "context_client.hpp" |
---|
[1412] | 5 | #include "timer.hpp" |
---|
[644] | 6 | |
---|
| 7 | namespace xios |
---|
| 8 | { |
---|
[873] | 9 | CSpatialTransformFilter::CSpatialTransformFilter(CGarbageCollector& gc, CSpatialTransformFilterEngine* engine, double outputValue, size_t inputSlotsCount) |
---|
| 10 | : CFilter(gc, inputSlotsCount, engine), outputDefaultValue(outputValue) |
---|
[644] | 11 | { /* Nothing to do */ } |
---|
| 12 | |
---|
| 13 | std::pair<boost::shared_ptr<CSpatialTransformFilter>, boost::shared_ptr<CSpatialTransformFilter> > |
---|
[1158] | 14 | CSpatialTransformFilter::buildFilterGraph(CGarbageCollector& gc, CGrid* srcGrid, CGrid* destGrid, bool hasMissingValue, double missingValue) |
---|
[644] | 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 | |
---|
[790] | 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()); |
---|
[827] | 27 | const std::vector<StdString>& auxInputs = gridTransformation->getAuxInputs(); |
---|
| 28 | size_t inputCount = 1 + (auxInputs.empty() ? 0 : auxInputs.size()); |
---|
[1158] | 29 | double defaultValue = (hasMissingValue) ? std::numeric_limits<double>::quiet_NaN() : 0.0; |
---|
[644] | 30 | |
---|
[1275] | 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 | |
---|
[790] | 43 | if (!lastFilter) |
---|
| 44 | lastFilter = filter; |
---|
| 45 | else |
---|
| 46 | filter->connectOutput(firstFilter, 0); |
---|
| 47 | |
---|
| 48 | firstFilter = filter; |
---|
[827] | 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 | |
---|
[790] | 56 | destGrid = gridTransformation->getGridSource(); |
---|
| 57 | } |
---|
| 58 | while (destGrid != srcGrid); |
---|
| 59 | |
---|
| 60 | return std::make_pair(firstFilter, lastFilter); |
---|
[644] | 61 | } |
---|
| 62 | |
---|
[873] | 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) |
---|
[1021] | 68 | onOutputReady(outputPacket); |
---|
[873] | 69 | } |
---|
| 70 | |
---|
[1275] | 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 | |
---|
[644] | 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 | { |
---|
[873] | 162 | /* Nothing to do */ |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | CDataPacketPtr CSpatialTransformFilterEngine::applyFilter(std::vector<CDataPacketPtr> data, double defaultValue) |
---|
| 166 | { |
---|
[644] | 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 | { |
---|
[827] | 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); |
---|
[832] | 178 | gridTransformation->computeAll(dataAuxInputs, packet->timestamp); |
---|
[827] | 179 | } |
---|
[644] | 180 | packet->data.resize(gridTransformation->getGridDestination()->storeIndex_client.numElements()); |
---|
[1158] | 181 | if (0 != packet->data.numElements()) |
---|
| 182 | (packet->data)(0) = defaultValue; |
---|
[644] | 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 | { |
---|
[1414] | 191 | CTimer::get("CSpatialTransformFilterEngine::apply").resume(); |
---|
| 192 | |
---|
[644] | 193 | CContextClient* client = CContext::getCurrent()->client; |
---|
| 194 | |
---|
[873] | 195 | // Get default value for output data |
---|
[1158] | 196 | bool ignoreMissingValue = false; |
---|
| 197 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
[1474] | 198 | if (0 != dataDest.numElements()) ignoreMissingValue = NumTraits<double>::isNan(dataDest(0)); |
---|
[873] | 199 | |
---|
[841] | 200 | const std::list<CGridTransformation::SendingIndexGridSourceMap>& listLocalIndexSend = gridTransformation->getLocalIndexToSendFromGridSource(); |
---|
| 201 | const std::list<CGridTransformation::RecvIndexGridDestinationMap>& listLocalIndexToReceive = gridTransformation->getLocalIndexToReceiveOnGridDest(); |
---|
| 202 | const std::list<size_t>& listNbLocalIndexToReceive = gridTransformation->getNbLocalIndexToReceiveOnGridDest(); |
---|
[873] | 203 | const std::list<std::vector<bool> >& listLocalIndexMaskOnDest = gridTransformation->getLocalMaskIndexOnGridDest(); |
---|
[888] | 204 | const std::vector<CGenericAlgorithmTransformation*>& listAlgos = gridTransformation->getAlgos(); |
---|
[644] | 205 | |
---|
[841] | 206 | CArray<double,1> dataCurrentDest(dataSrc.copy()); |
---|
[644] | 207 | |
---|
[841] | 208 | std::list<CGridTransformation::SendingIndexGridSourceMap>::const_iterator itListSend = listLocalIndexSend.begin(), |
---|
| 209 | iteListSend = listLocalIndexSend.end(); |
---|
| 210 | std::list<CGridTransformation::RecvIndexGridDestinationMap>::const_iterator itListRecv = listLocalIndexToReceive.begin(); |
---|
| 211 | std::list<size_t>::const_iterator itNbListRecv = listNbLocalIndexToReceive.begin(); |
---|
[873] | 212 | std::list<std::vector<bool> >::const_iterator itLocalMaskIndexOnDest = listLocalIndexMaskOnDest.begin(); |
---|
[888] | 213 | std::vector<CGenericAlgorithmTransformation*>::const_iterator itAlgo = listAlgos.begin(); |
---|
[841] | 214 | |
---|
[888] | 215 | for (; itListSend != iteListSend; ++itListSend, ++itListRecv, ++itNbListRecv, ++itLocalMaskIndexOnDest, ++itAlgo) |
---|
[709] | 216 | { |
---|
[841] | 217 | CArray<double,1> dataCurrentSrc(dataCurrentDest); |
---|
| 218 | const CGridTransformation::SendingIndexGridSourceMap& localIndexToSend = *itListSend; |
---|
[709] | 219 | |
---|
[841] | 220 | // Sending data from field sources to do transformations |
---|
| 221 | std::map<int, CArray<int,1> >::const_iterator itbSend = localIndexToSend.begin(), itSend, |
---|
| 222 | iteSend = localIndexToSend.end(); |
---|
| 223 | int idxSendBuff = 0; |
---|
| 224 | std::vector<double*> sendBuff(localIndexToSend.size()); |
---|
| 225 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
[644] | 226 | { |
---|
[841] | 227 | if (0 != itSend->second.numElements()) |
---|
| 228 | sendBuff[idxSendBuff] = new double[itSend->second.numElements()]; |
---|
[644] | 229 | } |
---|
| 230 | |
---|
[841] | 231 | idxSendBuff = 0; |
---|
| 232 | std::vector<MPI_Request> sendRecvRequest; |
---|
| 233 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
[644] | 234 | { |
---|
[841] | 235 | int destRank = itSend->first; |
---|
| 236 | const CArray<int,1>& localIndex_p = itSend->second; |
---|
| 237 | int countSize = localIndex_p.numElements(); |
---|
| 238 | for (int idx = 0; idx < countSize; ++idx) |
---|
[644] | 239 | { |
---|
[841] | 240 | sendBuff[idxSendBuff][idx] = dataCurrentSrc(localIndex_p(idx)); |
---|
[644] | 241 | } |
---|
[841] | 242 | sendRecvRequest.push_back(MPI_Request()); |
---|
| 243 | MPI_Isend(sendBuff[idxSendBuff], countSize, MPI_DOUBLE, destRank, 12, client->intraComm, &sendRecvRequest.back()); |
---|
[644] | 244 | } |
---|
| 245 | |
---|
[841] | 246 | // Receiving data on destination fields |
---|
| 247 | const CGridTransformation::RecvIndexGridDestinationMap& localIndexToReceive = *itListRecv; |
---|
| 248 | CGridTransformation::RecvIndexGridDestinationMap::const_iterator itbRecv = localIndexToReceive.begin(), itRecv, |
---|
| 249 | iteRecv = localIndexToReceive.end(); |
---|
| 250 | int recvBuffSize = 0; |
---|
| 251 | for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) recvBuffSize += itRecv->second.size(); //(recvBuffSize < itRecv->second.size()) |
---|
| 252 | //? itRecv->second.size() : recvBuffSize; |
---|
| 253 | double* recvBuff; |
---|
| 254 | if (0 != recvBuffSize) recvBuff = new double[recvBuffSize]; |
---|
| 255 | int currentBuff = 0; |
---|
| 256 | for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) |
---|
| 257 | { |
---|
| 258 | int srcRank = itRecv->first; |
---|
| 259 | int countSize = itRecv->second.size(); |
---|
| 260 | sendRecvRequest.push_back(MPI_Request()); |
---|
| 261 | MPI_Irecv(recvBuff + currentBuff, countSize, MPI_DOUBLE, srcRank, 12, client->intraComm, &sendRecvRequest.back()); |
---|
| 262 | currentBuff += countSize; |
---|
| 263 | } |
---|
| 264 | std::vector<MPI_Status> status(sendRecvRequest.size()); |
---|
| 265 | MPI_Waitall(sendRecvRequest.size(), &sendRecvRequest[0], &status[0]); |
---|
[709] | 266 | |
---|
[841] | 267 | dataCurrentDest.resize(*itNbListRecv); |
---|
[873] | 268 | const std::vector<bool>& localMaskDest = *itLocalMaskIndexOnDest; |
---|
| 269 | for (int i = 0; i < localMaskDest.size(); ++i) |
---|
| 270 | if (localMaskDest[i]) dataCurrentDest(i) = 0.0; |
---|
| 271 | else dataCurrentDest(i) = defaultValue; |
---|
| 272 | |
---|
[1158] | 273 | std::vector<bool> localInitFlag(dataCurrentDest.numElements(), true); |
---|
[841] | 274 | currentBuff = 0; |
---|
[1260] | 275 | bool firstPass=true; |
---|
[841] | 276 | for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) |
---|
| 277 | { |
---|
| 278 | int countSize = itRecv->second.size(); |
---|
[842] | 279 | const std::vector<std::pair<int,double> >& localIndex_p = itRecv->second; |
---|
[888] | 280 | (*itAlgo)->apply(localIndex_p, |
---|
| 281 | recvBuff+currentBuff, |
---|
| 282 | dataCurrentDest, |
---|
[918] | 283 | localInitFlag, |
---|
[1260] | 284 | ignoreMissingValue,firstPass); |
---|
[888] | 285 | |
---|
[841] | 286 | currentBuff += countSize; |
---|
[1260] | 287 | firstPass=false ; |
---|
[841] | 288 | } |
---|
| 289 | |
---|
[979] | 290 | (*itAlgo)->updateData(dataCurrentDest); |
---|
| 291 | |
---|
[841] | 292 | idxSendBuff = 0; |
---|
| 293 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
| 294 | { |
---|
| 295 | if (0 != itSend->second.numElements()) |
---|
| 296 | delete [] sendBuff[idxSendBuff]; |
---|
| 297 | } |
---|
| 298 | if (0 != recvBuffSize) delete [] recvBuff; |
---|
[709] | 299 | } |
---|
[841] | 300 | if (dataCurrentDest.numElements() != dataDest.numElements()) |
---|
| 301 | ERROR("CSpatialTransformFilterEngine::apply(const CArray<double, 1>& dataSrc, CArray<double,1>& dataDest)", |
---|
[1021] | 302 | "Incoherent between the received size and expected size. " << std::endl |
---|
| 303 | << "Expected size: " << dataDest.numElements() << std::endl |
---|
| 304 | << "Received size: " << dataCurrentDest.numElements()); |
---|
[841] | 305 | |
---|
| 306 | dataDest = dataCurrentDest; |
---|
[1412] | 307 | |
---|
[1414] | 308 | CTimer::get("CSpatialTransformFilterEngine::apply").suspend() ; |
---|
[644] | 309 | } |
---|
| 310 | } // namespace xios |
---|