summaryrefslogtreecommitdiffhomepage
path: root/chapel.html.markdown
diff options
context:
space:
mode:
authorIan Bertolacci <ian.bertolacci@gmail.com>2015-07-19 19:59:30 -0700
committerIan Bertolacci <ian.bertolacci@gmail.com>2015-07-19 19:59:30 -0700
commit155bf5d0a285de2c4dec4033c8e9c20f6a42072b (patch)
treeab074e198229a568d4861b21709c16d7deb47aca /chapel.html.markdown
parentbad5266d05485cd61ab1aa0884180ab78738cd9b (diff)
Added sync, single, and atomic vars.
Diffstat (limited to 'chapel.html.markdown')
-rw-r--r--chapel.html.markdown78
1 files changed, 77 insertions, 1 deletions
diff --git a/chapel.html.markdown b/chapel.html.markdown
index 7ac73bbb..4f857775 100644
--- a/chapel.html.markdown
+++ b/chapel.html.markdown
@@ -757,6 +757,83 @@ timer.clear( );
[ val in myBigArray ] val = 1 / val;
// or iterate over indicies
[ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx];
+
+proc countdown( seconds: int ){
+ for i in 1..seconds by -1 {
+ writeln( i );
+ sleep( 1 );
+ }
+}
+
+// sync vars have two states: empty and full.
+// If you read an empty variable or write a full variable, you are waited
+// until the variable is full or empty again
+var someSyncVar$: sync int; // varName$ is a convention not a law.
+sync {
+ begin {
+ writeln( "Waiting to read" );
+ var read_sync = someSyncVar$;
+ writeln( "value is ", read_sync );
+ }
+
+ begin {
+ writeln( "Writing in..." );
+ countdown( 3 );
+ someSyncVar$ = 123;
+ }
+}
+
+// single vars can only be written once. A read on an unwritten single results
+// in a wait, but when the variable has a value it can be read indefinitely
+var someSingleVar$: single int; // varName$ is a convention not a law.
+sync {
+ begin {
+ writeln( "Waiting to read" );
+ for i in 1..5 {
+ var read_single = someSingleVar$;
+ writeln( i,"th time around an the value is ", read_single );
+ }
+ }
+
+ begin {
+ writeln( "Writing in..." );
+ countdown( 3 );
+ someSingleVar$ = 5; // first and only write ever.
+ }
+}
+
+// atomic variables can be of type bool, int, uint, and real of any size.
+var uranium: atomic int;
+uranium.write( 238 ); // atomically write a variable
+writeln( uranium.read() ); // atomically read a variable
+// operations are described as functions, you could define your own operators.
+uranium.sub( 3 ); // atomically subtract a variable
+writeln( uranium.read() );
+var replaceWith = 239;
+var was = uranium.exchange( replaceWith );
+writeln( "uranium was ", was, " but is now ", replaceWith );
+var isEqualTo = 235;
+if uranium.compareExchange( isEqualTo, replaceWith ) {
+ writeln( "uranium was equal to ", isEqualTo,
+ " so replaced value with ", replaceWith );
+} else {
+ writeln( "uranium was not equal to ", isEqualTo,
+ " value stays the same... whatever it was" );
+}
+
+sync {
+ begin {
+ writeln( "Waiting to for uranium to be ", isEqualTo );
+ uranium.waitFor( isEqualTo );
+ writeln( "Uranium was set (by someone) to ", isEqualTo );
+ }
+
+ begin {
+ writeln( "Waiting to write uranium to ", isEqualTo );
+ countdown( 3 );
+ uranium.write( isEqualTo );
+ }
+}
```
Who is this tutorial for?
@@ -771,7 +848,6 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to
### What this tutorial is lacking:
* Modules and standard modules
- * Synchronize variables and atomic operations
* Multiple Locales (distributed memory system)
* ```proc main(){ ... }```
* Records