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