Hi Peter (and list) --
A colleague reminded me of something I perhaps should've said in my last post.
Traditionally (up through the version 1.8 release) Chapel has permitted races
like this in all parallel constructs. For example, in version 1.7, I could
have written:
var sum: int;
coforall i in 1..numTasks {
sum += i;
}
and had a similar race for my task parallel loop as the forall loop example
did.
However, as of version 1.8, we've changed the semantics for task parallel
constructs to help reduce the chances of hitting such errors accidentally
(while also improving performance, simplifying the run-time implementation,
etc.). In a future release (1.9, I hope), we intend to do the same for data
parallel constructs for similar reasons and consistency. In particular, for
variables that span lexical scopes into a task parallel concept, we now treat
that variable as though it was passed by 'blank' argument intent into the task.
For variables like integers, blank intent implies it is treated as 'const'
within a function, so the above loop in 1.8 would generate a compile time error
for trying to modify 'sum' within the loop since it's a constant in that
setting. The plan is to do the same for forall loops to avoid races like the
one in your original code.
Since synchronization types like sync and atomic variables are passed by
reference when passed by blank intent, they will work as intended. I.e.,
var sum: atomic int;
coforall i in 1..numTasks {
sum.add(i);
}
will safely accumulate into sum.
For more information on this feature, refer to
http://chapel.cray.com/language-changes.html or the language spec's 'Task
Intents' section in the Task Parallelism and Synchronization chapter.
-Brad
________________________________________
From: Brad Chamberlain [[email protected]]
Sent: Thursday, December 05, 2013 9:17 AM
To: Peter Kjellström; [email protected]
Cc: Michael Ferguson
Subject: RE: Performance observations for vs forall vs reduce
Hi Peter --
> Attempting to fix this I went looking for an example forall with proper
> syncronization. The first one I found was:
>
> http://chapel.cray.com/tutorials.html ->
> http://faculty.knox.edu/dbunde/teaching/chapel/ ->
> 2.1. Forall and Coforall ->
>
> var sum : int = 0;
> forall i in 1..1000000 {
> sum += i;
> }
> writeln(sum);
>
> Which by the looks of it contains the same mistake I did...
If you read the text just below this example, it points out that there is a
race condition with a forward reference for how to fix it (though the fix is
not quite as elegant as the one you used below -- I'll check with them to see
whether that was intentional.
> Reading the Forall part of the otherwise great "Productive Programming in
> Chapel: a Next-Generation Language for General, Locality-Aware Parallelism,
> University of Bergen Tutorial, April 10th, 2013." (also from
> http://chapel.cray.com/tutorials.html) didn't yield any clues or references to
> atomic or sync variables.
atomic and sync variables are considered part of the task parallel subset of
the language (though they can also be used in any other parallel or serial part
of the language), so are discussed in that subset of the tutorial slides.
> I proceeded to read the language spec that came with my 1.8.0 chapel and found
> information on sync variables and modified my forall to:
>
> config const N = 10;
> var sum$ : sync real = 0.0;
> forall i in 1..N by -1 do sum$ += 1.0/(i*i);
> writeln(sqrt(sum$*6));
This is a reasonable approach and has been the traditional way to do this in
Chapel; atomic types are a newer addition to the language and are the more
optimal way to express this, at least for compilers for which we've implemented
them using the compiler's/hardware's atomic primitives (primarily gcc at
present, I believe -- someone else may be able to correct me). You should be
able to write this as:
var sum: atomic real = 0.0;
forall i in 1..N by -1 do sum.add(1.0/(i*i));
Still musing over the 1- vs. 2-core performance results, the mechanism used to
achieve them, and whether I'm surprised...
-Brad
------------------------------------------------------------------------------
Sponsored by Intel(R) XDK
Develop, test and display web and hybrid apps with a single code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
_______________________________________________
Chapel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-users
------------------------------------------------------------------------------
Sponsored by Intel(R) XDK
Develop, test and display web and hybrid apps with a single code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
_______________________________________________
Chapel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-users