r/rakulang • • Sep 09 '22

Why is Parallel Processing taking more time than Single Processing in Raku?

I stole this code from https://marketing.raku.org/id/1524053519/any ,

and then i timed it to see if the claim was true.

Here is the code that i wrote!

my $time1 = DateTime.now;
(^∞).grep(*.is-prime)[100_000].say;
my $time2 = DateTime.now;
say "Execution time in single Processing is {$time2-$time1}";



my $time3 = DateTime.now;
(^∞).hyper.grep(*.is-prime)[100_000].say;
my $time4 = DateTime.now;

say "Execution time in parallel Processing is {$time4-$time3}";

And then observed that serial processing was much faster than parallel processing...

Here is what my Terminal Printed::

1299721

Execution time in single Processing is 1.09377485

1299721

Execution time in parallel Processing is 1.95315763

7 Upvotes

9 comments sorted by

7

u/liztormato Rakoon πŸ‡ΊπŸ‡¦ πŸ•ŠπŸŒ» Sep 09 '22 edited Sep 10 '22

The current algorithm for .hyper is rather rigid. It always takes a fixed number of entries from the source iterator, and then passes that on to a worker thread. All of that fetching and scheduling means overhead.

The default batch-size, as it is called, is 64. This is a way too low number for doing primality checks, as the amount of CPU burnt for checking 64 values is way less than the amount of CPU needed to fetch and schedule. That's why you see the results that you do.

You can change the batch size with the batch named argument. These are some timings from my M1 MacMini:

% time raku -e '(^∞).grep(*.is-prime)[1_000_000].say'
15485867
real	6.34s
user	6.34s
sys	0.03s

% r '(^∞).hyper.grep(*.is-prime)[1_000_000].say'
15485867
real	5.81s
user	12.68s
sys	1.12s

% time raku -e '(^∞).hyper(batch => 8192).grep(*.is-prime)[1_000_000].say'
15485867
real	1.42s
user	3.19s
sys	0.06s

As you can see, using hyper with a much larger batch size, makes it more than 4x as fast.

3

u/[deleted] Sep 09 '22

Wow, Thanks a lot u/liztormato .... Your explanation was really clear and succinct.
I just ran the code with your changes, and it was 3 times faster in my PC.
After playing around for a while i observed that batch size of 8192 was really a sweet spot. I thought increasing batch size would increase the speed but it didnt.

After referring to the documentation then, i understood that increasing batch size causes some threads to remain idle which is also bad, so a sweet spot should be balanced...

Once again thx a lot for your patient reply u/liztormato!!

3

u/raiph πŸ¦‹ Sep 10 '22

How many real cores do you have?

3

u/[deleted] Sep 10 '22

I have 2 cores and 4 logical processors in my Lenovo G50 Laptop!

→ More replies (2)

2

u/gfldex Sep 09 '22

In your case, the optimal batch size actually depends on the size of the primes you check. Right now there is no mechanism to alter batch size after the HyperSeq is formed.

3

u/[deleted] Sep 09 '22

Yes, Understood!

Thx u/gfldex!

2

u/liztormato Rakoon πŸ‡ΊπŸ‡¦ πŸ•ŠπŸŒ» Sep 09 '22

Indeed. This is on a todo list of many people, also on mine. The current design actually makes this pretty hard / hacky to do. So that part of the internals will need a re-design. Maybe someone will do a better version of hyper / race in module space.