timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
serialization.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
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 all
15// 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 THE
23// SOFTWARE.
24
25#pragma once
26
29#include "timemory/tpls/cereal/archives.hpp"
31
32#include <fstream>
33#include <string>
34
35#if defined(DISABLE_TIMEMORY) || defined(TIMEMORY_DISABLED) || \
36 (defined(TIMEMORY_ENABLED) && TIMEMORY_ENABLED == 0)
37
38namespace tim
39{
40//
41template <typename... Types, typename... Args>
42void
43generic_serialization(Args&&...)
44{}
45//
46} // namespace tim
47
48#else
49
50namespace tim
51{
52//--------------------------------------------------------------------------------------//
53/// \fn void generic_serialization(std::string fname, Tp obj, std::string mname,
54/// std::string dname)
55/// \param[in] fname Filename
56/// \param[in] obj Object to serialize
57/// \param[in] mname Main label for archive (default: "timemory")
58/// \param[in] dname Label for the data (default: "data")
59///
60/// \brief Generic function for serializing data. Uses the \ref
61/// tim::policy::output_archive to determine the output archive type and configure
62/// settings such as spacing, indentation width, and precision.
63///
64template <typename Tp, typename FuncT>
65void
67 const std::string& fname, const Tp& obj, const std::string& _main_name = "timemory",
68 const std::string& _data_name = "data",
69 FuncT&& _func = [](typename policy::output_archive_t<decay_t<Tp>>::type&) {})
70{
71 std::ofstream ofs{};
72 if(filepath::open(ofs, fname))
73 {
74 // ensure json write final block during destruction before the file is closed
75 using policy_type = policy::output_archive_t<decay_t<Tp>>;
76 auto oa = policy_type::get(ofs);
77 oa->setNextName(_main_name.c_str());
78 oa->startNode();
79 _func(*oa);
80 (*oa)(cereal::make_nvp(_data_name.c_str(), obj));
81 oa->finishNode();
82 }
83 if(ofs)
84 ofs << std::endl;
85 ofs.close();
86}
87//
88/// \fn void generic_serialization(std::string fname, Tp obj, std::string mname,
89/// std::string dname)
90/// \tparam ArchiveT Output archive type
91/// \tparam ApiT API tag for \ref tim::policy::output_archive look-up
92/// \param[in] fname Filename
93/// \param[in] obj Object to serialize
94/// \param[in] mname Main label for archive (default: "timemory")
95/// \param[in] dname Label for the data (default: "data")
96///
97/// \brief Generic function for serializing data. Uses the \ref
98/// tim::policy::output_archive to configure settings such as spacing, indentation width,
99/// and precision.
100///
101template <typename ArchiveT, typename ApiT = TIMEMORY_API, typename Tp, typename FuncT>
102void
104 const std::string& fname, const Tp& obj, const std::string& _main_name = "timemory",
105 const std::string& _data_name = "data",
106 FuncT&& _func = [](typename policy::output_archive_t<decay_t<Tp>>::type&) {})
107{
109 "Error! Not an output archive type");
110 std::ofstream ofs{};
111 if(filepath::open(ofs, fname))
112 {
113 // ensure json write final block during destruction before the file is closed
114 using policy_type = policy::output_archive<ArchiveT, ApiT>;
115 auto oa = policy_type::get(ofs);
116 oa->setNextName(_main_name.c_str());
117 oa->startNode();
118 _func(*oa);
119 (*oa)(cereal::make_nvp(_data_name.c_str(), obj));
120 oa->finishNode();
121 }
122 if(ofs)
123 ofs << std::endl;
124 ofs.close();
125}
126//
127/// \fn void generic_serialization(std::string fname, Tp obj, std::string mname,
128/// std::string dname)
129/// \tparam ArchiveT Output archive type
130/// \tparam ApiT API tag for \ref tim::policy::output_archive look-up
131/// \param[in] fname Filename
132/// \param[in] obj Object to serialize
133/// \param[in] mname Main label for archive (default: "timemory")
134/// \param[in] dname Label for the data (default: "data")
135///
136/// \brief Generic function for serializing data. Uses the \ref
137/// tim::policy::output_archive to configure settings such as spacing, indentation width,
138/// and precision.
139///
140template <typename ArchiveT, typename ApiT = TIMEMORY_API, typename Tp, typename FuncT>
141void
143 std::ostream& ofs, const Tp& obj, const std::string& _main_name = "timemory",
144 const std::string& _data_name = "data",
145 FuncT&& _func = [](typename policy::output_archive_t<decay_t<Tp>>::type&) {})
146{
147 // ensure json write final block during destruction before the file is closed
148 using policy_type = policy::output_archive<ArchiveT, ApiT>;
149 auto oa = policy_type::get(ofs);
150
151 if(!_main_name.empty())
152 {
153 oa->setNextName(_main_name.c_str());
154 oa->startNode();
155 }
156
157 // execute the function with extra data
158 _func(*oa);
159
160 (*oa)(cereal::make_nvp(_data_name, obj));
161
162 if(!_main_name.empty())
163 {
164 oa->finishNode();
165 }
166}
167//
168} // namespace tim
169
170#endif
bool open(std::ofstream &_ofs, std::string _fpath, Args &&... _args)
Definition: filepath.hpp:207
Definition: kokkosp.cpp:39
typename std::decay< T >::type decay_t
Alias template for decay.
Definition: types.hpp:194
void generic_serialization(const std::string &fname, const Tp &obj, const std::string &_main_name="timemory", const std::string &_data_name="data", FuncT &&_func=[](typename policy::output_archive_t< decay_t< Tp > >::type &) {})
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
auto get(const auto_bundle< Tag, Types... > &_obj)
static constexpr bool value
Definition: concepts.hpp:435
Provides a static get() function which return a shared pointer to an instance of the given archive fo...
Definition: policy.hpp:136