Stan Math Library  2.20.0
reverse mode automatic differentiation
map_rect_concurrent.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUNCTOR_MAP_RECT_CONCURRENT_HPP
2 #define STAN_MATH_PRIM_MAT_FUNCTOR_MAP_RECT_CONCURRENT_HPP
3 
6 
8 #include <boost/lexical_cast.hpp>
9 
10 #include <cstdlib>
11 #include <vector>
12 #include <thread>
13 
14 namespace stan {
15 namespace math {
16 namespace internal {
17 
39 inline int get_num_threads(int num_jobs) {
40  int num_threads = 1;
41 #ifdef STAN_THREADS
42  const char* env_stan_num_threads = std::getenv("STAN_NUM_THREADS");
43  if (env_stan_num_threads != nullptr) {
44  try {
45  const int env_num_threads
46  = boost::lexical_cast<int>(env_stan_num_threads);
47  if (env_num_threads > 0)
48  num_threads = env_num_threads;
49  else if (env_num_threads == -1)
50  num_threads = std::thread::hardware_concurrency();
51  else
52  invalid_argument("get_num_threads(int)", "STAN_NUM_THREADS",
53  env_stan_num_threads,
54  "The STAN_NUM_THREADS environment variable is '",
55  "' but it must be positive or -1");
56  } catch (boost::bad_lexical_cast) {
57  invalid_argument("get_num_threads(int)", "STAN_NUM_THREADS",
58  env_stan_num_threads,
59  "The STAN_NUM_THREADS environment variable is '",
60  "' but it must be a positive number or -1");
61  }
62  }
63  if (num_threads > num_jobs)
64  num_threads = num_jobs;
65 #endif
66  return num_threads;
67 }
68 
69 template <int call_id, typename F, typename T_shared_param,
70  typename T_job_param>
71 Eigen::Matrix<typename stan::return_type<T_shared_param, T_job_param>::type,
72  Eigen::Dynamic, 1>
74  const Eigen::Matrix<T_shared_param, Eigen::Dynamic, 1>& shared_params,
75  const std::vector<Eigen::Matrix<T_job_param, Eigen::Dynamic, 1>>&
76  job_params,
77  const std::vector<std::vector<double>>& x_r,
78  const std::vector<std::vector<int>>& x_i, std::ostream* msgs = nullptr);
79 
80 } // namespace internal
81 } // namespace math
82 } // namespace stan
83 
84 #endif
int get_num_threads(int num_jobs)
Get number of threads to use for num_jobs jobs.
Eigen::Matrix< typename stan::return_type< T_shared_param, T_job_param >::type, Eigen::Dynamic, 1 > map_rect_concurrent(const Eigen::Matrix< T_shared_param, Eigen::Dynamic, 1 > &shared_params, const std::vector< Eigen::Matrix< T_job_param, Eigen::Dynamic, 1 >> &job_params, const std::vector< std::vector< double >> &x_r, const std::vector< std::vector< int >> &x_i, std::ostream *msgs=nullptr)
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.

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