Tpetra_Import.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2008) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TPETRA_IMPORT_HPP
00030 #define TPETRA_IMPORT_HPP
00031 
00032 #include <Kokkos_DefaultNode.hpp>
00033 #include <Teuchos_Describable.hpp>
00034 #include <Teuchos_as.hpp>
00035 #include <Teuchos_OrdinalTraits.hpp>
00036 #include <Teuchos_RCP.hpp>
00037 #include <Teuchos_Array.hpp>
00038 #include <Teuchos_ArrayRCP.hpp>
00039 #include "Tpetra_Map.hpp"
00040 #include "Tpetra_Util.hpp"
00041 #include "Tpetra_ImportExportData.hpp"
00042 #include <iterator>
00043 
00044 namespace Tpetra {
00045 
00047 
00058   template <class LocalOrdinal, class GlobalOrdinal = LocalOrdinal, class Node = Kokkos::DefaultNode::DefaultNodeType>
00059   class Import: public Teuchos::Describable {
00060 
00061   public:
00062 
00064 
00065 
00067     Import(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & source, 
00068            const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & target);
00069 
00071     Import(const Import<LocalOrdinal,GlobalOrdinal,Node> & import);
00072 
00074     ~Import();
00075 
00077 
00079 
00080 
00082     size_t getNumSameIDs() const;
00083 
00085     size_t getNumPermuteIDs() const;
00086 
00088     Teuchos::ArrayView<const LocalOrdinal> getPermuteFromLIDs() const;
00089 
00091     Teuchos::ArrayView<const LocalOrdinal> getPermuteToLIDs() const;
00092 
00094     size_t getNumRemoteIDs() const;
00095 
00097     Teuchos::ArrayView<const LocalOrdinal> getRemoteLIDs() const;
00098 
00100     size_t getNumExportIDs() const;
00101 
00103     Teuchos::ArrayView<const LocalOrdinal> getExportLIDs() const;
00104 
00106     Teuchos::ArrayView<const int> getExportImageIDs() const;
00107 
00109     const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getSourceMap() const;
00110 
00112     const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getTargetMap() const;
00113 
00114     Distributor & getDistributor() const;
00115 
00117     Import<LocalOrdinal,GlobalOrdinal,Node>& operator = (const Import<LocalOrdinal,GlobalOrdinal,Node> & Source);
00118 
00120 
00122 
00123 
00125     virtual void print(std::ostream& os) const;
00126 
00128 
00129   private:
00130 
00131     Teuchos::RCP<ImportExportData<LocalOrdinal,GlobalOrdinal,Node> > ImportData_;
00132     Teuchos::RCP<Teuchos::Array<GlobalOrdinal> > remoteGIDs_;
00133 
00134     // subfunctions used by constructor
00135     //==============================================================================
00136     // sets up numSameIDs_, numPermuteIDs_, and numRemoteIDs_
00137     // these variables are already initialized to 0 by the ImportExportData ctr.
00138     // also sets up permuteToLIDs_, permuteFromLIDs_, and remoteLIDs_
00139     void setupSamePermuteRemote();
00140     void setupExport();
00141   };
00142 
00143   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00144   Import<LocalOrdinal,GlobalOrdinal,Node>::Import(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & source, 
00145                                                   const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & target) {
00146     ImportData_ = Teuchos::rcp(new ImportExportData<LocalOrdinal,GlobalOrdinal,Node>(source, target));
00147     // call subfunctions
00148     setupSamePermuteRemote();
00149     if( source->isDistributed()) {
00150       setupExport();
00151     }
00152     // don't need remoteGIDs_ anymore
00153     remoteGIDs_ = Teuchos::null;
00154   }
00155 
00156   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00157   Import<LocalOrdinal,GlobalOrdinal,Node>::Import(const Import<LocalOrdinal,GlobalOrdinal,Node> & import)
00158   : ImportData_(import.ImportData_)
00159   {}
00160 
00161   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00162   Import<LocalOrdinal,GlobalOrdinal,Node>::~Import() 
00163   {}
00164 
00165   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00166   size_t Import<LocalOrdinal,GlobalOrdinal,Node>::getNumSameIDs() const {
00167     return ImportData_->numSameIDs_;
00168   }
00169 
00170   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00171   size_t Import<LocalOrdinal,GlobalOrdinal,Node>::getNumPermuteIDs() const {
00172     return ImportData_->permuteFromLIDs_.size();
00173   }
00174 
00175   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00176   Teuchos::ArrayView<const LocalOrdinal> 
00177   Import<LocalOrdinal,GlobalOrdinal,Node>::getPermuteFromLIDs() const {
00178     return ImportData_->permuteFromLIDs_();
00179   }
00180 
00181   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00182   Teuchos::ArrayView<const LocalOrdinal>
00183   Import<LocalOrdinal,GlobalOrdinal,Node>::getPermuteToLIDs() const {
00184     return ImportData_->permuteToLIDs_();
00185   }
00186 
00187   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00188   size_t Import<LocalOrdinal,GlobalOrdinal,Node>::getNumRemoteIDs() const {
00189     return ImportData_->remoteLIDs_.size();
00190   }
00191 
00192   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00193   Teuchos::ArrayView<const LocalOrdinal> 
00194   Import<LocalOrdinal,GlobalOrdinal,Node>::getRemoteLIDs() const {
00195     return ImportData_->remoteLIDs_();
00196   }
00197 
00198   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00199   size_t Import<LocalOrdinal,GlobalOrdinal,Node>::getNumExportIDs() const {
00200     return ImportData_->exportLIDs_.size();
00201   }
00202 
00203   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00204   Teuchos::ArrayView<const LocalOrdinal> 
00205   Import<LocalOrdinal,GlobalOrdinal,Node>::getExportLIDs() const {
00206     return ImportData_->exportLIDs_();
00207   }
00208 
00209   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00210   Teuchos::ArrayView<const int> 
00211   Import<LocalOrdinal,GlobalOrdinal,Node>::getExportImageIDs() const {
00212     return ImportData_->exportImageIDs_();
00213   }
00214 
00215   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00216   const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & 
00217   Import<LocalOrdinal,GlobalOrdinal,Node>::getSourceMap() const {
00218     return ImportData_->source_;
00219   }
00220 
00221   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00222   const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & 
00223   Import<LocalOrdinal,GlobalOrdinal,Node>::getTargetMap() const {
00224     return ImportData_->target_;
00225   }
00226 
00227   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00228   Distributor & 
00229   Import<LocalOrdinal,GlobalOrdinal,Node>::getDistributor() const {
00230     return ImportData_->distributor_;
00231   }
00232 
00233   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00234   Import<LocalOrdinal,GlobalOrdinal,Node>& 
00235   Import<LocalOrdinal,GlobalOrdinal,Node>::operator=(const Import<LocalOrdinal,GlobalOrdinal,Node> & Source) {
00236     ImportData_ = Source.ImportData_;
00237     return *this;
00238   }
00239 
00240   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00241   void Import<LocalOrdinal,GlobalOrdinal,Node>::print(std::ostream& os) const {
00242     using std::endl;
00243     Teuchos::ArrayView<const LocalOrdinal> av;
00244     Teuchos::ArrayView<const int> avi;
00245     const Teuchos::RCP<const Teuchos::Comm<int> > & comm = getSourceMap()->getComm();
00246     int myImageID = comm->getRank();
00247     int numImages = comm->getSize();
00248     for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
00249       if(myImageID == imageCtr) {
00250         os << endl;
00251         if(myImageID == 0) { // this is the root node (only output this info once)
00252           os << "Import Data Members:" << endl;
00253         }
00254         os << "Image ID       : " << myImageID << endl;
00255         os << "permuteFromLIDs: {"; av = getPermuteFromLIDs(); std::copy(av.begin(),av.end(),std::ostream_iterator<LocalOrdinal>(os," ")); os << " }" << endl;
00256         os << "permuteToLIDs  : {"; av = getPermuteToLIDs();   std::copy(av.begin(),av.end(),std::ostream_iterator<LocalOrdinal>(os," ")); os << " }" << endl;
00257         os << "remoteLIDs     : {"; av = getRemoteLIDs();      std::copy(av.begin(),av.end(),std::ostream_iterator<LocalOrdinal>(os," ")); os << " }" << endl;
00258         os << "exportLIDs     : {"; av = getExportLIDs();      std::copy(av.begin(),av.end(),std::ostream_iterator<LocalOrdinal>(os," ")); os << " }" << endl;
00259         os << "exportImageIDs : {"; avi = getExportImageIDs();  std::copy(avi.begin(),avi.end(),std::ostream_iterator<int>(os," ")); os << " }" << endl;
00260         os << "numSameIDs     : " << getNumSameIDs() << endl;
00261         os << "numPermuteIDs  : " << getNumPermuteIDs() << endl;
00262         os << "numRemoteIDs   : " << getNumRemoteIDs() << endl;
00263         os << "numExportIDs   : " << getNumExportIDs() << endl;
00264       }
00265       // Do a few global ops to give I/O a chance to complete
00266       comm->barrier();
00267       comm->barrier();
00268       comm->barrier();
00269     }
00270     if (myImageID == 0) {
00271       os << "\nSource Map: " << endl; 
00272     }
00273     os << *getSourceMap();
00274     if (myImageID == 0) {
00275       os << "\nTarget Map: " << endl; 
00276     }
00277     os << *getTargetMap();
00278   }
00279 
00280 
00281   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00282   void Import<LocalOrdinal,GlobalOrdinal,Node>::setupSamePermuteRemote() {
00283     const Map<LocalOrdinal,GlobalOrdinal,Node> & source = *getSourceMap();
00284     const Map<LocalOrdinal,GlobalOrdinal,Node> & target = *getTargetMap();
00285     Teuchos::ArrayView<const GlobalOrdinal> sourceGIDs = source.getNodeElementList();
00286     Teuchos::ArrayView<const GlobalOrdinal> targetGIDs = target.getNodeElementList();
00287 
00288     // -- compute numSameIDs_ ---
00289     // go through GID lists of source and target. if the ith GID on both is the same, 
00290     // increment numSameIDs_ and try the next. as soon as you come to a pair that don't
00291     // match, give up.
00292     typename Teuchos::ArrayView<const GlobalOrdinal>::iterator sourceIter = sourceGIDs.begin(),
00293                                                          targetIter = targetGIDs.begin();
00294     while( sourceIter != sourceGIDs.end() && targetIter != targetGIDs.end() && *sourceIter == *targetIter )
00295     {
00296       ++ImportData_->numSameIDs_;
00297       ++sourceIter;
00298       ++targetIter;
00299     }
00300     // targetIter should now point to the GID of the first non-same entry or the end of targetGIDs
00301 
00302     // -- compute numPermuteIDs and numRemoteIDs --
00303     // -- fill permuteToLIDs_, permuteFromLIDs_, remoteGIDs_, and remoteLIDs_ --
00304     // go through remaining entries in targetGIDs. if source owns that GID, 
00305     // increment numPermuteIDs_, and add entries to permuteToLIDs_ and permuteFromLIDs_.
00306     // otherwise increment numRemoteIDs_ and add entries to remoteLIDs_ and remoteGIDs_.
00307     remoteGIDs_ = Teuchos::rcp( new Teuchos::Array<GlobalOrdinal>() );
00308     for (; targetIter != targetGIDs.end(); ++targetIter) {
00309       if (source.isNodeGlobalElement(*targetIter)) {
00310         // both source and target list this GID (*targetIter)
00311         // determine the LIDs for this GID on both Maps and add them to the permutation lists
00312         ImportData_->permuteToLIDs_.push_back(target.getLocalElement(*targetIter));
00313         ImportData_->permuteFromLIDs_.push_back(source.getLocalElement(*targetIter));
00314       }
00315       else {
00316         // this GID is on another processor; store it, along with its destination LID on this processor
00317         remoteGIDs_->push_back(*targetIter);
00318         ImportData_->remoteLIDs_.push_back(target.getLocalElement(*targetIter));
00319       }
00320     }
00321 
00322     TEST_FOR_EXCEPTION( (getNumRemoteIDs() > 0) && !source.isDistributed(), std::runtime_error, 
00323         Teuchos::typeName(*this) << "::setupSamePermuteRemote(): Target has remote LIDs but Source is not distributed globally.");
00324   }
00325 
00326 
00327   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00328   void Import<LocalOrdinal,GlobalOrdinal,Node>::setupExport() {
00329     const Map<LocalOrdinal,GlobalOrdinal,Node> & source = *getSourceMap();
00330 
00331     // create remoteImageID list: for each entry remoteGIDs[i],
00332     // remoteImageIDs[i] will contain the ImageID of the image that owns that GID.
00333     // check for GIDs that exist in target but not in source: we see this if getRemoteIndexList returns true
00334     Teuchos::ArrayView<GlobalOrdinal> remoteGIDs = (*remoteGIDs_)();
00335     Teuchos::Array<int> remoteImageIDs(remoteGIDs.size());
00336     TEST_FOR_EXCEPTION( source.getRemoteIndexList(remoteGIDs, remoteImageIDs()) == IDNotPresent, std::runtime_error,
00337         "Tpetra::Import::setupExport(): Target has GIDs not found in Source.");
00338 
00339     // sort remoteImageIDs in ascending order
00340     // apply same permutation to remoteGIDs_
00341     sort2(remoteImageIDs.begin(), remoteImageIDs.end(), remoteGIDs.begin());
00342 
00343     // call Distributor.createFromRecvs()
00344     // takes in remoteGIDs and remoteImageIDs_
00345     // returns exportLIDs_, exportImageIDs_ 
00346     Teuchos::ArrayRCP<GlobalOrdinal> exportGIDs;
00347     ImportData_->distributor_.createFromRecvs(remoteGIDs().getConst(), remoteImageIDs, exportGIDs, ImportData_->exportImageIDs_);
00348     // -- exportGIDs and exportImageIDs_ allocated by createFromRecvs (the former contains GIDs, we will convert to LIDs below) --
00349 
00350     // convert exportGIDs from GIDs to LIDs
00351     if (exportGIDs != Teuchos::null) {
00352       ImportData_->exportLIDs_ = Teuchos::arcp<LocalOrdinal>(exportGIDs.size());
00353     }
00354     typename Teuchos::ArrayRCP<LocalOrdinal>::const_iterator dst = ImportData_->exportLIDs_.begin();
00355     typename Teuchos::ArrayRCP<GlobalOrdinal>::const_iterator src = exportGIDs.begin();
00356     while (src != exportGIDs.end())
00357     {
00358       (*dst++) = source.getLocalElement(*src++);
00359     }
00360   }
00361 
00362 } // namespace Tpetra
00363 
00364 #endif // TPETRA_IMPORT_HPP

Generated on Wed May 12 21:40:14 2010 for Tpetra Matrix/Vector Services by  doxygen 1.4.7