Laboratory Exercises For Computer Science 295

Lab 2: The Shared-Memory Model in SR

Lab 2: The Shared-Memory Model in SR

Goals: This lab is the first in a sequence which introduces mechanisms and models for parallel processing in SR. As an application area, the lab also describes the computation of fractals and Julia sets -- a type of fractal which is used commonly to illustrate capabilities for parallel computation.

Julia Sets: The mathematical study of fractals includes both theoretical and practical components. Since this course focuses upon parallel computation, not the mathematics of fractals, our emphasis will be rather practical. Thus, here we only skim some theory as background for what follows.

Some Theory: The mathematical study of fractals and chaos theory begins by considering points of the plane as being identified by complex numbers. Thus, the point (x, y) corresponds to the complex number x+yi, where i is the square root of -1.

Now suppose we are given a function w = f(z), for complex numbers z = x+yi. For any z, we can look at the sequence: z, f(z), f(f(z)), f(f(f(z))), ..., f^n(z), ... where f^n(z) means that the function f has been applied n times. This sequence of points is called the orbit of z under function f.

The study of chaos and dynamical systems considers the types of orbits that might exist and various properties of these orbits.

For our purposes, we simply ask whether the sequence converges to a point, diverges toward infinity, or wanders somewhere in a contained region of the plane. More specifically, for a given function f, we can ask what points z are such that the orbit diverges toward infinity. For points where the orbit diverges, we can ask how quickly divergence occurs. (For example, does z, f(z), f(f(z)), ... stay in a relatively confined area for awhile before it moves away toward infinity?) In drawing fractals, we consider each point z in the plane and color it according to such divergence properties. More specifically, the Julia Set for f(z) is the set of points which lie at the boundary of those points for which the orbit diverges.

Some Practical Notes and an Example: As an example, consider f(z) = z² - 1. If z = x+yi, then is x²-y² + 2xyi, and f(x+yi) = x² - y² - 1 + 2xyi. That is, the x (or real) coordinate of f(x+yi) is x² - y² - 1, and the y (or imaginary) coordinate is 2xyi. Also, the size of z = x+yi is defined to be x² + y².

Next consider the squaring operation. If the size of a complex number is bigger than 1, then its square is still larger in size. Now suppose that the size of a complex number z is at least 2. Then its square has size at least 4, and z² - 1 has size at least 3. In fact, one could prove that if z has size at least 2, then the size of f(z) is increased by at least a factor of 3/2. Thus, if z is any number of size greater than 2, the sequence z, f(z), f(f(z)), ... must diverge toward infinity -- the sizes of successive numbers get bigger and bigger.

Now suppose we take any number z, and suppose that after we apply f to it several times we obtain a value whose size exceeds 2. Then the above shows that subsequent iterations of f will cause the result to diverge. This observation provides a simple algorithm for us to gain information for a fractal and the Julia Set.

    For a given z, compute f(z), f(f(z)), f(f(f(z))), etc.
    If the size of any element in the sequence exceeds 2, then 
        the sequence diverges toward infinity.
To give a geometric picture of a fractal or of the Julia Set, we compute some terms in the sequence z, f(z), f(f(z)), etc. If we obtain a size of at least 2, we record how many iterations were required for this to occur. On the other hand, if after 25 or 30 iterations the size of f(z) is small (less than 2), we might conclude the orbit of z is unlikely to diverge. Once we know the number of iterations for each z, we might color each point as follows:
       if if^n(z) converges:  color z black
  else if size of f^1 (z), f^2 (z), f^3 (z) > 2, color z white
  else if size of f^4 (z), f^5 (z), f^6 (z) > 2, color z light blue
  else if size of f^7 (z), f^8 (z), f^9 (z) > 2, color z red
  else if size of f^10(z), f^11(z), f^12(z) > 2, color z cyan
  else if size of f^13(z), f^14(z), f^15(z) > 2, color z green
  else if size of f^16(z), f^17(z), f^18(z) > 2, color z orange
  else if size of f^19(z), f^20(z), f^21(z) > 2, color z maroon
  else if size of f^22(z), f^23(z), f^24(z) > 2, color z light pink
Fractals and Parallel Computation: Using the above coloring scheme (or a set of colors of your own choosing), the resulting plot (or fractal) often is interesting visually. Fractals have nice aesthetic qualities, so graphing them seems worthwhile.

As a practical matter, we normally divide a rectangular region (e.g. -2 <= x <= 2, -2 <= y <= 2) into many pieces and sample a point for each piece. The color of the small pieces then depends upon the conclusions we reach for the orbits for that representative point.

From the standpoint of parallelism, fractals have the useful properties that they require considerable computation at each point and the results for one point are independent from those of another point. Thus, parallelism can be used in several ways to allow multiple processors to compute the appropriate colors independently for each point. For these reasons, the computation of fractals is a very common application for the testing of parallel hardware and architecture. The resulting programs are relatively simple, and the results are aesthetically pleasing.

Steps for this Lab: In this lab, we consider some mechanisms to compute the fractal corresponding to f(z) = z² - 1, based on a shared-memory model of parallelism. Later labs will consider the same computations using other models and will provide practice using the visualization tool XTango (or other tools) to obtain resulting pictures.

  1. Write a non-parallel SR program to perform the following: In writing your program, use appropriate constants as much as possible.
    For a given point (x, y) in the grid, compute the value of results[i,j] in a separate procedure with parameters for the point (x, y). Since we will be following a shared-memory model of computation, it is appropriate
    Run your program for a small grid (e.g., 5 points per unit).

  2. Rewrite your program, using SR's co statement (SR text, page 200), so that the procedure is called in parallel for each point.

    Run your program for a very small grid size (e.g., 5 divisions per unit, so [-2, 2] contains 21 x and 21 y values).

    Run your program with a large grid size (e.g., 100 divisions per unit). Explain what happens.

  3. Rewrite your program, transforming your procedure into the process construct (SR text, page 201). (Ignore the discussion of semaphores, which we will discuss later in the course.) In this revised program, use a final statement (SR text, page 200).

  4. Rewrite your program, so that each process computes an entire row of the results matrix.

  5. Rewrite your program, so that each process computes a collection of rows of the results matrix. (Thus, the approach will not be unlike the approach that JaJa takes to parallel matrix multiplication in Algorithm 1.1.)

Work to be turned in:


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/295/lab2.html

created January 13, 1997
last revised January 27, 1997