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#ifdef NEML2_HAS_JSON
33#include "nlohmann/json.hpp"
34#endif
35
36#include "neml2/base/OptionSet.h"
37
38namespace neml2
39{
40#ifdef NEML2_HAS_JSON
41using json = nlohmann::json;
42#endif
43
45{
47 TraceWriter(const std::filesystem::path & file);
50
52 TraceWriter(const TraceWriter &) = delete;
54 TraceWriter & operator=(const TraceWriter &) = delete;
56
58 const std::string filename;
59
61 std::ofstream out;
62
63#ifdef NEML2_HAS_JSON
64 void trace_duration_begin(const std::string & name,
65 const std::string & category,
66 const json & args = {},
67 unsigned int pid = 0);
68 void trace_duration_end(const std::string & name,
69 const std::string & category,
70 const json & args = {},
71 unsigned int pid = 0);
72 void trace_instant(const std::string & name,
73 const std::string & category,
74 const json & args = {},
75 const std::string & scope = "t",
76 unsigned int pid = 0);
77#endif
78
79private:
80#ifdef NEML2_HAS_JSON
82 void write_event_common(json & event,
83 const std::string & name,
84 const std::string & category,
85 const json & args,
86 const std::string & phase,
87 unsigned int pid);
89 void dump_event(const json & event, bool last = false);
90#endif
91
93 const std::chrono::high_resolution_clock::time_point _epoch;
94
96 std::mutex _mtx;
97};
98
100std::unordered_map<std::string, std::unique_ptr<TraceWriter>> & event_trace_writers();
101
112{
113public:
115
116 TracingInterface(std::string);
118 virtual ~TracingInterface() = default;
119
124
125 bool event_tracing_enabled() const { return _enabled; }
126
129
130private:
131 TraceWriter & init_writer(std::string);
132
134 bool _enabled;
135
137 TraceWriter * _writer;
138};
139} // 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:112
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:125
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
Definition TracingInterface.h:45
std::ofstream out
Output stream for the trace file.
Definition TracingInterface.h:61
const std::string filename
File name.
Definition TracingInterface.h:58
~TraceWriter()
Destructor.
Definition TracingInterface.cxx:62
TraceWriter(const std::filesystem::path &file)
Constructor.
Definition TracingInterface.cxx:42
TraceWriter(TraceWriter &&)=delete
TraceWriter & operator=(const TraceWriter &)=delete
TraceWriter(const TraceWriter &)=delete
Special methods.
TraceWriter & operator=(TraceWriter &&)=delete