// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2017 - 2025 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_element_access_h #define dealii_vector_element_access_h #include #include #include #include DEAL_II_NAMESPACE_OPEN namespace internal { template struct ElementAccess { public: static void add(const typename VectorType::value_type value, const types::global_dof_index i, VectorType &V); static void set(typename VectorType::value_type value, const types::global_dof_index i, VectorType &V); static typename VectorType::value_type get(const VectorType &V, const types::global_dof_index i); }; template inline void ElementAccess::add(const typename VectorType::value_type value, const types::global_dof_index i, VectorType &V) { V(i) += value; } template inline void ElementAccess::set(const typename VectorType::value_type value, const types::global_dof_index i, VectorType &V) { V(i) = value; } template inline typename VectorType::value_type ElementAccess::get(const VectorType &V, const types::global_dof_index i) { return V(i); } #ifdef DEAL_II_WITH_TRILINOS template <> inline void ElementAccess::add( const double value, const types::global_dof_index i, LinearAlgebra::EpetraWrappers::Vector &V) { // Extract local indices in the vector. Epetra_FEVector vector = V.trilinos_vector(); TrilinosWrappers::types::int_type trilinos_i = vector.Map().LID(static_cast(i)); vector[0][trilinos_i] += value; } template <> inline void ElementAccess::set( const double value, const types::global_dof_index i, LinearAlgebra::EpetraWrappers::Vector &V) { // Extract local indices in the vector. Epetra_FEVector vector = V.trilinos_vector(); TrilinosWrappers::types::int_type trilinos_i = vector.Map().LID(static_cast(i)); vector[0][trilinos_i] = value; } template <> inline double ElementAccess::get( const LinearAlgebra::EpetraWrappers::Vector &V, const types::global_dof_index i) { // Extract local indices in the vector. Epetra_FEVector vector = V.trilinos_vector(); TrilinosWrappers::types::int_type trilinos_i = vector.Map().LID(static_cast(i)); return vector[0][trilinos_i]; } # ifdef DEAL_II_TRILINOS_WITH_TPETRA template struct ElementAccess< LinearAlgebra::TpetraWrappers::Vector> { public: using VectorType = LinearAlgebra::TpetraWrappers::Vector; static void add(const typename VectorType::value_type value, const types::global_dof_index i, VectorType &V); static void set(typename VectorType::value_type value, const types::global_dof_index i, VectorType &V); static typename VectorType::value_type get(const VectorType &V, const types::global_dof_index i); }; template inline void ElementAccess< LinearAlgebra::TpetraWrappers::Vector>:: add(const typename VectorType::value_type value, const types::global_dof_index i, LinearAlgebra::TpetraWrappers::Vector &V) { // Extract local indices in the vector. auto vector = V.trilinos_vector(); TrilinosWrappers::types::int_type trilinos_i = vector.getMap()->getLocalElement( static_cast(i)); # if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) auto vector_2d = vector.template getLocalView( Tpetra::Access::ReadWrite); # else vector.template sync(); auto vector_2d = vector.template getLocalView(); # endif auto vector_1d = Kokkos::subview(vector_2d, Kokkos::ALL(), 0); # if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) // We're going to modify the data on host. vector.template modify(); # endif vector_1d(trilinos_i) += value; # if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) vector.template sync< typename Tpetra::Vector:: device_type::memory_space>(); # endif } template inline void ElementAccess< LinearAlgebra::TpetraWrappers::Vector>:: set(const typename VectorType::value_type value, const types::global_dof_index i, LinearAlgebra::TpetraWrappers::Vector &V) { // Extract local indices in the vector. auto vector = V.trilinos_vector(); TrilinosWrappers::types::int_type trilinos_i = vector.getMap()->getLocalElement( static_cast(i)); # if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) auto vector_2d = vector.template getLocalView( Tpetra::Access::ReadWrite); # else vector.template sync(); auto vector_2d = vector.template getLocalView(); # endif auto vector_1d = Kokkos::subview(vector_2d, Kokkos::ALL(), 0); // We're going to modify the data on host. # if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) vector.template modify(); # endif vector_1d(trilinos_i) = value; # if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) vector.template sync< typename Tpetra::Vector:: device_type::memory_space>(); # endif } template inline typename LinearAlgebra::TpetraWrappers::Vector::value_type ElementAccess< LinearAlgebra::TpetraWrappers::Vector>:: get(const LinearAlgebra::TpetraWrappers::Vector &V, const types::global_dof_index i) { // Extract local indices in the vector. # if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) const auto &vector = V.trilinos_vector(); auto vector_2d = vector.template getLocalView(Tpetra::Access::ReadOnly); # else auto vector = V.trilinos_vector(); vector.template sync(); auto vector_2d = vector.template getLocalView(); # endif auto vector_1d = Kokkos::subview(vector_2d, Kokkos::ALL(), 0); TrilinosWrappers::types::int_type trilinos_i = vector.getMap()->getLocalElement( static_cast(i)); return vector_1d(trilinos_i); } # endif #endif } // namespace internal DEAL_II_NAMESPACE_CLOSE #endif