Re: Idiomatic way of writing nested loops?

2017-07-18 Thread Russel Winder via Digitalmars-d-learn
On Tue, 2017-07-18 at 08:41 +, Anton Fediushin via Digitalmars-d-learn
wrote:
> On Tuesday, 18 July 2017 at 03:36:04 UTC, Nicholas Wilson wrote:
> > With regards to parallel, only use it on the outermost loop. 
> > Assuming you have more items in the outermost loop than you do 
> > threads parallelising more than one loop won't net you any 
> > speed.
> 
> Thank you! Yes, `parallel` runs only 4 threads on my machine, so 
> there is no reason to use it in nested loops.

But how many processors, cache sizes, memory speed, etc, etc, etc. The only
way of knowing what the fastest performance is is to try some things and get
some performance data. Even then the result only applies to that data on
that computer.

This is the sort of situation where philosophising about performance often
ends up with totally the wrong code.

-- 
Russel.
=
Dr Russel Winder t:+44 20 7585 2200   voip:sip:
russel.win...@ekiga.net
41 Buckmaster Road   m:+44 7770 465 077   xmpp:rus...@winder.org.uk
London SW11 1EN, UK  w: www.russel.org.uk skype:russel_winder

signature.asc
Description: This is a digitally signed message part


Re: Idiomatic way of writing nested loops?

2017-07-18 Thread Russel Winder via Digitalmars-d-learn
On Tue, 2017-07-18 at 03:36 +, Nicholas Wilson via Digitalmars-d-learn
wrote:
> On Monday, 17 July 2017 at 11:07:35 UTC, Anton Fediushin wrote:
> > […]
> > 
> > Also, I have a question about running this in parallel: if I 
> > want to use nested loops with `parallel` from 
> > `std.parallelism`, should I add `parallel` to every loop like 
> > this?
> > --
> > foreach(a; ["foo", "bar"].parallel) {
> >   foreach(b; ["baz", "foz", "bof"].parallel) {
> > foreach(c; ["FOO", "BAR"].parallel) {
> >   // Some operations on a, b and c
> > }
> >   }
> > }
> > --
> > I am worried about running thousands of threads, because in 
> > this case first `parallel` runs 2 tasks, every task runs 3 
> > tasks and every task runned inside a task runs 2 more tasks.

It is important to separate threads and tasks carefully here: as far as I am
aware the .parallel creates tasks not threads. The only threads are the ones
in the thread pool animatng the tasks. This having the thousands of tasks is
not a problem per se, since these are not threads.

The question of what the best decomposition for parallelism is has to be
determined by benchmarking – guesswork usually gets it wrong.

My prejudice here though is that for a loop structure such as this, unless
the computation at the centre is a biggy, you probably don't want the
.parallel on the inner loop. But I repeat only benchmarking will tell what
the best parallelism decomposition is.

> > So, how to write this in idiomatic D manner and run it _if 
> > possible_ in parallel?
> 
> With regards to parallel, only use it on the outermost loop. 
> Assuming you have more items in the outermost loop than you do 
> threads parallelising more than one loop won't net you any speed.

I am not convinced by this "idiom" of only the outer loop. It may be true
for some cases, but certtainly not all. This is task and thread pool based
parallelism here, not vector parallelism. Without knowing the actual
computational structure of the statements at the centre, there can be no
known best parallelism structure. Experimentation on medium sized data sets
before moving to the real ones is required to get the likely best
performance.


-- 
Russel.
=
Dr Russel Winder t:+44 20 7585 2200   voip:sip:
russel.win...@ekiga.net
41 Buckmaster Road   m:+44 7770 465 077   xmpp:rus...@winder.org.uk
London SW11 1EN, UK  w: www.russel.org.uk skype:russel_winder

signature.asc
Description: This is a digitally signed message part


Re: Idiomatic way of writing nested loops?

2017-07-18 Thread Anton Fediushin via Digitalmars-d-learn

On Tuesday, 18 July 2017 at 03:36:04 UTC, Nicholas Wilson wrote:
With regards to parallel, only use it on the outermost loop. 
Assuming you have more items in the outermost loop than you do 
threads parallelising more than one loop won't net you any 
speed.


Thank you! Yes, `parallel` runs only 4 threads on my machine, so 
there is no reason to use it in nested loops.


Re: Idiomatic way of writing nested loops?

2017-07-17 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 17 July 2017 at 11:07:35 UTC, Anton Fediushin wrote:
Hello! What is the best way of rewriting this code in idiomatic 
D manner?

--
foreach(a; ["foo", "bar"]) {
  foreach(b; ["baz", "foz", "bof"]) {
foreach(c; ["FOO", "BAR"]) {
  // Some operations on a, b and c
}
  }
}
--

Every array has at least 1 element, and adding/removing new 
"nested loops" should be as easy as possible.


Also, I have a question about running this in parallel: if I 
want to use nested loops with `parallel` from 
`std.parallelism`, should I add `parallel` to every loop like 
this?

--
foreach(a; ["foo", "bar"].parallel) {
  foreach(b; ["baz", "foz", "bof"].parallel) {
foreach(c; ["FOO", "BAR"].parallel) {
  // Some operations on a, b and c
}
  }
}
--
I am worried about running thousands of threads, because in 
this case first `parallel` runs 2 tasks, every task runs 3 
tasks and every task runned inside a task runs 2 more tasks.


So, how to write this in idiomatic D manner and run it _if 
possible_ in parallel?


With regards to parallel, only use it on the outermost loop. 
Assuming you have more items in the outermost loop than you do 
threads parallelising more than one loop won't net you any speed.


Re: Idiomatic way of writing nested loops?

2017-07-17 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Monday, 17 July 2017 at 11:55:47 UTC, Anton Fediushin wrote:
Thank you! I knew it is in the library! So, `parallel` will 
work just fine with this function, isn't it?


Yes


Re: Idiomatic way of writing nested loops?

2017-07-17 Thread Anton Fediushin via Digitalmars-d-learn

On Monday, 17 July 2017 at 11:32:45 UTC, Sebastiaan Koppe wrote:

On Monday, 17 July 2017 at 11:07:35 UTC, Anton Fediushin wrote:
Hello! What is the best way of rewriting this code in 
idiomatic D manner?


https://dlang.org/phobos/std_algorithm_setops.html#.cartesianProduct


Thank you! I knew it is in the library! So, `parallel` will work 
just fine with this function, isn't it?


Re: Idiomatic way of writing nested loops?

2017-07-17 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Monday, 17 July 2017 at 11:07:35 UTC, Anton Fediushin wrote:
Hello! What is the best way of rewriting this code in idiomatic 
D manner?


https://dlang.org/phobos/std_algorithm_setops.html#.cartesianProduct