Deutsch   English   Français   Italiano  
<v5r3su$e60t$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D'Oliveiro <ldo@nz.invalid>
Newsgroups: comp.lang.ada
Subject: Re: Parallel Sieve Of Eratosthenes
Date: Sun, 30 Jun 2024 08:10:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <v5r3su$e60t$2@dont-email.me>
References: <v5r3ma$e60t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 30 Jun 2024 10:10:07 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="4341965ad4e2fb75a1b93d496a5627b7";
	logging-data="464925"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+GR5R2Gt50z9jYlJFQNE/o"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:uXIMRkV4XtqfufG/zU8XZX4uAcc=
Bytes: 2733

This version uses a protected type to pass the stream of integers from
one task to the next. It seems to be much faster.
----
with Ada.Text_IO;
use Ada;
procedure parasieve2 is

    protected type int_buffer is

        entry put(i : in integer);
        entry get(i : out integer);

    private
        last_i : integer;
        got_i : boolean := false;
    end int_buffer;

    protected body int_buffer is

        entry put(i : in integer) when not got_i is
        begin
            last_i := i;
            got_i := true;
        end put;

        entry get(i : out integer) when got_i is
        begin
            i := last_i;
            got_i := false;
        end get;

    end int_buffer;

    type int_buffer_ptr is access int_buffer;

    task type child(from_parent : int_buffer_ptr) is
    end child;

    subtype offspring is child;
      -- need another name because "child" within child refers to
      -- current task, not to the type

    task body child is

        my_prime, i : integer;
        subchild : access offspring;
        to_child : int_buffer_ptr;

    begin
        from_parent.get(my_prime);
        Text_IO.Put_line(integer'image(my_prime));
        to_child := new int_buffer;
        subchild := new offspring(to_child);
        loop
            from_parent.get(i);
            if i mod my_prime /= 0 then
                to_child.put(i);
            end if;
        end loop;
    end child;

    to_first_child : int_buffer_ptr;
    first_child : access child;
    i : integer;

begin -- parasieve2
    i := 1;
    to_first_child := new int_buffer;
    first_child := new child(to_first_child);
    loop
        i := i + 1;
        to_first_child.put(i);
    end loop;
end parasieve2;