OGS
MatrixAssemblyStats.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <memory>
14
15#include "BaseLib/Logging.h"
16
18{
19struct Stats
20{
21 std::size_t count = 0;
22 std::size_t count_nonzero = 0;
23 std::size_t count_global = 0;
24
25 Stats& operator+=(Stats const& other)
26 {
27 count += other.count;
30
31 return *this;
32 }
33
34 void print(std::string const& matrix_or_vector_name) const
35 {
36 DBUG("Stats [{}]: {} elements added to the matrix cache.",
37 matrix_or_vector_name,
38 count);
39 DBUG("Stats [{}]: {} nonzero elements added to the matrix cache.",
40 matrix_or_vector_name,
42 DBUG("Stats [{}]: {} elements added to the global matrix.",
43 matrix_or_vector_name,
45 }
46};
47
49{
52
54 {
55 b += other.b;
56 Jac += other.Jac;
57
58 return *this;
59 }
60
61 void print() const
62 {
63 b.print("b");
64 Jac.print("J");
65 }
66};
67
68template <typename Data>
70 : public std::enable_shared_from_this<CumulativeStats<Data>>
71{
72 using Base = std::enable_shared_from_this<CumulativeStats<Data>>;
73
74public:
75 Data data;
76
77 static std::shared_ptr<CumulativeStats<Data>> create()
78 {
79 return std::shared_ptr<CumulativeStats<Data>>(
81 }
82
83 // Could return unique_ptr, but shared_ptr is more consistent with the
84 // create() method.
85 std::shared_ptr<CumulativeStats<Data>> clone()
86 {
87 return std::make_shared<CumulativeStats<Data>>(*this);
88 }
89
90 CumulativeStats(CumulativeStats<Data> const& other) = delete;
91
93 : Base{other},
94 data{},
95 parent_{other.parent_ ? other.parent_ : other.shared_from_this()},
97 {
98 }
99
101 : parent_{std::move(other.parent_)},
102 parent_mutex_{std::move(other.parent_mutex_)}
103 {
104 std::swap(data, other.data);
105 }
106
108 {
109 if (!parent_)
110 {
111 return;
112 }
113
114 std::lock_guard<std::mutex> const lock(*parent_mutex_);
115
116 DBUG("Adding cumulative stats to parent.");
117
118 parent_->data += data;
119 }
120
121 void print() const { data.print(); }
122
123private:
124 CumulativeStats() : parent_mutex_{std::make_shared<std::mutex>()} {}
125
126 std::shared_ptr<CumulativeStats<Data>> parent_;
127 std::shared_ptr<std::mutex> parent_mutex_;
128};
129} // namespace ProcessLib::Assembly
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
std::shared_ptr< std::mutex > parent_mutex_
static std::shared_ptr< CumulativeStats< Data > > create()
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()
CumulativeStats(CumulativeStats< Data > &&other)
MultiStats & operator+=(MultiStats const &other)
Stats & operator+=(Stats const &other)
void print(std::string const &matrix_or_vector_name) const