@@ -18700,21 +18700,27 @@ static bool ggml_thread_apply_affinity(bool * mask) {
18700
18700
return m != 0;
18701
18701
}
18702
18702
18703
- static bool ggml_thread_apply_process_priority (int32_t prio) {
18703
+ static bool ggml_thread_apply_thread_priority (int32_t prio) {
18704
18704
DWORD p = NORMAL_PRIORITY_CLASS;
18705
18705
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
+
18706
18713
switch (prio) {
18707
18714
case SCHED_PRIO_NORMAL: p = NORMAL_PRIORITY_CLASS; break;
18708
18715
case SCHED_PRIO_MEDIUM: p = ABOVE_NORMAL_PRIORITY_CLASS; break;
18709
18716
case SCHED_PRIO_HIGH: p = HIGH_PRIORITY_CLASS; break;
18710
18717
case SCHED_PRIO_REALTIME: p = REALTIME_PRIORITY_CLASS; break;
18711
18718
}
18712
18719
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
+ }
18718
18724
18719
18725
switch (prio) {
18720
18726
case SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
@@ -18723,43 +18729,53 @@ static bool ggml_thread_apply_thread_priority(int32_t prio) {
18723
18729
case SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
18724
18730
}
18725
18731
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
+ }
18727
18736
18737
+ return true;
18728
18738
}
18729
18739
18730
18740
#elif defined(__APPLE__)
18731
18741
#include <sys/types.h>
18732
18742
#include <sys/resource.h>
18733
18743
18734
18744
static bool ggml_thread_apply_affinity(const bool * mask) {
18745
+ // Not supported on Apple platforms
18735
18746
UNUSED(mask);
18736
18747
return true;
18737
18748
}
18738
18749
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;
18742
18753
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;
18747
18758
}
18748
18759
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
+ }
18752
18770
18753
- static bool ggml_thread_apply_thread_priority(int32_t prio) {
18754
- UNUSED(prio);
18755
18771
return true;
18756
18772
}
18757
18773
18758
18774
#else // posix?
18759
18775
18760
18776
static bool ggml_thread_apply_affinity(const bool * mask) {
18761
18777
cpu_set_t cpuset;
18762
- int32_t err;
18778
+ int err;
18763
18779
18764
18780
CPU_ZERO(&cpuset);
18765
18781
@@ -18779,46 +18795,31 @@ static bool ggml_thread_apply_affinity(const bool * mask) {
18779
18795
err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
18780
18796
#endif
18781
18797
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 );
18783
18799
return false;
18784
18800
}
18785
18801
18786
18802
return true;
18787
18803
}
18788
18804
18789
- static bool ggml_thread_apply_process_priority (int32_t prio) {
18805
+ static bool ggml_thread_apply_thread_priority (int32_t prio) {
18790
18806
struct sched_param p;
18791
18807
int32_t policy = SCHED_OTHER;
18792
-
18793
18808
switch (prio) {
18794
18809
case SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
18795
18810
case SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
18796
18811
case SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
18797
18812
case SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
18798
18813
}
18799
18814
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;
18817
18818
}
18818
18819
18819
18820
int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
18820
18821
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);
18822
18823
return false;
18823
18824
}
18824
18825
@@ -19380,7 +19381,6 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
19380
19381
}
19381
19382
#else
19382
19383
// Update main thread prio and affinity to match the current threadpool
19383
- ggml_thread_apply_process_priority(threadpool->prio);
19384
19384
ggml_thread_apply_thread_priority(threadpool->prio);
19385
19385
if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
19386
19386
ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
0 commit comments