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>
49inline std::remove_reference_t<T> to_arena(T&& a) {
50 // intentionally never returning a reference. If an object is just
51 // referenced it will likely go out of scope before it is used.
52 return std::forward<T>(a);
53}
54
65template <typename T, require_eigen_t<T>* = nullptr,
66 require_not_same_t<T, arena_t<T>>* = nullptr>
67inline arena_t<T> to_arena(const T& a) {
68 return arena_t<T>(a);
69}
70
81template <typename T>
82inline std::vector<T, arena_allocator<T>> to_arena(
83 const std::vector<T, arena_allocator<T>>& a) {
84 // What we want to do here is the same as moving input into output, except
85 // that we want input to be left unchanged. With any normal allocator that
86 // lead to deallocating memory twice (probably segfaulting). However,
87 // dealocation with `arena_allocator` is a no-op, so we can do that.
88 std::vector<T, arena_allocator<T>> res;
89 std::memcpy(static_cast<void*>(&res), static_cast<const void*>(&a),
90 sizeof(std::vector<T, arena_allocator<T>>));
91 return res;
92}
93
108template <typename T, require_same_t<T, arena_t<T>>* = nullptr>
109inline arena_t<std::vector<T>> to_arena(const std::vector<T>& a) {
110 return {a.begin(), a.end()};
111}
112
126template <typename T, require_not_same_t<T, arena_t<T>>* = nullptr>
127inline arena_t<std::vector<T>> to_arena(const std::vector<T>& a) {
129 res.reserve(a.size());
130 for (const T& i : a) {
131 res.push_back(to_arena(i));
132 }
133 return res;
134}
135
144template <bool Condition, typename T, std::enable_if_t<!Condition>* = nullptr>
145inline T to_arena_if(T&& a) {
146 return std::forward<T>(a);
147}
148
149template <bool Condition, typename T, std::enable_if_t<Condition>* = nullptr>
150inline arena_t<T> to_arena_if(const T& a) {
151 return to_arena(a);
152}
153
154} // namespace math
155} // namespace stan
156
157#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:145
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.