Skip to content

Commit dea7f80

Browse files
authored
Merge pull request #3378 from stan-dev/boost-repl-2
Replace boost/optional and boost/lexical_cast
2 parents badf521 + bc3e31c commit dea7f80

4 files changed

Lines changed: 30 additions & 35 deletions

File tree

stan/math/prim/core/init_threadpool_tbb.hpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include <stan/math/prim/err/invalid_argument.hpp>
55

6-
#include <boost/lexical_cast.hpp>
7-
86
#ifndef TBB_INTERFACE_NEW
97
#include <tbb/tbb_stddef.h>
108

@@ -20,7 +18,9 @@
2018
#include <tbb/task_scheduler_init.h>
2119
#endif
2220

21+
#include <charconv>
2322
#include <cstdlib>
23+
#include <string_view>
2424
#include <thread>
2525

2626
namespace stan {
@@ -44,32 +44,28 @@ namespace internal {
4444
* is invalid
4545
*/
4646
inline int get_num_threads() {
47-
int num_threads = 1;
4847
#ifdef STAN_THREADS
4948
const char* env_stan_num_threads = std::getenv("STAN_NUM_THREADS");
50-
if (env_stan_num_threads != nullptr) {
51-
try {
52-
const int env_num_threads
53-
= boost::lexical_cast<int>(env_stan_num_threads);
54-
if (env_num_threads > 0) {
55-
num_threads = env_num_threads;
56-
} else if (env_num_threads == -1) {
57-
num_threads = std::thread::hardware_concurrency();
58-
} else {
59-
invalid_argument("get_num_threads(int)", "STAN_NUM_THREADS",
60-
env_stan_num_threads,
61-
"The STAN_NUM_THREADS environment variable is '",
62-
"' but it must be positive or -1");
63-
}
64-
} catch (const boost::bad_lexical_cast&) {
65-
invalid_argument("get_num_threads(int)", "STAN_NUM_THREADS",
66-
env_stan_num_threads,
67-
"The STAN_NUM_THREADS environment variable is '",
68-
"' but it must be a positive number or -1");
69-
}
49+
if (env_stan_num_threads == nullptr) {
50+
return 1;
51+
}
52+
53+
const std::string_view value(env_stan_num_threads);
54+
int num_threads;
55+
const auto [end, error]
56+
= std::from_chars(value.begin(), value.end(), num_threads);
57+
if (error != std::errc() || end != value.end()
58+
|| (num_threads < 1 && num_threads != -1)) {
59+
invalid_argument("get_num_threads(int)", "STAN_NUM_THREADS",
60+
env_stan_num_threads,
61+
"The STAN_NUM_THREADS environment variable is '",
62+
"' but it must be a positive number or -1");
7063
}
64+
65+
return num_threads == -1 ? std::thread::hardware_concurrency() : num_threads;
66+
#else
67+
return 1;
7168
#endif
72-
return num_threads;
7369
}
7470

7571
} // namespace internal

stan/math/prim/fun/grad_2F1.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <stan/math/prim/fun/sign.hpp>
1414
#include <stan/math/prim/fun/hypergeometric_2F1.hpp>
1515
#include <cmath>
16-
#include <boost/optional.hpp>
1716

1817
namespace stan {
1918
namespace math {

stan/math/prim/fun/hypergeometric_2F1.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <stan/math/prim/fun/sqrt.hpp>
1818
#include <stan/math/prim/fun/square.hpp>
1919
#include <stan/math/prim/fun/hypergeometric_pFq.hpp>
20-
#include <boost/optional.hpp>
20+
#include <optional>
2121

2222
namespace stan {
2323
namespace math {
@@ -29,7 +29,7 @@ namespace internal {
2929
* more background (and other possible special-cases), see:
3030
* https://functions.wolfram.com/HypergeometricFunctions/Hypergeometric2F1/03/
3131
*
32-
* The return value is wrapped in a boost::optional<> type so that a void
32+
* The return value is wrapped in a std::optional<> type so that a void
3333
* return is possible if no special-case rules are applicable
3434
*
3535
* @tparam Ta1 Type of scalar first 'a' argument
@@ -43,7 +43,7 @@ namespace internal {
4343
* @return Gauss hypergeometric function
4444
*/
4545
template <typename Ta1, typename Ta2, typename Tb, typename Tz,
46-
typename RtnT = boost::optional<return_type_t<Ta1, Ta2, Tb, Tz>>,
46+
typename RtnT = std::optional<return_type_t<Ta1, Ta2, Tb, Tz>>,
4747
require_all_arithmetic_t<Ta1, Ta2, Tb, Tz>* = nullptr>
4848
inline RtnT hyper_2F1_special_cases(const Ta1& a1, const Ta2& a2, const Tb& b,
4949
const Tz& z) {
@@ -149,7 +149,7 @@ inline RtnT hyper_2F1_special_cases(const Ta1& a1, const Ta2& a2, const Tb& b,
149149
*/
150150
template <typename Ta1, typename Ta2, typename Tb, typename Tz,
151151
typename ScalarT = return_type_t<Ta1, Ta2, Tb, Tz>,
152-
typename OptT = boost::optional<ScalarT>,
152+
typename OptT = std::optional<ScalarT>,
153153
require_all_arithmetic_t<Ta1, Ta2, Tb, Tz>* = nullptr>
154154
inline return_type_t<Ta1, Ta2, Tb, Tz> hypergeometric_2F1(const Ta1& a1,
155155
const Ta2& a2,
@@ -168,15 +168,15 @@ inline return_type_t<Ta1, Ta2, Tb, Tz> hypergeometric_2F1(const Ta1& a1,
168168
// Check whether value can be calculated by any special-case rules
169169
// before estimating infinite sum
170170
OptT special_case_a1a2 = internal::hyper_2F1_special_cases(a1, a2, b, z);
171-
if (special_case_a1a2.is_initialized()) {
172-
return special_case_a1a2.get();
171+
if (special_case_a1a2.has_value()) {
172+
return special_case_a1a2.value();
173173
}
174174

175175
// Check whether any special case rules apply with 'a' arguments reversed
176176
// as 2F1(a1, a2, b, z) = 2F1(a2, a1, b, z)
177177
OptT special_case_a2a1 = internal::hyper_2F1_special_cases(a2, a1, b, z);
178-
if (special_case_a2a1.is_initialized()) {
179-
return special_case_a2a1.get();
178+
if (special_case_a2a1.has_value()) {
179+
return special_case_a2a1.value();
180180
}
181181

182182
Eigen::Matrix<double, 2, 1> a_args(2);

test/unit/math/prim/core/get_num_threads_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ TEST(get_num_threads, incorrect_values) {
2727

2828
set_n_threads("-2");
2929
EXPECT_THROW_MSG(stan::math::internal::get_num_threads(),
30-
std::invalid_argument, "must be positive or -1");
30+
std::invalid_argument, "positive number or -1");
3131

3232
set_n_threads("0");
3333
EXPECT_THROW_MSG(stan::math::internal::get_num_threads(),
34-
std::invalid_argument, "must be positive or -1");
34+
std::invalid_argument, "positive number or -1");
3535
}
3636
#else
3737
TEST(get_num_threads, correct_values_no_stan_threads) {

0 commit comments

Comments
 (0)