From: Lawrence D'Oliveiro <ldo@nz.invalid>
Subject: Re: Parallel Sieve Of Eratosthenes
Date: Sun, 30 Jun 2024 08:10:06 -0000 (UTC) [thread overview]
Message-ID: <v5r3su$e60t$2@dont-email.me> (raw)
In-Reply-To: v5r3ma$e60t$1@dont-email.me
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;
next prev parent reply other threads:[~2024-06-30 8:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-30 8:06 Parallel Sieve Of Eratosthenes Lawrence D'Oliveiro
2024-06-30 8:10 ` Lawrence D'Oliveiro [this message]
2024-06-30 16:36 ` J-P. Rosen
2024-07-01 0:02 ` Lawrence D'Oliveiro
2024-07-01 9:17 ` J-P. Rosen
2024-07-01 21:48 ` Lawrence D'Oliveiro
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox