Thyra_MultiVectorSerialization.hpp

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

Generated on Thu Sep 18 12:39:52 2008 for Thyra ANA Operator/VectorBase Interfaces and Related Software by doxygen 1.3.9.1