// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 1999 - 2024 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_vector_templates_h #define dealii_vector_templates_h #include #include #include #include #include #include #include #ifdef DEAL_II_WITH_PETSC # include #endif #ifdef DEAL_II_WITH_TRILINOS # include #endif #ifdef DEAL_II_TRILINOS_WITH_TPETRA # include #endif #include #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN template Vector::Vector(const Vector &v) { *this = v; } template void Vector::apply_givens_rotation(const std::array &csr, const size_type i, const size_type k) { auto &V = *this; const Number t = V(i); V(i) = csr[0] * V(i) + csr[1] * V(k); V(k) = -csr[1] * t + csr[0] * V(k); } template template Vector::Vector(const Vector &v) { *this = v; } #ifdef DEAL_II_WITH_PETSC namespace internal { template void copy_petsc_vector(const PETScWrappers::VectorBase &v, ::dealii::Vector &out) { if (v.size() == 0) { out.reinit(0); return; } // Create a sequential PETSc vector and then copy over the entries into // the deal.II vector. Vec sequential_vector; VecScatter scatter_context; PetscErrorCode ierr = VecScatterCreateToAll(v, &scatter_context, &sequential_vector); AssertThrow(ierr == 0, ExcPETScError(ierr)); ierr = VecScatterBegin( scatter_context, v, sequential_vector, INSERT_VALUES, SCATTER_FORWARD); AssertThrow(ierr == 0, ExcPETScError(ierr)); ierr = VecScatterEnd( scatter_context, v, sequential_vector, INSERT_VALUES, SCATTER_FORWARD); AssertThrow(ierr == 0, ExcPETScError(ierr)); const PetscScalar *start_ptr; ierr = VecGetArrayRead(sequential_vector, &start_ptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); const PETScWrappers::VectorBase::size_type v_size = v.size(); if (out.size() != v_size) out.reinit(v_size, true); internal::VectorOperations::copy(start_ptr, start_ptr + out.size(), out.begin()); ierr = VecRestoreArrayRead(sequential_vector, &start_ptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); ierr = VecScatterDestroy(&scatter_context); AssertNothrow(ierr == 0, ExcPETScError(ierr)); ierr = VecDestroy(&sequential_vector); AssertNothrow(ierr == 0, ExcPETScError(ierr)); } } // namespace internal template Vector::Vector(const PETScWrappers::VectorBase &v) { internal::copy_petsc_vector(v, *this); } #endif #ifdef DEAL_II_WITH_TRILINOS template Vector::Vector(const TrilinosWrappers::MPI::Vector &v) : values(v.size()) { if (size() != 0) { // Copy the distributed vector to // a local one at all processors // that know about the original vector. // TODO: There could // be a better solution than // this, but it has not yet been // found. TrilinosWrappers::MPI::Vector localized_vector; localized_vector.reinit(complete_index_set(size()), v.get_mpi_communicator()); localized_vector.reinit(v, false, true); Assert(localized_vector.size() == size(), ExcDimensionMismatch(localized_vector.size(), size())); // get a representation of the vector // and copy it TrilinosScalar **start_ptr; int ierr = localized_vector.trilinos_vector().ExtractView(&start_ptr); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); std::copy(start_ptr[0], start_ptr[0] + size(), begin()); maybe_reset_thread_partitioner(); } } #endif #ifdef DEAL_II_TRILINOS_WITH_TPETRA template template Vector::Vector( const LinearAlgebra::TpetraWrappers::Vector &v) : values(v.size()) { static_assert( std::is_same::value, "TpetraWrappers::Vector and dealii::Vector must use the same number type here."); if (size() != 0) { // Copy the distributed vector to // a local one at all processors // that know about the original vector. LinearAlgebra::TpetraWrappers::TpetraTypes::VectorType localized_vector( complete_index_set(size()) .template make_tpetra_map_rcp< LinearAlgebra::TpetraWrappers::TpetraTypes::NodeType< MemorySpace>>(), v.get_mpi_communicator()); Teuchos::RCP> importer = Tpetra::createImport(v.trilinos_vector().getMap(), localized_vector.getMap()); localized_vector.doImport(v.trilinos_vector(), *importer, Tpetra::INSERT); // get a kokkos view from the localized_vector # if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) auto localized_vector_2d = localized_vector.template getLocalView( Tpetra::Access::ReadOnly); # else localized_vector.template sync(); auto localized_vector_2d = localized_vector.template getLocalView(); # endif auto localized_vector_1d = Kokkos::subview(localized_vector_2d, Kokkos::ALL(), 0); const size_t local_length = localized_vector.getLocalLength(); Kokkos::DefaultHostExecutionSpace exec; Kokkos::View values_view(values.data(), local_length); Kokkos::deep_copy(exec, values_view, localized_vector_1d); exec.fence(); } } #endif template inline Vector & Vector::operator=(const Vector &v) { if (PointerComparison::equal(this, &v)) return *this; if (size() != v.size()) reinit(v, true); if (0 < size()) { dealii::internal::VectorOperations::Vector_copy copier( v.begin(), begin()); internal::VectorOperations::parallel_for(copier, 0, size(), thread_loop_partitioner); } return *this; } template template inline Vector & Vector::operator=(const Vector &v) { if (size() != v.size()) reinit(v, true); dealii::internal::VectorOperations::Vector_copy copier( v.begin(), begin()); internal::VectorOperations::parallel_for(copier, 0, size(), thread_loop_partitioner); return *this; } template inline void Vector::reinit(const size_type n, const bool omit_zeroing_entries) { do_reinit(n, omit_zeroing_entries, true); } template inline void Vector::grow_or_shrink(const size_type n) { values.resize(n); maybe_reset_thread_partitioner(); } template bool Vector::all_zero() const { Assert(size() != 0, ExcEmptyObject()); for (size_type i = 0; i < size(); ++i) if (values[i] != Number()) return false; return true; } template bool Vector::is_non_negative() const { Assert(size() != 0, ExcEmptyObject()); for (size_type i = 0; i < size(); ++i) if (!internal::VectorOperations::is_non_negative(values[i])) return false; return true; } template Vector & Vector::operator=(const Number s) { AssertIsFinite(s); if (s != Number()) Assert(size() != 0, ExcEmptyObject()); if (size() > 0) { internal::VectorOperations::Vector_set setter(s, values.begin()); internal::VectorOperations::parallel_for(setter, 0, size(), thread_loop_partitioner); } return *this; } template Vector & Vector::operator*=(const Number factor) { AssertIsFinite(factor); Assert(size() != 0, ExcEmptyObject()); internal::VectorOperations::Vectorization_multiply_factor vector_multiply(values.begin(), factor); internal::VectorOperations::parallel_for(vector_multiply, 0, size(), thread_loop_partitioner); return *this; } template void Vector::add(const Number a, const Vector &v) { AssertIsFinite(a); Assert(size() != 0, ExcEmptyObject()); Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); internal::VectorOperations::Vectorization_add_av vector_add_av( values.begin(), v.values.begin(), a); internal::VectorOperations::parallel_for(vector_add_av, 0, size(), thread_loop_partitioner); } template void Vector::sadd(const Number x, const Number a, const Vector &v) { AssertIsFinite(x); AssertIsFinite(a); Assert(size() != 0, ExcEmptyObject()); Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); internal::VectorOperations::Vectorization_sadd_xav vector_sadd_xav( values.begin(), v.values.begin(), a, x); internal::VectorOperations::parallel_for(vector_sadd_xav, 0, size(), thread_loop_partitioner); } template template Number Vector::operator*(const Vector &v) const { Assert(size() != 0, ExcEmptyObject()); if (PointerComparison::equal(this, &v)) return norm_sqr(); Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); Number sum; internal::VectorOperations::Dot dot(values.begin(), v.values.begin()); internal::VectorOperations::parallel_reduce( dot, 0, size(), sum, thread_loop_partitioner); AssertIsFinite(sum); return sum; } template typename Vector::real_type Vector::norm_sqr() const { Assert(size() != 0, ExcEmptyObject()); real_type sum; internal::VectorOperations::Norm2 norm2(values.begin()); internal::VectorOperations::parallel_reduce( norm2, 0, size(), sum, thread_loop_partitioner); AssertIsFinite(sum); return sum; } template Number Vector::mean_value() const { Assert(size() != 0, ExcEmptyObject()); Number sum; internal::VectorOperations::MeanValue mean(values.begin()); internal::VectorOperations::parallel_reduce( mean, 0, size(), sum, thread_loop_partitioner); return sum / real_type(size()); } template typename Vector::real_type Vector::l1_norm() const { Assert(size() != 0, ExcEmptyObject()); real_type sum; internal::VectorOperations::Norm1 norm1(values.begin()); internal::VectorOperations::parallel_reduce( norm1, 0, size(), sum, thread_loop_partitioner); return sum; } template typename Vector::real_type Vector::l2_norm() const { // if l2_norm()^2 is finite and non-zero, the answer is computed as // std::sqrt(norm_sqr()). If norm_sqr() is infinite or zero, the l2 norm // might still be finite. In that case, recompute it (this is a rare case, // so working on the vector twice is uncritical and paid off by the extended // precision) using the BLAS approach with a weight, see e.g. dnrm2.f. Assert(size() != 0, ExcEmptyObject()); real_type norm_square; internal::VectorOperations::Norm2 norm2(values.begin()); internal::VectorOperations::parallel_reduce( norm2, 0, size(), norm_square, thread_loop_partitioner); if (numbers::is_finite(norm_square) && norm_square >= std::numeric_limits::min()) return static_cast::real_type>( std::sqrt(norm_square)); else { real_type scale = 0.; real_type sum = 1.; for (size_type i = 0; i < size(); ++i) { if (values[i] != Number()) { const real_type abs_x = numbers::NumberTraits::abs(values[i]); if (scale < abs_x) { sum = 1 + sum * (scale / abs_x) * (scale / abs_x); scale = abs_x; } else sum += (abs_x / scale) * (abs_x / scale); } } AssertIsFinite(scale * std::sqrt(sum)); return static_cast::real_type>(scale * std::sqrt(sum)); } } template typename Vector::real_type Vector::lp_norm(const real_type p) const { Assert(size() != 0, ExcEmptyObject()); if (p == 1.) return l1_norm(); else if (p == 2.) return l2_norm(); real_type sum; internal::VectorOperations::NormP normp(values.begin(), p); internal::VectorOperations::parallel_reduce( normp, 0, size(), sum, thread_loop_partitioner); if (numbers::is_finite(sum) && sum >= std::numeric_limits::min()) return std::pow(sum, static_cast(1. / p)); else { real_type scale = 0.; real_type sum = 1.; for (size_type i = 0; i < size(); ++i) { if (values[i] != Number()) { const real_type abs_x = numbers::NumberTraits::abs(values[i]); if (scale < abs_x) { sum = 1. + sum * std::pow(scale / abs_x, p); scale = abs_x; } else sum += std::pow(abs_x / scale, p); } } return scale * std::pow(sum, static_cast(1. / p)); } } template typename Vector::real_type Vector::linfty_norm() const { Assert(size() != 0, ExcEmptyObject()); real_type max = 0.; for (size_type i = 0; i < size(); ++i) max = std::max(numbers::NumberTraits::abs(values[i]), max); return max; } template Number Vector::add_and_dot(const Number a, const Vector &V, const Vector &W) { Assert(size() != 0, ExcEmptyObject()); AssertDimension(size(), V.size()); AssertDimension(size(), W.size()); Number sum; internal::VectorOperations::AddAndDot adder(values.begin(), V.values.begin(), W.values.begin(), a); internal::VectorOperations::parallel_reduce( adder, 0, size(), sum, thread_loop_partitioner); AssertIsFinite(sum); return sum; } template void Vector::extract_subvector_to( const ArrayView &indices, const ArrayView &elements) const { AssertDimension(indices.size(), elements.size()); for (unsigned int i = 0; i < indices.size(); ++i) { AssertIndexRange(indices[i], size()); elements[i] = (*this)[indices[i]]; } } template Vector & Vector::operator+=(const Vector &v) { Assert(size() != 0, ExcEmptyObject()); Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); internal::VectorOperations::Vectorization_add_v vector_add( values.begin(), v.values.begin()); internal::VectorOperations::parallel_for(vector_add, 0, size(), thread_loop_partitioner); return *this; } template Vector & Vector::operator-=(const Vector &v) { Assert(size() != 0, ExcEmptyObject()); Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); internal::VectorOperations::Vectorization_subtract_v vector_subtract( values.begin(), v.values.begin()); internal::VectorOperations::parallel_for(vector_subtract, 0, size(), thread_loop_partitioner); return *this; } template void Vector::add(const Number v) { Assert(size() != 0, ExcEmptyObject()); internal::VectorOperations::Vectorization_add_factor vector_add( values.begin(), v); internal::VectorOperations::parallel_for(vector_add, 0, size(), thread_loop_partitioner); } template void Vector::add(const Number a, const Vector &v, const Number b, const Vector &w) { AssertIsFinite(a); AssertIsFinite(b); Assert(size() != 0, ExcEmptyObject()); Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); Assert(size() == w.size(), ExcDimensionMismatch(size(), w.size())); internal::VectorOperations::Vectorization_add_avpbw vector_add( values.begin(), v.values.begin(), w.values.begin(), a, b); internal::VectorOperations::parallel_for(vector_add, 0, size(), thread_loop_partitioner); } template void Vector::sadd(const Number x, const Vector &v) { AssertIsFinite(x); Assert(size() != 0, ExcEmptyObject()); Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); internal::VectorOperations::Vectorization_sadd_xv vector_sadd( values.begin(), v.values.begin(), x); internal::VectorOperations::parallel_for(vector_sadd, 0, size(), thread_loop_partitioner); } template void Vector::scale(const Vector &s) { Assert(size() != 0, ExcEmptyObject()); Assert(size() == s.size(), ExcDimensionMismatch(size(), s.size())); internal::VectorOperations::Vectorization_scale vector_scale( values.begin(), s.values.begin()); internal::VectorOperations::parallel_for(vector_scale, 0, size(), thread_loop_partitioner); } template template void Vector::scale(const Vector &s) { Assert(size() != 0, ExcEmptyObject()); Assert(size() == s.size(), ExcDimensionMismatch(size(), s.size())); for (size_type i = 0; i < size(); ++i) values[i] *= Number(s.values[i]); } template void Vector::equ(const Number a, const Vector &u) { AssertIsFinite(a); Assert(size() != 0, ExcEmptyObject()); Assert(size() == u.size(), ExcDimensionMismatch(size(), u.size())); internal::VectorOperations::Vectorization_equ_au vector_equ( values.begin(), u.values.begin(), a); internal::VectorOperations::parallel_for(vector_equ, 0, size(), thread_loop_partitioner); } template template void Vector::equ(const Number a, const Vector &u) { AssertIsFinite(a); Assert(size() != 0, ExcEmptyObject()); Assert(size() == u.size(), ExcDimensionMismatch(size(), u.size())); // set the result vector to a*u. we have to // convert the elements of u to the type of // the result vector. this is necessary // because // operator*(complex,complex) // is not defined by default for (size_type i = 0; i < size(); ++i) values[i] = a * Number(u.values[i]); } template Vector & Vector::operator=(const BlockVector &v) { if (v.size() != size()) reinit(v.size(), true); size_type this_index = 0; for (size_type b = 0; b < v.n_blocks(); ++b) for (size_type i = 0; i < v.block(b).size(); ++i, ++this_index) values[this_index] = v.block(b)(i); return *this; } #ifdef DEAL_II_WITH_PETSC template Vector & Vector::operator=(const PETScWrappers::VectorBase &v) { internal::copy_petsc_vector(v, *this); return *this; } #endif #ifdef DEAL_II_WITH_TRILINOS template Vector & Vector::operator=(const TrilinosWrappers::MPI::Vector &v) { if (v.size() != size()) reinit(v.size(), true); if (size() != 0) { // Copy the distributed vector to // a local one at all processors // that know about the original vector. // TODO: There could // be a better solution than // this, but it has not yet been // found. TrilinosWrappers::MPI::Vector localized_vector; localized_vector.reinit(complete_index_set(size()), v.get_mpi_communicator()); localized_vector.reinit(v, false, true); Assert(localized_vector.size() == size(), ExcDimensionMismatch(localized_vector.size(), size())); // get a representation of the vector // and copy it TrilinosScalar **start_ptr; int ierr = localized_vector.trilinos_vector().ExtractView(&start_ptr); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); std::copy(start_ptr[0], start_ptr[0] + size(), begin()); } return *this; } #endif #ifdef DEAL_II_TRILINOS_WITH_TPETRA template template Vector & Vector::operator=( const LinearAlgebra::TpetraWrappers::Vector &v) { static_assert( std::is_same::value, "TpetraWrappers::Vector and dealii::Vector must use the same number type here."); if (v.size() != size()) reinit(v.size(), true); if (size() != 0) { // Copy the distributed vector to // a local one at all processors // that know about the original vector. LinearAlgebra::TpetraWrappers::TpetraTypes::VectorType localized_vector( complete_index_set(size()) .template make_tpetra_map_rcp< LinearAlgebra::TpetraWrappers::TpetraTypes::NodeType< MemorySpace>>(), v.get_mpi_communicator()); Teuchos::RCP> importer = Tpetra::createImport(v.trilinos_vector().getMap(), localized_vector.getMap()); localized_vector.doImport(v.trilinos_vector(), *importer, Tpetra::INSERT); // get a kokkos view from the localized_vector # if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) auto localized_vector_2d = localized_vector.template getLocalView( Tpetra::Access::ReadOnly); # else localized_vector.template sync(); auto localized_vector_2d = localized_vector.template getLocalView(); # endif auto localized_vector_1d = Kokkos::subview(localized_vector_2d, Kokkos::ALL(), 0); const size_t local_length = localized_vector.getLocalLength(); Kokkos::DefaultHostExecutionSpace exec; Kokkos::View values_view(values.data(), local_length); Kokkos::deep_copy(exec, values_view, localized_vector_1d); exec.fence(); } return *this; } #endif template template bool Vector::operator==(const Vector &v) const { Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); // compare the two vector. we have to // convert the elements of v to the type of // the result vector. this is necessary // because // operator==(complex,complex) // is not defined by default for (size_type i = 0; i < size(); ++i) if (values[i] != Number(v.values[i])) return false; return true; } template void Vector::print(std::ostream &out, const unsigned int precision, const bool scientific, const bool across) const { Assert(size() != 0, ExcEmptyObject()); AssertThrow(out.fail() == false, ExcIO()); std::ios::fmtflags old_flags = out.flags(); unsigned int old_precision = out.precision(precision); 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 < size(); ++i) out << values[i] << ' '; else for (size_type i = 0; i < size(); ++i) out << values[i] << std::endl; out << std::endl; AssertThrow(out.fail() == false, ExcIO()); // reset output format out.flags(old_flags); out.precision(old_precision); } template void Vector::block_write(std::ostream &out) const { AssertThrow(out.fail() == false, ExcIO()); out << std::to_string(size()) << "\n["; out.write(reinterpret_cast(begin()), reinterpret_cast(end()) - reinterpret_cast(begin())); out << ']'; AssertThrow(out.fail() == false, ExcIO()); } template void Vector::block_read(std::istream &in) { AssertThrow(in.fail() == false, ExcIO()); size_type sz; char buf[16]; in.getline(buf, 16, '\n'); sz = std::atoi(buf); // fast initialization, since the // data elements are overwritten anyway reinit(sz, true); char c; in.read(&c, 1); AssertThrow(c == '[', ExcIO()); in.read(reinterpret_cast(begin()), reinterpret_cast(end()) - reinterpret_cast(begin())); in.read(&c, 1); AssertThrow(c == ']', ExcIO()); } template IndexSet Vector::locally_owned_elements() const { return complete_index_set(size()); } template std::size_t Vector::memory_consumption() const { return sizeof(*this) + values.memory_consumption() - sizeof(values); } template void Vector::maybe_reset_thread_partitioner() { if (size() >= 4 * internal::VectorImplementation::minimum_parallel_grain_size) { if (thread_loop_partitioner == nullptr) thread_loop_partitioner = std::make_shared(); } else thread_loop_partitioner.reset(); } template void Vector::do_reinit(const size_type new_size, const bool omit_zeroing_entries, const bool reset_partitioner) { values.resize_fast(new_size); if (!omit_zeroing_entries) values.fill(); if (reset_partitioner) maybe_reset_thread_partitioner(); } DEAL_II_NAMESPACE_CLOSE #endif