Automatic Differentiation
 
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
7#include <vector>
8#include <cstring>
9
10namespace stan {
11namespace math {
12
25template <typename T, require_not_same_t<T, arena_t<T>>* = nullptr,
26 require_not_container_t<T>* = nullptr,
27 require_not_matrix_cl_t<T>* = nullptr>
28inline arena_t<T> to_arena(T&& a) {
29 return std::forward<T>(a);
30}
31
46template <typename T, require_same_t<T, arena_t<T>>* = nullptr,
47 require_not_matrix_cl_t<T>* = nullptr,
48 require_not_std_vector_t<T>* = nullptr,
49 require_not_tuple_t<T>* = nullptr>
50inline std::remove_reference_t<T> to_arena(T&& a) {
51 // intentionally never returning a reference. If an object is just
52 // referenced it will likely go out of scope before it is used.
53 return std::forward<T>(a);
54}
55
66template <typename T, require_eigen_t<T>* = nullptr,
67 require_not_same_t<T, arena_t<T>>* = nullptr>
68inline arena_t<T> to_arena(const T& a) {
69 return arena_t<T>(a);
70}
71
82template <typename T>
83inline std::vector<T, arena_allocator<T>> to_arena(
84 const std::vector<T, arena_allocator<T>>& a) {
85 // What we want to do here is the same as moving input into output, except
86 // that we want input to be left unchanged. With any normal allocator that
87 // lead to deallocating memory twice (probably segfaulting). However,
88 // dealocation with `arena_allocator` is a no-op, so we can do that.
89 std::vector<T, arena_allocator<T>> res;
90 std::memcpy(static_cast<void*>(&res), static_cast<const void*>(&a),
91 sizeof(std::vector<T, arena_allocator<T>>));
92 return res;
93}
94
109template <typename T, require_same_t<T, arena_t<T>>* = nullptr>
110inline arena_t<std::vector<T>> to_arena(const std::vector<T>& a) {
111 return {a.begin(), a.end()};
112}
113
127template <typename T, require_not_same_t<T, arena_t<T>>* = nullptr>
128inline arena_t<std::vector<T>> to_arena(const std::vector<T>& a) {
130 res.reserve(a.size());
131 for (const T& i : a) {
132 res.push_back(to_arena(i));
133 }
134 return res;
135}
136
143template <typename Tuple, require_tuple_t<Tuple>* = nullptr>
144inline auto to_arena(Tuple&& tup) {
145 return stan::math::apply(
146 [](auto&&... args) {
147 return std::make_tuple(to_arena(std::forward<decltype(args)>(args))...);
148 },
149 std::forward<Tuple>(tup));
150}
151
160template <bool Condition, typename T, std::enable_if_t<!Condition>* = nullptr>
161inline T to_arena_if(T&& a) {
162 return std::forward<T>(a);
163}
164
165template <bool Condition, typename T, std::enable_if_t<Condition>* = nullptr>
166inline arena_t<T> to_arena_if(const T& a) {
167 return to_arena(a);
168}
169
170} // namespace math
171} // namespace stan
172
173#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:161
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.