NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
31#include <nlohmann/json.hpp>
32#include <unordered_map>
33
34#include "neml2/base/OptionSet.h"
35
36namespace neml2
37{
38using json = nlohmann::json;
39
41{
43 TraceWriter(const std::filesystem::path & file);
46
48 TraceWriter(const TraceWriter &) = delete;
50 TraceWriter & operator=(const TraceWriter &) = delete;
52
54 const std::string filename;
55
57 std::ofstream out;
58
59 void trace_duration_begin(const std::string & name,
60 const std::string & category,
61 const json & args = {},
62 unsigned int pid = 0);
63 void trace_duration_end(const std::string & name,
64 const std::string & category,
65 const json & args = {},
66 unsigned int pid = 0);
67 void trace_instant(const std::string & name,
68 const std::string & category,
69 const json & args = {},
70 const std::string & scope = "t",
71 unsigned int pid = 0);
72
73private:
75 void write_event_common(json & event,
76 const std::string & name,
77 const std::string & category,
78 const json & args,
79 const std::string & phase,
80 unsigned int pid);
82 void dump_event(const json & event, bool last = false);
83
85 const std::chrono::high_resolution_clock::time_point _epoch;
86
88 std::mutex _mtx;
89};
90
92std::unordered_map<std::filesystem::path, std::unique_ptr<TraceWriter>> & event_trace_writers();
93
104{
105public:
107
108 TracingInterface(std::string);
110 virtual ~TracingInterface() = default;
111
116
117 bool event_tracing_enabled() const { return _enabled; }
118
121
122private:
123 TraceWriter & init_writer(std::string);
124
126 bool _enabled;
127
129 TraceWriter * _writer;
130};
131} // namespace neml2
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:51
virtual ~TracingInterface()=default
TracingInterface & operator=(const TracingInterface &)=delete
TracingInterface(std::string)
Definition TracingInterface.cxx:147
TraceWriter & event_trace_writer() const
Get the event trace writer.
Definition TracingInterface.cxx:178
TracingInterface(const TracingInterface &)=default
bool event_tracing_enabled() const
Definition TracingInterface.h:117
static OptionSet expected_options()
Definition TracingInterface.cxx:138
TracingInterface(TracingInterface &&)=default
TracingInterface & operator=(TracingInterface &&)=delete
Definition DiagnosticsInterface.cxx:30
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
nlohmann::json json
Definition TracingInterface.h:38
std::unordered_map< fs::path, std::unique_ptr< TraceWriter > > & event_trace_writers()
All trace writers.
Definition TracingInterface.cxx:38
Definition TracingInterface.h:41
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:125
void trace_duration_end(const std::string &name, const std::string &category, const json &args={}, unsigned int pid=0)
Definition TracingInterface.cxx:114
std::ofstream out
Output stream for the trace file.
Definition TracingInterface.h:57
const std::string filename
File name.
Definition TracingInterface.h:54
~TraceWriter()
Destructor.
Definition TracingInterface.cxx:62
TraceWriter(const std::filesystem::path &file)
Constructor.
void trace_duration_begin(const std::string &name, const std::string &category, const json &args={}, unsigned int pid=0)
Definition TracingInterface.cxx:103
TraceWriter(TraceWriter &&)=delete
TraceWriter & operator=(const TraceWriter &)=delete
TraceWriter(const TraceWriter &)=delete
Special methods.
TraceWriter & operator=(TraceWriter &&)=delete