Loading [MathJax]/extensions/TeX/AMSsymbols.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
to_arena.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_TO_ARENA_HPP
2#define STAN_MATH_REV_FUN_TO_ARENA_HPP
3
8#include <vector>
9#include <cstring>
10
11namespace stan {
12namespace math {
13
26template <typename T, require_not_same_t<T, arena_t<T>>* = nullptr,
27 require_not_container_t<T>* = nullptr,
28 require_not_matrix_cl_t<T>* = nullptr>
29inline arena_t<T> to_arena(T&& a) {
30 return std::forward<T>(a);
31}
32
47template <typename T, require_same_t<T, arena_t<T>>* = nullptr,
48 require_not_matrix_cl_t<T>* = nullptr,
49 require_not_std_vector_t<T>* = nullptr,
50 require_not_tuple_t<T>* = nullptr>
51inline std::remove_reference_t<T> to_arena(T&& a) {
52 // intentionally never returning a reference. If an object is just
53 // referenced it will likely go out of scope before it is used.
54 return std::forward<T>(a);
55}
56
67template <typename T, require_eigen_t<T>* = nullptr,
68 require_not_same_t<T, arena_t<T>>* = nullptr>
69inline arena_t<T> to_arena(const T& a) {
70 return arena_t<T>(a);
71}
72
83template <typename T>
84inline std::vector<T, arena_allocator<T>> to_arena(
85 const std::vector<T, arena_allocator<T>>& a) {
86 // What we want to do here is the same as moving input into output, except
87 // that we want input to be left unchanged. With any normal allocator that
88 // lead to deallocating memory twice (probably segfaulting). However,
89 // dealocation with `arena_allocator` is a no-op, so we can do that.
90 std::vector<T, arena_allocator<T>> res;
91 std::memcpy(static_cast<void*>(&res), static_cast<const void*>(&a),
92 sizeof(std::vector<T, arena_allocator<T>>));
93 return res;
94}
95
110template <typename T, require_same_t<T, arena_t<T>>* = nullptr>
111inline arena_t<std::vector<T>> to_arena(const std::vector<T>& a) {
112 return {a.begin(), a.end()};
113}
114
128template <typename T, require_not_same_t<T, arena_t<T>>* = nullptr>
129inline arena_t<std::vector<T>> to_arena(const std::vector<T>& a) {
131 res.reserve(a.size());
132 for (const T& i : a) {
133 res.push_back(to_arena(i));
134 }
135 return res;
136}
137
144template <typename Tuple, require_tuple_t<Tuple>* = nullptr>
145inline auto to_arena(Tuple&& tup) {
146 return stan::math::apply(
147 [](auto&&... args) {
148 return std::make_tuple(to_arena(std::forward<decltype(args)>(args))...);
149 },
150 std::forward<Tuple>(tup));
151}
152
161template <bool Condition, typename T, std::enable_if_t<!Condition>* = nullptr>
162inline T to_arena_if(T&& a) {
163 return std::forward<T>(a);
164}
165
166template <bool Condition, typename T, std::enable_if_t<Condition>* = nullptr>
167inline arena_t<T> to_arena_if(const T& a) {
168 return to_arena(a);
169}
170
171} // namespace math
172} // namespace stan
173
174#endif
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
T to_arena_if(T &&a)
If the condition is true, converts given argument into a type that has any dynamic allocation on AD s...
Definition to_arena.hpp:162
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
std library compatible allocator that uses AD stack.