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{
54
56 {
57 M += other.M;
58 K += other.K;
59 b += other.b;
60 Jac += other.Jac;
61
62 return *this;
63 }
64
65 void print() const
66 {
67 M.print("M");
68 K.print("K");
69 b.print("b");
70 Jac.print("J");
71 }
72};
73
74template <typename Data>
76 : public std::enable_shared_from_this<CumulativeStats<Data>>
77{
78 using Base = std::enable_shared_from_this<CumulativeStats<Data>>;
79
80public:
81 Data data;
82
83 static std::shared_ptr<CumulativeStats<Data>> create()
84 {
85 return std::shared_ptr<CumulativeStats<Data>>(
87 }
88
89 // Could return unique_ptr, but shared_ptr is more consistent with the
90 // create() method.
91 std::shared_ptr<CumulativeStats<Data>> clone()
92 {
93 return std::make_shared<CumulativeStats<Data>>(*this);
94 }
95
96 CumulativeStats(CumulativeStats<Data> const& other) = delete;
97
99 : Base{other},
100 data{},
101 parent_{other.parent_ ? other.parent_ : other.shared_from_this()},
103 {
104 }
105
107 : parent_{std::move(other.parent_)},
108 parent_mutex_{std::move(other.parent_mutex_)}
109 {
110 std::swap(data, other.data);
111 }
112
114 {
115 if (!parent_)
116 {
117 return;
118 }
119
120 std::lock_guard<std::mutex> const lock(*parent_mutex_);
121
122 DBUG("Adding cumulative stats to parent.");
123
124 parent_->data += data;
125 }
126
127 void print() const { data.print(); }
128
129private:
130 CumulativeStats() : parent_mutex_{std::make_shared<std::mutex>()} {}
131
132 std::shared_ptr<CumulativeStats<Data>> parent_;
133 std::shared_ptr<std::mutex> parent_mutex_;
134};
135} // 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