source: XIOS/dev/dev_ym/XIOS_COUPLING/src/transformation/axis_algorithm_inverse.cpp @ 1878

Last change on this file since 1878 was 1784, checked in by ymipsl, 4 years ago
  • Preparing coupling functionalities.
  • Make some cleaner things

YM

File size: 11.4 KB
Line 
1/*!
2   \file axis_algorithm_inverse.hpp
3   \author Ha NGUYEN
4   \since 14 May 2015
5   \date 29 June 2015
6
7   \brief Algorithm for inversing an axis..
8 */
9#include "axis_algorithm_inverse.hpp"
10#include "context.hpp"
11#include "context_client.hpp"
12#include "axis.hpp"
13#include "grid.hpp"
14#include "grid_transformation_factory_impl.hpp"
15#include "inverse_axis.hpp"
16#include "client_client_dht_template.hpp"
17
18namespace xios {
19
20CGenericAlgorithmTransformation* CAxisAlgorithmInverse::create(CGrid* gridDst, CGrid* gridSrc,
21                                                               CTransformation<CAxis>* transformation,
22                                                               int elementPositionInGrid,
23                                                               std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
24                                                               std::map<int, int>& elementPositionInGridSrc2AxisPosition,
25                                                               std::map<int, int>& elementPositionInGridSrc2DomainPosition,
26                                                               std::map<int, int>& elementPositionInGridDst2ScalarPosition,
27                                                               std::map<int, int>& elementPositionInGridDst2AxisPosition,
28                                                               std::map<int, int>& elementPositionInGridDst2DomainPosition)
29TRY
30{
31  std::vector<CAxis*> axisListDestP = gridDst->getAxis();
32  std::vector<CAxis*> axisListSrcP  = gridSrc->getAxis();
33
34  CInverseAxis* inverseAxis = dynamic_cast<CInverseAxis*> (transformation);
35  int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid];
36  int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
37
38  return (new CAxisAlgorithmInverse(axisListDestP[axisDstIndex], axisListSrcP[axisSrcIndex], inverseAxis));
39}
40CATCH
41
42bool CAxisAlgorithmInverse::registerTrans()
43TRY
44{
45  CGridTransformationFactory<CAxis>::registerTransformation(TRANS_INVERSE_AXIS, create);
46}
47CATCH
48
49CAxisAlgorithmInverse::CAxisAlgorithmInverse(CAxis* axisDestination, CAxis* axisSource, CInverseAxis* inverseAxis)
50 : CAxisAlgorithmTransformation(axisDestination, axisSource)
51TRY
52{
53  if (axisDestination->n_glo.getValue() != axisSource->n_glo.getValue())
54  {
55    ERROR("CAxisAlgorithmInverse::CAxisAlgorithmInverse(CAxis* axisDestination, CAxis* axisSource)",
56           << "Two axis have different global size"
57           << "Size of axis source " <<axisSource->getId() << " is " << axisSource->n_glo.getValue()  << std::endl
58           << "Size of axis destination " <<axisDestination->getId() << " is " << axisDestination->n_glo.getValue());
59  }
60}
61CATCH
62
63void CAxisAlgorithmInverse::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
64TRY
65{
66  this->transformationMapping_.resize(1);
67  this->transformationWeight_.resize(1);
68
69  TransformationIndexMap& transMap = this->transformationMapping_[0];
70  TransformationWeightMap& transWeight = this->transformationWeight_[0];
71
72  int globalIndexSize = axisDestGlobalIndex_.size();
73  for (int idx = 0; idx < globalIndexSize; ++idx)
74  {
75    transMap[axisDestGlobalIndex_[idx]].push_back(axisDestGlobalSize_-axisDestGlobalIndex_[idx]-1);
76    transWeight[axisDestGlobalIndex_[idx]].push_back(1.0);
77  }
78
79  int niSrc   = axisSrc_->n.getValue();
80  int sizeSrc = axisSrc_->n_glo.getValue();
81  if (niSrc != sizeSrc) updateAxisValue();
82  else
83  {
84    for (int idx = 0; idx < sizeSrc; ++idx)
85    {
86      axisDest_->value(idx) = axisSrc_->value(sizeSrc-idx-1);
87    }
88  }
89}
90CATCH
91
92/*!
93  Update value on axis after inversing
94  After an axis is inversed, not only the data on it must be inversed but also the value
95*/
96void CAxisAlgorithmInverse::updateAxisValue()
97TRY
98{
99  CContext* context = CContext::getCurrent();
100  int clientRank = context->intraCommRank_;
101  int nbClient = context->intraCommSize_;
102
103  int niSrc     = axisSrc_->n.getValue();
104  int ibeginSrc = axisSrc_->begin.getValue();
105  int nSrc = axisSrc_->index.numElements();
106
107  CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank;
108  for (int idx = 0; idx < nSrc; ++idx)
109  {
110    if ((axisSrc_->mask)(idx))
111    {
112      globalIndex2ProcRank[(axisSrc_->index)(idx)].resize(1);
113      globalIndex2ProcRank[(axisSrc_->index)(idx)][0] = clientRank;
114    }
115  }
116
117  typedef std::unordered_map<size_t, std::vector<double> > GlobalIndexMapFromSrcToDest;
118  GlobalIndexMapFromSrcToDest globalIndexMapFromSrcToDest;
119  TransformationIndexMap& transMap = this->transformationMapping_[0];
120  TransformationIndexMap::const_iterator itb = transMap.begin(), ite = transMap.end(), it;
121  CArray<size_t,1> globalSrcIndex(transMap.size());
122  int localIndex = 0;
123  for (it = itb; it != ite; ++it)
124  {
125    size_t srcIndex = it->second[0];
126    globalIndexMapFromSrcToDest[srcIndex].resize(1);
127    globalIndexMapFromSrcToDest[srcIndex][0] = it->first;
128    globalSrcIndex(localIndex) = srcIndex;
129    ++localIndex;
130  }
131
132  CClientClientDHTInt dhtIndexProcRank(globalIndex2ProcRank, context->intraComm_);
133  dhtIndexProcRank.computeIndexInfoMapping(globalSrcIndex);
134  CClientClientDHTInt::Index2VectorInfoTypeMap& computedGlobalIndexOnProc = dhtIndexProcRank.getInfoIndexMap();
135  std::unordered_map<int, std::vector<size_t> > globalSrcIndexSendToProc;
136  for (int idx = 0; idx < localIndex; ++idx)
137  {
138    size_t tmpIndex = globalSrcIndex(idx);
139    if (1 == computedGlobalIndexOnProc.count(tmpIndex))
140    {
141      std::vector<int>& tmpVec = computedGlobalIndexOnProc[tmpIndex];
142      globalSrcIndexSendToProc[tmpVec[0]].push_back(tmpIndex);
143    }
144  }
145
146  std::unordered_map<int, std::vector<size_t> >::const_iterator itbIndex = globalSrcIndexSendToProc.begin(), itIndex,
147                                                                  iteIndex = globalSrcIndexSendToProc.end();
148  std::map<int,int> sendRankSizeMap,recvRankSizeMap;
149  int connectedClient = globalSrcIndexSendToProc.size();
150  int* recvCount=new int[nbClient];
151  int* displ=new int[nbClient];
152  int* sendRankBuff=new int[connectedClient];
153  int* sendSizeBuff=new int[connectedClient];
154  int n = 0;
155  for (itIndex = itbIndex; itIndex != iteIndex; ++itIndex, ++n)
156  {
157    sendRankBuff[n] = itIndex->first;
158    int sendSize = itIndex->second.size();
159    sendSizeBuff[n] = sendSize;
160    sendRankSizeMap[itIndex->first] = sendSize;
161  }
162  MPI_Allgather(&connectedClient,1,MPI_INT,recvCount,1,MPI_INT,context->intraComm_);
163
164  displ[0]=0 ;
165  for(int n=1;n<nbClient;n++) displ[n]=displ[n-1]+recvCount[n-1];
166  int recvSize=displ[nbClient-1]+recvCount[nbClient-1];
167  int* recvRankBuff=new int[recvSize];
168  int* recvSizeBuff=new int[recvSize];
169  MPI_Allgatherv(sendRankBuff,connectedClient,MPI_INT,recvRankBuff,recvCount,displ,MPI_INT,context->intraComm_);
170  MPI_Allgatherv(sendSizeBuff,connectedClient,MPI_INT,recvSizeBuff,recvCount,displ,MPI_INT,context->intraComm_);
171  for (int i = 0; i < nbClient; ++i)
172  {
173    int currentPos = displ[i];
174    for (int j = 0; j < recvCount[i]; ++j)
175      if (recvRankBuff[currentPos+j] == clientRank)
176      {
177        recvRankSizeMap[i] = recvSizeBuff[currentPos+j];
178      }
179  }
180
181  // Sending global index of grid source to corresponding process as well as the corresponding mask
182  std::vector<MPI_Request> requests;
183  std::vector<MPI_Status> status;
184  std::unordered_map<int, unsigned long* > recvGlobalIndexSrc;
185  std::unordered_map<int, double* > sendValueToDest;
186  for (std::map<int,int>::const_iterator itRecv = recvRankSizeMap.begin(); itRecv != recvRankSizeMap.end(); ++itRecv)
187  {
188    int recvRank = itRecv->first;
189    int recvSize = itRecv->second;
190    recvGlobalIndexSrc[recvRank] = new unsigned long [recvSize];
191    sendValueToDest[recvRank] = new double [recvSize];
192
193    requests.push_back(MPI_Request());
194    MPI_Irecv(recvGlobalIndexSrc[recvRank], recvSize, MPI_UNSIGNED_LONG, recvRank, 46, context->intraComm_, &requests.back());
195  }
196
197  std::unordered_map<int, unsigned long* > sendGlobalIndexSrc;
198  std::unordered_map<int, double* > recvValueFromSrc;
199  for (itIndex = itbIndex; itIndex != iteIndex; ++itIndex)
200  {
201    int sendRank = itIndex->first;
202    int sendSize = sendRankSizeMap[sendRank];
203    const std::vector<size_t>& sendIndexMap = itIndex->second;
204    std::vector<size_t>::const_iterator itbSend = sendIndexMap.begin(), iteSend = sendIndexMap.end(), itSend;
205    sendGlobalIndexSrc[sendRank] = new unsigned long [sendSize];
206    recvValueFromSrc[sendRank] = new double [sendSize];
207    int countIndex = 0;
208    for (itSend = itbSend; itSend != iteSend; ++itSend)
209    {
210      sendGlobalIndexSrc[sendRank][countIndex] = *itSend;
211      ++countIndex;
212    }
213
214    // Send global index source and mask
215    requests.push_back(MPI_Request());
216    MPI_Isend(sendGlobalIndexSrc[sendRank], sendSize, MPI_UNSIGNED_LONG, sendRank, 46, context->intraComm_, &requests.back());
217  }
218
219  status.resize(requests.size());
220  MPI_Waitall(requests.size(), &requests[0], &status[0]);
221
222
223  std::vector<MPI_Request>().swap(requests);
224  std::vector<MPI_Status>().swap(status);
225
226  // Okie, on destination side, we will wait for information of masked index of source
227  for (std::map<int,int>::const_iterator itSend = sendRankSizeMap.begin(); itSend != sendRankSizeMap.end(); ++itSend)
228  {
229    int recvRank = itSend->first;
230    int recvSize = itSend->second;
231
232    requests.push_back(MPI_Request());
233    MPI_Irecv(recvValueFromSrc[recvRank], recvSize, MPI_DOUBLE, recvRank, 48, context->intraComm_, &requests.back());
234  }
235
236  for (std::map<int,int>::const_iterator itRecv = recvRankSizeMap.begin(); itRecv != recvRankSizeMap.end(); ++itRecv)
237  {
238    int recvRank = itRecv->first;
239    int recvSize = itRecv->second;
240    double* sendValue = sendValueToDest[recvRank];
241    unsigned long* recvIndexSrc = recvGlobalIndexSrc[recvRank];
242    int realSendSize = 0;
243    for (int idx = 0; idx < recvSize; ++idx)
244    {
245      size_t globalIndex = *(recvIndexSrc+idx);
246      int localIndex = globalIndex - ibeginSrc;
247      *(sendValue + idx) = axisSrc_->value(localIndex);
248    }
249    // Okie, now inform the destination which source index are masked
250    requests.push_back(MPI_Request());
251    MPI_Isend(sendValueToDest[recvRank], recvSize, MPI_DOUBLE, recvRank, 48, context->intraComm_, &requests.back());
252  }
253  status.resize(requests.size());
254  MPI_Waitall(requests.size(), &requests[0], &status[0]);
255
256
257  size_t nGloAxisDest = axisDest_->n_glo.getValue() - 1;
258  for (std::map<int,int>::const_iterator itSend = sendRankSizeMap.begin(); itSend != sendRankSizeMap.end(); ++itSend)
259  {
260    int recvRank = itSend->first;
261    int recvSize = itSend->second;
262
263    double* recvValue = recvValueFromSrc[recvRank];
264    unsigned long* recvIndex = sendGlobalIndexSrc[recvRank];
265    for (int idx = 0; idx < recvSize; ++idx)
266    {
267      size_t globalIndex = *(recvIndex+idx);
268      int localIndex = ((nGloAxisDest-globalIndex) - axisDest_->begin);
269      axisDest_->value(localIndex) = *(recvValue + idx);
270    }
271  }
272
273  delete [] recvCount;
274  delete [] displ;
275  delete [] sendRankBuff;
276  delete [] recvRankBuff;
277  delete [] sendSizeBuff;
278  delete [] recvSizeBuff;
279
280  std::unordered_map<int, double* >::const_iterator itChar;
281  for (itChar = sendValueToDest.begin(); itChar != sendValueToDest.end(); ++itChar)
282    delete [] itChar->second;
283  for (itChar = recvValueFromSrc.begin(); itChar != recvValueFromSrc.end(); ++itChar)
284    delete [] itChar->second;
285  std::unordered_map<int, unsigned long* >::const_iterator itLong;
286  for (itLong = sendGlobalIndexSrc.begin(); itLong != sendGlobalIndexSrc.end(); ++itLong)
287    delete [] itLong->second;
288  for (itLong = recvGlobalIndexSrc.begin(); itLong != recvGlobalIndexSrc.end(); ++itLong)
289    delete [] itLong->second;
290}
291CATCH
292
293}
Note: See TracBrowser for help on using the repository browser.