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_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
00056
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
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
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
00081
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
00095
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
00123 in >> std::ws;
00124
00125 if( binaryMode() ) {
00126
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
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
00156
00157 TEST_FOR_EXCEPTION( true, std::logic_error, "Does not handle non-SPMD spaces yet" );
00158 }
00159 }
00160
00161 }
00162
00163 #endif // THYRA_SPMD_MULTI_VECTOR_SERIALIZER_HPP