Skip to content

Commit a71cf7c

Browse files
committed
Add JL_THREADPOOL_ID_*, fix missing a thread when setting affinities
1 parent 8bb6b8f commit a71cf7c

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,8 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_
902902
jl_n_markthreads = 0;
903903
jl_n_sweepthreads = 0;
904904
jl_n_gcthreads = 0;
905-
jl_n_threads_per_pool[0] = 0; // Interactive threadpool
906-
jl_n_threads_per_pool[1] = 1; // Default threadpool
905+
jl_n_threads_per_pool[JL_THREADPOOL_ID_INTERACTIVE] = 0;
906+
jl_n_threads_per_pool[JL_THREADPOOL_ID_DEFAULT] = 1;
907907
} else {
908908
post_image_load_hooks();
909909
}

src/julia.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,9 @@ extern JL_DLLIMPORT _Atomic(int) jl_n_threads;
20582058
extern JL_DLLIMPORT int jl_n_gcthreads;
20592059
extern int jl_n_markthreads;
20602060
extern int jl_n_sweepthreads;
2061+
2062+
#define JL_THREADPOOL_ID_INTERACTIVE 0
2063+
#define JL_THREADPOOL_ID_DEFAULT 1
20612064
extern JL_DLLIMPORT int *jl_n_threads_per_pool;
20622065

20632066
// environment entries

src/threading.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,9 @@ void jl_init_threading(void)
778778
}
779779

780780
jl_all_tls_states_size = nthreads + nthreadsi + ngcthreads;
781-
jl_n_threads_per_pool = (int*)malloc_s(2 * sizeof(int));
782-
jl_n_threads_per_pool[0] = nthreadsi;
783-
jl_n_threads_per_pool[1] = nthreads;
781+
jl_n_threads_per_pool = (int*)malloc_s(jl_n_threadpools * sizeof(int));
782+
jl_n_threads_per_pool[JL_THREADPOOL_ID_INTERACTIVE] = nthreadsi;
783+
jl_n_threads_per_pool[JL_THREADPOOL_ID_DEFAULT] = nthreads;
784784
assert(jl_all_tls_states_size > 0);
785785
jl_atomic_store_release(&jl_all_tls_states, (jl_ptls_t*)calloc(jl_all_tls_states_size, sizeof(jl_ptls_t)));
786786
jl_atomic_store_release(&jl_n_threads, jl_all_tls_states_size);
@@ -793,9 +793,9 @@ uv_barrier_t thread_init_done;
793793
void jl_start_threads(void)
794794
{
795795
int nthreads = jl_atomic_load_relaxed(&jl_n_threads);
796-
int ngcthreads = jl_n_gcthreads;
797-
int nthreadsi = jl_n_threads_per_pool[0];
798-
int nmutator_threads = nthreads - ngcthreads;
796+
int ninteractive_threads = jl_n_threads_per_pool[JL_THREADPOOL_ID_INTERACTIVE];
797+
int ndefault_threads = jl_n_threads_per_pool[JL_THREADPOOL_ID_DEFAULT];
798+
int nmutator_threads = nthreads - jl_n_gcthreads;
799799

800800
int cpumasksize = uv_cpumask_size();
801801
char *cp;
@@ -811,17 +811,19 @@ void jl_start_threads(void)
811811
if (cp && strcmp(cp, "0") != 0)
812812
exclusive = 1;
813813

814-
// exclusive use: affinitize threads, master thread on proc 0, rest
815-
// according to a 'compact' policy
814+
// exclusive use: affinitize threads, master thread on proc 0, threads in
815+
// default pool according to a 'compact' policy
816816
// non-exclusive: no affinity settings; let the kernel move threads about
817817
if (exclusive) {
818-
if (nmutator_threads - nthreadsi > jl_cpu_threads()) {
818+
if (ndefault_threads > jl_cpu_threads()) {
819819
jl_printf(JL_STDERR, "ERROR: Too many threads requested for %s option.\n", MACHINE_EXCLUSIVE_NAME);
820820
exit(1);
821821
}
822822
memset(mask, 0, cpumasksize);
823823

824-
if (nthreadsi == 0) {
824+
// If there are no interactive threads, the master thread is in the
825+
// default pool and we must affinitize it
826+
if (ninteractive_threads == 0) {
825827
mask[0] = 1;
826828
uvtid = uv_thread_self();
827829
uv_thread_setaffinity(&uvtid, mask, NULL, cpumasksize);
@@ -838,10 +840,13 @@ void jl_start_threads(void)
838840
t->tid = i;
839841
t->barrier = &thread_init_done;
840842
uv_thread_create(&uvtid, jl_threadfun, t);
841-
if (exclusive && i > nthreadsi) {
842-
mask[i - nthreadsi] = 1;
843+
844+
// Interactive pool threads get the low IDs, so check if this is a
845+
// default pool thread. The master thread is already on CPU 0.
846+
if (exclusive && i >= ninteractive_threads) {
847+
mask[i - ninteractive_threads] = 1;
843848
uv_thread_setaffinity(&uvtid, mask, NULL, cpumasksize);
844-
mask[i - nthreadsi] = 0;
849+
mask[i - ninteractive_threads] = 0;
845850
}
846851
uv_thread_detach(&uvtid);
847852
}

0 commit comments

Comments
 (0)