Notes for Parallel Scientific Computing, Feb 20, 2003

2000 (on course reserve).  This text has been the basis for our
discussion of shared memory concurrent programming, but there is much
more in the Andrews text than was said in class.

If you need to brush up your C language programming skills then I
advise: Brian W. Kernighan and Dennis M. Ritchie: The C Programming
Language, 2nd ed. (ANSI C), 1989.

Handout, February 20: One simple sequential C program that solves an
instance of the Laplace equation on a square mesh by point relaxation,
and one incomplete C program that uses Pthreads to do the same in
parallel, with one thread per interior mesh point.  The latter code is
missing all synchronization statements.  Please complete according to
taste.  You can uses mutexes, condition variables and/or semaphores.

This is truly the crudest discretization of the Laplace equation in
the simplest context.  We have a mesh of N*N cells, with the function
discretized by point values at the (N+1)*(N+1) vertices.  We have
fixed the values at the boundary vertices.  (Dirichlet boundary
conditions.)  At the interior (i,j) vertex the discretized Laplace
equation is

a[i,j-1]+a[i,j+1]+a[i-1,j]+a[i+1,j]-4*a[i,j] = 0

The solution procedure employs "simultaneous relaxation".  First, at
each interior point the quantity da[i,j] is evaluated such that after
the assignment a[i,j] = a[i,j]+da[i,j] the discrete Laplace equation
would be satisfied at the (i,j) point, were it not for changes to the
neighbouring values.  We iterate until convergence.  With the
convergence test used, the code terminates after about 100 iterations.

#include <stdio.h> #include <math.h> /* Solve a Laplace equation by simple point relaxation. One single problem instance; trivial code structure. */ #define N 6 #define tol 1.0e-8 /* N: size of the problem instance. The mesh has (N+1)*(N+1) points. */ /* tol: convergence tolerance. Iteration will stop when the maximum absolute change does not exceed tol. */ main() { int i, j, iters; double a[N+1][N+1], da[N+1][N+1], t0, maxda; /* iters: iteration counter */ /* a[0:N][0:N]: Initially arbitrary; the interior values a[1:N-1][1:N-1] evolve by point relaxation towards a solution of the Laplace equation with the given boundary values. */ /* da[0:N][0:N]: used to hold the change to a[0:N][0:N] */ /* maxda: maximum absolute value of da[1:N-1][1:N-1] */ /* Initialize */ for (i=0; i<=N; i++) { for (j=0; j<=N; j++) { if (i==0||i==N||j==0||j==N) a[i][j]=0.0; else a[i][j]=1.0; }; }; maxda=HUGE_VAL; iters=0; /* The problem instance just defined has boundary values identically 0 and initial interior values all equal to 1. The iterative process should converge towards the solution that has a[0:N][0:N] all 0. */ /* Iterate */ while (tol<maxda) { t0=0.0; for (i=1; i<N; i++) { for (j=1; j<N; j++) { da[i][j]=(a[i+1][j]+a[i-1][j]+a[i][j+1]+a[i][j-1]-4*a[i][j])/4; if (t0<fabs(da[i][j])) t0=fabs(da[i][j]); }; }; maxda=t0; for (i=1; i<N; i++) { for (j=1; j<N; j++) { a[i][j]=a[i][j]+da[i][j]; }; }; iters++; }; /* Print */ printf("%7d\n",iters); for (i=0; i<=N; i++) { for (j=0; j<=N; j++) { printf("%10.1e",a[i][j]); }; printf("\n"); }; }