Ignore:
Timestamp:
10/27/16 18:14:46 (5 years ago)
Author:
mhnguyen
Message:

Implementing domain interpolation weights exportation
The option 'file' locates file to read if it exists, otherwise a new file with name = 'file' will be created

+) Write interpolation weights into a file

Test
+) On Curie
+) Work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/transformation/domain_algorithm_interpolate.cpp

    r978 r982  
    4848 
    4949CDomainAlgorithmInterpolate::CDomainAlgorithmInterpolate(CDomain* domainDestination, CDomain* domainSource, CInterpolateDomain* interpDomain) 
    50 : CDomainAlgorithmTransformation(domainDestination, domainSource), interpDomain_(interpDomain) 
     50: CDomainAlgorithmTransformation(domainDestination, domainSource), interpDomain_(interpDomain), writeToFile_(false) 
    5151{ 
    5252  interpDomain_->checkValid(domainSource); 
     53  if ((CInterpolateDomain::mode_attr::write == interpDomain_->mode) && 
     54      (!interpDomain_->file.isEmpty())) 
     55    writeToFile_ = true; 
    5356} 
    5457 
     
    351354  } 
    352355 
     356  if (writeToFile_) 
     357     writeRemapInfo(interpMapValue); 
    353358  exchangeRemapInfo(interpMapValue); 
    354359 
     
    434439void CDomainAlgorithmInterpolate::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) 
    435440{ 
    436   if (!interpDomain_->file.isEmpty()) 
     441  if (CInterpolateDomain::mode_attr::read == interpDomain_->mode)   
    437442    readRemapInfo(); 
    438443  else 
    439     computeRemap(); 
     444  { 
     445    computeRemap();  
     446  } 
     447} 
     448 
     449void CDomainAlgorithmInterpolate::writeRemapInfo(std::map<int,std::vector<std::pair<int,double> > >& interpMapValue) 
     450{ 
     451  std::string filename = interpDomain_->file.getValue(); 
     452  writeInterpolationInfo(filename, interpMapValue); 
    440453} 
    441454 
    442455void CDomainAlgorithmInterpolate::readRemapInfo() 
    443456{ 
    444   CContext* context = CContext::getCurrent(); 
    445   CContextClient* client=context->client; 
    446   int clientRank = client->clientRank; 
    447  
    448457  std::string filename = interpDomain_->file.getValue(); 
    449458  std::map<int,std::vector<std::pair<int,double> > > interpMapValue; 
     
    637646  delete [] recvBuff; 
    638647} 
     648  
     649/*! Redefined some functions of CONetCDF4 to make use of them */ 
     650CDomainAlgorithmInterpolate::WriteNetCdf::WriteNetCdf(const StdString& filename, const MPI_Comm comm) 
     651  : CNc4DataOutput(filename, false, false, true, comm, false, true) {} 
     652int CDomainAlgorithmInterpolate::WriteNetCdf::addDimensionWrite(const StdString& name,  
     653                                                                const StdSize size) 
     654{ 
     655  CONetCDF4::addDimension(name, size);   
     656} 
     657 
     658int CDomainAlgorithmInterpolate::WriteNetCdf::addVariableWrite(const StdString& name, nc_type type, 
     659                                                               const std::vector<StdString>& dim) 
     660{ 
     661  CONetCDF4::addVariable(name, type, dim); 
     662} 
     663 
     664void CDomainAlgorithmInterpolate::WriteNetCdf::writeDataIndex(const CArray<int,1>& data, const StdString& name, 
     665                                                              bool collective, StdSize record, 
     666                                                              const std::vector<StdSize>* start, 
     667                                                              const std::vector<StdSize>* count) 
     668{ 
     669  CONetCDF4::writeData<int,1>(data, name, collective, record, start, count); 
     670} 
     671 
     672void CDomainAlgorithmInterpolate::WriteNetCdf::writeDataIndex(const CArray<double,1>& data, const StdString& name, 
     673                                                              bool collective, StdSize record, 
     674                                                              const std::vector<StdSize>* start, 
     675                                                              const std::vector<StdSize>* count) 
     676{ 
     677  CONetCDF4::writeData<double,1>(data, name, collective, record, start, count); 
     678} 
     679 
     680/* 
     681   Write interpolation weights into a file 
     682   \param [in] filename name of output file 
     683   \param interpMapValue mapping of global index of domain destination and domain source as well as the corresponding weight 
     684*/ 
     685void CDomainAlgorithmInterpolate::writeInterpolationInfo(std::string& filename, 
     686                                                         std::map<int,std::vector<std::pair<int,double> > >& interpMapValue) 
     687{ 
     688  CContext* context = CContext::getCurrent(); 
     689  CContextClient* client=context->client; 
     690 
     691  size_t n_src = domainSrc_->ni_glo * domainSrc_->nj_glo; 
     692  size_t n_dst = domainDest_->ni_glo * domainDest_->nj_glo; 
     693 
     694  long localNbWeight = 0; 
     695  long globalNbWeight; 
     696  long startIndex; 
     697  typedef std::map<int,std::vector<std::pair<int,double> > > IndexRemap; 
     698  IndexRemap::iterator itb = interpMapValue.begin(), it, 
     699                       ite = interpMapValue.end(); 
     700  for (it = itb; it!=ite; ++it) 
     701  { 
     702    localNbWeight += (it->second).size(); 
     703  } 
     704 
     705  CArray<int,1> src_idx(localNbWeight); 
     706  CArray<int,1> dst_idx(localNbWeight); 
     707  CArray<double,1> weights(localNbWeight); 
     708 
     709  int index = 0; 
     710  for (it = itb; it !=ite; ++it) 
     711  { 
     712    std::vector<std::pair<int,double> >& tmp = it->second; 
     713    for (int idx = 0; idx < tmp.size(); ++idx) 
     714    { 
     715      dst_idx(index) = it->first + 1; 
     716      src_idx(index) = tmp[idx].first + 1; 
     717      weights(index) = tmp[idx].second; 
     718      ++index; 
     719    }     
     720  } 
     721 
     722  MPI_Allreduce(&localNbWeight, &globalNbWeight, 1, MPI_LONG, MPI_SUM, client->intraComm); 
     723  MPI_Scan(&localNbWeight, &startIndex, 1, MPI_LONG, MPI_SUM, client->intraComm); 
     724   
     725  std::vector<StdSize> start(1, startIndex - localNbWeight); 
     726  std::vector<StdSize> count(1, localNbWeight); 
     727 
     728  WriteNetCdf netCdfWriter(filename, client->intraComm); 
     729 
     730  // netCdfWriter = CONetCDF4(filename, false, false, true, client->intraComm, false); 
     731 
     732  // Define some dimensions 
     733  netCdfWriter.addDimensionWrite("n_src", n_src); 
     734  netCdfWriter.addDimensionWrite("n_dst", n_dst); 
     735  netCdfWriter.addDimensionWrite("n_weight", globalNbWeight); 
     736   
     737  std::vector<StdString> dims(1,"n_weight"); 
     738 
     739  // Add some variables 
     740  netCdfWriter.addVariableWrite("src_idx", NC_INT, dims); 
     741  netCdfWriter.addVariableWrite("dst_idx", NC_INT, dims); 
     742  netCdfWriter.addVariableWrite("weight", NC_DOUBLE, dims); 
     743 
     744  // // Write variables 
     745  netCdfWriter.writeDataIndex(src_idx, "src_idx", true, 0, &start, &count); 
     746  netCdfWriter.writeDataIndex(dst_idx, "dst_idx", true, 0, &start, &count); 
     747  netCdfWriter.writeDataIndex(weights, "weight", true, 0, &start, &count); 
     748 
     749  netCdfWriter.closeFile(); 
     750} 
    639751 
    640752/*! 
Note: See TracChangeset for help on using the changeset viewer.