-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtask_thread.cpp
52 lines (42 loc) · 1.29 KB
/
task_thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) TorchMoE.
// SPDX-License-Identifier: Apache-2.0
// TorchMoE Team
#include "task_thread.h"
#include <pthread.h>
#include <iostream>
#include "common/time.h"
#include "utils/logger.h"
std::atomic_uint32_t kGPUCounter{0};
void SetThreadScheduling(std::thread& th, int policy, int priority)
{
sched_param sch_params;
sch_params.sched_priority = priority;
if (pthread_setschedparam(th.native_handle(), policy, &sch_params)) {
std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
assert(false);
}
}
void SetThreadAffinity(std::thread& th, int cpu_id)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu_id, &cpuset);
if (pthread_setaffinity_np(th.native_handle(), sizeof(cpu_set_t), &cpuset)) {
std::cerr << "Failed to set Thread affinity : " << std::strerror(errno) << std::endl;
assert(false);
}
}
void SetThreadAffinity(std::thread& th)
{
// get number of cpus
int num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
kCPUCounter++;
int cpu_id = kCPUCounter % num_cpus;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu_id, &cpuset);
if (pthread_setaffinity_np(th.native_handle(), sizeof(cpu_set_t), &cpuset)) {
std::cerr << "Failed to set Thread affinity : " << std::strerror(errno) << std::endl;
assert(false);
}
}