Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uv_thread_getaffinity when --threads=auto #42340

Merged
merged 27 commits into from
Feb 19, 2022
Merged
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4a8c738
Use uv_thread_getaffinity to bound nthreads
tkf Sep 21, 2021
4309748
Add some safepoint annotations
tkf Sep 22, 2021
d2df381
Use ternary expression
tkf Sep 23, 2021
eaf1908
Ignore EBADF on Windows
tkf Sep 23, 2021
b6ba167
Merge branch 'master' into useaffinity
tkf Dec 15, 2021
68bc481
Revert "Ignore EBADF on Windows"
tkf Dec 15, 2021
1221ea3
Merge branch 'master' into useaffinity
tkf Dec 16, 2021
2d22485
Merge branch 'master' into useaffinity
tkf Jan 13, 2022
c9b3ca5
Add jl_effective_threads
tkf Jan 13, 2022
dcd12d6
Merge branch 'master' into useaffinity
tkf Jan 16, 2022
a1572a2
Test affinity-based nthreads setup
tkf Jan 16, 2022
c2d3f9b
Tweak testing
tkf Jan 17, 2022
e64ff3a
Compare output without affinity setting
tkf Jan 17, 2022
81b1d66
DEBUG: print input/output of get_nthreads
tkf Jan 17, 2022
fc655aa
Merge branch 'master' into useaffinity
tkf Jan 17, 2022
4f65152
Document how `--threads=auto` now works
tkf Jan 17, 2022
c3039fc
Revert "DEBUG: print input/output of get_nthreads"
tkf Jan 17, 2022
f7252ec
Don't run get_nthreads test under rr
tkf Jan 17, 2022
140654c
Better error handling
tkf Jan 26, 2022
8c33dc1
Check if running under rr
tkf Jan 28, 2022
2375873
Mention -t=auto behavior elsewhere
tkf Jan 28, 2022
85cba54
Mention it in NEWS
tkf Jan 28, 2022
c37c34c
Merge branch 'master' into useaffinity
tkf Jan 28, 2022
cdd8223
Merge branch 'master' into useaffinity
tkf Jan 29, 2022
b263a8d
Check affinity support in one place
tkf Jan 30, 2022
ae74887
Merge branch 'master' into useaffinity
tkf Feb 17, 2022
3e2b70f
Merge branch 'master' into useaffinity
tkf Feb 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,30 @@ typedef DWORD (WINAPI *GAPC)(WORD);
#endif
#endif

static int num_threads_bound(void) JL_NOTSAFEPOINT
{
#ifdef _OS_DARWIN_
return INT_MAX;
#else
int masksize = uv_cpumask_size();
uv_thread_t tid = uv_thread_self();
char *cpumask = (char *)calloc(masksize, sizeof(char));
int err = uv_thread_getaffinity(&tid, cpumask, masksize);
if (err) {
free(cpumask);
jl_safe_printf("WARNING: failed to get thread affinity (%s %d)\n", uv_err_name(err),
err);
return INT_MAX;
}
int n = 0;
for (size_t i = 0; i < masksize; i++) {
n += cpumask[i];
}
free(cpumask);
return n;
#endif
}

// Apple's M1 processor is a big.LITTLE style processor, with 4x "performance"
// cores, and 4x "efficiency" cores. Because Julia expects to be able to run
// things like heavy linear algebra workloads on all cores, it's best for us
Expand All @@ -608,6 +632,7 @@ typedef DWORD (WINAPI *GAPC)(WORD);

JL_DLLEXPORT int jl_cpu_threads(void) JL_NOTSAFEPOINT
{
int upper_bound = num_threads_bound();
#if defined(HW_AVAILCPU) && defined(HW_NCPU)
size_t len = 4;
int32_t count;
Expand All @@ -631,22 +656,24 @@ JL_DLLEXPORT int jl_cpu_threads(void) JL_NOTSAFEPOINT
}
}
#endif
return count;
return upper_bound < count ? upper_bound : count;
#elif defined(_SC_NPROCESSORS_ONLN)
long count = sysconf(_SC_NPROCESSORS_ONLN);
if (count < 1)
return 1;
return count;
return upper_bound < count ? upper_bound : count;
#elif defined(_OS_WINDOWS_)
//Try to get WIN7 API method
GAPC gapc;
if (jl_dlsym(jl_kernel32_handle, "GetActiveProcessorCount", (void **)&gapc, 0)) {
return gapc(ALL_PROCESSOR_GROUPS);
DWORD count = gapc(ALL_PROCESSOR_GROUPS);
return upper_bound < count ? upper_bound : count;
}
else { //fall back on GetSystemInfo
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
DWORD count = info.dwNumberOfProcessors;
return upper_bound < count ? upper_bound : count;
}
#else
#warning "cpu core detection not defined for this platform"
Expand Down