fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.1k stars 171 forks source link

`optval` issue in `stdlib_random` module #592

Open zoziha opened 2 years ago

zoziha commented 2 years ago

Description

During a development, I found an unexpected situation: swapping optval(s, si) in the stdlib_random for if (present(s)) then .. form, and unit tests of macos and ubuntu systems failed.

image (see https://github.com/zoziha/stdlib/commits/update_random)

Possible Problem https://github.com/fortran-lang/stdlib/blob/100f76098c834df29a800b3dcb1a0a59f295ae12/src/stdlib_random.fypp#L123 Maybe:

res = res + int01 !?

cc @Jim-215-Fisher

Expected Behaviour

CI failed: https://github.com/zoziha/stdlib/actions/runs/1554566528

Expected behaviour: optval == if (present(..)) then ..

Version of stdlib

100f76098c834df29a800b3dcb1a0a59f295ae12

Platform and Architecture

MacOS, GFortran 9 10 11; Ubuntu, GFortran 10

Additional Information

No response

zoziha commented 2 years ago

The reason I modified the stdlib_random was because I planned to generate a downstream stdlib with only real64 and int32, so that I could use lightweight stdlib (see dp-stdlib) within fpm projects.

gareth-nx commented 2 years ago

@zoziha For what it's worth I've previously run into compiler problems at this part of the code (when using inter-procedural optimization). Although it would be good to hear from @Jim-215-Fisher, to my knowledge the line si = res + int01 is not a coding error -- the intention is indeed to update the random number generator state.

gareth-nx commented 2 years ago

To give a bit more context - if I compile stdlib with:

make -B -f Makefile.manual ADD_FYPPFLAGS=-DMAXRANK=4 FC=gfortran FFLAGS="-O3 -flto"

then I get a segfault on the test program in stdlib/src/tests/stats/test_random, which I believe is also related to this part of the code (it manifests in repeated calls to splitmix64).

But if I remove the link-time-optimization then it works OK, i.e. using

make -B -f Makefile.manual ADD_FYPPFLAGS=-DMAXRANK=4 FC=gfortran FFLAGS="-O3"

the same test passes.

iajzenszmi commented 2 years ago

The following may be relevant. concerning linkage time optimization causing segmentation fault https://stackoverflow.com/questions/68723476/why-does-link-time-optimization-cause-a-segmentation-fault

iajzenszmi commented 2 years ago

Link time optimization causing segmentation faults from a scholars context. https://scholar.google.com.au/scholar?hl=en&as_sdt=0%2C5&q=link+time+optimization+causes+segmentation+fault&btnG=

iajzenszmi commented 2 years ago

Undefined behaviours in computer programming, a scholarly reading list and bibliography, because undefined behaviors lead to segmentation faults. https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=undefined+behaviour+in+computer+programs&btnG=

iajzenszmi commented 2 years ago

Detecting undefined behavious in computer programming a scholarly bibliography and reading list, because undefined behaviours can lead to segmentation faults. https://scholar.google.com.au/scholar?hl=en&as_sdt=0%2C5&q=detecting+undefined+behaviour+in+computer+programs&btnG=