source: XIOS/dev/branch_openmp/src/filter/spatial_transform_filter.cpp @ 1220

Last change on this file since 1220 was 1220, checked in by yushan, 4 years ago

test_remap_omp tested on ADA except two fields

File size: 11.2 KB
Line 
1#include "spatial_transform_filter.hpp"
2#include "grid_transformation.hpp"
3#include "context.hpp"
4#include "context_client.hpp"
5
6namespace 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    printf("spalceFilter applied\n");
56    if (outputPacket)
57      onOutputReady(outputPacket);
58  }
59
60  CSpatialTransformFilterEngine::CSpatialTransformFilterEngine(CGridTransformation* gridTransformation)
61    : gridTransformation(gridTransformation)
62  {
63    if (!gridTransformation)
64      ERROR("CSpatialTransformFilterEngine::CSpatialTransformFilterEngine(CGridTransformation* gridTransformation)",
65            "Impossible to construct a spatial transform filter engine without a valid grid transformation.");
66  }
67
68  std::map<CGridTransformation*, boost::shared_ptr<CSpatialTransformFilterEngine> > *CSpatialTransformFilterEngine::engines_ptr = 0;
69
70  CSpatialTransformFilterEngine* CSpatialTransformFilterEngine::get(CGridTransformation* gridTransformation)
71  {
72    if (!gridTransformation)
73      ERROR("CSpatialTransformFilterEngine& CSpatialTransformFilterEngine::get(CGridTransformation* gridTransformation)",
74            "Impossible to get the requested engine, the grid transformation is invalid.");
75
76    if(engines_ptr == NULL) engines_ptr = new std::map<CGridTransformation*, boost::shared_ptr<CSpatialTransformFilterEngine> >;
77
78    std::map<CGridTransformation*, boost::shared_ptr<CSpatialTransformFilterEngine> >::iterator it = engines_ptr->find(gridTransformation);
79    if (it == engines_ptr->end())
80    {
81      boost::shared_ptr<CSpatialTransformFilterEngine> engine(new CSpatialTransformFilterEngine(gridTransformation));
82      it = engines_ptr->insert(std::make_pair(gridTransformation, engine)).first;
83    }
84
85    return it->second.get();
86  }
87
88  CDataPacketPtr CSpatialTransformFilterEngine::apply(std::vector<CDataPacketPtr> data)
89  {
90    /* Nothing to do */
91  }
92
93  CDataPacketPtr CSpatialTransformFilterEngine::applyFilter(std::vector<CDataPacketPtr> data, double defaultValue)
94  {
95    CDataPacketPtr packet(new CDataPacket);
96    packet->date = data[0]->date;
97    packet->timestamp = data[0]->timestamp;
98    packet->status = data[0]->status;
99
100    if (packet->status == CDataPacket::NO_ERROR)
101    {
102      if (1 < data.size())  // Dynamical transformations
103      {
104        std::vector<CArray<double,1>* > dataAuxInputs(data.size()-1);
105        for (size_t idx = 0; idx < dataAuxInputs.size(); ++idx) dataAuxInputs[idx] = &(data[idx+1]->data);
106        gridTransformation->computeAll(dataAuxInputs, packet->timestamp);
107      }
108      packet->data.resize(gridTransformation->getGridDestination()->storeIndex_client.numElements());
109      if (0 != packet->data.numElements())
110        (packet->data)(0) = defaultValue;
111      apply(data[0]->data, packet->data);
112    }
113
114    return packet;
115  }
116
117  void CSpatialTransformFilterEngine::apply(const CArray<double, 1>& dataSrc, CArray<double,1>& dataDest)
118  {
119    CContextClient* client = CContext::getCurrent()->client;
120
121    // Get default value for output data
122    bool ignoreMissingValue = false; 
123    double defaultValue = std::numeric_limits<double>::quiet_NaN();
124    if (0 != dataDest.numElements()) ignoreMissingValue = NumTraits<double>::isnan(dataDest(0));
125   
126    const std::list<CGridTransformation::SendingIndexGridSourceMap> *listLocalIndexSend_ptr = & (gridTransformation->getLocalIndexToSendFromGridSource());
127   
128    const std::list<CGridTransformation::SendingIndexGridSourceMap>& listLocalIndexSend = gridTransformation->getLocalIndexToSendFromGridSource();
129    const std::list<CGridTransformation::RecvIndexGridDestinationMap>& listLocalIndexToReceive = gridTransformation->getLocalIndexToReceiveOnGridDest();
130    const std::list<size_t>& listNbLocalIndexToReceive = gridTransformation->getNbLocalIndexToReceiveOnGridDest();
131    const std::list<std::vector<bool> >& listLocalIndexMaskOnDest = gridTransformation->getLocalMaskIndexOnGridDest();
132    const std::vector<CGenericAlgorithmTransformation*>& listAlgos = gridTransformation->getAlgos();
133
134    CArray<double,1> dataCurrentDest(dataSrc.copy());
135
136    std::list<CGridTransformation::SendingIndexGridSourceMap>::const_iterator itListSend  = listLocalIndexSend_ptr->begin(),
137                                                                              iteListSend = listLocalIndexSend_ptr->end();
138    std::list<CGridTransformation::RecvIndexGridDestinationMap>::const_iterator itListRecv = listLocalIndexToReceive.begin();
139    std::list<size_t>::const_iterator itNbListRecv = listNbLocalIndexToReceive.begin();
140    std::list<std::vector<bool> >::const_iterator itLocalMaskIndexOnDest = listLocalIndexMaskOnDest.begin();
141    std::vector<CGenericAlgorithmTransformation*>::const_iterator itAlgo = listAlgos.begin();
142
143    for (; itListSend != iteListSend; ++itListSend, ++itListRecv, ++itNbListRecv, ++itLocalMaskIndexOnDest, ++itAlgo)
144    {
145      CArray<double,1> dataCurrentSrc(dataCurrentDest);
146      const CGridTransformation::SendingIndexGridSourceMap& localIndexToSend = *itListSend;
147
148      // Sending data from field sources to do transformations
149      std::map<int, CArray<int,1> >::const_iterator itbSend = localIndexToSend.begin(), itSend,
150                                                    iteSend = localIndexToSend.end();
151      int idxSendBuff = 0;
152      std::vector<double*> sendBuff(localIndexToSend.size());
153      for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff)
154      {
155        if (0 != itSend->second.numElements())
156          sendBuff[idxSendBuff] = new double[itSend->second.numElements()];
157      }
158     
159      const CGridTransformation::RecvIndexGridDestinationMap& localIndexToReceive = *itListRecv;
160      CGridTransformation::RecvIndexGridDestinationMap::const_iterator itbRecv = localIndexToReceive.begin(), itRecv,
161                                                                       iteRecv = localIndexToReceive.end();
162
163      idxSendBuff = 0;
164      std::vector<ep_lib::MPI_Request> sendRecvRequest(localIndexToSend.size()+localIndexToReceive.size());
165      int position = 0;
166      for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff)
167      {
168        int destRank = itSend->first;
169        const CArray<int,1>& localIndex_p = itSend->second;
170        int countSize = localIndex_p.numElements();
171        for (int idx = 0; idx < countSize; ++idx)
172        {
173          sendBuff[idxSendBuff][idx] = dataCurrentSrc(localIndex_p(idx));
174        }
175        MPI_Isend(sendBuff[idxSendBuff], countSize, MPI_DOUBLE, destRank, 12, client->intraComm, &sendRecvRequest[position]);
176        position++;
177      }
178
179      // Receiving data on destination fields
180     
181      int recvBuffSize = 0;
182      for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv) recvBuffSize += itRecv->second.size(); //(recvBuffSize < itRecv->second.size())
183                                                                       //? itRecv->second.size() : recvBuffSize;
184      double* recvBuff;
185      if (0 != recvBuffSize) recvBuff = new double[recvBuffSize];
186      int currentBuff = 0;
187      for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv)
188      {
189        int srcRank = itRecv->first;
190        int countSize = itRecv->second.size();
191        MPI_Irecv(recvBuff + currentBuff, countSize, MPI_DOUBLE, srcRank, 12, client->intraComm, &sendRecvRequest[position]);
192        position++;
193        currentBuff += countSize;
194      }
195      std::vector<ep_lib::MPI_Status> status(sendRecvRequest.size());
196      MPI_Waitall(sendRecvRequest.size(), &sendRecvRequest[0], &status[0]);
197
198      dataCurrentDest.resize(*itNbListRecv);
199      const std::vector<bool>& localMaskDest = *itLocalMaskIndexOnDest;
200      for (int i = 0; i < localMaskDest.size(); ++i)
201        if (localMaskDest[i]) dataCurrentDest(i) = 0.0;
202        else dataCurrentDest(i) = defaultValue;
203
204      std::vector<bool> localInitFlag(dataCurrentDest.numElements(), true);
205      currentBuff = 0;
206      for (itRecv = itbRecv; itRecv != iteRecv; ++itRecv)
207      {
208        int countSize = itRecv->second.size();
209        const std::vector<std::pair<int,double> >& localIndex_p = itRecv->second;
210        (*itAlgo)->apply(localIndex_p,
211                         recvBuff+currentBuff,
212                         dataCurrentDest,
213                         localInitFlag,
214                         ignoreMissingValue);
215
216        currentBuff += countSize;
217      }
218
219      (*itAlgo)->updateData(dataCurrentDest);
220
221      idxSendBuff = 0;
222      for (itSend = itbSend; itSend != iteSend; ++itSend, ++idxSendBuff)
223      {
224        if (0 != itSend->second.numElements())
225          delete [] sendBuff[idxSendBuff];
226      }
227      if (0 != recvBuffSize) delete [] recvBuff;
228    }
229    if (dataCurrentDest.numElements() != dataDest.numElements())
230    ERROR("CSpatialTransformFilterEngine::apply(const CArray<double, 1>& dataSrc, CArray<double,1>& dataDest)",
231          "Incoherent between the received size and expected size. " << std::endl
232          << "Expected size: " << dataDest.numElements() << std::endl
233          << "Received size: " << dataCurrentDest.numElements());
234
235    dataDest = dataCurrentDest;
236  }
237} // namespace xios
Note: See TracBrowser for help on using the repository browser.