Previous: Examples Up: Examples Next: Scientific Mesh Computation
This program initializes the entries of a float-valued array.
#include <iostream.h> const int N = 10;float f (float i) { //evaluate polynomial 3xxx-7xx+2x+4 at x=i return 3*i*i*i - 7*i*i + 2*i + 4; }
int main() { //makes A[i] = f(i) and then outputs A[i] float A[N];
parfor (int i=0; i<N; i++) A[i] = f((float)i); //implicit barrier
for (int j=0; j<N; j++) cout << "A[" << j << "] is " << A[j] << endl; return 0; }
Each element of the array A[] is evaluated and assigned a value by a different thread of control. This is an instance of safe sharing of mutable variables between concurrent threads of execution because each individual element of the array A[] is a different mutable variable, and single elements are not shared between iterations of the parfor loop.