comp.lang.ada
 help / color / mirror / Atom feed
* 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously.
@ 2020-07-20 13:51 Olivier Henley
  2020-07-20 13:55 ` Olivier Henley
  2020-07-20 17:45 ` Jeffrey R. Carter
  0 siblings, 2 replies; 5+ messages in thread
From: Olivier Henley @ 2020-07-20 13:51 UTC (permalink / raw)


Hi everyone,

My goal is to distribute similar work across multiple tasks. Incidentally, I want those tasks to start simultaneously, each task having some 'indexed' work (discriminants), and ideally block from main until they are done with their workload.

I got this working by declaring the tasks individually. What I would like to achieve is to leverage 'System.Multiprocessors.Number_Of_CPUs' for the number of tasks created. What is the idiomatic way of achieving what I want?

I tried with an array of tasks, but the problem becomes I do not know either how to start them simultaneously with parametrization or coordinate their exit point with main.  

I am lurking for the most 'clean/simple' solution possible.  

You can see the actual working code fixed at 8 tasks here:

https://github.com/ohenley/xph_covid19/blob/master/src/xph_covid19.adb#L280-L287

Thx, Olivier

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously.
  2020-07-20 13:51 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously Olivier Henley
@ 2020-07-20 13:55 ` Olivier Henley
  2020-07-20 17:45 ` Jeffrey R. Carter
  1 sibling, 0 replies; 5+ messages in thread
From: Olivier Henley @ 2020-07-20 13:55 UTC (permalink / raw)



> https://github.com/ohenley/xph_covid19/blob/master/src/xph_covid19.adb#L280-L287

I forgot to mention, on my system, Number_Of_CPUs returns 8.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously.
  2020-07-20 13:51 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously Olivier Henley
  2020-07-20 13:55 ` Olivier Henley
@ 2020-07-20 17:45 ` Jeffrey R. Carter
  2020-07-20 20:31   ` Olivier Henley
  1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey R. Carter @ 2020-07-20 17:45 UTC (permalink / raw)


On 7/20/20 3:51 PM, Olivier Henley wrote:
> 
> My goal is to distribute similar work across multiple tasks. Incidentally, I want those tasks to start simultaneously, each task having some 'indexed' work (discriminants), and ideally block from main until they are done with their workload.
> 
> I tried with an array of tasks, but the problem becomes I do not know either how to start them simultaneously with parametrization or coordinate their exit point with main.

For simple parameterization, you can use a discriminant with a default that is a 
function call:

subtype Task_ID is Integer range 0 .. System.Multiprocessors.Number_Of_CPUs;
subtype Valid_Task_ID is Task_ID range 1 .. Task_ID'Last;

Last : Task_ID := 0;

function Next_ID return Valid_Task_ID is
begin
    Last := Last + 1;

    return Last;
end Next_ID;

task type T (ID : Valid_Task_ID := Next);

type T_Set is array (Valid_Task_ID) of T;

Worker : T_Set;

Each Worker (i) will have its ID determined during elaboration, and they will 
all start at the "begin" that follows the declaration of Worker. The order of 
the discriminants is arbitrary; there is no guarantee that Worker (I) will have 
ID of I. Elaboration is sequential, so each Worker will have a unique ID.

[I think Ada 2X will allow a way to insure that the ID equals the index, but I'm 
not sure how it will work.]

To block a subprogram until the Worker tasks all complete, declare them in a 
block statement:

Create_Workers : declare
    Worker : T_Set;
begin
    null;
end Create_Workers;

-- 
Jeff Carter
"You've got the brain of a four-year-old boy,
and I bet he was glad to get rid of it."
Horse Feathers
47

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously.
  2020-07-20 17:45 ` Jeffrey R. Carter
@ 2020-07-20 20:31   ` Olivier Henley
  2020-07-22 19:05     ` onox
  0 siblings, 1 reply; 5+ messages in thread
From: Olivier Henley @ 2020-07-20 20:31 UTC (permalink / raw)


> The order of the discriminants is arbitrary; there is no guarantee that Worker (I) will have ID of I. Elaboration is sequential, so each Worker will have a unique ID.

As long as all the tasks get a unique ID, I am fine.

Function as a discriminant at elaboration ... should have thought about it but it looks like I am missing some wisdom points. 

Thank you Jeffrey. 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously.
  2020-07-20 20:31   ` Olivier Henley
@ 2020-07-22 19:05     ` onox
  0 siblings, 0 replies; 5+ messages in thread
From: onox @ 2020-07-22 19:05 UTC (permalink / raw)


On Monday, July 20, 2020 at 10:31:46 PM UTC+2, olivier...@gmail.com wrote:
> > The order of the discriminants is arbitrary; there is no guarantee that Worker (I) will have ID of I. Elaboration is sequential, so each Worker will have a unique ID.
> As long as all the tasks get a unique ID, I am fine. 
> 
> Function as a discriminant at elaboration ... should have thought about it but it looks like I am missing some wisdom points. 
> 
> Thank you Jeffrey.

Another way is to use an extended return:

spec:

   type Worker;

   task type Worker_Task (Data : not null access constant Worker);

   type Worker is limited record
      ID : Positive;
      T  : Worker_Task (Worker'Access);
   end record;

   type Worker_Array is array (Positive range <>) of Worker;

   function Make_Workers return Worker_Array;

body:

   function Make_Workers return Worker_Array is
   begin
      return Result : Worker_Array (1 .. Positive (Count)) do
         for Index in Result'Range loop
            Result (Index).ID := Index;
         end loop;
      end return;
   end Make_Workers;

   Workers : constant Worker_Array := Make_Workers;
   pragma Unreferenced (Workers);

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-07-22 19:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-20 13:51 'Number_Of_CPUs' tasks creation, with discriminants, running simultaneously Olivier Henley
2020-07-20 13:55 ` Olivier Henley
2020-07-20 17:45 ` Jeffrey R. Carter
2020-07-20 20:31   ` Olivier Henley
2020-07-22 19:05     ` onox

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