[644] | 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 | { |
---|
[873] | 8 | CSpatialTransformFilter::CSpatialTransformFilter(CGarbageCollector& gc, CSpatialTransformFilterEngine* engine, double outputValue, size_t inputSlotsCount) |
---|
| 9 | : CFilter(gc, inputSlotsCount, engine), outputDefaultValue(outputValue) |
---|
[644] | 10 | { /* Nothing to do */ } |
---|
| 11 | |
---|
| 12 | std::pair<boost::shared_ptr<CSpatialTransformFilter>, boost::shared_ptr<CSpatialTransformFilter> > |
---|
[1158] | 13 | CSpatialTransformFilter::buildFilterGraph(CGarbageCollector& gc, CGrid* srcGrid, CGrid* destGrid, bool hasMissingValue, double missingValue) |
---|
[644] | 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 | |
---|
[790] | 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()); |
---|
[827] | 26 | const std::vector<StdString>& auxInputs = gridTransformation->getAuxInputs(); |
---|
| 27 | size_t inputCount = 1 + (auxInputs.empty() ? 0 : auxInputs.size()); |
---|
[1158] | 28 | double defaultValue = (hasMissingValue) ? std::numeric_limits<double>::quiet_NaN() : 0.0; |
---|
[873] | 29 | boost::shared_ptr<CSpatialTransformFilter> filter(new CSpatialTransformFilter(gc, engine, defaultValue, inputCount)); |
---|
[644] | 30 | |
---|
[790] | 31 | if (!lastFilter) |
---|
| 32 | lastFilter = filter; |
---|
| 33 | else |
---|
| 34 | filter->connectOutput(firstFilter, 0); |
---|
| 35 | |
---|
| 36 | firstFilter = filter; |
---|
[827] | 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 | |
---|
[790] | 44 | destGrid = gridTransformation->getGridSource(); |
---|
| 45 | } |
---|
| 46 | while (destGrid != srcGrid); |
---|
| 47 | |
---|
| 48 | return std::make_pair(firstFilter, lastFilter); |
---|
[644] | 49 | } |
---|
| 50 | |
---|
[873] | 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) |
---|
[1021] | 56 | onOutputReady(outputPacket); |
---|
[873] | 57 | } |
---|
| 58 | |
---|
[644] | 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 | { |
---|
[873] | 87 | /* Nothing to do */ |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | CDataPacketPtr CSpatialTransformFilterEngine::applyFilter(std::vector<CDataPacketPtr> data, double defaultValue) |
---|
| 91 | { |
---|
[644] | 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 | { |
---|
[827] | 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); |
---|
[832] | 103 | gridTransformation->computeAll(dataAuxInputs, packet->timestamp); |
---|
[827] | 104 | } |
---|
[644] | 105 | packet->data.resize(gridTransformation->getGridDestination()->storeIndex_client.numElements()); |
---|
[1158] | 106 | if (0 != packet->data.numElements()) |
---|
| 107 | (packet->data)(0) = defaultValue; |
---|
[644] | 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 | |
---|
[873] | 118 | // Get default value for output data |
---|
[1158] | 119 | bool ignoreMissingValue = false; |
---|
| 120 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
| 121 | if (0 != dataDest.numElements()) ignoreMissingValue = NumTraits<double>::isnan(dataDest(0)); |
---|
[873] | 122 | |
---|
[841] | 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(); |
---|
[873] | 126 | const std::list<std::vector<bool> >& listLocalIndexMaskOnDest = gridTransformation->getLocalMaskIndexOnGridDest(); |
---|
[888] | 127 | const std::vector<CGenericAlgorithmTransformation*>& listAlgos = gridTransformation->getAlgos(); |
---|
[644] | 128 | |
---|
[841] | 129 | CArray<double,1> dataCurrentDest(dataSrc.copy()); |
---|
[644] | 130 | |
---|
[841] | 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(); |
---|
[873] | 135 | std::list<std::vector<bool> >::const_iterator itLocalMaskIndexOnDest = listLocalIndexMaskOnDest.begin(); |
---|
[888] | 136 | std::vector<CGenericAlgorithmTransformation*>::const_iterator itAlgo = listAlgos.begin(); |
---|
[841] | 137 | |
---|
[888] | 138 | for (; itListSend != iteListSend; ++itListSend, ++itListRecv, ++itNbListRecv, ++itLocalMaskIndexOnDest, ++itAlgo) |
---|
[709] | 139 | { |
---|
[841] | 140 | CArray<double,1> dataCurrentSrc(dataCurrentDest); |
---|
| 141 | const CGridTransformation::SendingIndexGridSourceMap& localIndexToSend = *itListSend; |
---|
[709] | 142 | |
---|
[841] | 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) |
---|
[644] | 149 | { |
---|
[841] | 150 | if (0 != itSend->second.numElements()) |
---|
| 151 | sendBuff[idxSendBuff] = new double[itSend->second.numElements()]; |
---|
[644] | 152 | } |
---|
| 153 | |
---|
[841] | 154 | idxSendBuff = 0; |
---|
| 155 | std::vector<MPI_Request> sendRecvRequest; |
---|
| 156 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
[644] | 157 | { |
---|
[841] | 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) |
---|
[644] | 162 | { |
---|
[841] | 163 | sendBuff[idxSendBuff][idx] = dataCurrentSrc(localIndex_p(idx)); |
---|
[644] | 164 | } |
---|
[841] | 165 | sendRecvRequest.push_back(MPI_Request()); |
---|
| 166 | MPI_Isend(sendBuff[idxSendBuff], countSize, MPI_DOUBLE, destRank, 12, client->intraComm, &sendRecvRequest.back()); |
---|
[644] | 167 | } |
---|
| 168 | |
---|
[841] | 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]); |
---|
[709] | 189 | |
---|
[841] | 190 | dataCurrentDest.resize(*itNbListRecv); |
---|
[873] | 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 | |
---|
[1158] | 196 | std::vector<bool> localInitFlag(dataCurrentDest.numElements(), true); |
---|
[841] | 197 | currentBuff = 0; |
---|
| 198 | for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) |
---|
| 199 | { |
---|
| 200 | int countSize = itRecv->second.size(); |
---|
[842] | 201 | const std::vector<std::pair<int,double> >& localIndex_p = itRecv->second; |
---|
[888] | 202 | (*itAlgo)->apply(localIndex_p, |
---|
| 203 | recvBuff+currentBuff, |
---|
| 204 | dataCurrentDest, |
---|
[918] | 205 | localInitFlag, |
---|
[1158] | 206 | ignoreMissingValue); |
---|
[888] | 207 | |
---|
[841] | 208 | currentBuff += countSize; |
---|
| 209 | } |
---|
| 210 | |
---|
[979] | 211 | (*itAlgo)->updateData(dataCurrentDest); |
---|
| 212 | |
---|
[841] | 213 | idxSendBuff = 0; |
---|
| 214 | for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff) |
---|
| 215 | { |
---|
| 216 | if (0 != itSend->second.numElements()) |
---|
| 217 | delete [] sendBuff[idxSendBuff]; |
---|
| 218 | } |
---|
| 219 | if (0 != recvBuffSize) delete [] recvBuff; |
---|
[709] | 220 | } |
---|
[841] | 221 | if (dataCurrentDest.numElements() != dataDest.numElements()) |
---|
| 222 | ERROR("CSpatialTransformFilterEngine::apply(const CArray<double, 1>& dataSrc, CArray<double,1>& dataDest)", |
---|
[1021] | 223 | "Incoherent between the received size and expected size. " << std::endl |
---|
| 224 | << "Expected size: " << dataDest.numElements() << std::endl |
---|
| 225 | << "Received size: " << dataCurrentDest.numElements()); |
---|
[841] | 226 | |
---|
| 227 | dataDest = dataCurrentDest; |
---|
[644] | 228 | } |
---|
| 229 | } // namespace xios |
---|