From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 73619 invoked by alias); 8 Dec 2015 09:26:09 -0000 Mailing-List: contact fortran-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: fortran-owner@gcc.gnu.org Received: (qmail 73565 invoked by uid 89); 8 Dec 2015 09:26:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: outpost18.zedat.fu-berlin.de Received: from outpost18.zedat.fu-berlin.de (HELO outpost18.zedat.fu-berlin.de) (130.133.4.111) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 08 Dec 2015 09:26:05 +0000 Received: from relay1.zedat.fu-berlin.de ([130.133.4.67]) by outpost.zedat.fu-berlin.de (Exim 4.85) with esmtp (envelope-from ) id <1a6EX4-00203m-JR>; Tue, 08 Dec 2015 10:26:02 +0100 Received: from mx.physik.fu-berlin.de ([160.45.64.218]) by relay1.zedat.fu-berlin.de (Exim 4.85) with esmtps (envelope-from ) id <1a6EX4-000EIR-HO>; Tue, 08 Dec 2015 10:26:02 +0100 Received: from login1.physik.fu-berlin.de ([160.45.66.207]) by mx.physik.fu-berlin.de with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1a6EWz-0001o0-8k; Tue, 08 Dec 2015 10:25:57 +0100 Received: from tburnus by login1.physik.fu-berlin.de with local (Exim 4.80 #3 (Debian)) id 1a6EWz-0005vt-21; Tue, 08 Dec 2015 10:25:57 +0100 Date: Tue, 08 Dec 2015 09:26:00 -0000 From: Tobias Burnus To: Matthew Wahab Cc: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org, Alessandro Fanfarillo Subject: Re: [Fortran, Patch] Memory sync after coarray image control statements and assignment Message-ID: <20151208092556.GB29964@physik.fu-berlin.de> References: <20151207100645.GA28286@physik.fu-berlin.de> <56659312.6090700@foss.arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <56659312.6090700@foss.arm.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-SW-Source: 2015-12/txt/msg00057.txt.bz2 On Mon, Dec 07, 2015 at 02:09:22PM +0000, Matthew Wahab wrote: > >>I wonder whether using > >>__asm__ __volatile__ ("":::"memory"); > >>would be sufficient as it has a way lower overhead than > >>__sync_synchronize(). > > I don't know anything about Fortran or coarrays and I'm curious > whether this affects architectures with weak memory models. Is the > barrier only needed to stop reordering by the compiler or is does it > also need to stop reordering by the hardware? Short answer: I think no mfence is needed as either the communication is local (to the thread/process) - in which case the hardware will act correctly - or the communication is remote (different thread, process, communication to different computer via interlink [ethernet, infiniband, ...]); and in the later case, the communication library has to deal with it. However, I think that except for SYNC using __asm__ __volatile__ ("":::"memory"); is the wrong solution - even though it might work as band aid. * * * To gether some suggestions, here is how coarrays work. They are based on: everything is a local variable - except it is a coarray. And all communication is explicit - but is often single sideded. That scheme works well (also) with distributed memory and is similar to MPI (Message Passing Interface). Using integer :: var[*] one declares a coarray scalar variable. Accessing it as var = 5 tmp = var acts on the variable on the current 'image' (process). Using var[idx] = 5 ! Set 'var' in image 'idx' to '5' tmp = var[idx] ! Obtain value of 'var' on image 'idx' one accesses that variable on a remote image - except if 'idx' matches the current image; in that case, it acts on the local variable. In one segment (which ends at explicit or implicit synchronizations, e.g. using SYNC ALL): If the value of a is changed - either locally or by a remote image - then only that image is permitted to 'read' the value (or to change it as that would be another possibility for race conditions). Very often, coarray variables are arrays and heavily accessed as local varable in hot loops. But on the other hand, the value of the variable can be changed by external means - up to having hardware support to write into the memory from another computer. Thus, there two cases were the local view might fail: (a) when the variable has been changed in a previous segment by a remote process, e.g. var = 5 ! assue 'var' is a coarray sync all ! end of segment ! ! var is changed by a remote image sync all ! end of segment tmp = var where "var" might or might not have the value 5. Or more fancy without locks and global barriers: type(event_type) :: var_is_set[*] var = 5 event post(have_set_var[remote_idx]) ! Remote waits for event, sets out 'var' to 42 and ! then posts an event that it is set event wait(have_set_var) tmp = var where one tells the remote process that "var = 5" has been set and waits until it has set the local variable "var". (b) when the variable is changed on the same image by two means, e.g. var = 5 var[this_image()] = 42 tmp = var The communication maps to function calls like: __gfortran_caf_send(var_token, idx, 5) // var[idx] = 5 __gfortran_caf_get (tmp, var_token, idx) // tmp = var[idx] [Real commands, see https://gcc.gnu.org/onlinedocs/gfortran/index.html#toc_Coarray-Programming] It is assumed that the library called takes care of the hardware part, i.e. locking, mfence etc. - such that in the compiler itself, one only has to deal with restricting compiler optimizations to the places where it is permitted. A coarray comes into existance via: var = _gfortran_caf_register (size, &var_token); similar to malloc - and var_token identifies the coarray; what the content is, is left to the library. - For the good or worse, _gfortran_caf_register doesn't tell the compiler that it has clobbered "var" as in a way the pointer address escaped. Nor is "var" marked as volatile. Coming back to item (a): After a segment has ended (SYNC ALL, EVENT WAIT or similar), the compiler can no longer assume that coarray variables have the same value. Those variables can be hidden as in call foo() sync all call foo() where 'foo' doesn't know about the 'sync all' and the caller doesn't know whether 'foo' has accesses to coarrays (and whether it accesses them). I think __asm__ __volatile__ ("":::"memory"); should be sufficient in that case, assuming that the communication library takes are of making all changes available (mfence, __sync_synchronize or whatever). The other question is item (b): Here, one has an alias problem within the segment - but the alias only rarely happens. Code of the form var[idx] = ... usually does not access the local memory; it happens only in corner cases like: do i = 1, num_images var[i] = 5 end do which sets 'var' to 5 on all images - including the local image. This will produce code like (full example below): *var = 5 _gfortran_caf_send(var_token, idx, 42); tmp = *var where the frontend knows that _gfortran_caf_send might modify 'var' iff idx matches the local image. The question is only how to tell this the compiler. Using __asm__ __volatile__ ("":::"memory"); seems to be appropriate, even though something more tailored would be nice, something like: __asm__ __volatile__ ("": : "+rm" (*var) : "") but I am not sure whether that's valid and works - additionally, it assumes that one reads "*var" (i.e. that it is defined before), which is not required. (No idea whether -Wundefined would trigger.) I there some better way to express this? Cheers, Tobias PS: Full example: integer :: tmp integer :: var[*] var = 5 var[this_image()] = 42 tmp = var end This will generate a static constructor, run at program startup: _caf_init.1 () { // v-- allocate 4 byte var = _gfortran_caf_register (4, 0, &caf_token.0, 0B, 0B, 0); } and the (main) program code (slightly trimmed): static void * restrict caf_token.0; static integer(kind=4) * restrict var; void _caf_init.1 (void); *var = 4; desc.3.data = 42; _gfortran_caf_send (caf_token.0, 0B /* offset */ var, _gfortran_caf_this_image (0), &desc.2, 0B, &desc.3, 4, 4, 0); __asm__ __volatile__("":::"memory"); // new tmp = *var; The problem is that in that case the compiler does not know that "_gfortran_caf_send (caf_token.0," can modify "*var".