map_rect_reduce.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUNCTOR_MAP_RECT_REDUCE_HPP
2 #define STAN_MATH_REV_MAT_FUNCTOR_MAP_RECT_REDUCE_HPP
3 
9 
10 #include <vector>
11 
12 namespace stan {
13 namespace math {
14 namespace internal {
15 
16 template <typename F>
17 struct map_rect_reduce<F, var, var> {
18  matrix_d operator()(const vector_d& shared_params,
19  const vector_d& job_specific_params,
20  const std::vector<double>& x_r,
21  const std::vector<int>& x_i,
22  std::ostream* msgs = nullptr) const {
23  const size_type num_shared_params = shared_params.rows();
24  const size_type num_job_specific_params = job_specific_params.rows();
25  matrix_d out(1 + num_shared_params + num_job_specific_params, 0);
26 
27  try {
28  start_nested();
29  vector_v shared_params_v = to_var(shared_params);
30  vector_v job_specific_params_v = to_var(job_specific_params);
31 
32  vector_v fx_v
33  = F()(shared_params_v, job_specific_params_v, x_r, x_i, msgs);
34 
35  const size_type size_f = fx_v.rows();
36 
37  out.resize(Eigen::NoChange, size_f);
38 
39  for (size_type i = 0; i < size_f; ++i) {
40  out(0, i) = fx_v(i).val();
42  fx_v(i).grad();
43  for (size_type j = 0; j < num_shared_params; ++j)
44  out(1 + j, i) = shared_params_v(j).vi_->adj_;
45  for (size_type j = 0; j < num_job_specific_params; ++j)
46  out(1 + num_shared_params + j, i)
47  = job_specific_params_v(j).vi_->adj_;
48  }
50  } catch (const std::exception& e) {
52  throw;
53  }
54  return out;
55  }
56 };
57 
58 template <typename F>
59 struct map_rect_reduce<F, double, var> {
60  matrix_d operator()(const vector_d& shared_params,
61  const vector_d& job_specific_params,
62  const std::vector<double>& x_r,
63  const std::vector<int>& x_i,
64  std::ostream* msgs = nullptr) const {
65  const size_type num_job_specific_params = job_specific_params.rows();
66  matrix_d out(1 + num_job_specific_params, 0);
67 
68  try {
69  start_nested();
70  vector_v job_specific_params_v = to_var(job_specific_params);
71 
72  vector_v fx_v = F()(shared_params, job_specific_params_v, x_r, x_i, msgs);
73 
74  const size_type size_f = fx_v.rows();
75 
76  out.resize(Eigen::NoChange, size_f);
77 
78  for (size_type i = 0; i < size_f; ++i) {
79  out(0, i) = fx_v(i).val();
81  fx_v(i).grad();
82  for (size_type j = 0; j < num_job_specific_params; ++j)
83  out(1 + j, i) = job_specific_params_v(j).vi_->adj_;
84  }
86  } catch (const std::exception& e) {
88  throw;
89  }
90  return out;
91  }
92 };
93 
94 template <typename F>
95 struct map_rect_reduce<F, var, double> {
96  matrix_d operator()(const vector_d& shared_params,
97  const vector_d& job_specific_params,
98  const std::vector<double>& x_r,
99  const std::vector<int>& x_i,
100  std::ostream* msgs = nullptr) const {
101  const size_type num_shared_params = shared_params.rows();
102  matrix_d out(1 + num_shared_params, 0);
103 
104  try {
105  start_nested();
106  vector_v shared_params_v = to_var(shared_params);
107 
108  vector_v fx_v = F()(shared_params_v, job_specific_params, x_r, x_i, msgs);
109 
110  const size_type size_f = fx_v.rows();
111 
112  out.resize(Eigen::NoChange, size_f);
113 
114  for (size_type i = 0; i < size_f; ++i) {
115  out(0, i) = fx_v(i).val();
117  fx_v(i).grad();
118  for (size_type j = 0; j < num_shared_params; ++j)
119  out(1 + j, i) = shared_params_v(j).vi_->adj_;
120  }
122  } catch (const std::exception& e) {
124  throw;
125  }
126  return out;
127  }
128 };
129 
130 } // namespace internal
131 } // namespace math
132 } // namespace stan
133 
134 #ifdef STAN_REGISTER_MPI_MAP_RECT_ALL
135 
136 #undef STAN_REGISTER_MPI_MAP_RECT_ALL
137 
138 #define STAN_REGISTER_MPI_MAP_RECT_ALL(CALLID, FUNCTOR) \
139  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, double, double) \
140  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, double, var) \
141  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, var, double) \
142  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, var, var)
143 
144 #endif
145 
146 #endif
#define F(x, y, z)
Eigen::Matrix< var, Eigen::Dynamic, 1 > vector_v
Definition: typedefs.hpp:22
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Definition: typedefs.hpp:24
static void set_zero_all_adjoints_nested()
matrix_d operator()(const vector_d &shared_params, const vector_d &job_specific_params, const std::vector< double > &x_r, const std::vector< int > &x_i, std::ostream *msgs=nullptr) const
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:11
const double j
Definition: BetheBloch.cxx:29
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_d
Definition: typedefs.hpp:19
double e()
Definition: constants.hpp:89
matrix_d operator()(const vector_d &shared_params, const vector_d &job_specific_params, const std::vector< double > &x_r, const std::vector< int > &x_i, std::ostream *msgs=nullptr) const
matrix_d operator()(const vector_d &shared_params, const vector_d &job_specific_params, const std::vector< double > &x_r, const std::vector< int > &x_i, std::ostream *msgs=nullptr) const
static void recover_memory_nested()
std::vector< var > to_var(const std::vector< double > &v)
Definition: to_var.hpp:19
static void start_nested()