next up previous contents
Next: Solution Up: Parallel Loops Previous: Solution

 

The Mandelbrot Set - courtesy of Edinburgh Parallel Computing Centre.

The Mandelbrot Set is the set of numbers resulting from repeated iterations of the following function tex2html_wrap_inline3412

which, separating real and imaginary components, looks like,

eqnarray2771

for complex numbers, Z and C. This function is defined for complex values of C=(cr,ci) in the range ([-1.0,1.0], [-1.0,1.0]), with the initial conditions, z=c. In the case study, we will mainly be concerned with the Mandelbrot Set defined in the first quadrant of the complex plane, i.e. we will consider c in the range ([0.0, 1.0],[0.0,1.0]).

What is normally plotted is the number of iterations taken for z to reach some threshold value. We will take this threshold as tex2html_wrap_inline3426

and set an upper iteration limit of 255. This number is converted into a grey-scale or colour, and plotted at the point whose coordinates are (cr , ci).

Below is a fragment of a serial FORTRAN 77 code which computes the Mandelbrot set. This should outline the algorithm to be used in the full HPF version.

c Declare N*N arrays for real and imaginary parts of 
c arrays C (CR, CR) and Z (ZR, ZI)
c     ...
c Initialise arrays CR, CI
c Initialise arrays ZR=CR, ZI=CI
c     ...
c Initialise ZIS and ZRS to hold the squares of ZR and ZI
c     ...
      DO i = 0, 255
        DO j = 1, N
          DO k = 1, N
            IF (ZRS(j,k) + ZIS(j,k) .LE. 4.0 ) THEN
              ZRS(j,k) = ZR(j,k) * ZR(j,k)
              ZIS(j,k) = ZI(j,k) * ZI(j,k)
              ZI(j,k) = 2.0 * ZR(j,k) * ZI(j,k) + CI(j,k)
              ZR(j,k) = ZRS(j,k) - ZIS(j,k) + CR(j,k)
              COLOUR(j,k) = i
            END IF
          END DO
        END DO
      END DO
c ...

Write an HPF program, based on the above code fragment, which will compute the Mandelbrot set, using the Fortran 90 array features (WHERE and FORALL etc) to carry out the various stages. Use the following skeleton program (which is available by clicking here) to base your answer on.

      PROGRAM mandel
      IMPLICIT NONE

      INTEGER, PARAMETER :: N=64, RESOLUTION=255

! 1) Declare main arrays
!



! 2) Initialise cr and ci using FORALL
!



! 3) Initialise other arrays
!



! 4) Main loop: RESOLUTION number of iterations
!



! 5) Work: Fortran 90 translation of given FORTRAN 77 code
!



! Open output file
!
      OPEN(UNIT=10, FILE='mandel.pgm')
      WRITE(10, FMT='(''P2'',/,i3,2x,i3,/,i3)') N, N, RESOLUTION
      WRITE(10,*) colour
      CLOSE(UNIT=10)

      END

The following steps are needed:

        OPEN(UNIT=10,FILE='mandel.pgm')
        WRITE(10,fmt='(''P2'',/,I3,2X,I3,/,I3)') N, N, 255
        WRITE(10,*) colour
        CLOSE(10)

Compile and run the code on a single workstation, with the size of the arrays set with N = 128. View the resulting bitmap with

        xv mandel.pgm

and check that the code works correctly (i.e. you recognise the bitmap to be the well known Mandelbrot set).

Add HPF directives to parallelise the code.

Go back to Notes gif




next up previous contents
Next: Solution Up: Parallel Loops Previous: Solution

©University of Liverpool, 1997
Thu May 29 10:11:26 BST 1997
Not for commercial use.