Skip to content

Commit

Permalink
Add JL_THREADPOOL_ID_*, fix missing a thread when setting affinities
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Schweigel committed Jan 24, 2025
1 parent 57a7ee5 commit e8a391a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,8 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_
jl_n_markthreads = 0;
jl_n_sweepthreads = 0;
jl_n_gcthreads = 0;
jl_n_threads_per_pool[0] = 0; // Interactive threadpool
jl_n_threads_per_pool[1] = 1; // Default threadpool
jl_n_threads_per_pool[JL_THREADPOOL_ID_INTERACTIVE] = 0;
jl_n_threads_per_pool[JL_THREADPOOL_ID_DEFAULT] = 1;
} else {
post_image_load_hooks();
}
Expand Down
3 changes: 3 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,9 @@ extern JL_DLLIMPORT _Atomic(int) jl_n_threads;
extern JL_DLLIMPORT int jl_n_gcthreads;
extern int jl_n_markthreads;
extern int jl_n_sweepthreads;

#define JL_THREADPOOL_ID_INTERACTIVE 0
#define JL_THREADPOOL_ID_DEFAULT 1
extern JL_DLLIMPORT int *jl_n_threads_per_pool;

// environment entries
Expand Down
31 changes: 18 additions & 13 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,9 @@ void jl_init_threading(void)
}

jl_all_tls_states_size = nthreads + nthreadsi + ngcthreads;
jl_n_threads_per_pool = (int*)malloc_s(2 * sizeof(int));
jl_n_threads_per_pool[0] = nthreadsi;
jl_n_threads_per_pool[1] = nthreads;
jl_n_threads_per_pool = (int*)malloc_s(jl_n_threadpools * sizeof(int));
jl_n_threads_per_pool[JL_THREADPOOL_ID_INTERACTIVE] = nthreadsi;
jl_n_threads_per_pool[JL_THREADPOOL_ID_DEFAULT] = nthreads;
assert(jl_all_tls_states_size > 0);
jl_atomic_store_release(&jl_all_tls_states, (jl_ptls_t*)calloc(jl_all_tls_states_size, sizeof(jl_ptls_t)));
jl_atomic_store_release(&jl_n_threads, jl_all_tls_states_size);
Expand All @@ -793,9 +793,9 @@ uv_barrier_t thread_init_done;
void jl_start_threads(void)
{
int nthreads = jl_atomic_load_relaxed(&jl_n_threads);
int ngcthreads = jl_n_gcthreads;
int nthreadsi = jl_n_threads_per_pool[0];
int nmutator_threads = nthreads - ngcthreads;
int ninteractive_threads = jl_n_threads_per_pool[JL_THREADPOOL_ID_INTERACTIVE];
int ndefault_threads = jl_n_threads_per_pool[JL_THREADPOOL_ID_DEFAULT];
int nmutator_threads = nthreads - jl_n_gcthreads;

int cpumasksize = uv_cpumask_size();
char *cp;
Expand All @@ -811,17 +811,19 @@ void jl_start_threads(void)
if (cp && strcmp(cp, "0") != 0)
exclusive = 1;

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

if (nthreadsi == 0) {
// If there are no interactive threads, the master thread is in the
// default pool and we must affinitize it
if (ninteractive_threads == 0) {
mask[0] = 1;
uvtid = uv_thread_self();
uv_thread_setaffinity(&uvtid, mask, NULL, cpumasksize);
Expand All @@ -838,10 +840,13 @@ void jl_start_threads(void)
t->tid = i;
t->barrier = &thread_init_done;
uv_thread_create(&uvtid, jl_threadfun, t);
if (exclusive && i > nthreadsi) {
mask[i - nthreadsi] = 1;

// Interactive pool threads get the low IDs, so check if this is a
// default pool thread. The master thread is already on CPU 0.
if (exclusive && i >= ninteractive_threads) {
mask[i - ninteractive_threads] = 1;
uv_thread_setaffinity(&uvtid, mask, NULL, cpumasksize);
mask[i - nthreadsi] = 0;
mask[i - ninteractive_threads] = 0;
}
uv_thread_detach(&uvtid);
}
Expand Down

0 comments on commit e8a391a

Please sign in to comment.