1 cs4402 – parallel computing lecture 6 mpe library – graphics in mpi. parallel computer...

37
1 CS4402 – Parallel Computing Lecture 6 MPE Library – Graphics in MPI. Parallel Computer Graphics

Upload: kerry-parsons

Post on 26-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

1

CS4402 – Parallel Computing

Lecture 6

MPE Library – Graphics in MPI.

Parallel Computer Graphics

2

Visualization Problems

Visualization is a time-consuming process:- rendering issues.- computational issues, etc.

C Libraries can be used to visualization e.g. Open GL.MPE (Multi-Processing Environment) contains:

- Routines for creating logfiles for post-mortem analysis.- Debugger setup routines.- A shared-display parallel X graphics library.

1. Working with X windows.2. Drawing shapes.3. Mouse control.

3

X Window

X Window is a graphical space.All processor can access it to draw.Routines to work with:

- MPE_Open_graphics()- MPE_Close_graphics()- MPE_Update().

…..

P0

P1

P2

X Window

4

MPE Routines

Control Routines:MPE_Open_graphics(); MPE_Close_graphics(); MPE_Update();

Output Routines:MPE_Draw_point(); MPE_Draw_points();MPE_Draw_line(); MPE_Draw_circle(); MPE_Draw_rectangle();

MPE_Line_thickness();Color Routines:

MPE_Make_color_array(); MPE_Num_color();MPE_Add_RGB_color();

Input Routines:MPE_Get_mouse_press(); MPE_Get_drag_region();

Predefined Colors: MPE_WHITE, MPE_BLACK, MPE_RED, MPE_YELLOW, MPE_GREEN, MPE_CYAN, and MPE_BLUE

5

Control RoutinesMPE_Open_graphics (MPE_XGraph *window, MPI_Comm comm, char

*display, int x, int y, int width, int height, int is_collective);

Open a X window at x, y of sizes width, height. If -1 is passed for x and y, the user will be required to position the window.If NULL is passed for display, then the display will be configured automatically. Always pass 0 for is_collective. MPE_Open_graphics must be called by all nodes in comm.

MPE_Close_graphics (MPE_XGraph *window); Close the window associated with window. All processes must call this routine. Once any process has called MPE_Close_graphics, no process can call any other MPE routine.

Example:MPE_Open_graphics (&w, MPI_COMM_WORLD, NULL, -1, -1, width, height, 0);

// do some drawing

MPE_Close_graphics(&w);

6

Output RoutinesOutput routines draw points, circles, rectangles.The drawing color must be specified.

MPE_Draw_point (MPE_XGraph window, int x, int y, MPE_Color color); MPE_Draw_points(MPE_XGraph window, MPE_Point *points, int npoints); MPE_Draw_line (MPE_XGraph window, int x1, int y1, int x2, int y2, MPE_Color color); MPE_Fill_rectangle(MPE_XGraph window, int x, int y, int w, int h, MPE_Color color); MPE_Draw_circle (MPE_XGraph window, int x, int y, int r, MPE_Color color);

They specify:- The X window for graphics.- The elements of the shape to draw.- The color.

Use an MPE_Update after doing some graphics.Example:

MPE_Draw_point(w,100,100,MPE_BLACK);

7

MPE Mouse Routines

MPE_Get_mouse_press (MPE_XGraph window, int *x, int *y, int *button); Blocks until a mouse button is pressed in window. Then, the mouse position is returned in x and y. The number of the button that was pressed is returned in button.

MPE_Get_mouse_status (MPE_XGraph window, int *x, int *y, int *button, int *wasPressed);

It does exactly the same thing as MPE_Get_mouse_press, but returns immediately even if no button is pressed.

MPE_Drag_region (MPE_XGraph window, int *startx, int *starty, int *endx, int *endy);

Wait for the user to drag out a square on the screen.

8

Example 1: Draw several points#include <stdio.h>#include "mpi.h"

int main( int argc, char* argv[]){

int rank, size; int i,n=1000000; int width=500,height=500,x,y;MPE_Color * cls = {MPE_BLACK, MPE_RED, MPE_BLUE, MPE_GREEN};MPE_XGraph w;

MPI_Init( &argc, &argv );MPI_Comm_size( MPI_COMM_WORLD, &size );MPI_Comm_rank( MPI_COMM_WORLD, &rank );

MPE_Open_graphics (&w, MPI_COMM_WORLD, NULL, -1, -1, width, height, 0); for(i=0;i<n;i++){

x=(int)(500*random());y=(int)(500*random());MPE_Draw_point(w,x,y,cls[rank%cls.length]);

}MPE_Close_graphics(&w);

MPI_Finalize();return 0;

}

9

Monte Carlo Methods

Monte Carlo = Random Hard Combinatorial Problems:

- search with huge space.- NP complete - etc

Classical examples: - PI approximation.- Integral calculation.

10

Simple PI approximation

Throw n random points in [0,1]^2- some of them fall in the circle m

If n is big then m is big too. - hence n, m can be assimilated with area

area[circle] / area[square] ≈ m/n PI*R^2 / 4*R^2 = m/n PI = 4*m/n

11

PI program

Processor rank does:

- throw and draw n random points in the square.

- count when a point falls in the circle.

Reduce the number of points in the circle.

If processor 0 then

- approximate PI by PI=4*total_m/total_n

12

Example 2: Approximate PI.#include <stdio.h>#include "mpi.h“

int main( int argc, char* argv[]){ int rank, size; int i,n=1000000; int width=500,height=500,x,y;

int count, final_count;MPI_Init( &argc, &argv );MPI_Comm_size( MPI_COMM_WORLD, &size );MPI_Comm_rank( MPI_COMM_WORLD, &rank );MPE_Open_graphics (&w, MPI_COMM_WORLD, NULL, -1, -1, width, height, 0); for(i=0;i<n;i++){

x=(int)(500*random()); y=(int)(500*random());if (d(x,y,250,250)<250){ count++;

MPE_Draw_point(w,x,y,MPE_RED);}else MPE_Draw_point(w,x,y,MPE_BLUE);

}MPE_Close_graphics(&w);MPI_Reduce(&count,&final_count,1, MPI_INT,MPI_SUM,0, MPI_COMM_WORLD);if(rank==0){printf(“PI=%lf”,(4.*final_count)/(size*n));}MPI_Finalize();return 0;

}

13

Buffon Problem

Grid of horizontals with d

Throw n needles of length d

Some of them m intersect the lines

The PI can be calculated based on the equation

PI = 2*n / m

04/19/23 14

FRACTALS

04/19/23 15

Embarrassingly Parallel Computation

Embarrassingly (Pleasantly) Parallel Computation:

- No or less effort to divide the problem into separate tasks.

- No or less communication between distinct tasks.

Example:

- Find the number of primes.

- Approximate randomly the value of PI.

- Monte Carlo method & Buffon’s problem.

- Fractals.

04/19/23 16

04/19/23 17

04/19/23 18

Fractals

A fractal is a set of points such that:

- its fractal dimension is infinite [infinite detail at every point].

- satisfies self-similarity: any part of the fractal is similar with the fractal.

Generating a fractal is a iterative process:

- start from P0

- iteratively generate P1=F(P0), P2=F(P1), …, Pn=F(Pn-1), …

P0 is a set of initial points

F is a transformation:

Geometric transformations: translations, rotations, scaling, …

Non-Linear coordinate transformation.

04/19/23 19

Classification

The nature of F gives:

Deterministic Fractals:

Stochastic Fractals: F is a random process.

The aspect of the fractal set:

Self-Similar: parts are scaled down of a fractal pattern.

Self-Affine: formed by scaling with different parameters.

Non-Linear Fractals: formed by non-linear transformation.

The most popular fractals:

- Koch’s fractal or Snowflake.

- The Julia sets.

- The Mandelbrot set.

04/19/23 20

Koch’s curve

04/19/23 21

Julia Set

04/19/23 22

Mandelbrot Set

04/19/23 23

Complex Numbers

C the set complex numbers z=a+i*b, a,bR and i2=-1

Operations with Complex Numbers:

z1 + z2 = (a1+i*b1) + (a2+i*b2)=(a1 + a2 ) +i*(b1+ b2).

z1 * z2 = (a1+i*b1) * (a2+i*b2)=(a1*a2 - b1*b2 ) +i*(a1*b2 + b1*a2 ).

| z1 | = sqrt(a12+ b1

2)

b z=a+i*b

|z|

a

04/19/23 24

Complex Class# include <math.h>

typedef struct {double re,im;}complex;complex sum(complex z1, complex z2){

complex z;z.re=z1.re+z2.re;z.im=z1.im+z2.im;return z;

}

complex product(complex z1, complex z2){complex z;z.re=z1.re*z2.re- z1.im*z2.im; z.im=z1.re*z2.im+ z1.im*z2.re;return z;

}

double abs(complex z){return sqrt(z.re*z.re+z.im*z.im);

}}

04/19/23 25

We work with 2 rectangular areas.

The user space:

- Real coordinates (x,y)

- Bounded between [xMin,xMax]*[yMin,yMax]

The screen space

- Integer coordinates (i, j)

- Bounded between [0,w-1]*[0,h-1]

- Is upside down with the Oy axis downward

How to squeeze the user space into the screen space?

How to translate (x,y) in (i,j)?

Points vs Pixels

04/19/23 26

How to transform (x,y) into (i,j)

i=(x-xmin)/(xmax-xmin)*w;

j=(y-ymin)/(ymax-ymin)*h

How to transform (i,j) into (x,y)

x=xmin+i*(xmax-xmin)/w;

y=ymin+j*(ymax-ymin)/h

yMax (x,y) xMin xMax yMin

(0,0) w-1 (i,j) h-1

Points vs Pixels

04/19/23 27

Julia Sets – Self-Squaring Fractals

Consider the generating function F(z)=z2+c, z,c C.

Sequence of complex numbers: z0C and zn+1= zn2 + c.

Chaotic behaviour but two attractors for |zn|: 0 and +.

For a c C, Julia’s set Jc represents all the points whose orbit is finite.

04/19/23 28

Julia Sets – Algorithm

Inputs:

c C the complex number; [xmin,xmax] * [ymin,ymax] a region in plane.

Niter a number of iterations for orbits; R a threshold for the attractor .

Output: Jc the Julia set of c

Algorithm

For each pixel (i,j) on the screen

translate (i,j) into (x,y)

construct z0=x+j*y;

find the orbit of z0 [first Niter elements]

if (all the orbit points are under the threshold) draw (x,y)

04/19/23 29

for(i=0; i<=width; i++) for(j=0; j<width; j++){

int k =0;// construct the orbit of zz.re = XMIN + i*STEP; z.im = YMIN + j*STEP;for (k=0; k < NUMITER; k++) {

z = func(z,c);if (CompAbs(z) > R) break;

}

// test if the orbit in infiniteif (k>NUMITER-1) {

MPE_Draw_point(graph, i,j, MPE_YELLOW); MPE_Update(graph);

}else {

MPE_Draw_point(graph, i,j, MPE_RED); MPE_Update(graph);

}}

04/19/23 30

Julia Sets – || Algorithm

Remark 1.

The double for loop on (i,j) can be split into processors e.g.

uniform block or cyclic on i.

uniform block or cyclic on j.

No communication at all between processors, therefore this is

embarrassingly || computation.

Remark 2.

All processors draw a block of the fractal or several rows on the XGraph.

Prank knows the area to draw.

04/19/23 31

// Initialise the MPI environment and open the XWindow…

// work of processor rank

for(i=rank*width/size; i<=(rank+1)*width/size; i++) for(j=0; j<width; j++){

int k =0;// construct the orbit of zz.re = XMIN + i*STEP; z.im = YMIN + j*STEP;for (k=0; k < NUMITER; k++) {

z = func(z,c);if (CompAbs(z) > R) break;

}

// test if the orbit in infiniteif (k>NUMITER-1) {

MPE_Draw_point(graph, i,j, MPE_YELLOW); MPE_Update(graph);

}else {

MPE_Draw_point(graph, i,j, MPE_RED); MPE_Update(graph);}

}

04/19/23 32

04/19/23 33

04/19/23 34

Maldelbrot Set

Maldelbrot Set contains all the points cC such that

z0=0 and zn+1= zn2 + c has an finite orbit.

Inputs: [xmin,xmax] * [ymin,ymax] a region in plane.

Niter a number of iterations for orbits; R a threshold for the attractor .

Output:

M the Mandelbrot set.

Algorithm

For each (x,y) in [xmin,xmax] * [ymin,ymax]

c=x+i*y;

find the orbit of z0=0 while under the threshold.

if (all the orbit points are not under the threshold) draw c(x,y)

04/19/23 35

04/19/23 36

Fractal and Prime Numbers

Prime numbers can generate fractals.Remarks:

- If p>5 is prime then p%5 is 1,2,3,4.- 1,2,3,4 represent direction to do e.g. left, right, up down.- The fractal has the sizes w and h.

Step 1. Initialise a matrix of color with 0.Step 2. For each number p>5

If p is prime thenif(p%5==1)x=(x-1)%w;if(p%5==2)x=(x+1)%w;if(p%5==3)y=(y-1)%w;if(p%5==4)y=(y+1)%w;

Increase the color of (x,y)

Step 3. Draw the pixels with the color matrix.

04/19/23 37