This post is related this “Equivalent of str function (from python) in Fortran”, but I prefer to open a new post.
For a large code, I want to use a function equivalent to « TO_string » to convert an integer or a real to a character string to create some log output. There are several implementations, I’ve got one that is similar to those in the Fortran stdlib or other libraries (fstring or M_msg). In all these procedures, the integer (except for Fortran stdlib) or the real are written in a character string with the intrinsic « write ». These work fine.
However, when I’m using it with openmp with gfortran, I’ve got unexpected behavior. Some characters get mixed up or missing.
When, I’m using an opemmp critical zone enclosing the character string set up, the problem is solved. However, I don’t want to use a critical zone.
The following code (see below) is a minimal example showing this behavior. It has three tests:
- TO_string extract from QDUtilLib using OpenMP without critical zone
- TO_string extract from stdlib using OpenMP without critical zone
- TO_string extract from QDUtilLib using OpenMP with critical zone
The first two tests give unexpected behavior with gfortran (v10 and v12 on linux, v13 on macOS). The third test works as expected with gfortran (see below the output file res.txt)
The three tests work fine with ifx (v2023 on linux)
Concerning, the gfortran problem, I thought, it came from the writing in a character string. However, the stdlinb TO_string procedure for integer does not use this. So, it seems this problem is more general and related to the character manipulation.
Probably, it is a gfortran bug.
Do you know how to overcome this problem?
Fortran code:
teststring_OMP.f90 (4.2 KB)
output (gfortran v13, macOS)
res.txt (4.1 KB)