...
A simple HelloWorld example
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <stdio.h>
int main (int argc, char ** argv) {
finish {
async {
printf("Hello World !\n");
}
}
return 0;
}
|
...
Scope of variables
An async only knows about global variables and variables passed as a parameter to it (similarly to function calls). We We use a set of three clauses: IN, OUT and INOUT to specify how async interacts asyncs interact with variables declared in the spawn environmenttheir enclosing lexical scope.
- IN allows to copy a value from the spawn scope to the async body.
- OUT allows to copy a value back from the async body to the spawn scope.
INOUT, both copy IN and copy OUT.
This example shows how to write a simple parallel for loop. The loop's body contains an async to be spawned at each iteration. One can picture the execution of this code as if each iteration copies the current value of IN arguments, creates the async and then schedule it for execution. Note that since 'i' is passed by copy, the variable can be freely modified inside and outside the async body. However 'j' is not declared as an IN argument, hence is not visible in the async's body.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <stdio.h> int main (int argc, char ** argv) { int i = 0, j = 0; finish { for(i=0; i < LG; ) { async IN(i) { // 'i' passed by copy printf("Hello %d times !\n", i); } i++; // incr loop's 'i' } } return 0; } |
This example shows how to create a bunch of async in a loop. The loop index 'i' is passed as an IN argument so that its value is available in the async's body.