OGS
MatrixAssemblyStats.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <memory>
7
8#include "BaseLib/Logging.h"
9
11{
12struct Stats
13{
14 std::size_t count = 0;
15 std::size_t count_nonzero = 0;
16 std::size_t count_global = 0;
17
18 Stats& operator+=(Stats const& other)
19 {
20 count += other.count;
23
24 return *this;
25 }
26
27 void print(std::string const& matrix_or_vector_name) const
28 {
29 DBUG("Stats [{}]: {} elements added to the matrix cache.",
30 matrix_or_vector_name,
31 count);
32 DBUG("Stats [{}]: {} nonzero elements added to the matrix cache.",
33 matrix_or_vector_name,
35 DBUG("Stats [{}]: {} elements added to the global matrix.",
36 matrix_or_vector_name,
38 }
39};
40
41template <int NumMatrices>
43
44template <>
45struct MultiStats<1>
46{
49
51 {
52 b += other.b;
53 Jac += other.Jac;
54
55 return *this;
56 }
57
58 void print() const
59 {
60 b.print("b");
61 Jac.print("J");
62 }
63};
64
65template <>
66struct MultiStats<2>
67{
71
73 {
74 M += other.M;
75 K += other.K;
76 b += other.b;
77
78 return *this;
79 }
80
81 void print() const
82 {
83 M.print("M");
84 K.print("K");
85 b.print("b");
86 }
87};
88
89template <typename Data>
91 : public std::enable_shared_from_this<CumulativeStats<Data>>
92{
93 using Base = std::enable_shared_from_this<CumulativeStats<Data>>;
94
95public:
96 Data data{};
97
98 static std::shared_ptr<CumulativeStats<Data>> create(const int num_threads)
99 {
100 return std::shared_ptr<CumulativeStats<Data>>(
101 new CumulativeStats<Data>(num_threads));
102 }
103
104 // Could return unique_ptr, but shared_ptr is more consistent with the
105 // create() method.
106 std::shared_ptr<CumulativeStats<Data>> clone()
107 {
108 return std::make_shared<CumulativeStats<Data>>(*this);
109 }
110
112
114 : Base{other},
115 parent_{other.parent_ ? other.parent_ : other.shared_from_this()},
118 {
119 }
120
122 : parent_{std::move(other.parent_)},
123 parent_mutex_{std::move(other.parent_mutex_)},
125 {
126 std::swap(data, other.data);
127 }
128
130 {
131 if (!parent_)
132 {
133 return;
134 }
135
136 if (num_threads_ == 1)
137 {
138 parent_->data += data;
139 return;
140 }
141
142 std::lock_guard<std::mutex> const lock(*parent_mutex_);
143
144 DBUG("Adding cumulative stats to parent.");
145
146 parent_->data += data;
147 }
148
149 void print() const { data.print(); }
150
151private:
152 explicit CumulativeStats(const int num_threads)
153 : parent_mutex_{std::make_shared<std::mutex>()},
154 num_threads_{num_threads}
155 {
156 }
157
158 std::shared_ptr<CumulativeStats<Data>> parent_;
159 std::shared_ptr<std::mutex> parent_mutex_;
161};
162} // namespace ProcessLib::Assembly
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
std::shared_ptr< std::mutex > parent_mutex_
CumulativeStats(CumulativeStats< Data > &other)
CumulativeStats(CumulativeStats< Data > const &other)=delete
std::enable_shared_from_this< CumulativeStats< Data > > Base
std::shared_ptr< CumulativeStats< Data > > parent_
std::shared_ptr< CumulativeStats< Data > > clone()
static std::shared_ptr< CumulativeStats< Data > > create(const int num_threads)
CumulativeStats(CumulativeStats< Data > &&other)
MultiStats & operator+=(MultiStats const &other)
MultiStats & operator+=(MultiStats const &other)
Stats & operator+=(Stats const &other)
void print(std::string const &matrix_or_vector_name) const