From 2cd566a45ccffa579cce1702a28aea3ad0cf067f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 9 Nov 2010 10:22:20 -0500 Subject: [PATCH] Move gen_data_*.c from src/mandelbrot to src/plot_image & add README, Makefile. --- .../Python_Codes/plot_image.py | 120 ------------------ .../index.shtml.itex2MML | 32 +++-- src/mandelbrot/gen_data.c | 20 --- src/mandelbrot/gen_data_rectangle.c | 21 --- src/mandelbrot/gen_data_sharp.c | 19 --- src/plot_image/Makefile | 25 ++++ src/plot_image/README | 11 ++ src/plot_image/gen_data_rectangle.c | 19 +++ src/plot_image/gen_data_sharp.c | 19 +++ src/plot_image/gen_data_sinusoid.c | 21 +++ src/plot_image/plot_image.py | 4 +- 11 files changed, 117 insertions(+), 194 deletions(-) delete mode 100755 content/programming_strategies/Python_Codes/plot_image.py delete mode 100644 src/mandelbrot/gen_data.c delete mode 100644 src/mandelbrot/gen_data_rectangle.c delete mode 100644 src/mandelbrot/gen_data_sharp.c create mode 100644 src/plot_image/Makefile create mode 100644 src/plot_image/README create mode 100644 src/plot_image/gen_data_rectangle.c create mode 100644 src/plot_image/gen_data_sharp.c create mode 100644 src/plot_image/gen_data_sinusoid.c diff --git a/content/programming_strategies/Python_Codes/plot_image.py b/content/programming_strategies/Python_Codes/plot_image.py deleted file mode 100755 index 09fbab1..0000000 --- a/content/programming_strategies/Python_Codes/plot_image.py +++ /dev/null @@ -1,120 +0,0 @@ -#! /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() - diff --git a/content/programming_strategies/index.shtml.itex2MML b/content/programming_strategies/index.shtml.itex2MML index 1930a91..3f7b4eb 100644 --- a/content/programming_strategies/index.shtml.itex2MML +++ b/content/programming_strategies/index.shtml.itex2MML @@ -177,8 +177,9 @@ complex plane:

Sequential Implementation

-

The code mandelbrot.c generates the -Mandelbrot Set via a direct coding of the rules given above. This +

The code mandelbrot.c 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.

@@ -192,21 +193,23 @@ must translate the function range to a color palette 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 plot_image.py does precisely this. It is +href="../../src/plot_image.py">plot_image.py does precisely this. It is based on matplotlib (the tutorials, user's guide and examples found in the matplotlib site are easier to read with some previous knowledge of Python and Numerical Python).

-

Use gen_data.c, -gen_data_sharp.c, and -gen_data_rectangle.c to form -simple images by feeding sample data in +

Use gen_data_rectangle.c, +gen_data_sharp.c, +and gen_data_sinusoid.c +to form simple images by feeding sample data in plot_image.py. 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 plot_image.py to -learn how to use it. Practice the different options.

+300 \times N_y = 200$. Read the output of plot_image.py +--help to learn how to use it. Practice the different +options.

On the way to a Parallel Code

@@ -242,7 +245,8 @@ functions: set_grid() which sets the grid up and functions must have appropriate arguments. This code should produce the same results as the previous one.

-

This code is listed in MS1.c.

+

This code is listed in MS1.c.

Next we must take into account the slicing of the complex plane. Modify the code into a new serial code called @@ -259,7 +263,8 @@ for ( slice=0 ; slice < N_slices ; slice++ ) slices in the code as an example. This code should produce the same results as the previous one.

-

This code is listed in MS2.c.

+

This code is listed in MS2.c.

The parallel implementation is now relatively simple. You should write yet two new functions, master() and @@ -321,6 +326,7 @@ Some might hang the machine, yet allow the debugging to be done. it MS3.c, should produce the same image as the original serial code.

-

This code is listed in MS3.c

+

This code is listed in MS3.c

diff --git a/src/mandelbrot/gen_data.c b/src/mandelbrot/gen_data.c deleted file mode 100644 index 665cd2e..0000000 --- a/src/mandelbrot/gen_data.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -#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))); - } -} - diff --git a/src/mandelbrot/gen_data_rectangle.c b/src/mandelbrot/gen_data_rectangle.c deleted file mode 100644 index ca94d9b..0000000 --- a/src/mandelbrot/gen_data_rectangle.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -#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))); - } -} - diff --git a/src/mandelbrot/gen_data_sharp.c b/src/mandelbrot/gen_data_sharp.c deleted file mode 100644 index f62aecf..0000000 --- a/src/mandelbrot/gen_data_sharp.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#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); - } -} - diff --git a/src/plot_image/Makefile b/src/plot_image/Makefile new file mode 100644 index 0000000..d0e5377 --- /dev/null +++ b/src/plot_image/Makefile @@ -0,0 +1,25 @@ +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 diff --git a/src/plot_image/README b/src/plot_image/README new file mode 100644 index 0000000..59716a5 --- /dev/null +++ b/src/plot_image/README @@ -0,0 +1,11 @@ +`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). diff --git a/src/plot_image/gen_data_rectangle.c b/src/plot_image/gen_data_rectangle.c new file mode 100644 index 0000000..db1b920 --- /dev/null +++ b/src/plot_image/gen_data_rectangle.c @@ -0,0 +1,19 @@ +#include +#include + +#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); + } + } +} diff --git a/src/plot_image/gen_data_sharp.c b/src/plot_image/gen_data_sharp.c new file mode 100644 index 0000000..c0abf3e --- /dev/null +++ b/src/plot_image/gen_data_sharp.c @@ -0,0 +1,19 @@ +#include +#include + +#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); + } +} diff --git a/src/plot_image/gen_data_sinusoid.c b/src/plot_image/gen_data_sinusoid.c new file mode 100644 index 0000000..7c27c66 --- /dev/null +++ b/src/plot_image/gen_data_sinusoid.c @@ -0,0 +1,21 @@ +#include +#include + +#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))); + } + } +} diff --git a/src/plot_image/plot_image.py b/src/plot_image/plot_image.py index 188a73d..0262cfd 100755 --- a/src/plot_image/plot_image.py +++ b/src/plot_image/plot_image.py @@ -63,7 +63,9 @@ def read_data(stream, nx, ny): """ 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) -- 2.26.2