Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
Tpetra_Import_Util.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2008) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 
00042 #ifndef TPETRA_IMPORT_UTIL_HPP
00043 #define TPETRA_IMPORT_UTIL_HPP
00044 
00050 #include "Tpetra_ConfigDefs.hpp" // for map, vector, string, and iostream
00051 #include "Tpetra_Import.hpp"
00052 #include "Tpetra_HashTable.hpp"
00053 #include "Tpetra_Map.hpp"
00054 #include "Tpetra_Util.hpp"
00055 #include "Tpetra_Distributor.hpp"
00056 #include <Teuchos_Array.hpp>
00057 #include <utility>
00058 
00059 namespace Tpetra {
00060   namespace Import_Util {
00062 
00068     template <typename LocalOrdinal, typename GlobalOrdinal, typename Node>
00069     void getPidGidPairs(const Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> & Importer, Teuchos::Array< std::pair<int,GlobalOrdinal> > & gpids, bool use_minus_one_for_local);
00070 
00072 
00074     template <typename LocalOrdinal, typename GlobalOrdinal, typename Node>
00075     void getPids(const Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> & Importer, Teuchos::Array<int> &pids, bool use_minus_one_for_local);
00076 
00078 
00080     template <typename LocalOrdinal, typename GlobalOrdinal, typename Node>
00081     void getRemotePIDs(const Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> & Importer, Teuchos::Array<int> &RemotePIDs);
00082 
00083   }// end Import_Util
00084 }//end Tpetra
00085 
00086 
00087 
00088 
00092 
00093 //----------------------------------------------------------------------------
00094 template <typename LocalOrdinal, typename GlobalOrdinal, typename Node>
00095 void Tpetra::Import_Util::getPidGidPairs(const Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> & Importer, Teuchos::Array< std::pair<int,GlobalOrdinal> > & gpids, bool use_minus_one_for_local) {
00096   // Put the (PID,GID) pair in member of Importer.TargetMap() in gpids.  If use_minus_one_for_local==true, put in -1 instead of MyPID.
00097   const Tpetra::Distributor & D=Importer.getDistributor();
00098 
00099   LocalOrdinal ii;
00100   size_t  i,j,k;
00101   int mypid = Importer.getTargetMap()->getComm()->getRank();
00102   size_t N  = Importer.getTargetMap()->getNodeNumElements();
00103 
00104   // Get the importer's data
00105   Teuchos::ArrayView<const LocalOrdinal> RemoteLIDs  = Importer.getRemoteLIDs();
00106 
00107   // Get the distributor's data
00108   size_t NumReceives                           = D.getNumReceives();
00109   Teuchos::ArrayView<const int> ProcsFrom      = D.getImagesFrom();
00110   Teuchos::ArrayView<const size_t> LengthsFrom = D.getLengthsFrom();
00111 
00112   // Resize the outgoing data structure
00113   gpids.resize(N);
00114 
00115   // Start by claiming that I own all the data
00116   LocalOrdinal lzero = Teuchos::ScalarTraits<LocalOrdinal>::zero();
00117   if(use_minus_one_for_local)
00118     for(ii=lzero; Teuchos::as<size_t>(ii)<N; ii++) gpids[ii]=std::make_pair(-1,Importer.getTargetMap()->getGlobalElement(ii));
00119   else
00120     for(ii=lzero; Teuchos::as<size_t>(ii)<N; ii++) gpids[ii]=std::make_pair(mypid,Importer.getTargetMap()->getGlobalElement(ii));
00121 
00122   // Now, for each remote ID, record who actually owns it.  This loop follows the operation order in the
00123   // MpiDistributor so it ought to duplicate that effect.
00124   for(i=0,j=0; i<NumReceives; i++){
00125     int pid=ProcsFrom[i];
00126     for(k=0; k<LengthsFrom[i]; k++){
00127       if(pid!=mypid) gpids[RemoteLIDs[j]].first=pid;
00128       j++;
00129     }
00130   }
00131 }
00132 
00133 
00134 
00135 
00136 //----------------------------------------------------------------------------
00137 template <typename LocalOrdinal, typename GlobalOrdinal, typename Node>
00138 void Tpetra::Import_Util::getPids(const Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> & Importer, Teuchos::Array<int> &pids, bool use_minus_one_for_local) {
00139   const Tpetra::Distributor & D=Importer.getDistributor();
00140 
00141   LocalOrdinal ii;
00142   size_t  i,j,k;
00143   int mypid = Importer.getTargetMap()->getComm()->getRank();
00144   size_t N  = Importer.getTargetMap()->getNodeNumElements();
00145 
00146   // Get the importer's data
00147   Teuchos::ArrayView<const LocalOrdinal> RemoteLIDs  = Importer.getRemoteLIDs();
00148 
00149   // Get the distributor's data
00150   size_t NumReceives                           = D.getNumReceives();
00151   Teuchos::ArrayView<const int> ProcsFrom      = D.getImagesFrom();
00152   Teuchos::ArrayView<const size_t> LengthsFrom = D.getLengthsFrom();
00153 
00154   // Resize the outgoing data structure
00155   pids.resize(N);
00156 
00157   // Start by claiming that I own all the data
00158   LocalOrdinal lzero = Teuchos::ScalarTraits<LocalOrdinal>::zero();
00159   if(use_minus_one_for_local)
00160     for(ii=lzero; Teuchos::as<size_t>(ii)<N; ii++) pids[ii]=-1;
00161   else
00162     for(ii=lzero; Teuchos::as<size_t>(ii)<N; ii++) pids[ii]=mypid;
00163 
00164   // Now, for each remote ID, record who actually owns it.  This loop follows the operation order in the
00165   // MpiDistributor so it ought to duplicate that effect.
00166   for(i=0,j=0; i<NumReceives; i++){
00167     int pid=ProcsFrom[i];
00168     for(k=0; k<LengthsFrom[i]; k++){
00169       if(pid!=mypid) pids[RemoteLIDs[j]]=pid;
00170       j++;
00171     }
00172   }
00173 }
00174 
00175 
00176 //----------------------------------------------------------------------------
00177 template <typename LocalOrdinal, typename GlobalOrdinal, typename Node>
00178 void Tpetra::Import_Util::getRemotePIDs(const Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> & Importer, Teuchos::Array<int> &RemotePIDs) {
00179   const Tpetra::Distributor & D=Importer.getDistributor();
00180 
00181   size_t  i,j,k;
00182 
00183   // Get the importer's data
00184   Teuchos::ArrayView<const LocalOrdinal> RemoteLIDs  = Importer.getRemoteLIDs();
00185 
00186   // Get the distributor's data
00187   size_t NumReceives                           = D.getNumReceives();
00188   Teuchos::ArrayView<const int> ProcsFrom      = D.getImagesFrom();
00189   Teuchos::ArrayView<const size_t> LengthsFrom = D.getLengthsFrom();
00190 
00191   // Resize the outgoing data structure
00192   RemotePIDs.resize(Importer.getNumRemoteIDs());
00193 
00194   // Now, for each remote ID, record who actually owns it.  This loop follows the operation order in the
00195   // MpiDistributor so it ought to duplicate that effect.
00196   for(i=0,j=0; i<NumReceives; i++){
00197     int pid=ProcsFrom[i];
00198     for(k=0; k<LengthsFrom[i]; k++){
00199       RemotePIDs[j]=pid;
00200       j++;
00201     }
00202   }
00203 }
00204 
00205 
00206 
00207 
00208 #endif // TPETRA_IMPORT_UTIL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines