https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765
Bug ID: 114765
Summary: linking to libgomp and setting CPU_PROC_BIND causes
affinity reset
Product: gcc
Version: 13.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libgomp
Assignee: unassigned at gcc dot gnu.org
Reporter: stijn.deweirdt at ugent dot be
CC: jakub at gcc dot gnu.org
Target Milestone: ---
Compiling c code that has no openmp statements, and linking libgomp to it;
running that binary with OMP_PROC_BIND=true causes affinity "reset" to single
core. In output below see differene between "100" and "11" in reported
affinity.
(This may sound odd, but the real origin of this issue comes from some python
code that does an 'import torch' and then all proceeding multiprocessing code
was pinned to single core; (and in an environment where OMP_PROC_BIND was set
to true). in the case of torch, it was the init importing something inked to
openblas which in turn was linked to libgomp)
Code below print affinity (named ompissue.c below)
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
void print_affinity() {
cpu_set_t mask;
long nproc, i;
if (sched_getaffinity(0, sizeof(cpu_set_t), ) == -1) {
perror("sched_getaffinity");
assert(false);
}
nproc = sysconf(_SC_NPROCESSORS_ONLN);
printf("sched_getaffinity = ");
for (i = 0; i < nproc; i++) {
printf("%d ", CPU_ISSET(i, ));
}
printf("\n");
}
int main(void) {
cpu_set_t mask;
print_affinity();
}
compile and link with "gcc -l gomp ompissue.c"
and then it run:
[stdweird@w011h183 tmp]$ OMP_DISPLAY_ENV=true OMP_PROC_BIND=TRUE ./with_omp
OPENMP DISPLAY ENVIRONMENT BEGIN
_OPENMP = '201511'
[host] OMP_DYNAMIC = 'FALSE'
[host] OMP_NESTED = 'FALSE'
[host] OMP_NUM_THREADS = '1'
[host] OMP_SCHEDULE = 'DYNAMIC'
[host] OMP_PROC_BIND = 'TRUE'
[host] OMP_PLACES = '{0},{4},{1},{5},{2},{6},{3},{7}'
[host] OMP_STACKSIZE = '0'
[host] OMP_WAIT_POLICY = 'PASSIVE'
[host] OMP_THREAD_LIMIT = '4294967295'
[host] OMP_MAX_ACTIVE_LEVELS = '1'
[host] OMP_NUM_TEAMS = '0'
[host] OMP_TEAMS_THREAD_LIMIT = '0'
[all] OMP_CANCELLATION = 'FALSE'
[all] OMP_DEFAULT_DEVICE = '0'
[all] OMP_MAX_TASK_PRIORITY = '0'
[all] OMP_DISPLAY_AFFINITY = 'FALSE'
[host] OMP_AFFINITY_FORMAT = 'level %L thread %i affinity %A'
[host] OMP_ALLOCATOR = 'omp_default_mem_alloc'
[all] OMP_TARGET_OFFLOAD = 'DEFAULT'
OPENMP DISPLAY ENVIRONMENT END
sched_getaffinity = 1 0 0 0 0 0 0 0
[stdweird@w011h183 tmp]$ OMP_DISPLAY_ENV=true OMP_PROC_BIND=FALSE ./with_omp
OPENMP DISPLAY ENVIRONMENT BEGIN
_OPENMP = '201511'
[host] OMP_DYNAMIC = 'FALSE'
[host] OMP_NESTED = 'FALSE'
[host] OMP_NUM_THREADS = '1'
[host] OMP_SCHEDULE = 'DYNAMIC'
[host] OMP_PROC_BIND = 'FALSE'
[host] OMP_PLACES = ''
[host] OMP_STACKSIZE = '0'
[host] OMP_WAIT_POLICY = 'PASSIVE'
[host] OMP_THREAD_LIMIT = '4294967295'
[host] OMP_MAX_ACTIVE_LEVELS = '1'
[host] OMP_NUM_TEAMS = '0'
[host] OMP_TEAMS_THREAD_LIMIT = '0'
[all] OMP_CANCELLATION = 'FALSE'
[all] OMP_DEFAULT_DEVICE = '0'
[all] OMP_MAX_TASK_PRIORITY = '0'
[all] OMP_DISPLAY_AFFINITY = 'FALSE'
[host] OMP_AFFINITY_FORMAT = 'level %L thread %i affinity %A'
[host] OMP_ALLOCATOR = 'omp_default_mem_alloc'
[all] OMP_TARGET_OFFLOAD = 'DEFAULT'
OPENMP DISPLAY ENVIRONMENT END
sched_getaffinity = 1 1 1 1 1 1 1 1