Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Tsqr_printGlobalMatrix.hpp
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2009) 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 __Tsqr_printGlobalMatrix_hpp
00030 #define __Tsqr_printGlobalMatrix_hpp
00031 
00032 #include <Tsqr_MessengerBase.hpp>
00033 #include <Tsqr_Util.hpp>
00034 #include <Tsqr_Matrix.hpp>
00035 #include <Teuchos_ScalarTraits.hpp>
00036 
00037 #include <limits>
00038 #include <ostream>
00039 #include <stdexcept>
00040 
00041 
00042 namespace TSQR {
00043 
00058   template<class ConstMatrixViewType>
00059   void
00060   printGlobalMatrix (std::ostream& out,
00061          const ConstMatrixViewType& A_local,
00062          MessengerBase<typename ConstMatrixViewType::scalar_type>* const scalarComm,
00063          MessengerBase<typename ConstMatrixViewType::ordinal_type>* const ordinalComm)
00064     {
00065       typedef typename ConstMatrixViewType::ordinal_type LocalOrdinal;
00066       typedef typename ConstMatrixViewType::scalar_type Scalar;
00067       typedef Teuchos::ScalarTraits<Scalar> STS;
00068       using std::endl;
00069 
00070       const int myRank = scalarComm->rank ();
00071       const int nprocs = scalarComm->size ();
00072       const LocalOrdinal nrowsLocal = A_local.nrows();
00073       const LocalOrdinal ncols = A_local.ncols();
00074       const Scalar quiet_NaN = STS::nan();
00075 
00076       if (myRank == 0)
00077   {
00078     // Print the remote matrix data
00079     // out << "Processor " << my_rank << ":" << endl;
00080     print_local_matrix (out, A_local.nrows(), A_local.ncols(), 
00081             A_local.get(), A_local.lda());
00082 
00083     // Space for remote matrix data.  Other processors are allowed
00084     // to have different nrows_local values; we make space as
00085     // necessary.
00086     Matrix<LocalOrdinal, Scalar> A_remote (nrowsLocal, ncols, quiet_NaN);
00087 
00088     // Loop through all the other processors in order.
00089     // Fetch their matrix data and print it.
00090     for (int srcProc = 1; srcProc < nprocs; ++srcProc)
00091       {
00092         // Get processor proc's local matrix dimensions
00093         LocalOrdinal dims[2];
00094         ordinalComm->recv (&dims[0], 2, srcProc, 0);
00095 
00096         // Make space for the remote matrix data.
00097         //
00098         // mfh 13 Oct 2010: Teuchos::OrdinalTraits does not
00099         // currently have this feature.  It's OK to use
00100         // std::numeric_limits, since ordinal types in Trilinos
00101         // are intended to be built-in types (like int or long
00102         // long int).  std::numeric_limits only promises to work
00103         // for built-in types, unless someone has defined an
00104         // appropriate specialization.  Teuchos::ScalarTraits,
00105         // in contrast, has to work for non-built-in Scalar
00106         // types, like ARPREC or QD floating-point numbers.
00107         if (std::numeric_limits<LocalOrdinal>::is_signed)
00108     {
00109       if (dims[0] <= 0 || dims[1] <= 0)
00110         throw std::runtime_error ("Invalid dimensions of remote matrix");
00111     }
00112         else
00113     {
00114       if (dims[0] == 0 || dims[1] == 0)
00115         throw std::runtime_error ("Invalid dimensions of remote matrix");
00116     }
00117         A_remote.reshape (dims[0], dims[1]);
00118 
00119         // Receive the remote matrix data, which we assume is
00120         // stored contiguously.
00121         scalarComm->recv (A_remote.get(), dims[0]*dims[1], srcProc, 0);
00122 
00123         // Print the remote matrix data
00124         // out << "Processor " << proc << ":" << endl;
00125         print_local_matrix (out, dims[0], dims[0], A_remote.get(), A_remote.lda());
00126       }
00127   }
00128       else
00129   {
00130     // Send my local matrix dimensions to proc 0.
00131     int rootProc = 0;
00132     LocalOrdinal dims[2];
00133 
00134     dims[0] = nrowsLocal;
00135     dims[1] = ncols;
00136     ordinalComm->send (dims, 2, rootProc, 0);
00137 
00138     // Create a (contiguous) buffer and copy the data into it.
00139     Matrix< LocalOrdinal, Scalar > A_buf (nrowsLocal, ncols, quiet_NaN);
00140     A_buf.copy (A_local);
00141 
00142     // Send the actual data to proc 0.
00143     scalarComm->send (A_buf.get(), nrowsLocal*ncols, rootProc, 0);
00144   }
00145       scalarComm->barrier ();
00146     }
00147 
00148 } // namespace TSQR
00149 
00150 #endif // __Tsqr_printGlobalMatrix_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends