next up previous
Next: Operators Up: Program execution control Previous: Active process group

Accessing global variable

In a single process program, accessing the value of a variable is straight forward. Given a data reference, there is only one process can be used to read or write it. In a SPMD program, only the process which holds data can read and write the data. A traditional SPMD program achieves this by using if statements. For example,

  if(myid==1) 
    my_data=3;

will make sure that only my_data on process 1 is assigned to 3.

In the language we present here, the same thing is required, and not only for local variables but also for global variables. Say it in HPJava terms: when assigning data, the data owner must be the active process group.

We have already introduced the on construct to switch the active process group, there is a more convenient way to change the active process group according to the array element we want to access: at construct.

Suppose we still have b defined in previous section,

  on (q) {
    Location i=x|1;
    at(i)
      b[i]=3; //correct

    b[i]=3;   //error
  }

The assign statement guarded by an at construct is correct, the one without it may cause run time error if there is run time range checking.

In general, at construct has the form,

  at(Location i= ...) {
    array[i]=...
  }

inside the brace bracket, the active process group will be restricted by Location i. If i has already been declared, Location can be omitted in the construct.

This is similar to a traditional SPMD program like,

  if( own(array[global_local(i)]) ) {
    array[global_local(i)]=...
  }

In HPJava, a more powerful construct over can be used to combine the switching of the active process group with a loop,

  on(q)
    over(Location i= x|0:3)
      b[i]=3;

is semantically equivalent to5

  on(q) 
    for(int n=0;n<4;n++)
      at(Location i=x|n)
        b[i]=3;

Inside each iteration, the active process group is changed to q/i.

In general, over construct has the form,

$\begin{array}{l}
\;\;{\tt over}\; \mathtt{(Location\;
i={\em range\_expressio...
...et\_expression})\; \{ }\\
\;\;\;\;{\tt ...} \\
\;\;\mathtt{\}}\\
\end{array}$

Note, the = inside over construct header does not represent an assignment expression. It is used to compose an over loop control header, with its left hand side as a loop index and its right hand side as loop range in which the loop index takes value.

Similar as in at construct, if the loop index has already been declared in the program, keyword Location can be omitted.

In section 3, we will use more programs to show that by using the at and over construct it is quite convenient for a program to keep the active process group equal to the data owner group of the assigned data.

Generally speaking, when writing global data, the active process group should be equal to the data owner group. Bigger than the data owner group may cause runtime problem, smaller than the data owner group may cause the global variable have different values on different processes. When reading global data, the active process group should be not bigger than the data owner group. In any situations, the active process group must be contained in the data owner group. We call this access rule for global data. Some optional run-time checking may be added at compiling time to find code violating this rule, but it also should be kept in mind of the programmer to avoid the accessibility violation.

When accessing data on another process, HPJava needs explicit communication as in a ordinary SPMD program. The most important communication functions are introduced in section 2.5.


next up previous
Next: Operators Up: Program execution control Previous: Active process group
Bryan Carpenter 2002-07-12