Stan Math Library  2.20.0
reverse mode automatic differentiation
map_rect_mpi.hpp
Go to the documentation of this file.
1 #ifdef STAN_MPI
2 
3 #ifndef STAN_MATH_PRIM_MAT_FUNCTOR_MAP_RECT_MPI_HPP
5 #define STAN_MATH_PRIM_MAT_FUNCTOR_MAP_RECT_MPI_HPP
6 
12 
13 #include <vector>
14 
15 namespace stan {
16 namespace math {
17 namespace internal {
18 
19 template <int call_id, typename F, typename T_shared_param,
20  typename T_job_param>
21 Eigen::Matrix<typename stan::return_type<T_shared_param, T_job_param>::type,
22  Eigen::Dynamic, 1>
23 map_rect_mpi(
24  const Eigen::Matrix<T_shared_param, Eigen::Dynamic, 1>& shared_params,
25  const std::vector<Eigen::Matrix<T_job_param, Eigen::Dynamic, 1>>&
26  job_params,
27  const std::vector<std::vector<double>>& x_r,
28  const std::vector<std::vector<int>>& x_i, std::ostream* msgs = nullptr) {
29  typedef internal::map_rect_reduce<F, T_shared_param, T_job_param> ReduceF;
30  typedef internal::map_rect_combine<F, T_shared_param, T_job_param> CombineF;
31 
32  // whenever the cluster is already busy with some command we fall
33  // back to serial execution (possible if map_rect calls are nested
34  // or MPI facility used already in use)
35  try {
36  mpi_parallel_call<call_id, ReduceF, CombineF> job_chunk(
37  shared_params, job_params, x_r, x_i);
38 
39  return job_chunk.reduce_combine();
40  } catch (const mpi_is_in_use& e) {
41  return map_rect_concurrent<call_id, F>(shared_params, job_params, x_r, x_i,
42  msgs);
43  }
44 }
45 } // namespace internal
46 } // namespace math
47 } // namespace stan
48 
49 #define STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, SHARED, JOB) \
50  namespace stan { \
51  namespace math { \
52  namespace internal { \
53  typedef FUNCTOR mpi_mr_##CALLID##_##SHARED##_##JOB##_; \
54  typedef map_rect_reduce<mpi_mr_##CALLID##_##SHARED##_##JOB##_, SHARED, JOB> \
55  mpi_mr_##CALLID##_##SHARED##_##JOB##_red_; \
56  typedef map_rect_combine<mpi_mr_##CALLID##_##SHARED##_##JOB##_, SHARED, JOB> \
57  mpi_mr_##CALLID##_##SHARED##_##JOB##_comb_; \
58  typedef mpi_parallel_call<CALLID, mpi_mr_##CALLID##_##SHARED##_##JOB##_red_, \
59  mpi_mr_##CALLID##_##SHARED##_##JOB##_comb_> \
60  mpi_mr_##CALLID##_##SHARED##_##JOB##_pcall_; \
61  } \
62  } \
63  } \
64  STAN_REGISTER_MPI_DISTRIBUTED_APPLY( \
65  stan::math::internal::mpi_mr_##CALLID##_##SHARED##_##JOB##_pcall_)
66 
67 #define STAN_REGISTER_MPI_MAP_RECT_ALL(CALLID, FUNCTOR) \
68  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, double, double)
69 
70 // redefine register macro to use MPI variant
71 #undef STAN_REGISTER_MAP_RECT
72 #define STAN_REGISTER_MAP_RECT(CALLID, FUNCTOR) \
73  STAN_REGISTER_MPI_MAP_RECT_ALL(CALLID, FUNCTOR)
74 
75 #endif
76 
77 #endif
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:87

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