| 129 | } |
| 130 | |
| 131 | void SetCPUAffinity(int core) { |
| 132 | std::lock_guard<std::mutex> lock(Mutex()); |
| 133 | |
| 134 | size_t num_cpus = get_nprocs_conf(); |
| 135 | |
| 136 | cpu_set_t requested_set; |
| 137 | CPU_ZERO(&requested_set); |
| 138 | if (core != -1) { |
| 139 | if (core < 0 || (size_t)core >= num_cpus) { |
| 140 | DALI_WARN(make_string("Requested setting affinity to core ", core, |
| 141 | " but only ", num_cpus, " cores available. Ignoring...")); |
| 142 | GetNVMLAffinityMask(&requested_set, num_cpus); |
| 143 | } else { |
| 144 | CPU_SET(core, &requested_set); |
| 145 | } |
| 146 | } else { |
| 147 | GetNVMLAffinityMask(&requested_set, num_cpus); |
| 148 | } |
| 149 | |
| 150 | // Set the affinity |
| 151 | bool at_least_one_cpu_set = false; |
| 152 | for (std::size_t i = 0; i < num_cpus; i++) { |
| 153 | at_least_one_cpu_set |= CPU_ISSET(i, &requested_set); |
| 154 | } |
| 155 | if (!at_least_one_cpu_set) { |
| 156 | DALI_WARN("CPU affinity requested by user or recommended by nvml setting" |
| 157 | " does not meet allowed affinity for given DALI thread." |
| 158 | " Use taskset tool to check allowed affinity"); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | int error = pthread_setaffinity_np(pthread_self(), sizeof(requested_set), &requested_set); |
| 163 | if (error != 0) { |
| 164 | DALI_WARN("Setting affinity failed! Error code: " + to_string(error)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | } // namespace nvml |
no test coverage detected