ecmwf / ecbuild

A CMake-based build system, consisting of a collection of CMake macros and functions that ease the managing of software build systems
https://ecbuild.readthedocs.io
Apache License 2.0
26 stars 25 forks source link

Proper method for grabbing OMP_NUM_THREADS from environment #37

Closed BenjaminTJohnson closed 3 years ago

BenjaminTJohnson commented 3 years ago

When running ctest it appears that the tests added using ecbuild_add_test do not automatically grab the OMP_NUM_THREADS from the environment (e.g., bash).

(1) Is there a way to pick up the environment variable at runtime? (2) If not, is setting OMP keyword inside of ecbuild_add_test and using $ENV{OMP_NUM_THREADS} the recommended approach, or do I need to (also?) use the ENVIRONMENT keyword?

Thanks for any advice / commentary.

tlmquintino commented 3 years ago

Hi. Yes, indeed you need to use the ENVIRONMENT keyword of the ecbuild_add_test function.

ENVIRONMENT OMP_NUM_THREADS=$ENV{OMP_NUM_THREADS}

or

ENVIRONMENT OMP_NUM_THREADS=${cmake_variable_with_num_threads}

BenjaminTJohnson commented 3 years ago

@tlmquintino Thanks, much appreciated.

wdeconinck commented 3 years ago

@BenjaminTJohnson , I would like to add to the response.

ecbuild_add_test() explicitly sets OMP_NUM_THREADS=1 in the environment by default. This is to be independent of the user environment or platform environment. If you want something different, you need to modify the argument "OMP". If you like to test with e.g. 4 OpenMP threads:

ecbuild_add_test( ...  OMP 4 )

This will then set OMP_NUM_THREADS=4 in the environment. This will then always be deterministic for your tests.

If you use

ecbuild_add_test( ...  OMP $ENV{OMP_NUM_THREADS} )

then you pick up the number of OMP threads at configuration time, not run-time. The problem is also that you are not deterministic anymore.

If you want to run the test executable with a custom OMP_NUM_THREADS, e.g. in development, I suggest to run the test program manually without ctest.

You can also take following approach if you want to test the same program with different OMP_NUM_THREADS:


ecbuild_add_executable( TARGET mytest_program NOINSTALL  ... )   # executable will be in the test directory, not bin
ecbuild_add_test( TARGET mytest_omp2 COMMAND mytest_program OMP 2 )
ecbuild_add_test( TARGET mytest_omp4 COMMAND mytest_program OMP 4 )