You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

HC keywords in a nutshell:
  • async: asynchronously start a new task to execute a statement.
  • finish: execute a statement, but wait until all (transitively) spawned asyncs in statement's scope have terminated.

A simple HelloWorld example


In this simple example, we create an asynchronous task using an async block, which contains the code to display "HelloWorld". A finish block wraps the async which allows to wait for its complete execution before proceeding to the return statement.

HC HelloWorld
#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 use a set of three clauses: IN, OUT and INOUT to specify how asyncs interact with variables declared in their 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.

Parallel for loop and IN clause
#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;
}

 

 

  • No labels