Skip to content

Let command line --threads-per-process take precedence over OMP_NUM_THREADS - #7487

Open
blattms wants to merge 1 commit into
OPM:masterfrom
blattms:feature/precedence-for-ThreadsPerProcess
Open

blattms wants to merge 1 commit into
OPM:masterfrom
blattms:feature/precedence-for-ThreadsPerProcess

Conversation

@blattms

@blattms blattms commented Oct 2, 2026

Copy link
Copy Markdown
Member

If OpenMP is available, the value of command line --threads-per-process will determine how many threads will be used. The default number is 2 if command line argument is not used. To let OpenMP determine how many threads will be used use value -1 for the command line argument. In this case either the same number of threads as logical CPUs will be used if the environment variable OMP_NUM_THREADS is not set. If it is set then that number will be used.

Previously the values set in the environment variable OPM_NUM_THREADS always took precedence

…HREADS

If openMP is available, the value of command line --threads-per-process
will determine how many threads will be used. The default number is 2 if
command line argument is not used. To let OpenMP determine how many threads
will be used use value -1 for the command line argument. In this case either
the same number of threads as logical CPUs will be used if the environment
variable OMP_NUM_THREADS is not set. If it is set then that number will be
used.
@blattms blattms added the manual:new-feature This is a new feature and should be described in the manual label Oct 2, 2026
@blattms

blattms commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

jenkins build this please

constexpr int default_threads = 2;
const bool isSet = Parameters::IsSet<Parameters::ThreadsPerProcess>();
const int requested_threads = Parameters::Get<Parameters::ThreadsPerProcess>();
int threads = requested_threads > 0 ? requested_threads : default_threads;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that setting --threads-per-process=-1 did nothing in the current master, good to fix that!

However, on line 350, the variable threads is used. Should that be requested_threads? Reason: If we pass -1 on the command line and the environment variable is 1 we get a warning, because threads is 2, but the code will run without calling omp_set_num_threads() at all, which means (probably?) that the environment variable value will be used (=1).

threads = omp_num_threads;
if (isSet) {
OpmLog::warning("Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument.");
if (isSet && threads > omp_num_threads) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One could also argue that this warning is not important and the entire reading of the variable can be deleted. Not sure though!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some discussion, I have changed my mind: It is important. However it may be misleading as worded now. I have assumed that an explicit call to omp_set_num_threads() wins over the enviroment variable, however I have now learned that OMP_NUM_THREADS is used to set the size of the thread pool at program start. Therefore there is no way the program will actually use a larger number of threads than that, no matter what we pass to omp_set_num_threads(). So the warning should probably not say "Using specified value anyway", but "OMP_NUM_THREADS sets the maximum number of threads, so the specified value is ignored."

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

manual:new-feature This is a new feature and should be described in the manual

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants