env.cpp
Go to the documentation of this file.
1 //
2 // Created by Eduard Valeyev on 11/6/21.
3 //
4 
5 #include "ttg/util/env.h"
6 
7 #include <thread>
8 #include <stdexcept>
9 
10 #include <cstdlib>
11 
12 namespace ttg {
13  namespace detail {
14 
15  int num_threads() {
16  std::size_t result = 0;
17  const char* ttg_num_threads_cstr = std::getenv("TTG_NUM_THREADS");
18  if (ttg_num_threads_cstr) {
19  const auto result_long = std::atol(ttg_num_threads_cstr);
20  if (result_long >= 1)
21  result = static_cast<std::size_t>(result_long);
22  else
23  throw std::runtime_error("ttg: invalid value of environment variable TTG_NUM_THREADS");
24  } else {
25  result = std::thread::hardware_concurrency();
26  }
27  if (result > std::numeric_limits<int>::max())
28  throw std::runtime_error("ttg: number of threads exceeds the maximum limit");
29 
30  return static_cast<int>(result);
31  }
32 
34  bool result = false;
35  const char* ttg_force_device_comm_cstr = std::getenv("TTG_FORCE_DEVICE_COMM");
36  if (ttg_force_device_comm_cstr) {
37  const auto result_int = std::atoi(ttg_force_device_comm_cstr);
38  if (result_int) {
39  result = true;
40  }
41  }
42  return result;
43  }
44  } // namespace detail
45 } // namespace ttg
bool force_device_comm()
Definition: env.cpp:33
int num_threads()
Determine the number of compute threads to use by TTG when not given to ttg::initialize
Definition: env.cpp:15
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8