source: XIOS/dev/branch_openmp/src/transformation/axis_algorithm_inverse.cpp @ 1328

Last change on this file since 1328 was 1328, checked in by yushan, 6 years ago

dev_omp

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