public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Steve Kargl <sgk@troutmask.apl.washington.edu>
To: Thomas Koenig <tkoenig@netcologne.de>
Cc: fortran@gcc.gnu.org, kargl@gcc.gnu.org
Subject: Re: co-arrays and libgfortran
Date: Fri, 18 Dec 2020 07:52:52 -0800	[thread overview]
Message-ID: <20201218155252.GB96460@troutmask.apl.washington.edu> (raw)
In-Reply-To: <203c7d27-2303-f887-ea57-11b860f18a8f@netcologne.de>

On Fri, Dec 18, 2020 at 01:26:37PM +0100, Thomas Koenig wrote:
> 
> Am 18.12.20 um 05:28 schrieb Steve Kargl via Fortran:
> > Dear All,
> > 
> > At the risk of causing consternation among the few
> > active gfortran developers, I'll offer the observation
> > that libgfortran likely need to be built with -fcoarray=lib
> > if one wants to use OpenCoarrays.
> 
> I hope there can be another solution :-)
> 

One of these days, I'll figure a way to grab Nicolas and
your work.  My svn gcc tree is almost a year out of date.


> > I'm not sure what
> > the status of shared memory coarrays is.
> 
> It is starting to be usable.  It works for Toon's "random weather"
> test program, and there is a growing number of test cases that work
> (see https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=gcc/testsuite/gfortran.dg/caf-shared;hb=refs/heads/devel/coarray_native
> )
> but there are still quite a large number of things that don't.
> 
> > Why, you ask?
> 
> > J3 defined RANDOM_INIT() in a manner that requires it to be
> > aware of the backing coarray communication method.  That is,
> > RANDOM_INIT(repeatable=.false., image_distinct=.false.)
> > requires communication between image 1 and all other images
> > (if num_image > 1).  'repeatable=.false.' means that each
> > time a program is run, a different set of processor-dependent
> > seeds are used on image 1.  This is trivial to do with
> > Janne's reworking of random_seed() when he introduce gfortran
> > to xshiro++.  Now, if num_images() > 1, then all other images
> > are required to use the same set of seeds as image 1 to initial
> > their PRNG.  Therefore, the other images must ask image 1 for
> > its seeds.  I don't use co-arrays, so I'm unsure of the
> >   semantics; but, I have used MPI.  With MPI, one would have
> > image 1 broadcast the seeds and all other images would receive
> > the seeds.
> 
> The best way is probably to call different random_init routines
> depending on the value of flag_coarray.

That's what random_init() does.  The code for libgfortran/intrinsic/
random_init.f90 in the patch I submitted yesterday is cleaner to 
read and describes what I'm doing.

In particular, conv_intrinsic_random_init converts

  call random_init(repeatable, image_distinct)

to (could be some pass-by-value in here)
 
  _gfortran_random_init(repeatable, image_distinct, hidden)

where hidden is the value returned by this_image().  When I wrote
the function, this_image() maps to the library function in OpenCoarrays
via gfor_fndecl_caf_this_image.

  /* 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);
    }

If  gfor_fndecl_caf_this_image is rerouted to a shared memory implementation
for this_image(), then we're done; otherwise, we need to add an 

  else if (flag_coarray == GFC_FCOARRAY_SHARED)
    {
       /* map hidden to shared memory coarray's this_image() */
    }

> However, I am seeing some problems with synchronization with that
> approach. The standard does not say anything about needing SYNC ALL
> after random_init, or about implied synchronization.  That means
> copying could be problematic.
> 
> Hmm... I'll have to think about this somre more.

You're probably aware that the images are created async.

   5.3.4 Program execution

   Execution of a program consists of the asynchronous execution of
   a fixed number (which may be one) of its images.  Each image has
   its own execution state, floating-point status (17.7), and set of
   data objects, input/output units, and procedure pointers.

with MPI, the psuedocode would be

   if (this_image() == 1) then
      call random_seed()  ! processor-dependent seeds
      call mpi_send(seeds, ..., mpi_comm_world, ...)
   else
      call mpi_recv(seeds, ..., mpi_comm_world, ...)
   end if

I don't know if memory barriers are needed. As random_init.f90 is
Fortran, the psuedocode might be

   if (hidden < 2) then  
      ! get seeds on image 1
      call random_seed()
      call random_seed(get=seeds)
   else
      call co_broadcast(seeds,1)
   end if

This, however, would require libgfortran to be compiled with -fcoarry=lib
or shared.

-- 
Steve

  reply	other threads:[~2020-12-18 15:52 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-18  4:28 Steve Kargl
2020-12-18 12:26 ` Thomas Koenig
2020-12-18 15:52   ` Steve Kargl [this message]
2020-12-19 21:59     ` Steve Kargl
2020-12-20 10:52       ` Thomas Koenig
2020-12-20 17:47         ` Steve Kargl
2020-12-21  1:04           ` Steve Kargl
2020-12-21  2:01             ` Damian Rouson
2020-12-21 13:15     ` Thomas Koenig
2020-12-21 15:18       ` Steve Kargl
2020-12-21 23:44         ` Thomas Koenig
2020-12-22  0:18           ` Steve Kargl
2020-12-22  1:40             ` Steve Kargl
2020-12-22 10:04               ` Thomas Koenig
2020-12-22 18:32                 ` Steve Kargl
2020-12-22 19:33                   ` Steve Kargl
2020-12-22 20:12                     ` Steve Kargl
2020-12-22 20:20                       ` Steve Kargl
2020-12-22 22:08                         ` Steve Kargl
2020-12-22 22:22                           ` Steve Kargl
2020-12-22 23:24               ` Steve Kargl
2020-12-22 23:46                 ` Thomas Koenig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201218155252.GB96460@troutmask.apl.washington.edu \
    --to=sgk@troutmask.apl.washington.edu \
    --cc=fortran@gcc.gnu.org \
    --cc=kargl@gcc.gnu.org \
    --cc=tkoenig@netcologne.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).