NEML2 2.0.0
Loading...
Searching...
No Matches
TracingInterface.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include <fstream>
28#include <memory>
29#include <mutex>
30#include <unordered_map>
31
32#include "neml2/config.h"
33
34#ifdef NEML2_JSON
35#include "nlohmann/json.hpp"
36#endif
37
38#include "neml2/base/OptionSet.h"
39
40namespace neml2
41{
42#ifdef NEML2_JSON
43using json = nlohmann::json;
44#endif
45
47{
49 TraceWriter(const std::filesystem::path & file);
52
54 TraceWriter(const TraceWriter &) = delete;
56 TraceWriter & operator=(const TraceWriter &) = delete;
58
60 const std::string filename;
61
63 std::ofstream out;
64
65#ifdef NEML2_JSON
66 void trace_duration_begin(const std::string & name,
67 const std::string & category,
68 const json & args = {},
69 unsigned int pid = 0);
70 void trace_duration_end(const std::string & name,
71 const std::string & category,
72 const json & args = {},
73 unsigned int pid = 0);
74 void trace_instant(const std::string & name,
75 const std::string & category,
76 const json & args = {},
77 const std::string & scope = "t",
78 unsigned int pid = 0);
79#endif
80
81private:
82#ifdef NEML2_JSON
84 void write_event_common(json & event,
85 const std::string & name,
86 const std::string & category,
87 const json & args,
88 const std::string & phase,
89 unsigned int pid);
91 void dump_event(const json & event, bool last = false);
92#endif
93
95 const std::chrono::high_resolution_clock::time_point _epoch;
96
98 std::mutex _mtx;
99};
100
102std::unordered_map<std::string, std::unique_ptr<TraceWriter>> & event_trace_writers();
103
114{
115public:
117
118 TracingInterface(std::string);
120 virtual ~TracingInterface() = default;
121
126
127 bool event_tracing_enabled() const { return _enabled; }
128
131
132private:
133 TraceWriter & init_writer(std::string);
134
136 bool _enabled;
137
139 TraceWriter * _writer;
140};
141} // namespace neml2
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:51
Definition TracingInterface.h:114
virtual ~TracingInterface()=default
TracingInterface & operator=(const TracingInterface &)=delete
TracingInterface(std::string)
Definition TracingInterface.cxx:151
TraceWriter & event_trace_writer() const
Get the event trace writer.
Definition TracingInterface.cxx:186
TracingInterface(const TracingInterface &)=default
bool event_tracing_enabled() const
Definition TracingInterface.h:127
static OptionSet expected_options()
Definition TracingInterface.cxx:142
TracingInterface(TracingInterface &&)=default
TracingInterface & operator=(TracingInterface &&)=delete
Definition DiagnosticsInterface.cxx:30
std::unordered_map< std::string, std::unique_ptr< TraceWriter > > & event_trace_writers()
All trace writers.
Definition TracingInterface.cxx:36
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
nlohmann::json json
Definition TracingInterface.h:43
Definition TracingInterface.h:47
void trace_instant(const std::string &name, const std::string &category, const json &args={}, const std::string &scope="t", unsigned int pid=0)
Definition TracingInterface.cxx:128
void trace_duration_end(const std::string &name, const std::string &category, const json &args={}, unsigned int pid=0)
Definition TracingInterface.cxx:117
std::ofstream out
Output stream for the trace file.
Definition TracingInterface.h:63
const std::string filename
File name.
Definition TracingInterface.h:60
~TraceWriter()
Destructor.
Definition TracingInterface.cxx:62
TraceWriter(const std::filesystem::path &file)
Constructor.
Definition TracingInterface.cxx:42
void trace_duration_begin(const std::string &name, const std::string &category, const json &args={}, unsigned int pid=0)
Definition TracingInterface.cxx:106
TraceWriter(TraceWriter &&)=delete
TraceWriter & operator=(const TraceWriter &)=delete
TraceWriter(const TraceWriter &)=delete
Special methods.
TraceWriter & operator=(TraceWriter &&)=delete