Parallelization of a large array

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 10 15:16:50 PDT 2015


On Tuesday, 10 March 2015 at 22:11:57 UTC, Dennis Ritchie wrote:
> On Tuesday, 10 March 2015 at 21:27:42 UTC, Dennis Ritchie wrote:
>> Thanks.
>
> No, it does not suit me, because of the parallel array in a 
> foreach loop there is no break.
>
> import std.stdio;
> import std.algorithm;
> import std.parallelism;
>
> void main() {
>
> 	int b = 2;
>
> 	auto a = [1, 2, 2, 3];
>
> 	if (find(a, b).length != 0)
> 		writeln("Yes_");
>
> 	foreach (elem; a.parallel)
> 		if (elem == b)
> 			writeln("Yes");		// prints "Yes" twice
> }

Just add a condition variable.

import std.stdio;
import std.algorithm;
import std.parallelism;

void main() {

	int b = 2;

	auto a = [1, 2, 2, 3];

	if (find(a, b).length != 0)
		writeln("Yes_");
	
	auto found = false;

	foreach (elem; a.parallel)
		if (!found && elem == b)
		{
			writeln("Yes");
			found = true;
		}
}

This program pints:

Yes_
Yes


More information about the Digitalmars-d-learn mailing list