Download presentation
Presentation is loading. Please wait.
Published byKerry Parsons Modified over 9 years ago
1
1 CS4402 – Parallel Computing Lecture 6 MPE Library – Graphics in MPI. Parallel Computer Graphics
2
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
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().
4
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
5 Control Routines MPE_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
6 Output Routines Output 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
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
8 Example 1: Draw several points #include #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
9 Monte Carlo Methods Monte Carlo = Random Hard Combinatorial Problems: - search with huge space. - NP complete - etc Classical examples: - PI approximation. - Integral calculation.
10
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
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
12 Example 2: Approximate PI. #include #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
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
14
9/6/201514 FRACTALS
15
9/6/201515 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.
16
9/6/201516
17
9/6/201517
18
9/6/201518 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 P 0 - iteratively generate P 1 =F(P 0 ), P 2 =F(P 1 ), …, P n =F(P n-1 ), … P 0 is a set of initial points F is a transformation: Geometric transformations: translations, rotations, scaling, … Non-Linear coordinate transformation.
19
9/6/201519 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.
20
9/6/201520 Koch’s curve
21
9/6/201521 Julia Set
22
9/6/201522 Mandelbrot Set
23
9/6/201523 Complex Numbers C the set complex numbers z=a+i*b, a,b R and i 2 =-1 Operations with Complex Numbers: z 1 + z 2 = (a 1 +i*b 1 ) + (a 2 +i*b 2 )=(a 1 + a 2 ) +i*(b 1 + b 2 ). z 1 * z 2 = (a 1 +i*b 1 ) * (a 2 +i*b 2 )=(a 1 *a 2 - b 1 *b 2 ) +i*(a 1 *b 2 + b 1 *a 2 ). | z 1 | = sqrt(a 1 2 + b 1 2 ) bz=a+i*b |z| a
24
9/6/201524 Complex Class # include 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); }
25
9/6/201525 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
26
9/6/201526 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 Points vs Pixels
27
9/6/201527 Julia Sets – Self-Squaring Fractals Consider the generating function F(z)=z 2 +c, z,c C. Sequence of complex numbers: z 0 C and z n+1= z n 2 + c. Chaotic behaviour but two attractors for |z n |: 0 and + . For a c C, Julia’s set J c represents all the points whose orbit is finite.
28
9/6/201528 Julia Sets – Algorithm Inputs: c C the complex number; [x min,x max ] * [y min,y max ] a region in plane. N iter a number of iterations for orbits; R a threshold for the attractor . Output: J c the Julia set of c Algorithm For each pixel (i,j) on the screen translate (i,j) into (x,y) construct z 0 =x+j*y; find the orbit of z 0 [first N iter elements] if (all the orbit points are under the threshold) draw (x,y)
29
9/6/201529 for(i=0; i<=width; i++) for(j=0; j<width; j++) { int k =0; // construct the orbit of z z.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 infinite if (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); }
30
9/6/201530 Julia Sets – || Algorithm Remark 1. zThe double for loop on (i,j) can be split into processors e.g. y uniform block or cyclic on i. y uniform block or cyclic on j. zNo communication at all between processors, therefore this is embarrassingly || computation. Remark 2. zAll processors draw a block of the fractal or several rows on the XGraph. zP rank knows the area to draw.
31
9/6/201531 // 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 z z.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 infinite if (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); }
32
9/6/201532
33
9/6/201533
34
9/6/201534 Maldelbrot Set Maldelbrot Set contains all the points c C such that z 0 =0 and z n+1= z n 2 + c has an finite orbit. Inputs: [x min,x max ] * [y min,y max ] a region in plane. N iter a number of iterations for orbits; R a threshold for the attractor . Output: M the Mandelbrot set. Algorithm For each (x,y) in [x min,x max ] * [y min,y max ] c=x+i*y; find the orbit of z 0 =0 while under the threshold. if (all the orbit points are not under the threshold) draw c(x,y)
35
9/6/201535
36
9/6/201536 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 then if(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.
37
9/6/201537
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.