Download presentation
Presentation is loading. Please wait.
Published byHubert Hall Modified over 9 years ago
1
Computer Science 320 Parallel Image Generation
2
The Mandelbrot Set
3
A set of points defined as follows: given a point (x, y), compute a sequence of other points (a i, b i ), i = 0, 1, 2, … a 0 = 0 b 0 = 0 a i+1 = a i 2 – b i 2 + x b i+1 = 2 * a i * b i + y If each point in (a i, b i ) stays finite, (x, y) is in; otherwise, not in
4
The Mandelbrot Set If each point in (a i, b i ) stays finite, (x, y) is in; otherwise, not in Can’t do an infinite # of points, so if (a i, b i ) ever exceeds a distance of 2 from the origin, it’s not in or: if (a i 2 + b i 2 ) 1/2 > 2 for some i
5
The Mandelbrot Set or: if (a i 2 + b i 2 ) 1/2 > 2 for some i If we do 1,000 points, if i reaches this limit before the distance exceeds 2, we’ll call the point in, even if further tests might show it to be out
6
Program Resources Will use the Parallel Java Graphics (PJG) format, which is lossless and larger, but uses faster compression, than PNG format Will generate color values for each point and save these in a PJG file
7
Program Inputs Image width and height Coordinates of the image center point Image resolution in pixels per unit Maximum number of iterations to test for membership Exponent in formula to calculate pixel hues Output file name
8
Pixel Matrix, Image File, Hue Table // Create image matrix to store results. matrix = new int [height] [width]; image = new PJGColorImage (height, width, matrix); // Create table of hues for different iteration counts. huetable = new int [maxiter+1]; for (int i = 0; i < maxiter; ++ i){ huetable[i] = HSB.pack(/*hue*/ (float) Math.pow(((double)i) / ((double)maxiter), gamma), /*sat*/ 1.0f, /*bri*/ 1.0f); } huetable[maxiter] = HSB.pack (1.0f, 1.0f, 0.0f);
9
Optimizing Matrix Access // Compute all rows and columns. for (int r = 0; r < height; ++ r){ int[] matrix_r = matrix[r]; double y = ycenter + (yoffset - r) / resolution; for (int c = 0; c < width; ++ c){ double x = xcenter + (xoffset + c) / resolution; Allows access to a cell with a single index operation
10
Iterate Until Convergence // Iterate until convergence. int i = 0; double aold = 0.0; double bold = 0.0; double a = 0.0; double b = 0.0; double zmagsqr = 0.0; while (i < maxiter && zmagsqr <= 4.0){ ++ i; a = aold * aold – bold * bold + x; b = 2.0 * aold * bold + y; zmagsqr = a * a + b*b; aold = a; bold = b; } // Record number of iterations for pixel. matrix_r[c] = huetable[i];
11
Parallelize the Program Each pixel value can be computed independently, so divide the matrix rows among the threads All inputs are shared No per-thread or local variables need synchronization No padding needed
12
The Parallel for Loop // Compute all rows and columns. new ParallelTeam().execute(new ParallelRegion(){ public void run() throws Exception{ execute (0, height-1, new IntegerForLoop(){ public void run (int first, int last){ for (int r = first; r <= last; ++ r){ int[] matrix_r = matrix[r]; double y = ycenter + (yoffset - r) / resolution;
13
Behavior of Parallel Program
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.