Re: How do I break from loop when using parallel()?

2018-05-29 Thread Russel Winder via Digitalmars-d-learn
On Mon, 2018-05-28 at 21:04 +, Dr.No via Digitalmars-d-learn wrote:
>   import std.parallelism : parallel;
>   foreach(t; parallel(arr))
>   {
>   if(!doSomething(t)) {
>   return false;
>   }
>   }
> 
> It reuturns the run time error:
> 
> > std.parallelism.ParallelForeachError@(0): Cannot break from a 
> > parallel foreach loop using break, return, labeled 
> > break/continue or goto statements.
> 
> What's the proper way to break from loop?

It isn't a loop, it is a task scatter/gather, with each task running to
completion independent of all other tasks. Thus the concept of
break/return doesn't exist.

It could be argued that this is a bad use of foreach, but it is what it
is.

-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


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


Re: How do I break from loop when using parallel()?

2018-05-28 Thread Neia Neutuladh via Digitalmars-d-learn

On Monday, 28 May 2018 at 21:04:21 UTC, Dr.No wrote:

import std.parallelism : parallel;
foreach(t; parallel(arr))
{
if(!doSomething(t)) {
return false;
}
}

It reuturns the run time error:

std.parallelism.ParallelForeachError@(0): Cannot break from a 
parallel foreach loop using break, return, labeled 
break/continue or goto statements.


What's the proper way to break from loop?


By the time you try to break out of the loop, you've already 
executed the loop body for later elements in the collection. So 
if you could do that, it would give the wrong impression.


The loop body executes on several threads at once. The `return 
false` statement might be executing on a different thread, and 
`return` only returns on the same thread.


If you want to do this sort of thing (exit on first error, for 
instance), you can manually use TaskPool, schedule tasks on it, 
and use an atomic variable to exit early on each task if 
necessary.


Re: How do I break from loop when using parallel()?

2018-05-28 Thread WebFreak001 via Digitalmars-d-learn

On Monday, 28 May 2018 at 21:04:21 UTC, Dr.No wrote:

import std.parallelism : parallel;
foreach(t; parallel(arr))
{
if(!doSomething(t)) {
return false;
}
}

It reuturns the run time error:

std.parallelism.ParallelForeachError@(0): Cannot break from a 
parallel foreach loop using break, return, labeled 
break/continue or goto statements.


What's the proper way to break from loop?


you can't break out because other tasks would still continue to 
finish while the loop is breaking. So either you could use 
`throw` to error out or you could simply create a `bool isDone;` 
and at the start of the loop do `if (isDone) continue;` to make 
all next tasks finish quickly (though CPU usage will be very high 
for a moment, depending on the list size)


If you want total control and break manually (with an uncertainty 
of a few iterations) you can probably create a custom taskPool 
and use the .stop member function on it.


See https://dlang.org/phobos/std_parallelism.html


How do I break from loop when using parallel()?

2018-05-28 Thread Dr.No via Digitalmars-d-learn

import std.parallelism : parallel;
foreach(t; parallel(arr))
{
if(!doSomething(t)) {
return false;
}
}

It reuturns the run time error:

std.parallelism.ParallelForeachError@(0): Cannot break from a 
parallel foreach loop using break, return, labeled 
break/continue or goto statements.


What's the proper way to break from loop?