comp.lang.ada
 help / color / mirror / Atom feed
From: "(see below)" <yaldnif.w@blueyonder.co.uk>
Subject: Re: Robert Dewar's great article about the Strengths of Ada over other langauges in multiprocessing!
Date: Mon, 31 Mar 2008 15:17:56 +0100
Date: 2008-03-31T15:17:56+01:00	[thread overview]
Message-ID: <C416B124.E2182%yaldnif.w@blueyonder.co.uk> (raw)
In-Reply-To: C4169FA1.E214C%yaldnif.w@blueyonder.co.uk

On 31/03/2008 14:03, in article C4169FA1.E214C%yaldnif.w@blueyonder.co.uk,
"(see below)" <yaldnif.w@blueyonder.co.uk> wrote:

> On 31/03/2008 08:59, in article wvbrd4pbz5mj.fsf@astra06.norway.sun.com,
> "Ole-Hjalmar Kristensen"
> <ole-hjalmar.kristensen@substitute_employer_here.com> wrote:
>> If you do not refresh the cache at the beginning of a protected
>> procedure, how do you avoid reading stale data within the protected
>> object on a multiprocessor? And why the wording "cannot be used to
>> synchronize access to other *nonvolatile* shared variables"? Is the
>> implication that it *can* be used to synchronize other *volatile*
>> shared variables?
> 
> I think the wording is trying to cover all the bases.
> One clue is the phrase "_full_ cache consistency".
> The caches certainly need to be consistent with respect to the
> protected data, even for protected functions and procedures,
> but only entries ensure global consistency and so provide
> synchronization of data that is not local to the protected object.

In practice, I would expect even protected functions and procedures to need
a mutex, and that that would execute a memory barrier operation, so
enforcing global consistency. To verify this I ran some simple tests myself.

I implemented Simpson's algorithm for a lock-free shared variable.
Here is the relevant code:

> package Wait_Free_Atomicity is
> 
>     type an_Atomic is limited private;
>     
>     procedure Update  (Atomic_Item : in out an_Atomic; Item : in  an_Item);
>     procedure Inspect (Atomic_Item : in out an_Atomic; Item : out an_Item);
> 
> private
> 
>     type a_Bistate is new Boolean;
>     pragma Atomic (a_Bistate);
> 
>     type a_Slot_Matrix is array (a_Bistate, a_Bistate) of an_Item;
>     pragma Volatile_Components (a_Slot_Matrix);
>             
>     type a_Full_Column is array (a_Bistate) of a_Bistate;
>     pragma Atomic_Components (a_Full_Column);
>    
>    type an_Atomic is
>       limited record
>             Data_Slot_Matrix    : a_Slot_Matrix;
>             Last_Column_Updated : a_Full_Column  :=
>                   (others => a_Bistate'First);
>             Last_Row_Inspected, Last_Row_Updated : a_Bistate :=
>                   a_Bistate'First;
>             pragma Atomic (Last_Row_Inspected);
>             pragma Atomic (Last_Row_Updated);
>         end record;
> 
> end Wait_Free_Atomicity;
> 
> package body Wait_Free_Atomicity is
>         
>     procedure Update (Atomic_Item : in out an_Atomic; Item : in  an_Item) is
>         Row : constant a_Bistate := not Atomic_Item.Last_Row_Inspected;
>         Col : constant a_Bistate := not Atomic_Item.Last_Column_Updated(Row);
>     begin
>         Atomic_Item.Data_Slot_Matrix(Row, Col) := Item;
>         Atomic_Item.Last_Column_Updated(Row) := Col;
>         Atomic_Item.Last_Row_Updated := Row;
>         -- no explicit membar sync
>     end Update;
>     
>     procedure Inspect (Atomic_Item : in out an_Atomic; Item : out an_Item) is
>         Row : constant a_Bistate := Atomic_Item.Last_Row_Updated;
>         Col : a_Bistate;
>         pragma Atomic (Col);
>     begin
>         Atomic_Item.Last_Row_Inspected := Row;
>         Col  := Atomic_Item.Last_Column_Updated(Row);
>         Item := Atomic_Item.Data_Slot_Matrix(Row, Col);
>         -- no explicit membar sync
>     end Inspect;
> 
> end Wait_Free_Atomicity;

The test was to run it with the Item type being a 5-tuple of consecutive
integers, and checking that the inspecting task received correct tuples.

When run on a single-processor machine (Mac PowerBook) there were no
consistency failures.

When run on a dual-core machine (MacBook Pro with Core 2 Duo CPU) there
were many consistency failures.

When the body was modified as follows:

> package body Wait_Free_Atomicity is
>     
>     protected Mem_Bar is
>        entry Sync;
>     end Mem_Bar;
>     
>     protected body Mem_Bar is
>        entry Sync when Boolean'(True) is
>        begin
>           null;
>        end Sync;
>     end Mem_Bar;
>     
>     procedure Update (Atomic_Item : in out an_Atomic; Item : in  an_Item) is
>         Row : constant a_Bistate := not Atomic_Item.Last_Row_Inspected;
>         Col : constant a_Bistate := not Atomic_Item.Last_Column_Updated(Row);
>     begin
>         Atomic_Item.Data_Slot_Matrix(Row, Col) := Item;
>         Atomic_Item.Last_Column_Updated(Row) := Col;
>         Atomic_Item.Last_Row_Updated := Row;
>         Mem_Bar.Sync;
>     end Update;
>     
>     procedure Inspect (Atomic_Item : in out an_Atomic; Item : out an_Item) is
>         Row : constant a_Bistate := Atomic_Item.Last_Row_Updated;
>         Col : a_Bistate;
>         pragma Atomic (Col);
>     begin
>         Atomic_Item.Last_Row_Inspected := Row;
>         Col  := Atomic_Item.Last_Column_Updated(Row);
>         Item := Atomic_Item.Data_Slot_Matrix(Row, Col);
>         Mem_Bar.Sync;
>     end Inspect;
> 
> end Wait_Free_Atomicity;

Note that the tuples are external to the protected object.

The consistency failures went away.
The same result held when the Mem_Bar.Sync entry was replaced by a protected
procedure; ditto with a protected function.

For the sake of interest, here are the test results, with execution times
(CPU times exceed real times because there are 2 cores working
simultaneously):

20_000_000 updates

with no sync:
3689 consistency failures.
        0.90 real         0.73 user         0.09 sys

with entry sync:
No consistency failures.
      222.90 real       138.52 user       265.78 sys

with procedure sync:
No consistency failures.
      149.36 real       127.65 user       164.96 sys

with function sync:
No consistency failures.
      142.43 real       120.33 user       155.93 sys

-- 
Bill Findlay
<surname><forename> chez blueyonder.co.uk





  reply	other threads:[~2008-03-31 14:17 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-08  6:04 Robert Dewar's great article about the Strengths of Ada over other langauges in multiprocessing! ME
2008-03-08 22:11 ` Maciej Sobczak
2008-03-09  1:09   ` Christopher Henrich
2008-03-09 13:52     ` Maciej Sobczak
2008-03-09  1:51   ` Phaedrus
2008-03-09  3:17     ` Jeffrey R. Carter
2008-03-09 13:59     ` Maciej Sobczak
2008-03-09  3:15   ` Jeffrey R. Carter
2008-03-09 13:32     ` Maciej Sobczak
2008-03-09 14:02       ` Dmitry A. Kazakov
2008-03-09 18:26       ` Phaedrus
2008-03-10  0:04         ` Ray Blaak
2008-03-10  7:49           ` Georg Bauhaus
2008-03-10 16:48             ` Ray Blaak
2008-03-10  7:53           ` Phaedrus
2008-03-09 22:31       ` Jeffrey R. Carter
2008-03-10  3:53         ` gpriv
2008-03-10  3:04       ` Robert Dewar's great article about the Strengths of Ada over Larry Kilgallen
2008-03-10  9:23         ` Maciej Sobczak
2008-03-10 19:01           ` Jeffrey R. Carter
2008-03-10 22:00             ` Maciej Sobczak
2008-03-11  0:48               ` Jeffrey R. Carter
2008-03-11  7:12                 ` Pascal Obry
2008-03-11  8:59                 ` Maciej Sobczak
2008-03-11  9:49                   ` GNAT bug, Assert_Failure at atree.adb:2893 Ludovic Brenta
2008-03-14 20:03                   ` Robert Dewar's great article about the Strengths of Ada over Ivan Levashew
2008-03-22 21:12           ` Florian Weimer
2008-03-09  8:20   ` Robert Dewar's great article about the Strengths of Ada over other langauges in multiprocessing! Pascal Obry
2008-03-09  9:39     ` Georg Bauhaus
2008-03-09 12:40     ` Vadim Godunko
2008-03-09 13:37       ` Dmitry A. Kazakov
2008-03-09 14:41         ` Vadim Godunko
2008-03-10 20:51           ` Randy Brukardt
2008-03-10 22:30             ` Niklas Holsti
2008-03-10  9:56         ` Ole-Hjalmar Kristensen
2008-03-11 13:58       ` george.priv
2008-03-11 15:41         ` Vadim Godunko
2008-03-12  0:32           ` gpriv
2008-03-12 13:33             ` Maciej Sobczak
2008-03-12 14:41               ` gpriv
2008-03-12 15:22                 ` Vadim Godunko
2008-03-13  0:34                   ` gpriv
2008-03-12 16:28                 ` Maciej Sobczak
2008-03-12 17:24                   ` Samuel Tardieu
2008-03-13  8:41                     ` Maciej Sobczak
2008-03-13 15:20                       ` Samuel Tardieu
2008-03-12 23:54                   ` gpriv
2008-03-13  9:40                     ` Maciej Sobczak
2008-03-13 10:49                       ` Peter C. Chapin
2008-03-13 13:03                         ` Alex R. Mosteo
2008-03-13 14:02                           ` gpriv
2008-03-14  1:12                           ` Randy Brukardt
2008-03-14 10:16                             ` Alex R. Mosteo
2008-03-13 11:42                       ` gpriv
2008-03-13 16:10                         ` Maciej Sobczak
2008-03-13 16:16                           ` gpriv
2008-03-13 22:01                             ` Simon Wright
2008-03-13 22:25                             ` Maciej Sobczak
2008-03-14  2:07                               ` gpriv
2008-03-14  9:29                                 ` Maciej Sobczak
2008-03-14 21:54                                 ` Simon Wright
2008-03-15  2:29                                   ` gpriv
2008-03-15 13:29                                     ` Maciej Sobczak
2008-03-15 16:09                                       ` gpriv
2008-03-11 22:09       ` gpriv
2008-03-09 13:50     ` Maciej Sobczak
2008-03-09 14:54       ` Pascal Obry
2008-03-10 21:24   ` Randy Brukardt
2008-03-11 10:12     ` Alex R. Mosteo
2008-03-22 22:43     ` Florian Weimer
2008-03-26 13:49       ` Ole-Hjalmar Kristensen
2008-03-26 21:27         ` Florian Weimer
2008-03-27  9:31           ` Ole-Hjalmar Kristensen
2008-03-27 23:10             ` Florian Weimer
2008-03-28  9:51               ` Ole-Hjalmar Kristensen
2008-03-28 18:12                 ` Florian Weimer
2008-03-28 21:45                   ` Randy Brukardt
2008-03-31  7:59                   ` Ole-Hjalmar Kristensen
2008-03-31 13:03                     ` (see below)
2008-03-31 14:17                       ` (see below) [this message]
2008-04-01  9:02                       ` Ole-Hjalmar Kristensen
2008-04-01 14:12                         ` (see below)
2008-04-02  7:22                           ` Ole-Hjalmar Kristensen
2008-04-02 14:59                             ` (see below)
2008-04-04  6:36                               ` Ole-Hjalmar Kristensen
2008-04-04 13:56                                 ` (see below)
2008-04-04 17:36                                   ` Georg Bauhaus
2008-04-04 17:40                                     ` (see below)
2008-04-15 12:05                               ` Ole-Hjalmar Kristensen
2008-04-17  4:46                                 ` Randy Brukardt
2008-03-28  6:34             ` Randy Brukardt
2008-04-29  7:15   ` Ivan Levashew
2008-05-01  2:03     ` Steve Whalen
2008-03-14 19:20 ` Mike Silva
2008-03-14 20:43   ` Ed Falis
2008-03-22 22:51 ` Florian Weimer
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox