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

Read CPU mask of main thread early to avoid reading it after it's been reset by other libraries #738

Merged
merged 3 commits into from
Aug 15, 2023

Conversation

msimberg
Copy link
Contributor

@msimberg msimberg commented Aug 14, 2023

Part of #568. This is part one that helps work around problems when using pika together with OpenMP. This makes topology store the current CPU mask on construction that can then be retrieved using get_cpubind_mask_main_thread. This also makes sure that the topology object is constructed early in a global constructor so that the mask is actually read on the main thread.

This change helps if OpenMP sets the binding of the main thread when it first encounters a parallel region. LLVM OpenMP seems to behave this way. However, GNU OpenMP actually sets the binding in a global constructor (https://github.com/gcc-mirror/gcc/blob/7879f589af911ea6a910d08919014b0b2df1b4b1/libgomp/env.c#L2409), and since it's called in a shared library we can't directly influence when that happens I will open a follow-up PR to also allow overriding the mask explicitly for the cases when GNU OpenMP is used (most cases...). One can in theory control the order in which constructors in shared libraries are called by playing around with linking order, but this seems like a very error-prone approach so I'm not even going to try to go that way.

Fly-by: I've renamed create_topology to get_topology since that's what it's primarily used for (it does also create the object, but only on the first call...).

@msimberg msimberg added this to the 0.18.0 milestone Aug 14, 2023
@msimberg msimberg self-assigned this Aug 14, 2023
@msimberg msimberg changed the title Get cpu mask early Read CPU mask of main thread early to avoid reading it after it's been reset by other libraries Aug 14, 2023
@msimberg
Copy link
Contributor Author

bors try

bors bot added a commit that referenced this pull request Aug 14, 2023
@bors
Copy link
Contributor

bors bot commented Aug 14, 2023

try

Build failed:

@msimberg msimberg marked this pull request as ready for review August 14, 2023 14:02
@msimberg
Copy link
Contributor Author

bors try

bors bot added a commit that referenced this pull request Aug 14, 2023
@bors
Copy link
Contributor

bors bot commented Aug 14, 2023

try

Build failed:

@msimberg msimberg force-pushed the get-cpu-mask-early branch from 4633bb2 to 7426fad Compare August 15, 2023 12:56
@msimberg
Copy link
Contributor Author

bors try

@msimberg
Copy link
Contributor Author

bors merge

bors bot added a commit that referenced this pull request Aug 15, 2023
738: Read CPU mask of main thread early to avoid reading it after it's been reset by other libraries r=msimberg a=msimberg

Part of #568. This is part one that helps work around problems when using pika together with OpenMP. This makes `topology` store the current CPU mask on construction that can then be retrieved using `get_cpubind_mask_main_thread`. This also makes sure that the `topology` object is constructed early in a global constructor so that the mask is actually read on the main thread.

This change helps if OpenMP sets the binding of the main thread when it first encounters a parallel region. LLVM OpenMP seems to behave this way. However, GNU OpenMP actually sets the binding in a global constructor (https://github.com/gcc-mirror/gcc/blob/7879f589af911ea6a910d08919014b0b2df1b4b1/libgomp/env.c#L2409), and since it's called in a shared library we can't directly influence when that happens I will open a follow-up PR to also allow overriding the mask explicitly for the cases when GNU OpenMP is used (most cases...). One can in theory control the order in which constructors in shared libraries are called by playing around with linking order, but this seems like a very error-prone approach so I'm not even going to try to go that way.

Fly-by: I've renamed `create_topology` to `get_topology` since that's what it's primarily used for (it does also create the object, but only on the first call...).

Co-authored-by: Mikael Simberg <mikael.simberg@iki.fi>
@bors
Copy link
Contributor

bors bot commented Aug 15, 2023

Build failed:

@msimberg
Copy link
Contributor Author

bors merge

@bors
Copy link
Contributor

bors bot commented Aug 15, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 698f30d into pika-org:main Aug 15, 2023
@msimberg msimberg deleted the get-cpu-mask-early branch August 15, 2023 15:06
@bors
Copy link
Contributor

bors bot commented Aug 16, 2023

try

Merge conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants