I’m running the following basic parallelized Hello World program in VS Code:
program Hello
Use omp_lib
Implicit None
INTEGER :: nthreads
nthreads = 4
CALL OMP_SET_NUM_THREADS(nthreads)
write(*,*) omp_get_num_procs()
write(*,*) omp_get_max_threads()
write(*,*) omp_get_num_threads()
!$OMP PARALLEL
PRINT *, "Hello from process: ", OMP_GET_THREAD_NUM()
!$OMP END PARALLEL
end program Hello
This correctly prints out “Hello from process: X” 4 times, but then the program doesn’t exit. Is this expected behavior? I haven’t found any posts online that address this.
No, this is not what I would expect. It seems that the call to OMP_SET_NUM_THREADS does not work properly. If I leave it out, the program does finish (on my laptop it uses 24 cores by default). This is the behaviour with gfortran. If I use ifx, thne the original program stops nicely.
One curious thing: with both compilers (and with or without the call to OMP_SET_NUM_THREADS) I get the value 1 for OMP_GET_NUM_THREADS. Odd.
With gfortran 13.2 under the MSYS2 environment on Windows, this program correctly exits. I suspect some bad interaction (?) between gfortran and VS code.
[quote=“hirschbergerm, post:5, topic:9413”]
Actually I just tested Arjen’s idea of removing OMP_SET_NUM_THREADS() and the program finished executing.
Why would this be the case. It’s not urgent right now, but in the future I think that’s a variable I’d like to be able to control.[/quote]
For performance reasons (I think), it’s generally recommended to set the number of threads using the OMP_NUM_THREADS environment variable once for all rather than using the omp_set_num_threads() routine. That said, it shoud be work… Did you try using the NUM_THREADS() clause instead of the routine? !$OMP PARALLEL NUM_THREADS(nthreads)
The GNU Fortran runtime library uses various C library functions that depend on the locale, such as strtod and snprintf . In order to work correctly in locale-aware programs that set the locale using setlocale , the locale is reset to the default “C” locale while executing a formatted READ or WRITE statement. On targets supporting the POSIX 2008 per-thread locale functions (e.g. newlocale , uselocale , freelocale ), these are used and thus the global locale set using setlocale or the per-thread locales in other threads are not affected. However, on targets lacking this functionality, the global LC_NUMERIC locale is set to “C” during the formatted I/O. Thus, on such targets it’s not safe to call setlocale concurrently from another thread while a Fortran formatted I/O operation is in progress. Also, other threads doing something dependent on the LC_NUMERIC locale might not work correctly if a formatted I/O operation is in progress in another thread.
This problem with using CALL OMP_SET_NUM_THREADS(nthreads) was introduced after Gfortran ver 11.1.0 on the Windows version supplied by equation.com
implementation. I have not been able to get a response to reporting this issue.
I have not tested other sources of Windows versions of Gfortran, since 11.1.0.
As noted, using the NUM_THREADS clause fixed the issue, but this requires a rework of all previous codes.
I have previously noted this issue, including at the end of: