Stan Math Library  2.20.0
reverse mode automatic differentiation
map_rect_combine.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUNCTOR_MAP_RECT_COMBINE_HPP
2 #define STAN_MATH_PRIM_MAT_FUNCTOR_MAP_RECT_COMBINE_HPP
3 
7 
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 namespace internal {
13 
14 /* Template class for the combine step of map_rect which implements
15  * the CombineF concept. The concept requires that
16  *
17  * - A nullary constructor creates a null combiner (used on the
18  * children)
19  *
20  * - A constructor which takes the shared and job-specific parameters
21  * (used on the root/main process)
22  *
23  * - Provides an operator() which takes as two arguments: (i) the
24  * function outputs as a ragged matrix and (ii) as second argument
25  * the output sizes of each function evaluation.
26  *
27  * This functor inserts the concatenated outputs of all reduce
28  * operations into the autodiff stack. The concatenated results are
29  * stored in a double only matrix and is ragged according to the
30  * output sizes of each job.
31  *
32  * @tparam F type of user functor
33  * @tparam T_shared_param type of shared parameters
34  * @tparam T_job_param type of job specific parameters
35  */
36 template <typename F, typename T_shared_param, typename T_job_param>
38  typedef operands_and_partials<
39  Eigen::Matrix<T_shared_param, Eigen::Dynamic, 1>,
40  Eigen::Matrix<T_job_param, Eigen::Dynamic, 1>>
42  std::vector<ops_partials_t> ops_partials_;
43 
44  const std::size_t num_shared_operands_;
45  const std::size_t num_job_operands_;
46 
47  public:
48  typedef Eigen::Matrix<
50  Eigen::Dynamic, 1>
52 
54  : ops_partials_(), num_shared_operands_(0), num_job_operands_(0) {}
56  const Eigen::Matrix<T_shared_param, Eigen::Dynamic, 1>& shared_params,
57  const std::vector<Eigen::Matrix<T_job_param, Eigen::Dynamic, 1>>&
58  job_params)
59  : ops_partials_(),
60  num_shared_operands_(shared_params.rows()),
61  num_job_operands_(dims(job_params)[1]) {
62  ops_partials_.reserve(job_params.size());
63  for (const auto& job_param : job_params)
64  ops_partials_.emplace_back(shared_params, job_param);
65  }
66 
67  result_t operator()(const matrix_d& world_result,
68  const std::vector<int>& world_f_out) {
69  const std::size_t num_jobs = world_f_out.size();
70  const std::size_t offset_job_params
71  = is_constant_all<T_shared_param>::value ? 1 : 1 + num_shared_operands_;
72  const std::size_t size_world_f_out = sum(world_f_out);
73 
74  result_t out(size_world_f_out);
75 
76  for (std::size_t i = 0, ij = 0; i != num_jobs; ++i) {
77  for (int j = 0; j != world_f_out[i]; ++j, ++ij) {
79  ops_partials_[i].edge1_.partials_
80  = world_result.block(1, ij, num_shared_operands_, 1);
81 
83  ops_partials_[i].edge2_.partials_
84  = world_result.block(offset_job_params, ij, num_job_operands_, 1);
85 
86  out(ij) = ops_partials_[i].build(world_result(0, ij));
87  }
88  }
89 
90  return out;
91  }
92 };
93 
94 } // namespace internal
95 } // namespace math
96 } // namespace stan
97 
98 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
int rows(const Eigen::Matrix< T, R, C > &m)
Return the number of rows in the specified matrix, vector, or row vector.
Definition: rows.hpp:20
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
This template builds partial derivatives with respect to a set of operands.
void dims(const T &x, std::vector< int > &result)
Definition: dims.hpp:11
result_t operator()(const matrix_d &world_result, const std::vector< int > &world_f_out)
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
map_rect_combine(const Eigen::Matrix< T_shared_param, Eigen::Dynamic, 1 > &shared_params, const std::vector< Eigen::Matrix< T_job_param, Eigen::Dynamic, 1 >> &job_params)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_d
Type for matrix of double values.
Definition: typedefs.hpp:19
Eigen::Matrix< typename stan::return_type< T_shared_param, T_job_param >::type, Eigen::Dynamic, 1 > result_t

     [ Stan Home Page ] © 2011–2018, Stan Development Team.