comp.lang.ada
 help / color / mirror / Atom feed
* Maximum Number Of Tasks?
@ 2013-11-12  9:53 FritzVonBraun
  2013-11-12 10:59 ` Jacob Sparre Andersen
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: FritzVonBraun @ 2013-11-12  9:53 UTC (permalink / raw)


I was wondering about the maximum number of tasks in Ada but I couldnt 
find any info. The question is, is a task in Ada technically similar to 
a thread in Windows under the hood? Threads are restricted by the stack 
size that each thread has reserved, so in practice the maximum number of 
threads is about 2000.

The reason I'm asking is that I wonder if Ada provides a more 
comfortable solution to the thread pool problem. In C++ for example I 
create a number of threads roughly equal to the number of processor 
cores and then have a number of Jobs that are distributed over the 
threads and which implement a time sharing system by returning control 
to the thread which then assigns time to another Job.

Would I have to do the same in Ada or are tasks meant to be "micro 
objects' of which many can be created and the Ada runtime does 
effectively what my threadpool system does in C++

Thanks for any info

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

* Re: Maximum Number Of Tasks?
  2013-11-12  9:53 Maximum Number Of Tasks? FritzVonBraun
@ 2013-11-12 10:59 ` Jacob Sparre Andersen
  2013-11-12 12:52 ` Georg Bauhaus
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Jacob Sparre Andersen @ 2013-11-12 10:59 UTC (permalink / raw)


FritzVonBraun wrote:

> I was wondering about the maximum number of tasks in Ada but I couldnt
> find any info.

Probably because it is both compiler, hardware and operating system
dependent.

> The question is, is a task in Ada technically similar to a thread in
> Windows under the hood?

That depends on which compiler you use.  Some (most?) versions of GNAT
use operating system threads to implement tasks.  Janus/Ada implements
tasks in its own run-time system.

> Threads are restricted by the stack size that each thread has
> reserved, so in practice the maximum number of threads is about 2000.

I just made a quick test on my laptop.  It appears that I can create
32041 tasks before I have to do something special to avoid problems.

The test was done on a Debian 7.2 system with the GNAT 4.6 compiler
distributed with Debian.

The test program:

with Ada.Text_IO; use Ada.Text_IO;

procedure Task_Demo is
   task type Demo_Task (Index : Positive) is
      entry Stop;
   end Demo_Task;
   type Demo_Task_Reference is access Demo_Task;

   task body Demo_Task is
   begin
      Put_Line (Positive'Image (Index) & " launched.");
      accept Stop;
      Put_Line (Positive'Image (Index) & " stopping.");
   exception
      when others =>
         Put_Line (Positive'Image (Index) & " terminated by an exception.");
   end Demo_Task;

   Collection : array (1 .. 32_041) of Demo_Task_Reference;
begin
   for I in Collection'Range loop
      Collection (I) := new Demo_Task (Index => I);
   end loop;

   delay 1.0;

   for I in Collection'Range loop
      Collection (I).Stop;
   end loop;
end Task_Demo;

Reducing the stack size for the individual tasks does not seem to make a
difference.

> The reason I'm asking is that I wonder if Ada provides a more
> comfortable solution to the thread pool problem. In C++ for example I
> create a number of threads roughly equal to the number of processor
> cores and then have a number of Jobs that are distributed over the
> threads and which implement a time sharing system by returning control
> to the thread which then assigns time to another Job.
>
> Would I have to do the same in Ada or are tasks meant to be "micro
> objects' of which many can be created and the Ada runtime does
> effectively what my threadpool system does in C++

That depends on your compiler.

Greetings,

Jacob
-- 
"Two silk worms had a race. They ended up in a tie."

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

* Re: Maximum Number Of Tasks?
  2013-11-12  9:53 Maximum Number Of Tasks? FritzVonBraun
  2013-11-12 10:59 ` Jacob Sparre Andersen
@ 2013-11-12 12:52 ` Georg Bauhaus
  2013-12-06  3:26   ` Brad Moore
  2013-11-12 13:21 ` mockturtle
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Georg Bauhaus @ 2013-11-12 12:52 UTC (permalink / raw)


On 12.11.13 10:53, FritzVonBraun wrote:
> I was wondering about the maximum number of tasks in Ada but I couldnt find
> any info. The question is, is a task in Ada technically similar to a thread in
> Windows under the hood? Threads are restricted by the stack size that each
> thread has reserved, so in practice the maximum number of threads is about 2000.
> 
> The reason I'm asking is that I wonder if Ada provides a more comfortable
> solution to the thread pool problem. In C++ for example I create a number of
> threads roughly equal to the number of processor cores and then have a number
> of Jobs that are distributed over the threads and which implement a time
> sharing system by returning control to the thread which then assigns time to
> another Job.

If you have jobs that do not require intermittent communication among
them, then in particular, I'd be sure to have a look at the Paraffin
library. http://paraffin.sourceforge.net/

As an example of a different setup, I have seen a program that had
the number of tasks be about 4x that of processors; all ran at the
"same" time and the number of tasks was suggested by the program's
logic, not by either hardware or OS.
Load distribution seemed very well handled (GNAT on GNU/Linux in this
case), 4 x #CPU was a sweet spot regarding the number of tasks.

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

* Re: Maximum Number Of Tasks?
  2013-11-12  9:53 Maximum Number Of Tasks? FritzVonBraun
  2013-11-12 10:59 ` Jacob Sparre Andersen
  2013-11-12 12:52 ` Georg Bauhaus
@ 2013-11-12 13:21 ` mockturtle
  2013-11-12 20:02   ` Ludovic Brenta
  2013-11-12 15:54 ` Jeffrey Carter
  2013-11-14 13:00 ` Marius Amado-Alves
  4 siblings, 1 reply; 10+ messages in thread
From: mockturtle @ 2013-11-12 13:21 UTC (permalink / raw)


On Tuesday, November 12, 2013 10:53:53 AM UTC+1, FritzVonBraun wrote:
> I was wondering about the maximum number of tasks in Ada but I couldnt 
> find any info. The question is, is a task in Ada technically similar to 
> a thread in Windows under the hood? Threads are restricted by the stack 
> size that each thread has reserved, so in practice the maximum number of 
> threads is about 2000.
> 

I remember that I saw at FOSDEM 2013 in the "Ada Developer Room" a demo that plotted a Mandelbrot set by using a matrix of tasks.  A participant asked then "can you do that with 10_000 tasks?"  The size of the matrix was changed to 100 x 100 and everything worked smoothly; so, in that case you could create at least 10_000 tasks.  The PC was a laptop running some kind of Linux + GNAT, if I remember correctly.

Riccardo

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

* Re: Maximum Number Of Tasks?
  2013-11-12  9:53 Maximum Number Of Tasks? FritzVonBraun
                   ` (2 preceding siblings ...)
  2013-11-12 13:21 ` mockturtle
@ 2013-11-12 15:54 ` Jeffrey Carter
  2013-11-12 16:17   ` Dmitry A. Kazakov
  2013-11-14 13:00 ` Marius Amado-Alves
  4 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2013-11-12 15:54 UTC (permalink / raw)


On 11/12/2013 02:53 AM, FritzVonBraun wrote:
>
> Would I have to do the same in Ada or are tasks meant to be "micro objects' of
> which many can be created and the Ada runtime does effectively what my
> threadpool system does in C++

Tasks should reflect inherent concurrency in the problem space, not some aspect 
of the hardware or OS. I have never encountered any problem following this rule, 
whether it was 60-70 Ada-83 tasks on a 640 KB DOS system in the 1980s or 
hundreds of tasks on Linux this year.

-- 
Jeff Carter
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail
17


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

* Re: Maximum Number Of Tasks?
  2013-11-12 15:54 ` Jeffrey Carter
@ 2013-11-12 16:17   ` Dmitry A. Kazakov
  0 siblings, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2013-11-12 16:17 UTC (permalink / raw)


On Tue, 12 Nov 2013 08:54:18 -0700, Jeffrey Carter wrote:

> On 11/12/2013 02:53 AM, FritzVonBraun wrote:
>>
>> Would I have to do the same in Ada or are tasks meant to be "micro objects' of
>> which many can be created and the Ada runtime does effectively what my
>> threadpool system does in C++

If you have native tasking then tasks are as fat as threads. If you have
tasking implemented within one thread (rare), tasks can be "micro", but
then they most likely will be unable to perform I/O concurrently.

> Tasks should reflect inherent concurrency in the problem space, not some aspect 
> of the hardware or OS.

It is not that simple. The problem space may encompass the hardware, e.g.
in the case of communication and services. Which is typically the case when
worker tasks pool comes in question.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: Maximum Number Of Tasks?
  2013-11-12 13:21 ` mockturtle
@ 2013-11-12 20:02   ` Ludovic Brenta
  2013-11-12 20:04     ` Ludovic Brenta
  0 siblings, 1 reply; 10+ messages in thread
From: Ludovic Brenta @ 2013-11-12 20:02 UTC (permalink / raw)


mockturtle writes:
> On Tuesday, November 12, 2013 10:53:53 AM UTC+1, FritzVonBraun wrote:
>> I was wondering about the maximum number of tasks in Ada but I couldnt 
>> find any info. The question is, is a task in Ada technically similar to 
>> a thread in Windows under the hood? Threads are restricted by the stack 
>> size that each thread has reserved, so in practice the maximum number of 
>> threads is about 2000.
>> 
>
> I remember that I saw at FOSDEM 2013 in the "Ada Developer Room" a
> demo that plotted a Mandelbrot set by using a matrix of tasks.  A
> participant asked then "can you do that with 10_000 tasks?"  The size
> of the matrix was changed to 100 x 100 and everything worked smoothly;
> so, in that case you could create at least 10_000 tasks.  The PC was a
> laptop running some kind of Linux + GNAT, if I remember correctly.
>
> Riccardo

Yes, you remember correctly; I was the one doing that demo :)

That's because the Linux kernel allocates virtual address space to each
task but does not allocate any physical memory (RAM or swap) unless and
until the task writes to memory (i.e. creates variables on its stack).
Even then, Linux only allocates the pages actually written to, not the
full 2 MiB (or whatever the default is) per task.

After the demo, I re-ran the program at home and saw it allocate 19.8
GiB of virtual address space (I re-checked just now) and thought: gosh
am I lucky I've been running 64-bit Linux since 2006 :)

-- 
Ludovic Brenta.


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

* Re: Maximum Number Of Tasks?
  2013-11-12 20:02   ` Ludovic Brenta
@ 2013-11-12 20:04     ` Ludovic Brenta
  0 siblings, 0 replies; 10+ messages in thread
From: Ludovic Brenta @ 2013-11-12 20:04 UTC (permalink / raw)


Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

> mockturtle writes:
>> On Tuesday, November 12, 2013 10:53:53 AM UTC+1, FritzVonBraun wrote:
>>> I was wondering about the maximum number of tasks in Ada but I couldnt 
>>> find any info. The question is, is a task in Ada technically similar to 
>>> a thread in Windows under the hood? Threads are restricted by the stack 
>>> size that each thread has reserved, so in practice the maximum number of 
>>> threads is about 2000.
>>> 
>>
>> I remember that I saw at FOSDEM 2013 in the "Ada Developer Room" a
>> demo that plotted a Mandelbrot set by using a matrix of tasks.  A
>> participant asked then "can you do that with 10_000 tasks?"  The size
>> of the matrix was changed to 100 x 100 and everything worked smoothly;
>> so, in that case you could create at least 10_000 tasks.  The PC was a
>> laptop running some kind of Linux + GNAT, if I remember correctly.
>>
>> Riccardo
>
> Yes, you remember correctly; I was the one doing that demo :)
>
> That's because the Linux kernel allocates virtual address space to each
> task but does not allocate any physical memory (RAM or swap) unless and
> until the task writes to memory (i.e. creates variables on its stack).
> Even then, Linux only allocates the pages actually written to, not the
> full 2 MiB (or whatever the default is) per task.
>
> After the demo, I re-ran the program at home and saw it allocate 19.8
> GiB of virtual address space (I re-checked just now) and thought: gosh
> am I lucky I've been running 64-bit Linux since 2006 :)

Oh and by the way, I will introduce task pools in my demo at FOSDEM 2014
:)

-- 
Ludovic Brenta.


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

* Re: Maximum Number Of Tasks?
  2013-11-12  9:53 Maximum Number Of Tasks? FritzVonBraun
                   ` (3 preceding siblings ...)
  2013-11-12 15:54 ` Jeffrey Carter
@ 2013-11-14 13:00 ` Marius Amado-Alves
  4 siblings, 0 replies; 10+ messages in thread
From: Marius Amado-Alves @ 2013-11-14 13:00 UTC (permalink / raw)


Ada has a pragma to define the memory size associated with a task.
I've used this to good effect with GNAT on Windows and Macs.


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

* Re: Maximum Number Of Tasks?
  2013-11-12 12:52 ` Georg Bauhaus
@ 2013-12-06  3:26   ` Brad Moore
  0 siblings, 0 replies; 10+ messages in thread
From: Brad Moore @ 2013-12-06  3:26 UTC (permalink / raw)


On 12/11/2013 5:52 AM, Georg Bauhaus wrote:
> On 12.11.13 10:53, FritzVonBraun wrote:
>> I was wondering about the maximum number of tasks in Ada but I couldnt find
>> any info. The question is, is a task in Ada technically similar to a thread in
>> Windows under the hood? Threads are restricted by the stack size that each
>> thread has reserved, so in practice the maximum number of threads is about 2000.
>>
>> The reason I'm asking is that I wonder if Ada provides a more comfortable
>> solution to the thread pool problem. In C++ for example I create a number of
>> threads roughly equal to the number of processor cores and then have a number
>> of Jobs that are distributed over the threads and which implement a time
>> sharing system by returning control to the thread which then assigns time to
>> another Job.
>
> If you have jobs that do not require intermittent communication among
> them, then in particular, I'd be sure to have a look at the Paraffin
> library. http://paraffin.sourceforge.net/
>

Paraffin has had several flavours of task pools implemented for some 
time now. You can do as you suggest and have a task pool that has the 
same number of tasks as there are cores in your system, and distribute 
work across these tasks.

There are three main "flavours" of task pool.

1) No pool at all, just allocate workers on the fly and distribute work 
across the set of workers
2) A task pool that can be dynamically (or statically) created that 
contains a bounded (or unbounded) number of workers that can be applied 
to any number of parallelism opportunities. These task pools allow a 
worker to migrate to cores, if the OS supports migration (eg. Windows 
and Linux)
3) A Ravenscar compliant task pool that is more suitable for real time, 
where the workers must be statically allocated to cores, and cannot migrate.

To see a demo of these task pools, you could try running the
test_parallel_loops or test_parallel_recursion executables that are 
included with the source for Paraffin.

To see the Ravenscar version of these task pools, you would need to
execute the test_ravenscar_parallel_loops and 
test_ravenscar_parallel_recursion examples.

Rather surprisingly, there is not a significant difference between these 
three task pool models. In general using a task pool will give
a slight edge in performance over creating workers on the fly, but the 
difference is barely noticeable in these examples.

Also, intermittent communication between the workers is OK, as long as
the communication has the necessary safeguards.

For instance you could have several workers performing some lengthy 
calculation, that briefly writes some value to an IO port. If the
IO port was wrapped in a protected object, it may make sense to allow
the multiple workers to access this protected object to perform I/O.

Brad.


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

end of thread, other threads:[~2013-12-06  3:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-12  9:53 Maximum Number Of Tasks? FritzVonBraun
2013-11-12 10:59 ` Jacob Sparre Andersen
2013-11-12 12:52 ` Georg Bauhaus
2013-12-06  3:26   ` Brad Moore
2013-11-12 13:21 ` mockturtle
2013-11-12 20:02   ` Ludovic Brenta
2013-11-12 20:04     ` Ludovic Brenta
2013-11-12 15:54 ` Jeffrey Carter
2013-11-12 16:17   ` Dmitry A. Kazakov
2013-11-14 13:00 ` Marius Amado-Alves

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