Skip to content

Commit 8186e96

Browse files
max-krasnyanskyfmz
authored andcommitted
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.
1 parent a7496bf commit 8186e96

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

ggml/src/ggml.c

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18700,21 +18700,27 @@ static bool ggml_thread_apply_affinity(bool * mask) {
1870018700
return m != 0;
1870118701
}
1870218702

18703-
static bool ggml_thread_apply_process_priority(int32_t prio) {
18703+
static bool ggml_thread_apply_thread_priority(int32_t prio) {
1870418704
DWORD p = NORMAL_PRIORITY_CLASS;
1870518705

18706+
if (prio == SCHED_PRIO_NORMAL) {
18707+
// Keep inherited policy/priority
18708+
return true;
18709+
}
18710+
18711+
// On Windows we have to update Process Priority Class in order to set Thread priority.
18712+
1870618713
switch (prio) {
1870718714
case SCHED_PRIO_NORMAL: p = NORMAL_PRIORITY_CLASS; break;
1870818715
case SCHED_PRIO_MEDIUM: p = ABOVE_NORMAL_PRIORITY_CLASS; break;
1870918716
case SCHED_PRIO_HIGH: p = HIGH_PRIORITY_CLASS; break;
1871018717
case SCHED_PRIO_REALTIME: p = REALTIME_PRIORITY_CLASS; break;
1871118718
}
1871218719

18713-
return SetPriorityClass(GetCurrentProcess(), p);
18714-
}
18715-
18716-
static bool ggml_thread_apply_thread_priority(int32_t prio) {
18717-
DWORD p = NORMAL_PRIORITY_CLASS;
18720+
if (!SetPriorityClass(GetCurrentProcess(), p)) {
18721+
fprintf(stderr, "warn: failed to set process priority class %d : (%d)\n", prio, (int) GetLastError());
18722+
return false;
18723+
}
1871818724

1871918725
switch (prio) {
1872018726
case SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
@@ -18723,43 +18729,53 @@ static bool ggml_thread_apply_thread_priority(int32_t prio) {
1872318729
case SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
1872418730
}
1872518731

18726-
return SetThreadPriority(GetCurrentThread(), p);
18732+
if (!SetThreadPriority(GetCurrentThread(), p)) {
18733+
fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError());
18734+
return false;
18735+
}
1872718736

18737+
return true;
1872818738
}
1872918739

1873018740
#elif defined(__APPLE__)
1873118741
#include <sys/types.h>
1873218742
#include <sys/resource.h>
1873318743

1873418744
static bool ggml_thread_apply_affinity(const bool * mask) {
18745+
// Not supported on Apple platforms
1873518746
UNUSED(mask);
1873618747
return true;
1873718748
}
1873818749

18739-
static bool ggml_thread_apply_process_priority(int32_t prio) {
18740-
int32_t p = 0;
18741-
18750+
static bool ggml_thread_apply_thread_priority(int32_t prio) {
18751+
struct sched_param p;
18752+
int32_t policy = SCHED_OTHER;
1874218753
switch (prio) {
18743-
case SCHED_PRIO_NORMAL: p = 0; break;
18744-
case SCHED_PRIO_MEDIUM: p = -5; break;
18745-
case SCHED_PRIO_HIGH: p = -10; break;
18746-
case SCHED_PRIO_REALTIME: p = -20; break;
18754+
case SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
18755+
case SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
18756+
case SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
18757+
case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
1874718758
}
1874818759

18749-
int32_t r = setpriority(PRIO_PROCESS, 0, p);
18750-
return r != -1;
18751-
}
18760+
if (prio == SCHED_PRIO_NORMAL) {
18761+
// Keep inherited policy/priority
18762+
return true;
18763+
}
18764+
18765+
int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
18766+
if (err != 0) {
18767+
fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
18768+
return false;
18769+
}
1875218770

18753-
static bool ggml_thread_apply_thread_priority(int32_t prio) {
18754-
UNUSED(prio);
1875518771
return true;
1875618772
}
1875718773

1875818774
#else // posix?
1875918775

1876018776
static bool ggml_thread_apply_affinity(const bool * mask) {
1876118777
cpu_set_t cpuset;
18762-
int32_t err;
18778+
int err;
1876318779

1876418780
CPU_ZERO(&cpuset);
1876518781

@@ -18779,46 +18795,31 @@ static bool ggml_thread_apply_affinity(const bool * mask) {
1877918795
err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
1878018796
#endif
1878118797
if (err != 0) {
18782-
//fprintf(stderr, "warn: failed to set affinity mask 0x%llx (err %d: %s)\n", (unsigned long long)mask, err, strerror(err));
18798+
fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err);
1878318799
return false;
1878418800
}
1878518801

1878618802
return true;
1878718803
}
1878818804

18789-
static bool ggml_thread_apply_process_priority(int32_t prio) {
18805+
static bool ggml_thread_apply_thread_priority(int32_t prio) {
1879018806
struct sched_param p;
1879118807
int32_t policy = SCHED_OTHER;
18792-
1879318808
switch (prio) {
1879418809
case SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
1879518810
case SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
1879618811
case SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
1879718812
case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
1879818813
}
1879918814

18800-
int32_t err = sched_setscheduler(0, policy, &p);
18801-
if (err != 0) {
18802-
//fprintf(stderr, "warn: failed to set process priority %d (err %d)\n", prio, err);
18803-
return false;
18804-
}
18805-
18806-
return true;
18807-
}
18808-
18809-
static bool ggml_thread_apply_thread_priority(int32_t prio) {
18810-
struct sched_param p;
18811-
int32_t policy = SCHED_OTHER;
18812-
switch (prio) {
18813-
case SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
18814-
case SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
18815-
case SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
18816-
case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
18815+
if (prio == SCHED_PRIO_NORMAL) {
18816+
// Keep inherited policy/priority
18817+
return true;
1881718818
}
1881818819

1881918820
int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
1882018821
if (err != 0) {
18821-
//fprintf(stderr, "warn: failed to set thread priority %d (err %d)\n", prio, err);
18822+
fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
1882218823
return false;
1882318824
}
1882418825

@@ -19380,7 +19381,6 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
1938019381
}
1938119382
#else
1938219383
// Update main thread prio and affinity to match the current threadpool
19383-
ggml_thread_apply_process_priority(threadpool->prio);
1938419384
ggml_thread_apply_thread_priority(threadpool->prio);
1938519385
if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
1938619386
ggml_thread_apply_affinity(threadpool->workers[0].cpumask);

0 commit comments

Comments
 (0)