Thyra_SpmdMultiVectorSerializer.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) 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 THYRA_SPMD_MULTI_VECTOR_SERIALIZER_HPP
00030 #define THYRA_SPMD_MULTI_VECTOR_SERIALIZER_HPP
00031 
00032 #include "Thyra_SpmdMultiVectorSerializerDecl.hpp"
00033 #include "Thyra_SpmdVectorSpaceDefaultBase.hpp"
00034 #include "Thyra_MultiVectorBase.hpp"
00035 #include "Thyra_DetachedMultiVectorView.hpp"
00036 
00037 namespace Thyra {
00038 
00039 template<class Scalar>
00040 SpmdMultiVectorSerializer<Scalar>::SpmdMultiVectorSerializer(
00041   const bool  binaryMode
00042   )
00043   :binaryMode_(binaryMode)
00044 {}
00045 
00046 template<class Scalar>
00047 void SpmdMultiVectorSerializer<Scalar>::serialize(
00048   const MultiVectorBase<Scalar>& mv, std::ostream& out
00049   ) const
00050 {
00051   Teuchos::RefCountPtr<const SpmdVectorSpaceBase<Scalar> >
00052     mpi_vec_spc = Teuchos::rcp_dynamic_cast<const SpmdVectorSpaceBase<Scalar> >(mv.range());
00053   out.precision(std::numeric_limits<Scalar>::digits10+4);
00054   if( mpi_vec_spc.get() ) {
00055     // This is a mpi-based vector space so let's just write the local
00056     // multi-vector elements (row-by-row).
00057     const Index
00058       localOffset = mpi_vec_spc->localOffset(),
00059       localSubDim = mpi_vec_spc->localSubDim();
00060     const Range1D localRng( localOffset, localOffset+localSubDim-1 ); 
00061     ConstDetachedMultiVectorView<Scalar> local_mv(mv,localRng,Range1D());
00062     out << localSubDim << " " << local_mv.numSubCols() << std::endl;
00063     if( binaryMode() ) {
00064       // Write column-wise for better cache performance
00065       for( Index j = 0; j < local_mv.numSubCols(); ++j )
00066         out.write( reinterpret_cast<const char*>(&local_mv(0,j)), sizeof(Scalar)*localSubDim );
00067     }
00068     else {
00069       // Write row-wise for better readability
00070       for( Index i = 0; i < localSubDim; ++i ) {
00071         out << " " << i;
00072         for( Index j = 0; j < local_mv.numSubCols(); ++j ) {
00073           out << " " << local_mv(i,j);
00074         }
00075         out << std::endl;
00076       }
00077     }
00078   }
00079   else {
00080     //  This is a serial (or locally replicated) vector space so
00081     // just write all of the multi-vector elements here.
00082     TEST_FOR_EXCEPTION( true, std::logic_error, "Does not handle non-SPMD spaces yet" );
00083   }
00084 }
00085 
00086 template<class Scalar>
00087 void SpmdMultiVectorSerializer<Scalar>::deserialize(
00088   std::istream& in, MultiVectorBase<Scalar>* mv
00089   ) const
00090 {
00091   Teuchos::RefCountPtr<const SpmdVectorSpaceBase<Scalar> >
00092     mpi_vec_spc = Teuchos::rcp_dynamic_cast<const SpmdVectorSpaceBase<Scalar> >(mv->range());
00093   if( mpi_vec_spc.get() ) {
00094     // This is a mpi-based vector space so let's just read the local
00095     // multi-vector elements (row-by-row).
00096     const Index
00097       localOffset = mpi_vec_spc->localOffset(),
00098       localSubDim = mpi_vec_spc->localSubDim();
00099     const Range1D localRng( localOffset, localOffset+localSubDim-1 ); 
00100     DetachedMultiVectorView<Scalar> local_mv(*mv,localRng,Range1D());
00101 #ifdef TEUCHOS_DEBUG
00102     TEST_FOR_EXCEPTION( !in, std::logic_error, "Error, premature end of input!" );
00103 #endif
00104     Index localSubDim_in;
00105     in >> localSubDim_in;
00106 #ifdef TEUCHOS_DEBUG
00107     TEST_FOR_EXCEPTION(
00108       localSubDim != localSubDim_in, std::logic_error
00109       , "Error, localSubDim = "<<localSubDim<<" does not match the read in value of "
00110       "localSubDim_in = "<<localSubDim_in<<"!"
00111       );
00112 #endif
00113     Index numSubCols_in;
00114     in >> numSubCols_in;
00115 #ifdef TEUCHOS_DEBUG
00116     TEST_FOR_EXCEPTION(
00117       local_mv.numSubCols() != numSubCols_in, std::logic_error
00118       , "Error, numSubCols = "<<local_mv.numSubCols()<<" does not match the read in value of "
00119       "numSubCols_in = "<<numSubCols_in<<"!"
00120       );
00121 #endif
00122     // Get rid of extra newline after first line
00123     in >> std::ws;
00124     // Get the elements
00125     if( binaryMode() ) {
00126       // Column-wise
00127       for( Index j = 0; j < local_mv.numSubCols(); ++j )
00128         in.read( reinterpret_cast<char*>(&local_mv(0,j)), sizeof(Scalar)*localSubDim );
00129     }
00130     else {
00131       // Row-wise
00132       for( Index i = 0; i < localSubDim; ++i ) {
00133 #ifdef TEUCHOS_DEBUG
00134         TEST_FOR_EXCEPTION( !in, std::logic_error, "Error, premature end of input!" );
00135 #endif
00136         Index i_in;
00137         in >> i_in;
00138 #ifdef TEUCHOS_DEBUG
00139         TEST_FOR_EXCEPTION(
00140           i != i_in, std::logic_error
00141           , "Error, i = "<<i<<" does not match the read in value of "
00142           "i_in = "<<i_in<<"!"
00143           );
00144 #endif
00145         for( Index j = 0; j < local_mv.numSubCols(); ++j ) {
00146 #ifdef TEUCHOS_DEBUG
00147           TEST_FOR_EXCEPTION( !in, std::logic_error, "Error, premature end of input!" );
00148 #endif
00149           in >> local_mv(i,j);
00150         }
00151       }
00152     }
00153   }
00154   else {
00155     //  This is a serial (or locally replicated) vector space so
00156     // just read all of the multi-vector elements here.
00157     TEST_FOR_EXCEPTION( true, std::logic_error, "Does not handle non-SPMD spaces yet" );
00158   }
00159 }
00160 
00161 } // end namespace Thyra
00162 
00163 #endif // THYRA_SPMD_MULTI_VECTOR_SERIALIZER_HPP

Generated on Thu Sep 18 12:33:03 2008 for Thyra Package Browser (Single Doxygen Collection) by doxygen 1.3.9.1