+++ /dev/null
-#! /usr/bin/env python
-
-from __future__ import division
-from pylab import *
-import sys
-import Numeric
-
-
-#
-# plot_image.py
-#
-# Step 1: make this file executable
-#
-# chmod +x plot_image.py
-#
-# Step 2: pipe data in python script
-#
-# ./gen_data | ./plot_image -s nx ny -c nc -t 'image title'
-#
-# with optional arguments
-# -s nx ,ny image size [16x16]
-# -c nc number of contour levels [none]
-# -t ' ' image title ['some like it hot']
-#
-# additional: -g gray map [jet map]
-# -h hot map
-#
-# ref: matplotlib web site
-# Michel Vallieres, 2007
-#
-
- # dummy function to initialize Z
-def func3(x,y):
- return 0.005*x*y
-
- # defaults
-mycontours = 0
-nx = 16
-ny = 16
-mytitle = 'Some like it hot'
-mymap = cm.jet
-
- # parse command line arguments
-n = len( sys.argv )
-i = 1
-while i < n:
- if sys.argv[i].find("s") == 1:
- nx = int( sys.argv[i+1] )
- ny = int( sys.argv[i+2] )
- i = i + 2
- elif sys.argv[i].find("c") == 1:
- mycontours = int( sys.argv[i+1] )
- i = i + 1
- elif sys.argv[i].find("t") == 1:
- mytitle = sys.argv[i+1]
- i = i + 1
- elif sys.argv[i].find("g") == 1:
- mymap = cm.gray
- elif sys.argv[i].find("h") == 1:
- mymap = cm.hot
- else:
- print " Syntax: script -s nx ny -c "
- i = i + 1
-
- # identification
-print "Plot_image"
-print "Title: ", mytitle
-print "Image size: ", nx, ny
-print "# countour lines: ", mycontours
-
-
- # set grid
-x = range( nx )
-y = range( ny )
-
-X,Y = meshgrid( x, y )
-
-Z = func3( X, Y )
-
- # read in data
-for j in y:
- for i in x:
- Z[j,i] = input()
-
- # min & max
-min_data = Z[0,0]
-max_data = Z[0,0]
-for i in x:
- for j in y:
- if Z[j,i] < min_data:
- min_data = Z[j,i]
- if Z[j,i] > max_data:
- max_data = Z[j,i]
-
-print "Data range: ", min_data, max_data
-
-
- # colored image
-im = imshow( Z, interpolation='bilinear', origin='lower',
- cmap=mymap, extent=(1,nx-1.0,1,ny-1.0) )
-
- # contour lines
-if mycontours > 0:
- dcont = ( max_data - min_data ) / ( mycontours - 1 )
- cset = contour( Z, arange(min_data,max_data,dcont),
- origin='lower',
- linewidths=2,
- extent=(0,nx-1,0,ny-1)
- )
-
- clabel( cset, inline=1, fmt='%1.1f', fontsize=10 )
-
-
- # render picture
-axis('off')
-
-colorbar()
-title( mytitle )
-show()
-
<h3 id="MS2">Sequential Implementation</h3>
-<p>The code <a href="src/mandelbrot.c">mandelbrot.c</a> generates the
-Mandelbrot Set via a direct coding of the rules given above. This
+<p>The code <a
+href="../../src/mandelbrot/mandelbrot.c">mandelbrot.c</a> generates
+the Mandelbrot Set via a direct coding of the rules given above. This
leads to a somewhat slow algorithm to generate the Mandelbrot Set; so
be it! We only need to use this code as an example as how to implement
a parallel version of the code.</p>
range. For our purpose, this tool must read in a 2-dimensional array
containing the values of $f(x,y)$ on a 2-dimensional lattice and
produce the color image. The Python script <a
-href="src/plot_image.py">plot_image.py</a> does precisely this. It is
+href="../../src/plot_image.py">plot_image.py</a> does precisely this. It is
based on <a href="http://matplotlib.sourceforge.net/">matplotlib</a>
(the tutorials, user's guide and examples found in the matplotlib site
are easier to read with some previous knowledge of <a
href="http://www.python.org/">Python</a> and <a
href="http://numpy.scipy.org/">Numerical Python</a>).</p>
-<p>Use <a href="src/gen_data.c">gen_data.c</a>,
-<a href="src/gen_data_sharp.c">gen_data_sharp.c</a>, and
-<a href="src/gen_data_rectangle.c">gen_data_rectangle.c</a> to form
-simple images by feeding sample data in
+<p>Use <a
+href="../../src/plot_image/gen_data_rectangle.c">gen_data_rectangle.c</a>,
+<a href="../../src/plot_image/gen_data_sharp.c">gen_data_sharp.c</a>,
+and <a
+href="../../src/plot_image/gen_data_sinusoid.c">gen_data_sinusoid.c</a>
+to form simple images by feeding sample data in
<code>plot_image.py</code>. These codes produce images of size $N_x =
-200 \times N_y = 200$ and $N_x = 300 \times N_y = 200$ for the third
-code respectively. Read the comments in <code>plot_image.py</code> to
-learn how to use it. Practice the different options.</p>
+300 \times N_y = 200$. Read the output of <code>plot_image.py
+--help</code> to learn how to use it. Practice the different
+options.</p>
<h3 id="MS3">On the way to a Parallel Code</h3>
functions must have appropriate arguments. This code should produce
the same results as the previous one.</p>
-<p>This code is listed in <a href="src/MS-codes/MS1.c">MS1.c</a>.</p>
+<p>This code is listed in <a
+href="../../src/mandelbrot/MS1.c">MS1.c</a>.</p>
<p>Next we must take into account the slicing of the complex
plane. Modify the code into a new serial code called
slices in the code as an example. This code should produce the same
results as the previous one.</p>
-<p>This code is listed in <a href="src/MS-codes/MS2.c">MS2.c</a>.</p>
+<p>This code is listed in <a
+href="../../src/mandelbrot/MS2.c">MS2.c</a>.</p>
<p>The parallel implementation is now relatively simple. You should
write yet two new functions, <code>master()</code> and
it <code>MS3.c</code>, should produce the same image as the original
serial code.</p>
-<p>This code is listed in <a href="src/MS-codes/MS3.c">MS3.c</a></p>
+<p>This code is listed in <a
+href="../../src/mandelbrot/MS3.c">MS3.c</a></p>
<!--#include virtual="$root_directory/shared/footer.shtml"-->
+++ /dev/null
-#include <stdio.h>
-#include <math.h>
-
-#define N_POINTS 200
-
-main()
-{
- int i, j;
- double x, y, dx, dy;
-
- dx = 2*M_PI/N_POINTS;
- dy = 2*M_PI/N_POINTS;
- for (j = 0; j < N_POINTS; j++)
- for (i = 0; i < N_POINTS; i++) {
- x = i*dx;
- y = j*dy;
- printf("%f \n", 5.0*(1 + sin(x)*cos(y)));
- }
-}
-
+++ /dev/null
-#include <stdio.h>
-#include <math.h>
-
-#define N_POINTSx 300
-#define N_POINTSy 200
-
-main()
-{
- int i, j;
- double x, y, dx, dy;
-
- dx = 2*M_PI/N_POINTSx;
- dy = dx;
- for (j = 0; j < N_POINTSy; j++)
- for (i = 0; i < N_POINTSx; i++) {
- x = i*dx;
- y = j*dy;
- printf("%f \n", 5.0*(1 + sin(x)*cos(y)));
- }
-}
-
+++ /dev/null
-#include <stdio.h>
-#include <math.h>
-
-#define N_POINTS 200
-
-main()
-{
- int i, j;
- double x, y, value;
-
- for (j = 0; j < N_POINTS; j++)
- for (i = 0; i < N_POINTS; i++) {
- value = 0.0;
- if ( j > N_POINTS/2 )
- value = 1.5;
- printf("%f \n", value);
- }
-}
-
--- /dev/null
+CC = /usr/bin/mpicc
+CFLAGS =
+LD = $(CC)
+LDFLAGS =
+RM = /bin/rm
+EXAMPLES = rectangle sharp sinusoid
+EXECS = $(EXAMPLES:%=gen_data_%)
+FIGURES = $(EXAMPLES:%=%.png)
+
+all: $(FIGURES)
+
+%.o : %.c
+ $(CC) -c $(CFLAGS) -o $@ $^
+
+$(EXECS) : % : %.o
+ $(LD) $(LDFLAGS) -o $@ $^
+
+$(FIGURES) : %.png : gen_data_% plot_image.py
+ ./$< | ./plot_image.py -t "$(<:%.png=%)" -s 300,200 -o "$@"
+
+clean:
+ $(RM) -f *.o $(EXECS) $(FIGURES)
+
+# Interesting Makefile sections
+# 4.12.1 Syntax of Static Pattern Rules
--- /dev/null
+`Simple image plotting.
+
+Generate some example images with::
+
+ $ make
+
+Expected output:
+
+* rectangle.png High values in a wide, centered rectangle.
+* sharp.png High values in the top half of the figure.
+* sinusoid.png 5*(1+sin(x)*cos(y)) for x,y in [0,2pi).
--- /dev/null
+#include <stdio.h>
+#include <math.h>
+
+#define N_X 300
+#define N_Y 200
+
+main()
+{
+ int i, j, z;
+
+ for (j = 0; j < N_Y; j++) {
+ for (i = 0; i < N_X; i++) {
+ z = 0;
+ if (i > N_X/5 && i < N_X*4/5 && j > N_Y/3 && j < N_Y*2/3)
+ z = 1;
+ printf("%d\n", z);
+ }
+ }
+}
--- /dev/null
+#include <stdio.h>
+#include <math.h>
+
+#define N_X 300
+#define N_Y 200
+
+main()
+{
+ int i, j;
+ double x, y, value;
+
+ for (j = 0; j < N_Y; j++)
+ for (i = 0; i < N_X; i++) {
+ value = 0.0;
+ if (j > N_Y / 2)
+ value = 1.5;
+ printf("%f\n", value);
+ }
+}
--- /dev/null
+#include <stdio.h>
+#include <math.h>
+
+#define N_X 300
+#define N_Y 200
+
+main()
+{
+ int i, j;
+ double x, y, dx, dy;
+
+ dx = 2 * M_PI / N_X;
+ dy = 2 * M_PI / N_Y;
+ for (j = 0; j < N_Y; j++) {
+ for (i = 0; i < N_X; i++) {
+ x = i * dx;
+ y = j * dy;
+ printf("%f \n", 5.0 * (1 + sin(x) * cos(y)));
+ }
+ }
+}
"""
X,Y = pylab.meshgrid(range(nx+1), range(ny+1))
Z = numpy.loadtxt(stream)
- Z = Z.reshape([x-1 for x in X.shape])
+ assert Z.size == nx*ny, 'Z.size = %d != %d = %dx%d' % (
+ Z.size, nx*ny, nx, ny)
+ Z = Z.reshape([x-1 for x in X.shape])
return (X,Y,Z)