source: XIOS/dev/dev_trunk_graph/src/transformation/axis_algorithm/axis_algorithm_inverse.cpp @ 2019

Last change on this file since 2019 was 2019, checked in by yushan, 3 years ago

Graph intermedia commit to a tmp branch

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