Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

An async only knows about global variables and variables passed as a parameter to it (similarly to function calls). We use a set of three clauses: IN, OUT and INOUT to specify how the code within asyncs interact interacts with variables declared in their enclosing lexical scope.

  • IN allows to copy a value lists variables whose value is copied from the enclosing spawn scope to the async scope at the point of the creation of the async body.
  • OUT allows to copy a value lists variables whose value is copied back from the async body scope to the enclosing spawn scope when the async finishes its execution.
  • INOUT , lists variables that are copied both copy IN and OUT.

The variable copy in IN, OUT and INOUT clauses is shallow: pointers are only copied by value. 

This Next 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 copyvalue, the variable can be freely modified inside and outside the async body. However 'j' is not declared as an IN argument, hence it is not visible in the async's body.

Code Block
languagecpp
titleParallel for loop and IN clause
linenumberstrue
#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 copyvalue
        printf("Hello %d times !\n", i);
      }
      i++; // incr loop's 'i'
    }
  }
  return 0;
}

...