threadpool: avoid updating process priority on the platforms that do not require it

On Windows we need to change overall process priority class in order to set thread priorities,
but on Linux, Mac, etc we do not need to touch the overall process settings.
This commit is contained in:
Max Krasnyansky 2024-08-26 12:25:48 -07:00 committed by fmz
parent a7496bf7e5
commit 8186e9615f

View file

@ -18700,9 +18700,16 @@ static bool ggml_thread_apply_affinity(bool * mask) {
return m != 0; return m != 0;
} }
static bool ggml_thread_apply_process_priority(int32_t prio) { static bool ggml_thread_apply_thread_priority(int32_t prio) {
DWORD p = NORMAL_PRIORITY_CLASS; DWORD p = NORMAL_PRIORITY_CLASS;
if (prio == SCHED_PRIO_NORMAL) {
// Keep inherited policy/priority
return true;
}
// On Windows we have to update Process Priority Class in order to set Thread priority.
switch (prio) { switch (prio) {
case SCHED_PRIO_NORMAL: p = NORMAL_PRIORITY_CLASS; break; case SCHED_PRIO_NORMAL: p = NORMAL_PRIORITY_CLASS; break;
case SCHED_PRIO_MEDIUM: p = ABOVE_NORMAL_PRIORITY_CLASS; break; case SCHED_PRIO_MEDIUM: p = ABOVE_NORMAL_PRIORITY_CLASS; break;
@ -18710,12 +18717,11 @@ static bool ggml_thread_apply_process_priority(int32_t prio) {
case SCHED_PRIO_REALTIME: p = REALTIME_PRIORITY_CLASS; break; case SCHED_PRIO_REALTIME: p = REALTIME_PRIORITY_CLASS; break;
} }
return SetPriorityClass(GetCurrentProcess(), p); if (!SetPriorityClass(GetCurrentProcess(), p)) {
fprintf(stderr, "warn: failed to set process priority class %d : (%d)\n", prio, (int) GetLastError());
return false;
} }
static bool ggml_thread_apply_thread_priority(int32_t prio) {
DWORD p = NORMAL_PRIORITY_CLASS;
switch (prio) { switch (prio) {
case SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break; case SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
case SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break; case SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break;
@ -18723,8 +18729,12 @@ static bool ggml_thread_apply_thread_priority(int32_t prio) {
case SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break; case SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
} }
return SetThreadPriority(GetCurrentThread(), p); if (!SetThreadPriority(GetCurrentThread(), p)) {
fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError());
return false;
}
return true;
} }
#elif defined(__APPLE__) #elif defined(__APPLE__)
@ -18732,26 +18742,32 @@ static bool ggml_thread_apply_thread_priority(int32_t prio) {
#include <sys/resource.h> #include <sys/resource.h>
static bool ggml_thread_apply_affinity(const bool * mask) { static bool ggml_thread_apply_affinity(const bool * mask) {
// Not supported on Apple platforms
UNUSED(mask); UNUSED(mask);
return true; return true;
} }
static bool ggml_thread_apply_process_priority(int32_t prio) {
int32_t p = 0;
switch (prio) {
case SCHED_PRIO_NORMAL: p = 0; break;
case SCHED_PRIO_MEDIUM: p = -5; break;
case SCHED_PRIO_HIGH: p = -10; break;
case SCHED_PRIO_REALTIME: p = -20; break;
}
int32_t r = setpriority(PRIO_PROCESS, 0, p);
return r != -1;
}
static bool ggml_thread_apply_thread_priority(int32_t prio) { static bool ggml_thread_apply_thread_priority(int32_t prio) {
UNUSED(prio); struct sched_param p;
int32_t policy = SCHED_OTHER;
switch (prio) {
case SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
case SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
case SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
}
if (prio == SCHED_PRIO_NORMAL) {
// Keep inherited policy/priority
return true;
}
int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
if (err != 0) {
fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
return false;
}
return true; return true;
} }
@ -18759,7 +18775,7 @@ static bool ggml_thread_apply_thread_priority(int32_t prio) {
static bool ggml_thread_apply_affinity(const bool * mask) { static bool ggml_thread_apply_affinity(const bool * mask) {
cpu_set_t cpuset; cpu_set_t cpuset;
int32_t err; int err;
CPU_ZERO(&cpuset); CPU_ZERO(&cpuset);
@ -18779,27 +18795,7 @@ static bool ggml_thread_apply_affinity(const bool * mask) {
err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset); err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
#endif #endif
if (err != 0) { if (err != 0) {
//fprintf(stderr, "warn: failed to set affinity mask 0x%llx (err %d: %s)\n", (unsigned long long)mask, err, strerror(err)); fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err);
return false;
}
return true;
}
static bool ggml_thread_apply_process_priority(int32_t prio) {
struct sched_param p;
int32_t policy = SCHED_OTHER;
switch (prio) {
case SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
case SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
case SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
}
int32_t err = sched_setscheduler(0, policy, &p);
if (err != 0) {
//fprintf(stderr, "warn: failed to set process priority %d (err %d)\n", prio, err);
return false; return false;
} }
@ -18816,9 +18812,14 @@ static bool ggml_thread_apply_thread_priority(int32_t prio) {
case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break; case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
} }
if (prio == SCHED_PRIO_NORMAL) {
// Keep inherited policy/priority
return true;
}
int32_t err = pthread_setschedparam(pthread_self(), policy, &p); int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
if (err != 0) { if (err != 0) {
//fprintf(stderr, "warn: failed to set thread priority %d (err %d)\n", prio, err); fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
return false; return false;
} }
@ -19380,7 +19381,6 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
} }
#else #else
// Update main thread prio and affinity to match the current threadpool // Update main thread prio and affinity to match the current threadpool
ggml_thread_apply_process_priority(threadpool->prio);
ggml_thread_apply_thread_priority(threadpool->prio); ggml_thread_apply_thread_priority(threadpool->prio);
if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) { if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
ggml_thread_apply_affinity(threadpool->workers[0].cpumask); ggml_thread_apply_affinity(threadpool->workers[0].cpumask);