public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset
@ 2024-04-18 11:31 stijn.deweirdt at ugent dot be
  2024-04-18 11:38 ` [Bug libgomp/114765] " jakub at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: stijn.deweirdt at ugent dot be @ 2024-04-18 11:31 UTC (permalink / raw)
  To: gcc-bugs

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 "1000000" and "111111" 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 <assert.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void print_affinity() {
    cpu_set_t mask;
    long nproc, i;

    if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) == -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, &mask));
    }
    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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libgomp/114765] linking to libgomp and setting CPU_PROC_BIND causes affinity reset
  2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
@ 2024-04-18 11:38 ` jakub at gcc dot gnu.org
  2024-04-18 11:58 ` stijn.deweirdt at ugent dot be
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-18 11:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
That is how it should behave.  Don't use OMP_PROC_BIND=true unless that is what
you want...

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libgomp/114765] linking to libgomp and setting CPU_PROC_BIND causes affinity reset
  2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
  2024-04-18 11:38 ` [Bug libgomp/114765] " jakub at gcc dot gnu.org
@ 2024-04-18 11:58 ` stijn.deweirdt at ugent dot be
  2024-04-18 12:02 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: stijn.deweirdt at ugent dot be @ 2024-04-18 11:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765

--- Comment #2 from Stijn De Weirdt <stijn.deweirdt at ugent dot be> ---
hi jakub,

apologies for my ignorance, but how is working as expected? i can't make this
up from the description of the variable; and it's unexpected since there is no
openmp code being run (naively then; clearly libgomp does something).

we have an environment where we set OMP_PROC_BIND=true because openmp code
clearly benefits from it; but it also affects code where libgomp is simply
linked to it, but nothing else is done with it.

if i would understand why or what libgomp does, i might be able to find some
workaround.

many thanks,

stijn

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libgomp/114765] linking to libgomp and setting CPU_PROC_BIND causes affinity reset
  2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
  2024-04-18 11:38 ` [Bug libgomp/114765] " jakub at gcc dot gnu.org
  2024-04-18 11:58 ` stijn.deweirdt at ugent dot be
@ 2024-04-18 12:02 ` jakub at gcc dot gnu.org
  2024-04-18 12:19 ` stijn.deweirdt at ugent dot be
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-18 12:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Stijn De Weirdt from comment #2)
> apologies for my ignorance, but how is working as expected? i can't make
> this up from the description of the variable; and it's unexpected since
> there is no openmp code being run (naively then; clearly libgomp does
> something).

libgomp has library constructors which already do something.

> we have an environment where we set OMP_PROC_BIND=true because openmp code
> clearly benefits from it; but it also affects code where libgomp is simply
> linked to it, but nothing else is done with it.

See the OpenMP standard.  By using that env variable (or similar), you request
that the initial thread binds to a certain CPU/thread (the OMP_PROC_BIND=true
case is less specified than when using say OMP_PLACES and exact rules which
thread binds to which place).

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libgomp/114765] linking to libgomp and setting CPU_PROC_BIND causes affinity reset
  2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
                   ` (2 preceding siblings ...)
  2024-04-18 12:02 ` jakub at gcc dot gnu.org
@ 2024-04-18 12:19 ` stijn.deweirdt at ugent dot be
  2024-04-18 14:01 ` amonakov at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: stijn.deweirdt at ugent dot be @ 2024-04-18 12:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765

--- Comment #4 from Stijn De Weirdt <stijn.deweirdt at ugent dot be> ---
hi jakub,


thanks for the explanation. so if i understand this, the main thread/process of
any binary linked with libgomp becomes an OpenMP thread, because of the libgomp
constructor doing something. and thus the OMP_PROC_BIND works as intended.

and what happens with other threads created in the code in such a case? these
are not openmp threads? or those are openmp threads, but related to the
"nesting"/"active levels"?

and can we "delay" the OMP_PROC_BIND safely (eg start the binary without
OMP_PROC_BIND, and "enable" it in the main; i am more thinking in python code
here: so after the import, we set the enviroment with OMP_PROC_BIND=true). will
this cause pinning for openmp threads started afterwards?

anyway, apologies again for my confusion and lack of detailed understanding. i
still find it a bit unexpected ;)

stijn

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libgomp/114765] linking to libgomp and setting CPU_PROC_BIND causes affinity reset
  2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
                   ` (3 preceding siblings ...)
  2024-04-18 12:19 ` stijn.deweirdt at ugent dot be
@ 2024-04-18 14:01 ` amonakov at gcc dot gnu.org
  2024-04-18 14:22 ` jakub at gcc dot gnu.org
  2024-04-18 17:00 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: amonakov at gcc dot gnu.org @ 2024-04-18 14:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amonakov at gcc dot gnu.org

--- Comment #5 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
Can libgomp defer changing affinity of the initial thread to the launch of the
first parallel region (i.e. change it only at thread pool initialization,
together with new threads)?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libgomp/114765] linking to libgomp and setting CPU_PROC_BIND causes affinity reset
  2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
                   ` (4 preceding siblings ...)
  2024-04-18 14:01 ` amonakov at gcc dot gnu.org
@ 2024-04-18 14:22 ` jakub at gcc dot gnu.org
  2024-04-18 17:00 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-18 14:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Alexander Monakov from comment #5)
> Can libgomp defer changing affinity of the initial thread to the launch of
> the first parallel region (i.e. change it only at thread pool
> initialization, together with new threads)?

The creation of a parallel region is very performance sensitive area, so
slowing it down is undesirable.  But more importantly, if the initial thread
switches binding only at that point, it will mean memory allocated by the
initial thread might no longer be close to that thread.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libgomp/114765] linking to libgomp and setting CPU_PROC_BIND causes affinity reset
  2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
                   ` (5 preceding siblings ...)
  2024-04-18 14:22 ` jakub at gcc dot gnu.org
@ 2024-04-18 17:00 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-18 17:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114765

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=113698
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Invalid as explained. Also see PR 113698 .

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-04-18 17:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18 11:31 [Bug libgomp/114765] New: linking to libgomp and setting CPU_PROC_BIND causes affinity reset stijn.deweirdt at ugent dot be
2024-04-18 11:38 ` [Bug libgomp/114765] " jakub at gcc dot gnu.org
2024-04-18 11:58 ` stijn.deweirdt at ugent dot be
2024-04-18 12:02 ` jakub at gcc dot gnu.org
2024-04-18 12:19 ` stijn.deweirdt at ugent dot be
2024-04-18 14:01 ` amonakov at gcc dot gnu.org
2024-04-18 14:22 ` jakub at gcc dot gnu.org
2024-04-18 17:00 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).