From: Andre Vehreschild <vehre@gmx.de>
To: GCC-Fortran-ML <fortran@gcc.gnu.org>
Cc: GCC-Patches-ML <gcc-patches@gcc.gnu.org>,
Jerry D <jvdelisle2@gmail.com>,
Steve Kargl <sgk@troutmask.apl.washington.edu>
Subject: [Patch, Fortran, backport 2 gcc-11] PR98301 Re: RANDOM_INIT() and coarray Fortran
Date: Sat, 5 Jun 2021 16:04:51 +0200 [thread overview]
Message-ID: <20210605160451.3f6af2b2@vepi2> (raw)
In-Reply-To: <20210522133911.4075c2e4@vepi2>
[-- Attachment #1: Type: text/plain, Size: 351 bytes --]
Hi all,
I was asked to backport the patch for pr98301 to gcc-11. The patches have
been in mainline for two weeks without any defect reports I could fined. The
patch for mainline applied with a bit of shift cleanly.
Regstested fine on x86_64/f33. Ok for backport gcc-11?
Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
[-- Attachment #2: pr98301_gcc11.log --]
[-- Type: text/x-log, Size: 688 bytes --]
Steve Kargl <kargl@gcc.gnu.org>
PR fortran/98301 - random_init() is broken
Correct implementation of random_init() when -fcoarray=lib is given.
Backport from mainline.
gcc/fortran/ChangeLog:
PR fortran/98301
* trans-decl.c (gfc_build_builtin_function_decls): Move decl.
* trans-intrinsic.c (conv_intrinsic_random_init): Use bool for
lib-call of caf_random_init instead of logical (4-byte).
* trans.h: Add tree var for random_init.
libgfortran/ChangeLog:
PR fortran/98301
* caf/libcaf.h (_gfortran_caf_random_init): New function.
* caf/single.c (_gfortran_caf_random_init): New function.
* gfortran.map: Added fndecl.
* intrinsics/random_init.f90: Implement random_init.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: pr98301_gcc11.patch --]
[-- Type: text/x-patch, Size: 12839 bytes --]
diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index 7cded0a3ede..4fa39f71a21 100644
--- a/gcc/fortran/trans-decl.c
+++ b/gcc/fortran/trans-decl.c
@@ -170,6 +170,7 @@ tree gfor_fndecl_co_min;
tree gfor_fndecl_co_reduce;
tree gfor_fndecl_co_sum;
tree gfor_fndecl_caf_is_present;
+tree gfor_fndecl_caf_random_init;
/* Math functions. Many other math functions are handled in
@@ -233,7 +234,7 @@ tree gfor_fndecl_cgemm;
tree gfor_fndecl_zgemm;
/* RANDOM_INIT function. */
-tree gfor_fndecl_random_init;
+tree gfor_fndecl_random_init; /* libgfortran, 1 image only. */
static void
gfc_add_decl_to_parent_function (tree decl)
@@ -3516,6 +3517,8 @@ gfc_build_intrinsic_function_decls (void)
void_type_node, 3, gfc_logical4_type_node, gfc_logical4_type_node,
gfc_int4_type_node);
+ // gfor_fndecl_caf_rand_init is defined in the lib-coarray section below.
+
gfor_fndecl_sc_kind = gfc_build_library_function_decl_with_spec (
get_identifier (PREFIX("selected_char_kind")), ". . R ",
gfc_int4_type_node, 2, gfc_charlen_type_node, pchar_type_node);
@@ -4081,6 +4084,10 @@ gfc_build_builtin_function_decls (void)
get_identifier (PREFIX("caf_is_present")), ". r . r ",
integer_type_node, 3, pvoid_type_node, integer_type_node,
pvoid_type_node);
+
+ gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
+ get_identifier (PREFIX("caf_random_init")),
+ void_type_node, 2, logical_type_node, logical_type_node);
}
gfc_build_intrinsic_function_decls ();
diff --git a/gcc/fortran/trans-intrinsic.c b/gcc/fortran/trans-intrinsic.c
index 68090d4defc..2c094f326e6 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -3837,38 +3837,43 @@ conv_intrinsic_random_init (gfc_code *code)
{
stmtblock_t block;
gfc_se se;
- tree arg1, arg2, arg3, tmp;
- tree logical4_type_node = gfc_get_logical_type (4);
+ tree arg1, arg2, tmp;
+ /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL. */
+ tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
+ ? logical_type_node
+ : gfc_get_logical_type (4);
/* Make the function call. */
gfc_init_block (&block);
gfc_init_se (&se, NULL);
- /* Convert REPEATABLE to a LOGICAL(4) entity. */
+ /* Convert REPEATABLE to the desired LOGICAL entity. */
gfc_conv_expr (&se, code->ext.actual->expr);
gfc_add_block_to_block (&block, &se.pre);
- arg1 = fold_convert (logical4_type_node, gfc_evaluate_now (se.expr, &block));
+ arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
gfc_add_block_to_block (&block, &se.post);
- /* Convert IMAGE_DISTINCT to a LOGICAL(4) entity. */
+ /* Convert IMAGE_DISTINCT to the desired LOGICAL entity. */
gfc_conv_expr (&se, code->ext.actual->next->expr);
gfc_add_block_to_block (&block, &se.pre);
- arg2 = fold_convert (logical4_type_node, gfc_evaluate_now (se.expr, &block));
+ arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
gfc_add_block_to_block (&block, &se.post);
- /* Create the hidden argument. For non-coarray codes and -fcoarray=single,
- simply set this to 0. For -fcoarray=lib, generate a call to
- THIS_IMAGE() without arguments. */
- arg3 = build_int_cst (gfc_get_int_type (4), 0);
if (flag_coarray == GFC_FCOARRAY_LIB)
{
- arg3 = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image,
- 1, arg3);
- se.expr = fold_convert (gfc_get_int_type (4), arg3);
+ tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
+ 2, arg1, arg2);
+ }
+ else
+ {
+ /* The ABI for libgfortran needs to be maintained, so a hidden
+ argument must be include if code is compiled with -fcoarray=single
+ or without the option. Set to 0. */
+ tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
+ tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
+ 3, arg1, arg2, arg3);
}
- tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init, 3,
- arg1, arg2, arg3);
gfc_add_expr_to_block (&block, tmp);
return gfc_finish_block (&block);
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index 8c6f82ff1b1..69d3fdcfdac 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -969,6 +969,7 @@ extern GTY(()) tree gfor_fndecl_ieee_procedure_exit;
/* RANDOM_INIT. */
extern GTY(()) tree gfor_fndecl_random_init;
+extern GTY(()) tree gfor_fndecl_caf_random_init;
/* True if node is an integer constant. */
#define INTEGER_CST_P(node) (TREE_CODE(node) == INTEGER_CST)
diff --git a/libgfortran/caf/libcaf.h b/libgfortran/caf/libcaf.h
index 5abb753f6fd..c66d0379042 100644
--- a/libgfortran/caf/libcaf.h
+++ b/libgfortran/caf/libcaf.h
@@ -261,4 +261,6 @@ void _gfortran_caf_stopped_images (gfc_descriptor_t *,
int _gfortran_caf_is_present (caf_token_t, int, caf_reference_t *);
+void _gfortran_caf_random_init (bool, bool);
+
#endif /* LIBCAF_H */
diff --git a/libgfortran/caf/single.c b/libgfortran/caf/single.c
index a291c4452c9..fc8e3b3b94a 100644
--- a/libgfortran/caf/single.c
+++ b/libgfortran/caf/single.c
@@ -3135,3 +3135,13 @@ _gfortran_caf_is_present (caf_token_t token,
}
return memptr != NULL;
}
+
+/* Reference the libraries implementation. */
+extern void _gfortran_random_init (int32_t, int32_t, int32_t);
+
+void _gfortran_caf_random_init (bool repeatable, bool image_distinct)
+{
+ /* In a single image implementation always forward to the gfortran
+ routine. */
+ _gfortran_random_init (repeatable, image_distinct, 1);
+}
diff --git a/libgfortran/gfortran.map b/libgfortran/gfortran.map
index f74436fd338..32579831a65 100644
--- a/libgfortran/gfortran.map
+++ b/libgfortran/gfortran.map
@@ -1629,3 +1629,8 @@ GFORTRAN_10.2 {
_gfortran_mfindloc1_c10;
_gfortran_sfindloc1_c10;
} GFORTRAN_10;
+
+GFORTRAN_12 {
+ global:
+ _gfortran_caf_random_init;
+} GFORTRAN_10.2;
diff --git a/libgfortran/intrinsics/random_init.f90 b/libgfortran/intrinsics/random_init.f90
index e5b4087efd9..1200225e182 100644
--- a/libgfortran/intrinsics/random_init.f90
+++ b/libgfortran/intrinsics/random_init.f90
@@ -1,94 +1,100 @@
! Copyright (C) 2018-2021 Free Software Foundation, Inc.
! Contributed by Steven G. Kargl <kargl@gcc.gnu.org>
-!
+!
! This file is part of the GNU Fortran runtime library (libgfortran).
-!
+!
! Libgfortran is free software; you can redistribute it and/or
! modify it under the terms of the GNU General Public
! License as published by the Free Software Foundation; either
! version 3 of the License, or (at your option) any later version.
-!
+!
! Libgfortran is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
-!
+!
! Under Section 7 of GPL version 3, you are granted additional
! permissions described in the GCC Runtime Library Exception, version
! 3.1, as published by the Free Software Foundation.
-!
+!
! You should have received a copy of the GNU General Public License and
! a copy of the GCC Runtime Library Exception along with this program;
! see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
! <http://www.gnu.org/licenses/>.
!
-!
! WARNING: This file should never be compiled with an option that changes
! default logical kind from 4 to some other value or changes default integer
-! kind from from 4 to some other value.
-!
-!
-! There are four combinations of repeatable and image_distinct. If a program
-! is compiled without the -fcoarray= option or with -fcoarray=single, then
-! execution of the compiled executable does not use image_distinct as it is
-! irrelevant (although required). The behavior is as follows:
-!
-! call random_init(.true., .true.)
+! kind from 4 to some other value.
!
-! The sequence of random numbers is repeatable within an instance of program
-! execution. That is, calls to random_init(.true., .true.) during the
-! execution will reset the sequence of RN to the same sequence. If the
-! program is compiled with -fcoarray=lib and multiple images are instantiated,
-! then each image accesses a repeatable distinct sequence of random numbers.
-! There are no guarantees that multiple execution of the program will access
-! the same sequence.
+! There are four combinations of repeatable and image_distinct. The
+! language below is from the F2018 standard (actually, J3/18-007r1).
!
-! call random_init(.false., .false.)
-! call random_init(.false., .true.)
+! This routine is only used for non-coarray programs or with programs
+! compiled with -fcoarray=single. Use of -fcoarray=lib or -fcoarray=shared
+! requires different routines due to the need for communication between
+! images under case(iv).
!
-! The sequence of random numbers is determined from process-dependent seeds.
-! On each execution of the executable, different seeds will be used. For
-! -fcoarray=lib and multiple instantiated images, each image will use
-! process-dependent seeds. In other words, the two calls have identical
-! behavior.
+! Technically, neither image_distinct nor image_num are now needed. The
+! interface to _gfortran_random_init() is maintained for libgfortran ABI.
+! Note, the Fortran standard requires the image_distinct argument, so
+! it will always have a valid value, and the frontend generates an value
+! of 0 for image_num.
!
-! call random_init(.true., .false.)
-!
-! For a program compiled without the -fcoarray= option or with
-! -fcoarray=single, a single image is instantiated when the executable is
-! run. If the executable causes multiple images to be instantiated, then
-! image_distinct=.false. in one image cannot affect the sequence of random
-! numbers in another image. As gfortran gives each image its own independent
-! PRNG, this condition is automatically satisfied.
-!
-impure subroutine _gfortran_random_init(repeatable, image_distinct, hidden)
+impure subroutine _gfortran_random_init(repeatable, image_distinct, image_num)
implicit none
logical, value, intent(in) :: repeatable
logical, value, intent(in) :: image_distinct
- integer, value, intent(in) :: hidden
+ integer, value, intent(in) :: image_num
logical, save :: once = .true.
- integer :: nseed
+ integer :: nseed, lcg_seed
integer, save, allocatable :: seed(:)
- if (once) then
- once = .false.
- call random_seed(size=nseed)
- allocate(seed(nseed))
- call random_seed(get=seed)
+ if (repeatable) then
+ if (once) then
+ once = .false.
+ call random_seed(size=nseed)
+ allocate(seed(nseed))
+ lcg_seed = 57911963
+ call _gfortran_lcg(seed)
+ end if
+ call random_seed(put=seed)
+ else
+ call random_seed()
!
- ! To guarantee that seed is distinct on multiple images, add the hidden
- ! argument (which is the image index).
+ ! This cannot happen; but, prevent gfortran complaining about
+ ! unused variables.
!
- if (image_distinct) seed = seed + hidden
+ if (image_num > 2) then
+ block
+ use iso_fortran_env, only : error_unit
+ write(error_unit, '(A)') 'whoops: random_init(.false., .false.)'
+ if (image_distinct) error stop image_num + 1
+ error stop image_num
+ end block
+ end if
end if
- if (repeatable) then
- call random_seed(put=seed);
- else
- call random_seed();
- end if
+ contains
+ !
+ ! SK Park and KW Miller, ``Random number generators: good ones are hard
+ ! to find,'' Comm. ACM, 31(10), 1192--1201, (1988).
+ !
+ ! Implementation of a prime modulus multiplicative linear congruential
+ ! generator, which avoids overflow and provides the full period.
+ !
+ impure elemental subroutine _gfortran_lcg(i)
+ implicit none
+ integer, intent(out) :: i
+ integer, parameter :: a = 16807 ! Multiplier
+ integer, parameter :: m = huge(a) ! Modulus
+ integer, parameter :: q = 127773 ! Quotient to avoid overflow
+ integer, parameter :: r = 2836 ! Remainder to avoid overflow
+ lcg_seed = a * mod(lcg_seed, q) - r * (lcg_seed / q)
+ if (lcg_seed <= 0) lcg_seed = lcg_seed + m
+ i = lcg_seed
+ end subroutine _gfortran_lcg
end subroutine _gfortran_random_init
next prev parent reply other threads:[~2021-06-05 14:05 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20210403172846.GA14134@troutmask.apl.washington.edu>
[not found] ` <CACR8rvcRRnz4fzbzp+b-J6Y040Udy6v99NH4_1TXfnAmBDmSaA@mail.gmail.com>
[not found] ` <20210404053331.GA18048@troutmask.apl.washington.edu>
[not found] ` <20210423184357.643efbdf@vepi2>
[not found] ` <20210423171817.GA75072@troutmask.apl.washington.edu>
[not found] ` <20210424124945.46b1da51@vepi2>
[not found] ` <20210425200334.GA94808@troutmask.apl.washington.edu>
[not found] ` <20210426123636.7eb24c37@vepi2>
2021-05-03 9:21 ` [Ping, Patch, Fortran, Update 2] " Andre Vehreschild
2021-05-03 15:20 ` Steve Kargl
2021-05-21 8:09 ` [Ping^2, Patch, Fortran] " Andre Vehreschild
2021-05-21 15:08 ` Steve Kargl
2021-05-22 2:38 ` Jerry D
2021-05-22 11:39 ` Andre Vehreschild
2021-05-22 17:58 ` Martin Liška
2021-05-23 11:59 ` Andre Vehreschild
2021-05-23 12:17 ` Martin Liška
2021-06-05 14:04 ` Andre Vehreschild [this message]
2021-06-05 16:27 ` [Patch, Fortran, backport 2 gcc-11] " Steve Kargl
2021-06-06 10:14 ` [COMITTED, Patch, " Andre Vehreschild
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210605160451.3f6af2b2@vepi2 \
--to=vehre@gmx.de \
--cc=fortran@gcc.gnu.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=jvdelisle2@gmail.com \
--cc=sgk@troutmask.apl.washington.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).