Automatic Differentiation
 
Loading...
Searching...
No Matches
profiling.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_PROFILING_HPP
2#define STAN_MATH_REV_CORE_PROFILING_HPP
3
10#include <tbb/concurrent_unordered_map.h>
11#include <iostream>
12#include <sstream>
13#include <thread>
14
15namespace stan {
16namespace math {
17
22 private:
23 bool active_;
24
32 std::chrono::time_point<std::chrono::steady_clock> fwd_pass_tp_;
33 std::chrono::time_point<std::chrono::steady_clock> rev_pass_tp_;
36
37 public:
39 : active_(false),
40 fwd_pass_time_(0.0),
41 rev_pass_time_(0.0),
47 fwd_pass_tp_(std::chrono::steady_clock::now()),
48 rev_pass_tp_(std::chrono::steady_clock::now()),
51
52 bool is_active() const noexcept { return active_; }
53
54 template <typename T>
60 }
61 fwd_pass_tp_ = std::chrono::steady_clock::now();
62 active_ = true;
63 }
64
65 template <typename T>
74 } else {
76 }
77 fwd_pass_time_ += std::chrono::duration<double>(
78 std::chrono::steady_clock::now() - fwd_pass_tp_)
79 .count();
80 active_ = false;
81 }
82
83 void rev_pass_start() { rev_pass_tp_ = std::chrono::steady_clock::now(); }
84
86 rev_pass_time_ += std::chrono::duration<double>(
87 std::chrono::steady_clock::now() - rev_pass_tp_)
88 .count();
90 }
91
92 size_t get_chain_stack_used() const noexcept {
94 };
95
96 size_t get_nochain_stack_used() const noexcept {
98 };
99
100 size_t get_num_no_AD_fwd_passes() const noexcept {
101 return n_fwd_no_AD_passes_;
102 }
103
104 size_t get_num_AD_fwd_passes() const noexcept { return n_fwd_AD_passes_; }
105
106 size_t get_num_fwd_passes() const noexcept {
108 }
109
110 double get_fwd_time() const noexcept { return fwd_pass_time_; }
111
112 size_t get_num_rev_passes() const noexcept { return n_rev_passes_; }
113
114 double get_rev_time() const noexcept { return rev_pass_time_; }
115};
116
117using profile_key = std::pair<std::string, std::thread::id>;
118
119namespace internal {
121 std::size_t operator()(const profile_key& key) const {
122 return std::hash<std::string>()(key.first)
123 ^ std::hash<std::thread::id>()(key.second);
124 }
125};
127 bool operator()(const profile_key& lhs, const profile_key& rhs) const {
128 return lhs.first == rhs.first && lhs.second == rhs.second;
129 }
130};
131
132} // namespace internal
133
134using profile_map = tbb::concurrent_unordered_map<profile_key, profile_info,
137
150template <typename T>
151class profile {
154
155 public:
156 profile(std::string name, profile_map& profiles)
157 : key_({name, std::this_thread::get_id()}) {
158 profile_map::iterator p = profiles.find(key_);
159 if (p == profiles.end()) {
160 profiles[key_] = profile_info();
161 }
162 profile_ = &profiles[key_];
163 if (profile_->is_active()) {
164 std::ostringstream msg;
165 msg << "Profile '" << key_.first << "' already started!";
166 throw std::runtime_error(msg.str());
167 }
169 if (!is_constant<T>::value) {
171 [profile = this->profile_]() mutable { profile->rev_pass_stop(); });
172 }
173 }
178 [profile = this->profile_]() mutable { profile->rev_pass_start(); });
179 }
180 }
181};
182
183} // namespace math
184} // namespace stan
185#endif
size_t get_num_AD_fwd_passes() const noexcept
size_t get_nochain_stack_used() const noexcept
Definition profiling.hpp:96
size_t get_chain_stack_used() const noexcept
Definition profiling.hpp:92
std::chrono::time_point< std::chrono::steady_clock > fwd_pass_tp_
Definition profiling.hpp:32
bool is_active() const noexcept
Definition profiling.hpp:52
size_t get_num_rev_passes() const noexcept
size_t get_num_no_AD_fwd_passes() const noexcept
size_t get_num_fwd_passes() const noexcept
double get_fwd_time() const noexcept
double get_rev_time() const noexcept
std::chrono::time_point< std::chrono::steady_clock > rev_pass_tp_
Definition profiling.hpp:33
Class used for storing profiling information.
Definition profiling.hpp:21
profile(std::string name, profile_map &profiles)
profile_info * profile_
Profiles C++ lines where the object is in scope.
tbb::concurrent_unordered_map< profile_key, profile_info, internal::hash_profile_key, internal::equal_profile_key > profile_map
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
std::pair< std::string, std::thread::id > profile_key
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
STL namespace.
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
static thread_local AutodiffStackStorage * instance_
bool operator()(const profile_key &lhs, const profile_key &rhs) const
std::size_t operator()(const profile_key &key) const