Async

From Mesham
Jump to navigationJump to search

Syntax

async[ ]

Semantics

This type will specify that the communication to be carried out should be done so asynchronously. Asynchronous communication is often very useful and, if used correctly, can increase the efficiency of some applications (although care must be taken.) There are a number of different ways that the results of asynchronous communication can be accepted, when the asynchronous operation is honoured then the data is placed into the variable, however when exactly the operation will be honoured is none deterministic and care must be taken if using dirty values.

The sync keyword allows the programmer to either synchronise ALL or a specific variable's asynchronous communication. The programmer must ensure that all asynchronous communications have been honoured before the process exits, otherwise bad things will happen!

Examples

function void main() {
   var a:Int::allocated[multiple[]] :: channel[0,1] :: async[];
   var p;
   par p from 0 to 2 {
      a:=89;
      var q:=20;
      q:=a;
      sync q;
   };
};

In this example, a is declared to be an integer, allocated to all processes, and to act as an asynchronous channel between processes 0 and 1. In the par loop, the assignment a:=89 is applicable on process 0 only, resulting in an asynchronous send. Each process executes the assignment and declaration var q:=20 but only process 1 will execute the last assignment q:=a, resulting in an asynchronous receive. Each process then synchronises all the communications relating to variable q.

function void main() {
   var a:Int::allocated[single[on[1]]];
   var b:Int::allocated[single[on[2]]] :: async[];
   var c:Int::allocated[single[on[3]]] :: async[];
   a:=b;
   c:=a;
   b:=c;
   sync;
};

This example demonstrates the use of the async type in terms of default shared variable style communication. In the assignment a:=b, processor 2 will issue an asynchronous send and processor 1 will issue a synchronous (standard) receive. The second assignment, c:=a, processor 3 will issue an asynchronous receive and processor 1 a synchronous send. In the last assignment, b:=c, both processors (3 and 2) will issue asynchronous communication calls (send and receive respectively.) The last line of the program will force each process to wait and complete all asynchronous communications.

Since: Version 0.5