comp.lang.ada
 help / color / mirror / Atom feed
From: "G.B." <bauhaus@notmyhomepage.invalid>
Subject: cobegin ... coend
Date: Sat, 1 Jul 2017 09:11:57 +0200
Date: 2017-07-01T09:11:57+02:00	[thread overview]
Message-ID: <oj7hos$4p8$1@dont-email.me> (raw)

A chapter about concurrent processes an older book
introduces the notation
      *cobegin* S1; S2; ...; Sn *coend*.
It is also presenting a procedure as an example. An attempt
at translating it into Ada is given below.

Ignoring the premiss that Ada processes are tasks and
that designing algorithms should therefore not ignore
tasks, is there a known good way of translating the
start of processes from *cobegin*...*coend* style to tasks?
Or from OpenMP to tasks, I should think. (Paraffin only?)

The example procedure Copy copies a sequence of records to
another sequence, emptying the source. Package Sequences is
an instance of Ada.Containers.Doubly_Linked_Lists.

    procedure Get (Item     :    out Copyable;
                   Sequence : in out Sequences.List);

    procedure Put (Item     : in     Copyable;
                   Sequence : in out Sequences.List);

    procedure Copy (F, G : in out Sequences.List)
    is
       S, T      : Copyable;
       Completed : Boolean;
    begin
       if not F.Is_Empty then
          Completed := False;
          Get (S, F);
          loop
             T := S;
             -- cobegin
             Put (T, G);
             if F.Is_Empty then
                Completed := True;
             else
                Get (S, F);
             end if;
             -- coend
             exit when Completed;
          end loop;
       end if;
    end Copy;

The "processes" in the marked section, i.e. Put
and if-statement use disjoint sets of variables.

My first translation has picked the easier part, by having
a local task perform Put (T, G).

    procedure Copy_1 (F, G : in out Sequences.List)
    is
       S, T      : Copyable;
       Completed : Boolean;

       task Assistant is
          entry Put;
       end Assistant;

       task body Assistant is
       begin
          loop
             select accept Put; Put (T, G);
             or terminate;
             end select;
          end loop;
       end Assistant;

    begin
       if not F.Is_Empty then
          Completed := False;
          Get (S, F);
          loop
             T := S;
             -- cobegin
             Assistant.Put;
             if F.Is_Empty then
                Completed := True;
             else
                Get (S, F);
             end if;
             -- coend
             exit when Completed;
          end loop;
       end if;
    end Copy_1;



The second "process" between *cobegin* and *coend* seemed trickier,
as the surrounding control structure depends on the process's result,
i.e., on variable Completed having been set in time. So, the loop
needs to wait for another rendezvous, for example.

Are such things planned for Ada 2X's new parallel features?

             reply	other threads:[~2017-07-01  7:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-01  7:11 G.B. [this message]
2017-07-01 10:47 ` cobegin ... coend Lucretia
2017-07-03  0:17 ` Randy Brukardt
replies disabled

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