00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00054
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
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
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
00079
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
00091
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
00119 in >> std::ws;
00120
00121 if( binaryMode() ) {
00122
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
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
00152
00153 TEST_FOR_EXCEPTION( true, std::logic_error, "Does not handle non-MPI spaces yet" );
00154 }
00155 }
00156
00157 }
00158
00159 #endif // THYRA_MULTI_VECTOR_SERIALIZATION_HPP