|
| 1 | +#include <AMReX_OpenMP.H> |
| 2 | +#include <AMReX.H> |
| 3 | +#include <AMReX_ParmParse.H> |
| 4 | +#include <AMReX_Print.H> |
| 5 | + |
| 6 | +#if defined(__APPLE__) |
| 7 | +#include <sys/types.h> |
| 8 | +#include <sys/sysctl.h> |
| 9 | +#endif |
| 10 | + |
| 11 | +#if defined(_WIN32) |
| 12 | +#include <windows.h> |
| 13 | +#endif |
| 14 | + |
| 15 | +#include <cstdlib> |
| 16 | +#include <fstream> |
| 17 | +#include <iostream> |
| 18 | +#include <optional> |
| 19 | +#include <set> |
| 20 | +#include <sstream> |
| 21 | +#include <string> |
| 22 | +#include <thread> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | + |
| 26 | +namespace amrex |
| 27 | +{ |
| 28 | + int |
| 29 | + numUniquePhysicalCores () |
| 30 | + { |
| 31 | + int ncores; |
| 32 | + |
| 33 | +#if defined(__APPLE__) |
| 34 | + size_t len = sizeof(ncores); |
| 35 | + // See hw.physicalcpu and hw.physicalcpu_max |
| 36 | + // https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_system_capabilities/ |
| 37 | + // https://developer.apple.com/documentation/kernel/1387446-sysctlbyname |
| 38 | + if (sysctlbyname("hw.physicalcpu", &ncores, &len, NULL, 0) == -1) { |
| 39 | + if (system::verbose > 0) { |
| 40 | + amrex::Print() << "numUniquePhysicalCores(): Error receiving hw.physicalcpu! " |
| 41 | + << "Defaulting to visible cores.\n"; |
| 42 | + } |
| 43 | + ncores = int(std::thread::hardware_concurrency()); |
| 44 | + } |
| 45 | +#elif defined(__linux__) |
| 46 | + std::set<std::vector<int>> uniqueThreadSets; |
| 47 | + int cpuIndex = 0; |
| 48 | + |
| 49 | + while (true) { |
| 50 | + // for each logical CPU in cpuIndex from 0...N-1 |
| 51 | + std::string path = "/sys/devices/system/cpu/cpu" + std::to_string(cpuIndex) + "/topology/thread_siblings_list"; |
| 52 | + std::ifstream file(path); |
| 53 | + if (!file.is_open()) { |
| 54 | + break; // no further CPUs to check |
| 55 | + } |
| 56 | + |
| 57 | + // find its siblings |
| 58 | + std::vector<int> siblings; |
| 59 | + std::string line; |
| 60 | + if (std::getline(file, line)) { |
| 61 | + std::stringstream ss(line); |
| 62 | + std::string token; |
| 63 | + |
| 64 | + // Possible syntax: 0-3, 8-11, 14,17 |
| 65 | + // https://github.com/torvalds/linux/blob/v6.5/Documentation/ABI/stable/sysfs-devices-system-cpu#L68-L72 |
| 66 | + while (std::getline(ss, token, ',')) { |
| 67 | + size_t dashPos = token.find('-'); |
| 68 | + if (dashPos != std::string::npos) { |
| 69 | + // Range detected |
| 70 | + int start = std::stoi(token.substr(0, dashPos)); |
| 71 | + int end = std::stoi(token.substr(dashPos + 1)); |
| 72 | + for (int i = start; i <= end; ++i) { |
| 73 | + siblings.push_back(i); |
| 74 | + } |
| 75 | + } else { |
| 76 | + siblings.push_back(std::stoi(token)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // and record the siblings group |
| 82 | + // (assumes: ascending and unique sets per cpuIndex) |
| 83 | + uniqueThreadSets.insert(siblings); |
| 84 | + cpuIndex++; |
| 85 | + } |
| 86 | + |
| 87 | + if (cpuIndex == 0) { |
| 88 | + if (system::verbose > 0) { |
| 89 | + amrex::Print() << "numUniquePhysicalCores(): Error reading CPU info.\n"; |
| 90 | + } |
| 91 | + ncores = int(std::thread::hardware_concurrency()); |
| 92 | + } else { |
| 93 | + ncores = int(uniqueThreadSets.size()); |
| 94 | + } |
| 95 | +#elif defined(_WIN32) |
| 96 | + DWORD length = 0; |
| 97 | + bool result = GetLogicalProcessorInformation(NULL, &length); |
| 98 | + |
| 99 | + if (!result) { |
| 100 | + if (system::verbose > 0) { |
| 101 | + amrex::Print() << "numUniquePhysicalCores(): Failed to get logical processor information! " |
| 102 | + << "Defaulting to visible cores.\n"; |
| 103 | + } |
| 104 | + ncores = int(std::thread::hardware_concurrency()); |
| 105 | + } |
| 106 | + else { |
| 107 | + std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> buffer(length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)); |
| 108 | + if (!GetLogicalProcessorInformation(&buffer[0], &length)) { |
| 109 | + if (system::verbose > 0) { |
| 110 | + amrex::Print() << "numUniquePhysicalCores(): Failed to get logical processor information! " |
| 111 | + << "Defaulting to visible cores.\n"; |
| 112 | + } |
| 113 | + ncores = int(std::thread::hardware_concurrency()); |
| 114 | + } else { |
| 115 | + ncores = 0; |
| 116 | + for (const auto& info : buffer) { |
| 117 | + if (info.Relationship == RelationProcessorCore) { |
| 118 | + ncores++; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +#else |
| 124 | + // TODO: |
| 125 | + // BSD |
| 126 | + if (system::verbose > 0) { |
| 127 | + amrex::Print() << "numUniquePhysicalCores(): Unknown system. Defaulting to visible cores.\n"; |
| 128 | + } |
| 129 | + ncores = int(std::thread::hardware_concurrency()); |
| 130 | +#endif |
| 131 | + return ncores; |
| 132 | + } |
| 133 | +} // namespace amrex |
| 134 | + |
| 135 | +#ifdef AMREX_USE_OMP |
| 136 | +namespace amrex::OpenMP |
| 137 | +{ |
| 138 | + void init_threads () |
| 139 | + { |
| 140 | + amrex::ParmParse pp("amrex"); |
| 141 | + std::string omp_threads = "system"; |
| 142 | + pp.queryAdd("omp_threads", omp_threads); |
| 143 | + |
| 144 | + auto to_int = [](std::string const & str_omp_threads) { |
| 145 | + std::optional<int> num; |
| 146 | + try { num = std::stoi(str_omp_threads); } |
| 147 | + catch (...) { /* nothing */ } |
| 148 | + return num; |
| 149 | + }; |
| 150 | + |
| 151 | + if (omp_threads == "system") { |
| 152 | + // default or OMP_NUM_THREADS environment variable |
| 153 | + } else if (omp_threads == "nosmt") { |
| 154 | + char const *env_omp_num_threads = std::getenv("OMP_NUM_THREADS"); |
| 155 | + if (env_omp_num_threads != nullptr && amrex::system::verbose > 1) { |
| 156 | + amrex::Print() << "amrex.omp_threads was set to nosmt," |
| 157 | + << "but OMP_NUM_THREADS was set. Will keep " |
| 158 | + << "OMP_NUM_THREADS=" << env_omp_num_threads << ".\n"; |
| 159 | + } else { |
| 160 | + omp_set_num_threads(numUniquePhysicalCores()); |
| 161 | + } |
| 162 | + } else { |
| 163 | + std::optional<int> num_omp_threads = to_int(omp_threads); |
| 164 | + if (num_omp_threads.has_value()) { |
| 165 | + omp_set_num_threads(num_omp_threads.value()); |
| 166 | + } |
| 167 | + else { |
| 168 | + if (amrex::system::verbose > 0) { |
| 169 | + amrex::Print() << "amrex.omp_threads has an unknown value: " |
| 170 | + << omp_threads |
| 171 | + << " (try system, nosmt, or a positive integer)\n"; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} // namespace amrex::OpenMP |
| 177 | +#endif // AMREX_USE_OMP |
0 commit comments