source: XIOS/dev/XIOS_DEV_CMIP6/src/filter/spatial_transform_filter.cpp @ 1414

Last change on this file since 1414 was 1414, checked in by ymipsl, 7 years ago

More timer instrumentation...

YM

File size: 13.8 KB
Line 
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
7namespace 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").resume(); 
192   
193    CContextClient* client = CContext::getCurrent()->client;
194
195    // Get default value for output data
196    bool ignoreMissingValue = false; 
197    double defaultValue = std::numeric_limits<double>::quiet_NaN();
198    if (0 != dataDest.numElements()) ignoreMissingValue = NumTraits<double>::isnan(dataDest(0));
199
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();
203    const std::list<std::vector<bool> >& listLocalIndexMaskOnDest = gridTransformation->getLocalMaskIndexOnGridDest();
204    const std::vector<CGenericAlgorithmTransformation*>& listAlgos = gridTransformation->getAlgos();
205
206    CArray<double,1> dataCurrentDest(dataSrc.copy());
207
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();
212    std::list<std::vector<bool> >::const_iterator itLocalMaskIndexOnDest = listLocalIndexMaskOnDest.begin();
213    std::vector<CGenericAlgorithmTransformation*>::const_iterator itAlgo = listAlgos.begin();
214
215    for (; itListSend != iteListSend; ++itListSend, ++itListRecv, ++itNbListRecv, ++itLocalMaskIndexOnDest, ++itAlgo)
216    {
217      CArray<double,1> dataCurrentSrc(dataCurrentDest);
218      const CGridTransformation::SendingIndexGridSourceMap& localIndexToSend = *itListSend;
219
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)
226      {
227        if (0 != itSend->second.numElements())
228          sendBuff[idxSendBuff] = new double[itSend->second.numElements()];
229      }
230
231      idxSendBuff = 0;
232      std::vector<MPI_Request> sendRecvRequest;
233      for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff)
234      {
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)
239        {
240          sendBuff[idxSendBuff][idx] = dataCurrentSrc(localIndex_p(idx));
241        }
242        sendRecvRequest.push_back(MPI_Request());
243        MPI_Isend(sendBuff[idxSendBuff], countSize, MPI_DOUBLE, destRank, 12, client->intraComm, &sendRecvRequest.back());
244      }
245
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]);
266
267      dataCurrentDest.resize(*itNbListRecv);
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
273      std::vector<bool> localInitFlag(dataCurrentDest.numElements(), true);
274      currentBuff = 0;
275      bool firstPass=true; 
276      for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv)
277      {
278        int countSize = itRecv->second.size();
279        const std::vector<std::pair<int,double> >& localIndex_p = itRecv->second;
280        (*itAlgo)->apply(localIndex_p,
281                         recvBuff+currentBuff,
282                         dataCurrentDest,
283                         localInitFlag,
284                         ignoreMissingValue,firstPass);
285
286        currentBuff += countSize;
287        firstPass=false ;
288      }
289
290      (*itAlgo)->updateData(dataCurrentDest);
291
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;
299    }
300    if (dataCurrentDest.numElements() != dataDest.numElements())
301    ERROR("CSpatialTransformFilterEngine::apply(const CArray<double, 1>& dataSrc, CArray<double,1>& dataDest)",
302          "Incoherent between the received size and expected size. " << std::endl
303          << "Expected size: " << dataDest.numElements() << std::endl
304          << "Received size: " << dataCurrentDest.numElements());
305
306    dataDest = dataCurrentDest;
307
308    CTimer::get("CSpatialTransformFilterEngine::apply").suspend() ;
309  }
310} // namespace xios
Note: See TracBrowser for help on using the repository browser.