From 92e930a04b00c1e57f601d018044ed2e435dffff Mon Sep 17 00:00:00 2001 From: Timo Heister Date: Sun, 3 Sep 2017 15:09:41 -0400 Subject: [PATCH] ReadWriteVector and TrilinosWrappers::MPI::Vector print in parallel Print index and value for ::print() of ReadWriteVector and TrilinosWrappers::MPI::Vector. --- .../deal.II/lac/read_write_vector.templates.h | 14 ++-- source/lac/trilinos_vector.cc | 42 +++++++----- tests/lac/readwritevector_print.cc | 44 ++++++++++++ tests/lac/readwritevector_print.output | 7 ++ tests/trilinos/print_01.cc | 68 +++++++++++++++++++ tests/trilinos/print_01.mpirun=2.output | 37 ++++++++++ 6 files changed, 186 insertions(+), 26 deletions(-) create mode 100644 tests/lac/readwritevector_print.cc create mode 100644 tests/lac/readwritevector_print.output create mode 100644 tests/trilinos/print_01.cc create mode 100644 tests/trilinos/print_01.mpirun=2.output diff --git a/include/deal.II/lac/read_write_vector.templates.h b/include/deal.II/lac/read_write_vector.templates.h index 7938f76efa..722ca39030 100644 --- a/include/deal.II/lac/read_write_vector.templates.h +++ b/include/deal.II/lac/read_write_vector.templates.h @@ -25,6 +25,8 @@ #include #include +#include + #ifdef DEAL_II_WITH_PETSC # include #endif @@ -444,8 +446,7 @@ namespace LinearAlgebra const bool scientific) const { AssertThrow (out, ExcIO()); - std::ios::fmtflags old_flags = out.flags(); - unsigned int old_precision = out.precision (precision); + boost::io::ios_flags_saver restore_flags(out); out.precision (precision); if (scientific) @@ -456,15 +457,12 @@ namespace LinearAlgebra out << "IndexSet: "; stored_elements.print(out); out << std::endl; - for (unsigned int i=0; in_elements(); ++i) - out << val[i] << std::endl; + unsigned int i=0; + for (const auto &idx : this->stored_elements) + out << "[" << idx << "]: " << val[i++] << '\n'; out << std::flush; - AssertThrow (out, ExcIO()); - // reset output format - out.flags (old_flags); - out.precision(old_precision); } diff --git a/source/lac/trilinos_vector.cc b/source/lac/trilinos_vector.cc index daecaa7e31..1e731a0143 100644 --- a/source/lac/trilinos_vector.cc +++ b/source/lac/trilinos_vector.cc @@ -761,33 +761,39 @@ namespace TrilinosWrappers AssertThrow (out, ExcIO()); boost::io::ios_flags_saver restore_flags(out); - // get a representation of the - // vector and loop over all - // the elements TODO: up to - // now only local data printed - // out! Find a way to neatly - // output distributed data... - TrilinosScalar *val; - int leading_dimension; - int ierr = vector->ExtractView (&val, &leading_dimension); - AssertThrow (ierr == 0, ExcTrilinosError(ierr)); out.precision (precision); if (scientific) out.setf (std::ios::scientific, std::ios::floatfield); else out.setf (std::ios::fixed, std::ios::floatfield); - if (across) - for (size_type i=0; i(val[i]) << ' '; + if (size() != local_size()) + { + auto global_id = [&] (const size_type index) + { + return vector->Map().GID(static_cast(index)); + }; + out << "size:" << size() << " local_size:" << local_size() << " :" << std::endl; + for (size_type i=0; i(val[i]) << std::endl; - out << std::endl; + { + TrilinosScalar *val; + int leading_dimension; + int ierr = vector->ExtractView (&val, &leading_dimension); + + AssertThrow (ierr == 0, ExcTrilinosError(ierr)); + if (across) + for (size_type i=0; i(val[i]) << ' '; + else + for (size_type i=0; i(val[i]) << std::endl; + out << std::endl; + } - // restore the representation - // of the vector AssertThrow (out, ExcIO()); } diff --git a/tests/lac/readwritevector_print.cc b/tests/lac/readwritevector_print.cc new file mode 100644 index 0000000000..2226204132 --- /dev/null +++ b/tests/lac/readwritevector_print.cc @@ -0,0 +1,44 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2015 - 2016 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + +// Check printing + +#include "../tests.h" +#include +#include + + +void test() +{ + IndexSet is(10); + is.add_range(1,3); + is.add_index(7); + LinearAlgebra::ReadWriteVector vec(is); + deallog << "size: " << vec.n_elements() < +#include +#include +#include + +#include + +void test() +{ + IndexSet is(8); + unsigned int rank = Utilities::MPI::this_mpi_process(MPI_COMM_WORLD); + if (rank==0) + is.add_range(0,4); + if (rank==1) + is.add_range(4,8); + is.compress(); + + deallog << "owned IndexSet: "; + is.print(deallog); + + IndexSet is_ghosted(8); + is_ghosted.add_range(2,6); + deallog << "ghosted IndexSet: "; + is_ghosted.print(deallog); + + TrilinosWrappers::MPI::Vector tril_vector(is); + TrilinosWrappers::MPI::Vector tril_vector_ghosted; + tril_vector_ghosted.reinit(is, is_ghosted, MPI_COMM_WORLD); + for (unsigned int i=0; i<8; ++i) + tril_vector[i] = i; + tril_vector.compress(VectorOperation::insert); + + deallog << "trilinos vec:" << std::endl; + tril_vector.print(deallog.get_file_stream()); + + tril_vector_ghosted = tril_vector; + deallog << "trilinos vec ghosted:" << std::endl; + tril_vector_ghosted.print(deallog.get_file_stream()); + + deallog << "OK" <