public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* RANDOM_INIT() and coarray Fortran
@ 2021-04-03 17:28 Steve Kargl
  2021-04-04  3:30 ` Damian Rouson
  0 siblings, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-04-03 17:28 UTC (permalink / raw)
  To: fortran

What to do about RANDOM_INIT() and coarray Fortran?

The main issue is that if one compiles with -fcoarray=lib
(or the WIP -fcoarray=shared), then RANDOM_INIT() may
require communication between images.  Thus, RANDOM_INIT()
cannot live in libgfortran for at least -fcoarray=lib.

Consider the simple code:

subroutine foo
   call random_init(.true., .false.)
end subroutine foo

I have updated the patch for
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98301
to use a stub routine for -fcoarray=lib and -fcoarray=shared.
Anyone, who knows how to use git, is encouraged to commit the patch.

For -fcoarray=none (default option) and -fcoarray=single, the
patch will cause gfortran to generate

__attribute__((fn spec (". ")))
void foo ()
{
  _gfortran_random_init (1, 0, 0);
}

_gfortran_random_init() live in libgfortran and it has been updated
to meet the intended requires of the Fortran standard.

With -fcoarray=lib and -fcoarray=shared, gfortran will now generate

__attribute__((fn spec (". ")))
void foo ()
{
  _gfortran_random_init_foobar (1, 0);
}

where _gfortran_random_init_foobar() lives in libgfortran.  It prints
an error message that RANDOM_INIT() is not yet supported for coarray
Fortran and exits.  Someone, who cares about coarray Fortran, can fix
-fcoarray=lib and -fcoarray=shared by updating trans-decl.c (see the
FIXME for random_init()) to emit 

__attribute__((fn spec (". ")))
void foo ()
{
  _gfortran_caf_random_init (1, 0);
}

or

__attribute__((fn spec (". ")))
void foo ()
{
  _gfortran_cas_random_init (1, 0);
}

-- 
Steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: RANDOM_INIT() and coarray Fortran
  2021-04-03 17:28 RANDOM_INIT() and coarray Fortran Steve Kargl
@ 2021-04-04  3:30 ` Damian Rouson
  2021-04-04  5:33   ` Steve Kargl
  0 siblings, 1 reply; 22+ messages in thread
From: Damian Rouson @ 2021-04-04  3:30 UTC (permalink / raw)
  To: Steve Kargl; +Cc: gfortran, Andre Vehreschild

Hi Steve,

I hope the gfortran developers won't commit a patch that replaces
existing behavior with a stub that simply emits an error message.  It
has been a while since I looked at the previous emails regarding
problems with the current behavior so I'm not expressing an opinion
about the current behavior.  I'm simply advocating against breaking
existing codes that rely on the current behavior if nothing better is
being provided.

Or as a compromise, would you mind changing the patch so that the
error message is emitted only in the problematic cases? You presented
one problematic case out of the four possible combinations of
RANDOM_INIT() arguments.  With only four possible combinations to
enumerate, I hope this suggestion isn't burdensome.

Regarding "someone who cares about coarray Fortran," finding people to
work on such an effort is quite challenging.  I believe Andre
Verheschild has some limited availability so I'm cc'ing him and will
discuss it with him if he's interested.  If you know others who might
be interested, please let us know.

Damian

On Sat, Apr 3, 2021 at 10:28 AM Steve Kargl via Fortran
<fortran@gcc.gnu.org> wrote:
>
> What to do about RANDOM_INIT() and coarray Fortran?
>
> The main issue is that if one compiles with -fcoarray=lib
> (or the WIP -fcoarray=shared), then RANDOM_INIT() may
> require communication between images.  Thus, RANDOM_INIT()
> cannot live in libgfortran for at least -fcoarray=lib.
>
> Consider the simple code:
>
> subroutine foo
>    call random_init(.true., .false.)
> end subroutine foo
>
> I have updated the patch for
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98301
> to use a stub routine for -fcoarray=lib and -fcoarray=shared.
> Anyone, who knows how to use git, is encouraged to commit the patch.
>
> For -fcoarray=none (default option) and -fcoarray=single, the
> patch will cause gfortran to generate
>
> __attribute__((fn spec (". ")))
> void foo ()
> {
>   _gfortran_random_init (1, 0, 0);
> }
>
> _gfortran_random_init() live in libgfortran and it has been updated
> to meet the intended requires of the Fortran standard.
>
> With -fcoarray=lib and -fcoarray=shared, gfortran will now generate
>
> __attribute__((fn spec (". ")))
> void foo ()
> {
>   _gfortran_random_init_foobar (1, 0);
> }
>
> where _gfortran_random_init_foobar() lives in libgfortran.  It prints
> an error message that RANDOM_INIT() is not yet supported for coarray
> Fortran and exits.  Someone, who cares about coarray Fortran, can fix
> -fcoarray=lib and -fcoarray=shared by updating trans-decl.c (see the
> FIXME for random_init()) to emit
>
> __attribute__((fn spec (". ")))
> void foo ()
> {
>   _gfortran_caf_random_init (1, 0);
> }
>
> or
>
> __attribute__((fn spec (". ")))
> void foo ()
> {
>   _gfortran_cas_random_init (1, 0);
> }
>
> --
> Steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: RANDOM_INIT() and coarray Fortran
  2021-04-04  3:30 ` Damian Rouson
@ 2021-04-04  5:33   ` Steve Kargl
  2021-04-23 16:43     ` [Patch, Fortran] PR98301 " Andre Vehreschild
  0 siblings, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-04-04  5:33 UTC (permalink / raw)
  To: Damian Rouson; +Cc: gfortran, Andre Vehreschild

On Sat, Apr 03, 2021 at 08:30:31PM -0700, Damian Rouson wrote:
> Hi Steve,
> 
> I hope the gfortran developers won't commit a patch that replaces
> existing behavior with a stub that simply emits an error message.

The current behavior is incorrect with respect to the Fortran standard
if one runs a compiled program multiple times.  The patch fixes a bug.

> It has been a while since I looked at the previous emails regarding
> problems with the current behavior so I'm not expressing an opinion
> about the current behavior.  I'm simply advocating against breaking
> existing codes that rely on the current behavior if nothing better is
> being provided.

It cannot be helped.  The underlying coarray implementation must
handle RANDOM_INIT().  AFAICT, there must be communication between
images if RANDOM_INIT() is used.  libgfortran is not compiled with
-fcoarray=lib (or -fcoarray=shared), so the coarray implementation(s)
must deal with RANDOM_INIT().

> Or as a compromise, would you mind changing the patch so that the
> error message is emitted only in the problematic cases? You presented
> one problematic case out of the four possible combinations of
> RANDOM_INIT() arguments.  With only four possible combinations to
> enumerate, I hope this suggestion isn't burdensome.
Consider,

   read(*,*) repeatable, image_distinct
   call random_init(repeatable, image_distinct)

for -fcoarray=none or -fcoarray=single, the image_distinct
argument is irrevelant.   This is simply translated to 

_gfortran_random_init(repeatable, image_distinct)

For -fcoarray=lib (and by extension OpenCoarray), a simple 1 line
patch on top of my patch would generate

_gfortran_caf_random_init(repeatable, image_distinct)

where is it assumed the function is coarray aware.  Similarly, if
-fcoarray=shared, then a 1 line patch would generate 

_gfortran_cas_random_init(repeatable, image_distinct)

where is it assumed the function is coarray aware.

There are 4 cases:
(1)   repeatable=.true., image_distinct=.true. 
(2)   repeatable=.true., image_distinct=.false.
(3)   repeatable=.false., image_distinct=.true.
(4)   repeatable=.false., image_distinct=.false.

IIRC, cases (1)-(3) don't require communication.  case (4) does.
That is, case (4) requires the same set of random seeds to be
used on all images.

> Regarding "someone who cares about coarray Fortran," finding people to
> work on such an effort is quite challenging.

Finding people willing to work on gfortran is as challenging if
not more so.  A year ago, I posted in c.l.f a list of 20+ PRs
with patches that I had created.  I don't do git.  It would take 
someone with git-fu little time to take my patches and apply 
them to a source tree.  Testing was done by me, but I would
encourage git-fu aware individuals to bootstrap and test the patches.
Then commit the patch.  Harald has stepped up and grabbed a few.
TO BE CLEAR, I AM NOT RANTING AT THE PEOPLE WHO HAVE CONTRIBUTED
AND MAINTAINED GFORTRAN FOR YEARS.  gfortran needs new blood, or
it is destined for the bit bucket.

> I believe Andre
> Verheschild has some limited availability so I'm cc'ing him and will
> discuss it with him if he's interested.  If you know others who might
> be interested, please let us know.

Don't know any new people who are interested in gfortran.

-- 
Steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-04  5:33   ` Steve Kargl
@ 2021-04-23 16:43     ` Andre Vehreschild
  2021-04-23 17:18       ` Steve Kargl
  0 siblings, 1 reply; 22+ messages in thread
From: Andre Vehreschild @ 2021-04-23 16:43 UTC (permalink / raw)
  To: GCC-Fortran-ML; +Cc: Steve Kargl, Damian Rouson, Andre Vehreschild

[-- Attachment #1: Type: text/plain, Size: 4725 bytes --]

Hi folks,

please find attached the library part for supporting RANDOM_INIT for
coarray=lib enabled fortran translations. There is also a patch for the
Opencoarray library to add RANDOM_INIT there.

I am not sure, whether I have modified the gfortran.map file in the libgfortran
directory correctly, or even had to. So please take specific care if that is
correct.

Bootstrapped and regtested fine on x86_64/f33.

Ok for trunk?

Regards,
	Andre

PR fortran/98301 - random_init() is broken

Correct implementation of random_init() when -fcoarray=lib is given.

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).

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: Modified comment.



On Sat, 3 Apr 2021 22:33:31 -0700
Steve Kargl via Fortran <fortran@gcc.gnu.org> wrote:

> On Sat, Apr 03, 2021 at 08:30:31PM -0700, Damian Rouson wrote:
> > Hi Steve,
> >
> > I hope the gfortran developers won't commit a patch that replaces
> > existing behavior with a stub that simply emits an error message.
>
> The current behavior is incorrect with respect to the Fortran standard
> if one runs a compiled program multiple times.  The patch fixes a bug.
>
> > It has been a while since I looked at the previous emails regarding
> > problems with the current behavior so I'm not expressing an opinion
> > about the current behavior.  I'm simply advocating against breaking
> > existing codes that rely on the current behavior if nothing better is
> > being provided.
>
> It cannot be helped.  The underlying coarray implementation must
> handle RANDOM_INIT().  AFAICT, there must be communication between
> images if RANDOM_INIT() is used.  libgfortran is not compiled with
> -fcoarray=lib (or -fcoarray=shared), so the coarray implementation(s)
> must deal with RANDOM_INIT().
>
> > Or as a compromise, would you mind changing the patch so that the
> > error message is emitted only in the problematic cases? You presented
> > one problematic case out of the four possible combinations of
> > RANDOM_INIT() arguments.  With only four possible combinations to
> > enumerate, I hope this suggestion isn't burdensome.
> Consider,
>
>    read(*,*) repeatable, image_distinct
>    call random_init(repeatable, image_distinct)
>
> for -fcoarray=none or -fcoarray=single, the image_distinct
> argument is irrevelant.   This is simply translated to
>
> _gfortran_random_init(repeatable, image_distinct)
>
> For -fcoarray=lib (and by extension OpenCoarray), a simple 1 line
> patch on top of my patch would generate
>
> _gfortran_caf_random_init(repeatable, image_distinct)
>
> where is it assumed the function is coarray aware.  Similarly, if
> -fcoarray=shared, then a 1 line patch would generate
>
> _gfortran_cas_random_init(repeatable, image_distinct)
>
> where is it assumed the function is coarray aware.
>
> There are 4 cases:
> (1)   repeatable=.true., image_distinct=.true.
> (2)   repeatable=.true., image_distinct=.false.
> (3)   repeatable=.false., image_distinct=.true.
> (4)   repeatable=.false., image_distinct=.false.
>
> IIRC, cases (1)-(3) don't require communication.  case (4) does.
> That is, case (4) requires the same set of random seeds to be
> used on all images.
>
> > Regarding "someone who cares about coarray Fortran," finding people to
> > work on such an effort is quite challenging.
>
> Finding people willing to work on gfortran is as challenging if
> not more so.  A year ago, I posted in c.l.f a list of 20+ PRs
> with patches that I had created.  I don't do git.  It would take
> someone with git-fu little time to take my patches and apply
> them to a source tree.  Testing was done by me, but I would
> encourage git-fu aware individuals to bootstrap and test the patches.
> Then commit the patch.  Harald has stepped up and grabbed a few.
> TO BE CLEAR, I AM NOT RANTING AT THE PEOPLE WHO HAVE CONTRIBUTED
> AND MAINTAINED GFORTRAN FOR YEARS.  gfortran needs new blood, or
> it is destined for the bit bucket.
>
> > I believe Andre
> > Verheschild has some limited availability so I'm cc'ing him and will
> > discuss it with him if he's interested.  If you know others who might
> > be interested, please let us know.
>
> Don't know any new people who are interested in gfortran.
>


--
Andre Vehreschild * Email: vehre ad gmx dot de

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr98301.patch --]
[-- Type: text/x-patch, Size: 6287 bytes --]

diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index c99336cb19d..de0e8fb0314 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
@@ -234,7 +235,6 @@ tree gfor_fndecl_zgemm;

 /* RANDOM_INIT function.  */
 tree gfor_fndecl_random_init;      /* libgfortran, 1 image only.  */
-tree gfor_fndecl_caf_random_init;  /* OpenCoarray call.  */
 tree gfor_fndecl_cas_random_init;  /* Shared memory coarray.  */

 static void
@@ -3517,23 +3517,17 @@ 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.
+
 /* FIXME: This is a temporary workaround until someone that uses coarray
    Fortran and random_init() can implement the OpenCoarray and/or the
    shared memory routines.  Both require communication between images, so
    these routines cannot live in libgfortran.  */
 #if 1
-  gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
-	get_identifier (PREFIX("random_init_foobar")),
-	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
-
   gfor_fndecl_cas_random_init = gfc_build_library_function_decl (
 	get_identifier (PREFIX("random_init_foobar")),
 	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
 #else
-  gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
-	get_identifier (PREFIX("caf_random_init")),
-	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
-
   gfor_fndecl_cas_random_init = gfc_build_library_function_decl (
 	get_identifier (PREFIX("cas_random_init")),
 	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
@@ -4104,6 +4098,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 900c6198d9e..8319e94b893 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -3828,27 +3828,27 @@ conv_intrinsic_random_init (gfc_code *code)
   stmtblock_t block;
   gfc_se se;
   tree arg1, arg2, tmp;
-  tree logical4_type_node = gfc_get_logical_type (4);
+  /* 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.  */
   if (flag_coarray == GFC_FCOARRAY_LIB)
     {
       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
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 b594b59849a..626804c37dd 100644
--- a/libgfortran/gfortran.map
+++ b/libgfortran/gfortran.map
@@ -1634,3 +1634,8 @@ GFORTRAN_11 {
   global:
   _gfortran_random_init_foobar;
 } GFORTRAN_10.2;
+
+GFORTRAN_12 {
+  global:
+  _gfortran_caf_random_init;
+} GFORTRAN_11;
diff --git a/libgfortran/intrinsics/random_init.f90 b/libgfortran/intrinsics/random_init.f90
index bbb45937087..233a1ca073a 100644
--- a/libgfortran/intrinsics/random_init.f90
+++ b/libgfortran/intrinsics/random_init.f90
@@ -100,7 +100,7 @@ impure subroutine _gfortran_random_init(repeatable, image_distinct, image_num)
 end subroutine _gfortran_random_init
 !
 ! This is a temporary stub implementation until random_init is
-! implemented for -fcoarray=shared and -fcoarray=lib.
+! implemented for -fcoarray=shared.
 !
 subroutine _gfortran_random_init_foobar(repeatable, image_distinct)


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-23 16:43     ` [Patch, Fortran] PR98301 " Andre Vehreschild
@ 2021-04-23 17:18       ` Steve Kargl
  2021-04-24 10:49         ` [Patch, Fortran, Update] " Andre Vehreschild
  0 siblings, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-04-23 17:18 UTC (permalink / raw)
  To: Andre Vehreschild; +Cc: GCC-Fortran-ML, Damian Rouson, Andre Vehreschild

Andre,

Thanks for taking care of OpenCoarray portion of RANDOM_INIT.
My last non-coarray aware patch is attached to the PR in bugzilla.
Since the change over to git, I no longer commit to the source tree.
I suggest combining your patch with my patch if you intend to 
commit; otherwise, attach your patch to the PR and sit patiently
until someone can do the combined commit.

-- 
steve  


On Fri, Apr 23, 2021 at 06:43:57PM +0200, Andre Vehreschild wrote:
> Hi folks,
> 
> please find attached the library part for supporting RANDOM_INIT for
> coarray=lib enabled fortran translations. There is also a patch for the
> Opencoarray library to add RANDOM_INIT there.
> 
> I am not sure, whether I have modified the gfortran.map file in the libgfortran
> directory correctly, or even had to. So please take specific care if that is
> correct.
> 
> Bootstrapped and regtested fine on x86_64/f33.
> 
> Ok for trunk?
> 
> Regards,
> 	Andre
> 
> PR fortran/98301 - random_init() is broken
> 
> Correct implementation of random_init() when -fcoarray=lib is given.
> 
> 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).
> 
> 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: Modified comment.
> 
> 
> 
> On Sat, 3 Apr 2021 22:33:31 -0700
> Steve Kargl via Fortran <fortran@gcc.gnu.org> wrote:
> 
> > On Sat, Apr 03, 2021 at 08:30:31PM -0700, Damian Rouson wrote:
> > > Hi Steve,
> > >
> > > I hope the gfortran developers won't commit a patch that replaces
> > > existing behavior with a stub that simply emits an error message.
> >
> > The current behavior is incorrect with respect to the Fortran standard
> > if one runs a compiled program multiple times.  The patch fixes a bug.
> >
> > > It has been a while since I looked at the previous emails regarding
> > > problems with the current behavior so I'm not expressing an opinion
> > > about the current behavior.  I'm simply advocating against breaking
> > > existing codes that rely on the current behavior if nothing better is
> > > being provided.
> >
> > It cannot be helped.  The underlying coarray implementation must
> > handle RANDOM_INIT().  AFAICT, there must be communication between
> > images if RANDOM_INIT() is used.  libgfortran is not compiled with
> > -fcoarray=lib (or -fcoarray=shared), so the coarray implementation(s)
> > must deal with RANDOM_INIT().
> >
> > > Or as a compromise, would you mind changing the patch so that the
> > > error message is emitted only in the problematic cases? You presented
> > > one problematic case out of the four possible combinations of
> > > RANDOM_INIT() arguments.  With only four possible combinations to
> > > enumerate, I hope this suggestion isn't burdensome.
> > Consider,
> >
> >    read(*,*) repeatable, image_distinct
> >    call random_init(repeatable, image_distinct)
> >
> > for -fcoarray=none or -fcoarray=single, the image_distinct
> > argument is irrevelant.   This is simply translated to
> >
> > _gfortran_random_init(repeatable, image_distinct)
> >
> > For -fcoarray=lib (and by extension OpenCoarray), a simple 1 line
> > patch on top of my patch would generate
> >
> > _gfortran_caf_random_init(repeatable, image_distinct)
> >
> > where is it assumed the function is coarray aware.  Similarly, if
> > -fcoarray=shared, then a 1 line patch would generate
> >
> > _gfortran_cas_random_init(repeatable, image_distinct)
> >
> > where is it assumed the function is coarray aware.
> >
> > There are 4 cases:
> > (1)   repeatable=.true., image_distinct=.true.
> > (2)   repeatable=.true., image_distinct=.false.
> > (3)   repeatable=.false., image_distinct=.true.
> > (4)   repeatable=.false., image_distinct=.false.
> >
> > IIRC, cases (1)-(3) don't require communication.  case (4) does.
> > That is, case (4) requires the same set of random seeds to be
> > used on all images.
> >
> > > Regarding "someone who cares about coarray Fortran," finding people to
> > > work on such an effort is quite challenging.
> >
> > Finding people willing to work on gfortran is as challenging if
> > not more so.  A year ago, I posted in c.l.f a list of 20+ PRs
> > with patches that I had created.  I don't do git.  It would take
> > someone with git-fu little time to take my patches and apply
> > them to a source tree.  Testing was done by me, but I would
> > encourage git-fu aware individuals to bootstrap and test the patches.
> > Then commit the patch.  Harald has stepped up and grabbed a few.
> > TO BE CLEAR, I AM NOT RANTING AT THE PEOPLE WHO HAVE CONTRIBUTED
> > AND MAINTAINED GFORTRAN FOR YEARS.  gfortran needs new blood, or
> > it is destined for the bit bucket.
> >
> > > I believe Andre
> > > Verheschild has some limited availability so I'm cc'ing him and will
> > > discuss it with him if he's interested.  If you know others who might
> > > be interested, please let us know.
> >
> > Don't know any new people who are interested in gfortran.
> >
> 
> 
> --
> Andre Vehreschild * Email: vehre ad gmx dot de

> diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
> index c99336cb19d..de0e8fb0314 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
> @@ -234,7 +235,6 @@ tree gfor_fndecl_zgemm;
> 
>  /* RANDOM_INIT function.  */
>  tree gfor_fndecl_random_init;      /* libgfortran, 1 image only.  */
> -tree gfor_fndecl_caf_random_init;  /* OpenCoarray call.  */
>  tree gfor_fndecl_cas_random_init;  /* Shared memory coarray.  */
> 
>  static void
> @@ -3517,23 +3517,17 @@ 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.
> +
>  /* FIXME: This is a temporary workaround until someone that uses coarray
>     Fortran and random_init() can implement the OpenCoarray and/or the
>     shared memory routines.  Both require communication between images, so
>     these routines cannot live in libgfortran.  */
>  #if 1
> -  gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
> -	get_identifier (PREFIX("random_init_foobar")),
> -	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
> -
>    gfor_fndecl_cas_random_init = gfc_build_library_function_decl (
>  	get_identifier (PREFIX("random_init_foobar")),
>  	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
>  #else
> -  gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
> -	get_identifier (PREFIX("caf_random_init")),
> -	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
> -
>    gfor_fndecl_cas_random_init = gfc_build_library_function_decl (
>  	get_identifier (PREFIX("cas_random_init")),
>  	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
> @@ -4104,6 +4098,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 900c6198d9e..8319e94b893 100644
> --- a/gcc/fortran/trans-intrinsic.c
> +++ b/gcc/fortran/trans-intrinsic.c
> @@ -3828,27 +3828,27 @@ conv_intrinsic_random_init (gfc_code *code)
>    stmtblock_t block;
>    gfc_se se;
>    tree arg1, arg2, tmp;
> -  tree logical4_type_node = gfc_get_logical_type (4);
> +  /* 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.  */
>    if (flag_coarray == GFC_FCOARRAY_LIB)
>      {
>        tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
> 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 b594b59849a..626804c37dd 100644
> --- a/libgfortran/gfortran.map
> +++ b/libgfortran/gfortran.map
> @@ -1634,3 +1634,8 @@ GFORTRAN_11 {
>    global:
>    _gfortran_random_init_foobar;
>  } GFORTRAN_10.2;
> +
> +GFORTRAN_12 {
> +  global:
> +  _gfortran_caf_random_init;
> +} GFORTRAN_11;
> diff --git a/libgfortran/intrinsics/random_init.f90 b/libgfortran/intrinsics/random_init.f90
> index bbb45937087..233a1ca073a 100644
> --- a/libgfortran/intrinsics/random_init.f90
> +++ b/libgfortran/intrinsics/random_init.f90
> @@ -100,7 +100,7 @@ impure subroutine _gfortran_random_init(repeatable, image_distinct, image_num)
>  end subroutine _gfortran_random_init
>  !
>  ! This is a temporary stub implementation until random_init is
> -! implemented for -fcoarray=shared and -fcoarray=lib.
> +! implemented for -fcoarray=shared.
>  !
>  subroutine _gfortran_random_init_foobar(repeatable, image_distinct)
> 


-- 
Steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Patch, Fortran, Update] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-23 17:18       ` Steve Kargl
@ 2021-04-24 10:49         ` Andre Vehreschild
  2021-04-24 15:44           ` Steve Kargl
  2021-04-25 20:03           ` Steve Kargl
  0 siblings, 2 replies; 22+ messages in thread
From: Andre Vehreschild @ 2021-04-24 10:49 UTC (permalink / raw)
  To: Steve Kargl; +Cc: GCC-Fortran-ML, Damian Rouson, Andre Vehreschild

[-- Attachment #1: Type: text/plain, Size: 1739 bytes --]

Hi Steve, hi all,

thank you for pointing that out, Steve. When I started the work, I told
myself, that I have to remember to add your patch to the submit. Well, that did
not last for more than eight hours and I had forgotten.

So here is now the combination of Steve's and my patch (attached).

Bootstrapped and regtested ok on x86_64/f33.

@Steve: Is this your correct mail address for the changelog or do you prefer a
different one?

Regards,
	Andre

Changelog:

Steve Kargl  <sgk@troutmask.apl.washington.edu>

PR fortran/98301 - random_init() is broken

Correct implementation of random_init() when -fcoarray=lib is given.

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.



On Fri, 23 Apr 2021 10:18:17 -0700
Steve Kargl <sgk@troutmask.apl.washington.edu> wrote:

> Andre,
>
> Thanks for taking care of OpenCoarray portion of RANDOM_INIT.
> My last non-coarray aware patch is attached to the PR in bugzilla.
> Since the change over to git, I no longer commit to the source tree.
> I suggest combining your patch with my patch if you intend to
> commit; otherwise, attach your patch to the PR and sit patiently
> until someone can do the combined commit.
>


--
Andre Vehreschild * Email: vehre ad gmx dot de

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr98301.patch --]
[-- Type: text/x-patch, Size: 14563 bytes --]

diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index cc9d85543ca..de0e8fb0314 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,8 @@ 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.  */
+tree gfor_fndecl_cas_random_init;  /* Shared memory coarray.  */

 static void
 gfc_add_decl_to_parent_function (tree decl)
@@ -3515,6 +3517,22 @@ 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.
+
+/* FIXME: This is a temporary workaround until someone that uses coarray
+   Fortran and random_init() can implement the OpenCoarray and/or the
+   shared memory routines.  Both require communication between images, so
+   these routines cannot live in libgfortran.  */
+#if 1
+  gfor_fndecl_cas_random_init = gfc_build_library_function_decl (
+	get_identifier (PREFIX("random_init_foobar")),
+	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
+#else
+  gfor_fndecl_cas_random_init = gfc_build_library_function_decl (
+	get_identifier (PREFIX("cas_random_init")),
+	void_type_node, 2, gfc_logical4_type_node, gfc_logical4_type_node);
+#endif
+
   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);
@@ -4080,6 +4098,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 cceef8f34ac..8319e94b893 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -3827,38 +3827,50 @@ 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);
+    }
+#if 0
+  else if (flag_coarray == GFC_FCOARRAY_SHARED)
+    {
+      tmp = build_call_expr_loc (input_location, gfor_fndecl_cas_random_init,
+				 2, arg1, arg2);
+    }
+#endif
+  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..a82b5adec94 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -969,6 +969,8 @@ 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;
+extern GTY(()) tree gfor_fndecl_cas_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..ad19409bfb2 100644
--- a/libgfortran/gfortran.map
+++ b/libgfortran/gfortran.map
@@ -1629,3 +1629,9 @@ GFORTRAN_10.2 {
   _gfortran_mfindloc1_c10;
   _gfortran_sfindloc1_c10;
 } GFORTRAN_10;
+
+GFORTRAN_12 {
+  global:
+  _gfortran_caf_random_init;
+  _gfortran_random_init_foobar;
+} GFORTRAN_10.2;
diff --git a/libgfortran/intrinsics/random_init.f90 b/libgfortran/intrinsics/random_init.f90
index e5b4087efd9..233a1ca073a 100644
--- a/libgfortran/intrinsics/random_init.f90
+++ b/libgfortran/intrinsics/random_init.f90
@@ -1,94 +1,125 @@
 ! 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:
+! kind from 4 to some other value.
 !
-! call random_init(.true., .true.)
+! There are four combinations of repeatable and image_distinct.  The
+! language below is from the F2018 standard (actually, J3/18-007r1).
 !
-! 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.
+! 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).
 !
-! call random_init(.false., .false.)
-! call random_init(.false., .true.)
+! 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.
 !
-! 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.
-!
-! 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
+!
+! This is a temporary stub implementation until random_init is
+! implemented for -fcoarray=shared.
+!
+subroutine _gfortran_random_init_foobar(repeatable, image_distinct)
+
+   use iso_fortran_env, only : error_unit
+
+   implicit none
+
+   logical, value, intent(in) :: repeatable
+   logical, value, intent(in) :: image_distinct
+
+   block
+      write(error_unit, '(A)') &
+      &  'random_init: not yet supported with coarray Fortran'
+      error stop 1
+   end block
+
+   if (.false. .and. (repeatable .and. image_distinct)) then
+      write(error_unit, '(A)') 'random_init: Unreachable'
+      error stop 1
+   end if
+
+end subroutine _gfortran_random_init_foobar

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Patch, Fortran, Update] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-24 10:49         ` [Patch, Fortran, Update] " Andre Vehreschild
@ 2021-04-24 15:44           ` Steve Kargl
  2021-04-24 15:56             ` Dr. Andre Vehreschild
  2021-04-25 20:03           ` Steve Kargl
  1 sibling, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-04-24 15:44 UTC (permalink / raw)
  To: Andre Vehreschild; +Cc: GCC-Fortran-ML, Damian Rouson, Andre Vehreschild

On Sat, Apr 24, 2021 at 12:49:45PM +0200, Andre Vehreschild wrote:
> 
> @Steve: Is this your correct mail address for the changelog or do you prefer a
> different one?
> 

I still have my kargl@gcc.gnu.org email address.  Please use that one.
I'll look over the combined patch later today.

-- 
Steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Patch, Fortran, Update] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-24 15:44           ` Steve Kargl
@ 2021-04-24 15:56             ` Dr. Andre Vehreschild
  0 siblings, 0 replies; 22+ messages in thread
From: Dr. Andre Vehreschild @ 2021-04-24 15:56 UTC (permalink / raw)
  To: Steve Kargl; +Cc: Andre Vehreschild, GCC-Fortran-ML, Damian Rouson

Ok, I changed it in the log-file already.

- Andre

On Sat, 24 Apr 2021 08:44:24 -0700
Steve Kargl <sgk@troutmask.apl.washington.edu> wrote:

> On Sat, Apr 24, 2021 at 12:49:45PM +0200, Andre Vehreschild wrote:
> > 
> > @Steve: Is this your correct mail address for the changelog or do you
> > prefer a different one?
> >   
> 
> I still have my kargl@gcc.gnu.org email address.  Please use that one.
> I'll look over the combined patch later today.
> 


-- 
                                          Dr. Andre Vehreschild
Phone:  +49 2234 2509627                  +49 178 3837536
E-Mail: info@badgersystems.de             vehre@badgersystem.de
WWW:    www.badgersystems.de

Badger Systems GmbH
Firmensitz: Lessingstr. 26, 50858 Köln
Registergericht: Amtsgericht Köln, HRB 89654
Umsatzsteuer-Identifikationsnummer: DE815667719
Geschäftsführer: Dr. Lexi Pimenidis

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Patch, Fortran, Update] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-24 10:49         ` [Patch, Fortran, Update] " Andre Vehreschild
  2021-04-24 15:44           ` Steve Kargl
@ 2021-04-25 20:03           ` Steve Kargl
  2021-04-26 10:36             ` [Patch, Fortran, Update 2] " Andre Vehreschild
  1 sibling, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-04-25 20:03 UTC (permalink / raw)
  To: Andre Vehreschild; +Cc: GCC-Fortran-ML, Damian Rouson, Andre Vehreschild

On Sat, Apr 24, 2021 at 12:49:45PM +0200, Andre Vehreschild wrote:
> Hi Steve, hi all,
> 
> thank you for pointing that out, Steve. When I started the work, I told
> myself, that I have to remember to add your patch to the submit. Well, that did
> not last for more than eight hours and I had forgotten.
> 
> So here is now the combination of Steve's and my patch (attached).
> 
> Bootstrapped and regtested ok on x86_64/f33.
> 
> @Steve: Is this your correct mail address for the changelog or do you prefer a
> different one?
> 
> Regards,
> 	Andre
> 
> Changelog:
> 
> Steve Kargl  <sgk@troutmask.apl.washington.edu>
> 
> PR fortran/98301 - random_init() is broken
> 
> Correct implementation of random_init() when -fcoarray=lib is given.
> 
> 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.
> 

Andre,

The patch looks fine to me.  I wonder, however, if we should 
comment out all of the shared memory stuff, i.e., the _cas_
stuff.  I don't know when Thomas/Nicolas will merge their 
work-in-progress.

-- 
steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Patch, Fortran, Update 2] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-25 20:03           ` Steve Kargl
@ 2021-04-26 10:36             ` Andre Vehreschild
  2021-05-03  9:21               ` [Ping, Patch, " Andre Vehreschild
  0 siblings, 1 reply; 22+ messages in thread
From: Andre Vehreschild @ 2021-04-26 10:36 UTC (permalink / raw)
  To: Steve Kargl; +Cc: GCC-Fortran-ML, Damian Rouson, Andre Vehreschild

[-- Attachment #1: Type: text/plain, Size: 1325 bytes --]

Hi Steve, hi all,

I agree.  The cas-things have been removed (I will put the patch for them into
the pr98301 ticket, so safe them), streamlining the patch a bit more.

Bootstraped and regtested ok on x86_64-linux/f33. Ok for trunk?

Regards,
	Andre

Steve Kargl  <kargl@gcc.gnu.org>

PR fortran/98301 - random_init() is broken

Correct implementation of random_init() when -fcoarray=lib is given.

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/98302
	* 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.



On Sun, 25 Apr 2021 13:03:34 -0700
Steve Kargl <sgk@troutmask.apl.washington.edu> wrote:

> Andre,
>
> The patch looks fine to me.  I wonder, however, if we should
> comment out all of the shared memory stuff, i.e., the _cas_
> stuff.  I don't know when Thomas/Nicolas will merge their
> work-in-progress.
>


--
Andre Vehreschild * Email: vehre ad gmx dot de

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr98301.patch --]
[-- Type: text/x-patch, Size: 12839 bytes --]

diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index cc9d85543ca..7365dde47bf 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)
@@ -3515,6 +3516,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);
@@ -4080,6 +4083,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 cceef8f34ac..3f38ec8de85 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -3827,38 +3827,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

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping, Patch, Fortran, Update 2] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-04-26 10:36             ` [Patch, Fortran, Update 2] " Andre Vehreschild
@ 2021-05-03  9:21               ` Andre Vehreschild
  2021-05-03 15:20                 ` Steve Kargl
  0 siblings, 1 reply; 22+ messages in thread
From: Andre Vehreschild @ 2021-05-03  9:21 UTC (permalink / raw)
  To: GCC-Fortran-ML; +Cc: GCC-Patches-ML, Steve Kargl

Ping!

Ok for trunk?

I have looked at other patches, but none was patching any location I have
worked on previously. Therefore I can't return the favor of reviewing any
currently open patches and have to ask for volunteers here.

- Andre

On Mon, 26 Apr 2021 12:36:36 +0200
Andre Vehreschild via Fortran <fortran@gcc.gnu.org> wrote:

> Hi Steve, hi all,
>
> I agree.  The cas-things have been removed (I will put the patch for them into
> the pr98301 ticket, so safe them), streamlining the patch a bit more.
>
> Bootstraped and regtested ok on x86_64-linux/f33. Ok for trunk?
>
> Regards,
> 	Andre
>
> Steve Kargl  <kargl@gcc.gnu.org>
>
> PR fortran/98301 - random_init() is broken
>
> Correct implementation of random_init() when -fcoarray=lib is given.
>
> 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/98302
> 	* 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.
>
>
>
> On Sun, 25 Apr 2021 13:03:34 -0700
> Steve Kargl <sgk@troutmask.apl.washington.edu> wrote:
>
> > Andre,
> >
> > The patch looks fine to me.  I wonder, however, if we should
> > comment out all of the shared memory stuff, i.e., the _cas_
> > stuff.  I don't know when Thomas/Nicolas will merge their
> > work-in-progress.
> >
>
>
> --
> Andre Vehreschild * Email: vehre ad gmx dot de


--
Andre Vehreschild * Email: vehre ad gmx dot de

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping, Patch, Fortran, Update 2] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-03  9:21               ` [Ping, Patch, " Andre Vehreschild
@ 2021-05-03 15:20                 ` Steve Kargl
  2021-05-21  8:09                   ` [Ping^2, Patch, Fortran] " Andre Vehreschild
  0 siblings, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-05-03 15:20 UTC (permalink / raw)
  To: Andre Vehreschild; +Cc: GCC-Fortran-ML, GCC-Patches-ML

On Mon, May 03, 2021 at 11:21:10AM +0200, Andre Vehreschild wrote:
> Ping!
> 
> Ok for trunk?
> 
> I have looked at other patches, but none was patching any location I have
> worked on previously. Therefore I can't return the favor of reviewing any
> currently open patches and have to ask for volunteers here.
> 
> - Andre
> 

I doubt I'm allowed to approve a patch, where I wrote a portion
of it.  However, if no one else steps forward in the next day 
or two, then I think you should commit.

-- 
Steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping^2, Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-03 15:20                 ` Steve Kargl
@ 2021-05-21  8:09                   ` Andre Vehreschild
  2021-05-21 15:08                     ` Steve Kargl
  0 siblings, 1 reply; 22+ messages in thread
From: Andre Vehreschild @ 2021-05-21  8:09 UTC (permalink / raw)
  To: Steve Kargl; +Cc: GCC-Fortran-ML, GCC-Patches-ML

[-- Attachment #1: Type: text/plain, Size: 942 bytes --]

Ping, ping!

Please find attached a rebased version of the patch for the RANDOM_INIT issue
with coarray Fortran. Nothing changed to the previous version, just rebased to
current master.

Regtested fine on x86_64-linux/f33. Ok for trunk?

- Andre

On Mon, 3 May 2021 08:20:36 -0700
Steve Kargl <sgk@troutmask.apl.washington.edu> wrote:

> On Mon, May 03, 2021 at 11:21:10AM +0200, Andre Vehreschild wrote:
> > Ping!
> >
> > Ok for trunk?
> >
> > I have looked at other patches, but none was patching any location I have
> > worked on previously. Therefore I can't return the favor of reviewing any
> > currently open patches and have to ask for volunteers here.
> >
> > - Andre
> >
>
> I doubt I'm allowed to approve a patch, where I wrote a portion
> of it.  However, if no one else steps forward in the next day
> or two, then I think you should commit.
>


--
Andre Vehreschild * Email: vehre ad gmx dot de

[-- Attachment #2: pr98301_v2.log --]
[-- Type: text/x-log, Size: 664 bytes --]

Steve Kargl  <kargl@gcc.gnu.org>

PR fortran/98301 - random_init() is broken

Correct implementation of random_init() when -fcoarray=lib is given.

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_v2.patch --]
[-- Type: text/x-patch, Size: 12839 bytes --]

diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index 406b4aeb1d4..c32bd05bb1b 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 4d7451479d3..db9248c0043 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -3827,38 +3827,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

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping^2, Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  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
  0 siblings, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-05-21 15:08 UTC (permalink / raw)
  To: Andre Vehreschild; +Cc: GCC-Fortran-ML, GCC-Patches-ML

On Fri, May 21, 2021 at 10:09:02AM +0200, Andre Vehreschild wrote:
> Ping, ping!
> 
> Please find attached a rebased version of the patch for the RANDOM_INIT issue
> with coarray Fortran. Nothing changed to the previous version, just rebased to
> current master.
> 
> Regtested fine on x86_64-linux/f33. Ok for trunk?
> 

I think you've down your due diligence with 2 pings.
I would commit.

-- 
steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping^2, Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-21 15:08                     ` Steve Kargl
@ 2021-05-22  2:38                       ` Jerry D
  2021-05-22 11:39                         ` Andre Vehreschild
  0 siblings, 1 reply; 22+ messages in thread
From: Jerry D @ 2021-05-22  2:38 UTC (permalink / raw)
  To: Steve Kargl, Andre Vehreschild; +Cc: GCC-Patches-ML, GCC-Fortran-ML

yes, please commit

On 5/21/21 8:08 AM, Steve Kargl via Fortran wrote:
> On Fri, May 21, 2021 at 10:09:02AM +0200, Andre Vehreschild wrote:
>> Ping, ping!
>>
>> Please find attached a rebased version of the patch for the RANDOM_INIT issue
>> with coarray Fortran. Nothing changed to the previous version, just rebased to
>> current master.
>>
>> Regtested fine on x86_64-linux/f33. Ok for trunk?
>>
> I think you've down your due diligence with 2 pings.
> I would commit.
>


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping^2, Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-22  2:38                       ` Jerry D
@ 2021-05-22 11:39                         ` Andre Vehreschild
  2021-05-22 17:58                           ` Martin Liška
  2021-06-05 14:04                           ` [Patch, Fortran, backport 2 gcc-11] " Andre Vehreschild
  0 siblings, 2 replies; 22+ messages in thread
From: Andre Vehreschild @ 2021-05-22 11:39 UTC (permalink / raw)
  To: Jerry D; +Cc: Steve Kargl, GCC-Patches-ML, GCC-Fortran-ML

Hi Steve and Jerry,

thanks for the ok'ing.

Committed as https://gcc.gnu.org/g:26ca6dbda23bc6dfab96ce07afa70ebacedfaf9c
and https://gcc.gnu.org/g:c4771b3438a8cd9afcef1762957b763f8df3fa6e (for the
missing changelog entries).

- Andre

On Fri, 21 May 2021 19:38:00 -0700
Jerry D <jvdelisle2@gmail.com> wrote:

> yes, please commit
>
> On 5/21/21 8:08 AM, Steve Kargl via Fortran wrote:
> > On Fri, May 21, 2021 at 10:09:02AM +0200, Andre Vehreschild wrote:
> >> Ping, ping!
> >>
> >> Please find attached a rebased version of the patch for the RANDOM_INIT
> >> issue with coarray Fortran. Nothing changed to the previous version, just
> >> rebased to current master.
> >>
> >> Regtested fine on x86_64-linux/f33. Ok for trunk?
> >>
> > I think you've down your due diligence with 2 pings.
> > I would commit.
> >
>


--
Andre Vehreschild * Email: vehre ad gmx dot de

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping^2, Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-22 11:39                         ` Andre Vehreschild
@ 2021-05-22 17:58                           ` Martin Liška
  2021-05-23 11:59                             ` Andre Vehreschild
  2021-06-05 14:04                           ` [Patch, Fortran, backport 2 gcc-11] " Andre Vehreschild
  1 sibling, 1 reply; 22+ messages in thread
From: Martin Liška @ 2021-05-22 17:58 UTC (permalink / raw)
  To: Andre Vehreschild, Jerry D; +Cc: GCC-Patches-ML, GCC-Fortran-ML, Steve Kargl

On 5/22/21 1:39 PM, Andre Vehreschild via Gcc-patches wrote:
> Hi Steve and Jerry,
> 
> thanks for the ok'ing.
> 
> Committed as https://gcc.gnu.org/g:26ca6dbda23bc6dfab96ce07afa70ebacedfaf9c
> and https://gcc.gnu.org/g:c4771b3438a8cd9afcef1762957b763f8df3fa6e (for the
> missing changelog entries).

Hello.

About the missing changelog entries. The will be added automatically by Daily bump.
You can check it with:
./contrib/gcc-changelog/git_check_commit.py 26ca6dbda23bc6dfab96ce07afa70ebacedfaf9c -p

What's missing for you so that you pushed c4771b3438a8cd9afcef1762957b763f8df3fa6e?

Thanks,
Martin

> 
> - Andre
> 
> On Fri, 21 May 2021 19:38:00 -0700
> Jerry D <jvdelisle2@gmail.com> wrote:
> 
>> yes, please commit
>>
>> On 5/21/21 8:08 AM, Steve Kargl via Fortran wrote:
>>> On Fri, May 21, 2021 at 10:09:02AM +0200, Andre Vehreschild wrote:
>>>> Ping, ping!
>>>>
>>>> Please find attached a rebased version of the patch for the RANDOM_INIT
>>>> issue with coarray Fortran. Nothing changed to the previous version, just
>>>> rebased to current master.
>>>>
>>>> Regtested fine on x86_64-linux/f33. Ok for trunk?
>>>>
>>> I think you've down your due diligence with 2 pings.
>>> I would commit.
>>>
>>
> 
> 
> --
> Andre Vehreschild * Email: vehre ad gmx dot de
> 


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping^2, Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-22 17:58                           ` Martin Liška
@ 2021-05-23 11:59                             ` Andre Vehreschild
  2021-05-23 12:17                               ` Martin Liška
  0 siblings, 1 reply; 22+ messages in thread
From: Andre Vehreschild @ 2021-05-23 11:59 UTC (permalink / raw)
  To: Martin Liška; +Cc: GCC-Patches-ML, GCC-Fortran-ML

Hi Martin,

thanks for pointing that out. I haven't committed for quite some time now and
could not find on the webpage how this works nowadays. I was thinking that the
special gcc-git-command should have added the Changelog entries automagically
and immediately. That they are added by a daily bump is new to me. Thanks for
giving me more insight.

So do I need to revert the commit c4771b3438a8cd9afcef1762957b763f8df3fa6e to
fix the Changelogs or how do we proceed from here? 

Is there a webpage which describes the current state-of-art of committing to
gcc for folks that do not follow every discussion on the mailing lists?

Thanks for your help,
	Andre
On Sat, 22 May 2021 19:58:57 +0200
Martin Liška <mliska@suse.cz> wrote:

> On 5/22/21 1:39 PM, Andre Vehreschild via Gcc-patches wrote:
> > Hi Steve and Jerry,
> > 
> > thanks for the ok'ing.
> > 
> > Committed as https://gcc.gnu.org/g:26ca6dbda23bc6dfab96ce07afa70ebacedfaf9c
> > and https://gcc.gnu.org/g:c4771b3438a8cd9afcef1762957b763f8df3fa6e (for the
> > missing changelog entries).  
> 
> Hello.
> 
> About the missing changelog entries. The will be added automatically by Daily
> bump. You can check it with:
> ./contrib/gcc-changelog/git_check_commit.py
> 26ca6dbda23bc6dfab96ce07afa70ebacedfaf9c -p
> 
> What's missing for you so that you pushed
> c4771b3438a8cd9afcef1762957b763f8df3fa6e?
> 
> Thanks,
> Martin
> 
> > 
> > - Andre
> > 
> > On Fri, 21 May 2021 19:38:00 -0700
> > Jerry D <jvdelisle2@gmail.com> wrote:
> >   
> >> yes, please commit
> >>
> >> On 5/21/21 8:08 AM, Steve Kargl via Fortran wrote:  
> >>> On Fri, May 21, 2021 at 10:09:02AM +0200, Andre Vehreschild wrote:  
> >>>> Ping, ping!
> >>>>
> >>>> Please find attached a rebased version of the patch for the RANDOM_INIT
> >>>> issue with coarray Fortran. Nothing changed to the previous version, just
> >>>> rebased to current master.
> >>>>
> >>>> Regtested fine on x86_64-linux/f33. Ok for trunk?
> >>>>  
> >>> I think you've down your due diligence with 2 pings.
> >>> I would commit.
> >>>  
> >>  
> > 
> > 
> > --
> > Andre Vehreschild * Email: vehre ad gmx dot de
> >   
> 


-- 
Andre Vehreschild * Email: vehre ad gmx dot de 

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Ping^2, Patch, Fortran] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-23 11:59                             ` Andre Vehreschild
@ 2021-05-23 12:17                               ` Martin Liška
  0 siblings, 0 replies; 22+ messages in thread
From: Martin Liška @ 2021-05-23 12:17 UTC (permalink / raw)
  To: Andre Vehreschild; +Cc: GCC-Patches-ML, GCC-Fortran-ML

On 5/23/21 1:59 PM, Andre Vehreschild wrote:
> Hi Martin,
> 
> thanks for pointing that out. I haven't committed for quite some time now and
> could not find on the webpage how this works nowadays. I was thinking that the
> special gcc-git-command should have added the Changelog entries automagically
> and immediately. That they are added by a daily bump is new to me. Thanks for
> giving me more insight.

Sure, I'm fully aware that occasional committers are not aware of that.

> 
> So do I need to revert the commit c4771b3438a8cd9afcef1762957b763f8df3fa6e to
> fix the Changelogs or how do we proceed from here?

I've just done that.

> Is there a webpage which describes the current state-of-art of committing to
> gcc for folks that do not follow every discussion on the mailing lists?

It's documented here:
https://gcc.gnu.org/codingconventions.html#ChangeLogs

But it's far from a good location. I'm going to improve it.

Cheers,
Martin

> 
> Thanks for your help,
> 	Andre
> On Sat, 22 May 2021 19:58:57 +0200
> Martin Liška <mliska@suse.cz> wrote:
> 
>> On 5/22/21 1:39 PM, Andre Vehreschild via Gcc-patches wrote:
>>> Hi Steve and Jerry,
>>>
>>> thanks for the ok'ing.
>>>
>>> Committed as https://gcc.gnu.org/g:26ca6dbda23bc6dfab96ce07afa70ebacedfaf9c
>>> and https://gcc.gnu.org/g:c4771b3438a8cd9afcef1762957b763f8df3fa6e (for the
>>> missing changelog entries).
>>
>> Hello.
>>
>> About the missing changelog entries. The will be added automatically by Daily
>> bump. You can check it with:
>> ./contrib/gcc-changelog/git_check_commit.py
>> 26ca6dbda23bc6dfab96ce07afa70ebacedfaf9c -p
>>
>> What's missing for you so that you pushed
>> c4771b3438a8cd9afcef1762957b763f8df3fa6e?
>>
>> Thanks,
>> Martin
>>
>>>
>>> - Andre
>>>
>>> On Fri, 21 May 2021 19:38:00 -0700
>>> Jerry D <jvdelisle2@gmail.com> wrote:
>>>    
>>>> yes, please commit
>>>>
>>>> On 5/21/21 8:08 AM, Steve Kargl via Fortran wrote:
>>>>> On Fri, May 21, 2021 at 10:09:02AM +0200, Andre Vehreschild wrote:
>>>>>> Ping, ping!
>>>>>>
>>>>>> Please find attached a rebased version of the patch for the RANDOM_INIT
>>>>>> issue with coarray Fortran. Nothing changed to the previous version, just
>>>>>> rebased to current master.
>>>>>>
>>>>>> Regtested fine on x86_64-linux/f33. Ok for trunk?
>>>>>>   
>>>>> I think you've down your due diligence with 2 pings.
>>>>> I would commit.
>>>>>   
>>>>   
>>>
>>>
>>> --
>>> Andre Vehreschild * Email: vehre ad gmx dot de
>>>    
>>
> 
> 


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Patch, Fortran, backport 2 gcc-11] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-05-22 11:39                         ` Andre Vehreschild
  2021-05-22 17:58                           ` Martin Liška
@ 2021-06-05 14:04                           ` Andre Vehreschild
  2021-06-05 16:27                             ` Steve Kargl
  1 sibling, 1 reply; 22+ messages in thread
From: Andre Vehreschild @ 2021-06-05 14:04 UTC (permalink / raw)
  To: GCC-Fortran-ML; +Cc: GCC-Patches-ML, Jerry D, Steve Kargl

[-- 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

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [Patch, Fortran, backport 2 gcc-11] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-06-05 14:04                           ` [Patch, Fortran, backport 2 gcc-11] " Andre Vehreschild
@ 2021-06-05 16:27                             ` Steve Kargl
  2021-06-06 10:14                               ` [COMITTED, Patch, " Andre Vehreschild
  0 siblings, 1 reply; 22+ messages in thread
From: Steve Kargl @ 2021-06-05 16:27 UTC (permalink / raw)
  To: Andre Vehreschild; +Cc: GCC-Fortran-ML, GCC-Patches-ML, Jerry D

On Sat, Jun 05, 2021 at 04:04:51PM +0200, Andre Vehreschild wrote:
> 
> 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?
> 

I think the backport is fine.  

-- 
Steve

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [COMITTED, Patch, Fortran, backport 2 gcc-11] PR98301 Re: RANDOM_INIT() and coarray Fortran
  2021-06-05 16:27                             ` Steve Kargl
@ 2021-06-06 10:14                               ` Andre Vehreschild
  0 siblings, 0 replies; 22+ messages in thread
From: Andre Vehreschild @ 2021-06-06 10:14 UTC (permalink / raw)
  To: Steve Kargl; +Cc: GCC-Fortran-ML, GCC-Patches-ML, Jerry D, Damian Rouson

Hi Steve, hi all,

the patch for pr98301 has been backported to gcc-11 as
002745ca3668fc5e87c22acc81caaeaaadf9c47a

Regards,
	Andre

On Sat, 5 Jun 2021 09:27:16 -0700
Steve Kargl <sgk@troutmask.apl.washington.edu> wrote:

> On Sat, Jun 05, 2021 at 04:04:51PM +0200, Andre Vehreschild wrote:
> >
> > 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?
> >
>
> I think the backport is fine.
>


--
Andre Vehreschild * Email: vehre ad gmx dot de

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2021-06-06 10:15 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-03 17:28 RANDOM_INIT() and coarray Fortran Steve Kargl
2021-04-04  3:30 ` Damian Rouson
2021-04-04  5:33   ` Steve Kargl
2021-04-23 16:43     ` [Patch, Fortran] PR98301 " Andre Vehreschild
2021-04-23 17:18       ` Steve Kargl
2021-04-24 10:49         ` [Patch, Fortran, Update] " Andre Vehreschild
2021-04-24 15:44           ` Steve Kargl
2021-04-24 15:56             ` Dr. Andre Vehreschild
2021-04-25 20:03           ` Steve Kargl
2021-04-26 10:36             ` [Patch, Fortran, Update 2] " Andre Vehreschild
2021-05-03  9:21               ` [Ping, Patch, " 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                           ` [Patch, Fortran, backport 2 gcc-11] " Andre Vehreschild
2021-06-05 16:27                             ` Steve Kargl
2021-06-06 10:14                               ` [COMITTED, Patch, " Andre Vehreschild

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).