From 5612dee3633595c2c8c96593efcf2eb1b9fb7c41 Mon Sep 17 00:00:00 2001 From: Tobias Leibner <tobias.leibner@googlemail.com> Date: Thu, 3 Sep 2020 12:24:09 +0200 Subject: [PATCH] [matrix-view] fix copying --- dune/xt/la/container/matrix-view.hh | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/dune/xt/la/container/matrix-view.hh b/dune/xt/la/container/matrix-view.hh index 4de864c6a..6c91cd4d6 100644 --- a/dune/xt/la/container/matrix-view.hh +++ b/dune/xt/la/container/matrix-view.hh @@ -11,8 +11,9 @@ #ifndef DUNE_XT_LA_CONTAINER_MATRIX_VIEW_HH #define DUNE_XT_LA_CONTAINER_MATRIX_VIEW_HH +#include <mutex> + #include <dune/xt/common/exceptions.hh> -#include <dune/xt/common/parallel/threadstorage.hh> #include "matrix-interface.hh" @@ -85,7 +86,7 @@ public: , past_last_row_(0) , first_col_(0) , past_last_col_(0) - , pattern_(std::shared_ptr<SparsityPatternDefault>(nullptr)) + , pattern_(nullptr) { DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "This constructor does not make sense for MatrixView"); } @@ -100,7 +101,7 @@ public: , past_last_row_(0) , first_col_(0) , past_last_col_(0) - , pattern_(std::shared_ptr<SparsityPatternDefault>(nullptr)) + , pattern_(nullptr) { DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "This constructor does not make sense for MatrixView"); } @@ -116,9 +117,11 @@ public: , past_last_row_(past_last_row) , first_col_(first_col) , past_last_col_(past_last_col) - , pattern_(std::shared_ptr<SparsityPatternDefault>(nullptr)) + , pattern_init_flag_(std::make_shared<std::once_flag>()) + , pattern_(nullptr) {} + ConstMatrixView(const ThisType& other) = default; ConstMatrixView(ThisType&& other) = default; // No assigment as this is a const view @@ -258,8 +261,9 @@ public: const SparsityPatternDefault& get_pattern() const { - initialize_pattern(); - return **pattern_; + std::call_once(*pattern_init_flag_, + [this]() { this->pattern_ = std::make_shared<SparsityPatternDefault>(this->pattern()); }); + return *pattern_; } operator Matrix() const @@ -273,18 +277,13 @@ public: } private: - void initialize_pattern() const - { - if (!*pattern_) - *pattern_ = std::make_shared<SparsityPatternDefault>(pattern()); - } - const Matrix& matrix_; const size_t first_row_; const size_t past_last_row_; const size_t first_col_; const size_t past_last_col_; - mutable XT::Common::PerThreadValue<std::shared_ptr<SparsityPatternDefault>> pattern_; + std::shared_ptr<std::once_flag> pattern_init_flag_; + mutable std::shared_ptr<SparsityPatternDefault> pattern_; }; // class ConstMatrixView template <class MatrixImp> -- GitLab