Download presentation
Presentation is loading. Please wait.
Published byLee Charles Modified over 8 years ago
1
Computer Science 320 Barrier Actions
2
1-D Continuous Cellular Automata 1-D array of cells, each having a value between 0.0 and 1.0 Each cell has a neighborhood consisting of itself and the cells to its left and right The array wraps arround to accommodate neighbors of first and last cells
3
Processing a 1-D CCA Initially, all cells are 0, except for X C/2 = 1 For each cell, calculate a new value by multiplying the average of the neighbors by a constant A, adding another constant B, and keeping the fractional part
4
Processing a 1-D CCA sX0X1X2X3X4X5X6X7X8 X9 0000001000 0 111/1211/1211/1211/121/41/41/411/1211/12 11/12 25/65/65/611/187/181/67/1811/185/6 5/6 33/43/473/10819/3611/3625/10811/3619/3673/108 3/4 42/352/8146/8134/8122/8116/8122/8134/8146/81 52/81 5551/972 527/972 149/324109/32423/10853/32423/108109/324 149/324 After 5 iterations on10 cells, with A = 1 and B = 11/12 Note the rational number results
5
Imaging the 1-D CCA After 5 iterations on10 cells, with A = 1 and B = 11/12
6
Imaging the 1-D CCA After 200 iterations on 400 cells, with A = 1 and B = 11/12
7
Resources for Rational Arithmetic edu.rit.smp.ca.BigRational –assign –add –mul –fracPart –normalize –floatValue
8
Program Design: Data Could use a byte matrix for the pixel data and a BigRational matrix for the cells But O(SC) complexity: limits both the number of cells and the number of steps when scaling up
9
Program Design: Data Represent just the current row and the previous row (2 arrays of C cells) Swap the array references after each step to avoid copying cells Also need just the row of current pixel values, not the whole matrix
10
CCASeq import edu.rit.image.GrayImageRow; import edu.rit.image.PJGGrayImage; import edu.rit.image.PJGImage; import edu.rit.util.Range; // Constants. static final BigRational ZERO = new BigRational ("0"); static final BigRational ONE = new BigRational ("1"); static final BigRational ONE_THIRD = new BigRational ("1/3"); // Command line arguments. static int C; static int S; static BigRational A; static BigRational B; static File imagefile;
11
CCASeq // Old and new cell arrays. static BigRational[] currentCell; static BigRational[] nextCell; // Grayscale image matrix. static byte[][] pixelmatrix; static PJGGrayImage image; static PJGImage.Writer writer; // One row of the grayscale image matrix. static byte[] pixelrow; static GrayImageRow imagerow;
12
CCASeq // Parse command line arguments. if (args.length != 5) usage(); C = Integer.parseInt (args[0]); S = Integer.parseInt (args[1]); A = new BigRational (args[2]).mul(ONE_THIRD); B = new BigRational (args[3]); imagefile = new File (args[4]); // Allocate storage for old and new cell arrays. Initialize all cells to // 0, except center cell to 1. currentCell = new BigRational[C]; nextCell = new BigRational[C]; for (int i = 0; i < C; ++ i){ currentCell[i] = new BigRational(); nextCell[i] = new BigRational(); } currentCell[C/2].assign(ONE);
13
CCASeq // Set up pixel matrix, image, and image writer. pixelmatrix = new byte [S+1] []; image = new PJGGrayImage (S+1, C, pixelmatrix); writer = image.prepareToWrite(new BufferedOutputStream (new FileOutputStream(imagefile))); // Allocate storage for one pixel matrix row. pixelrow = new byte[C]; imagerow = new GrayImageRow(pixelrow); imagerow.setInterpretation(PJGGrayImage.ZERO_IS_WHITE);
14
CCASeq // Do S time steps. for (int s = 0; s < S; ++ s){ // Calculate next state of each cell. for (int i = 0; i < C; ++ i){ nextCell[i].assign (currentCell[i]).add (currentCell[(i-1+C)%C]).add (currentCell[(i+1)%C]).mul (A).add (B).normalize().fracPart(); } // Write current CA state to image file. writeCurrentCell(s); // Advance one time step -- swap old and new cell arrays. BigRational[] tmp = currentCell; currentCell = nextCell; nextCell = tmp; }
15
CCASeq private static void writeCurrentCell(int r) throws IOException{ // Set image row's gray values based on current cell states. for (int i = 0; i < C; ++ i) imagerow.setPixel(i, currentCell[i].floatValue()); // Set row r of the pixel matrix. pixelmatrix[r] = pixelrow; // Write row-r slice of the image to the image file. writer.writeRowSlice(new Range(r, r)); }
16
Parallelize! Calculation of each row depends on previous row, so can’t have a parallel outer loop But calcs within a row are independent, so can have a parallel inner loop But must synchronize array reference swaps and writing pixel data to file
17
Barrier Action All threads in a parallel for loop wait at a barrier until all are finished A barrier action allows one thread to do some cleanup while the others continue to wait at the barrier
18
Barrier Action new ParallelTeam().execute(new ParallelRegion(){... execute(0, 99, new IntegerForLoop(){ public void run(){ for (in I = first; I <= last; ++i) // Loop body } }, new BarrierAction(){ public void run(){ // Code to execute in a single thread } });... ));
19
Barrier Action
20
CCASmp // Do S time steps. Sequential outer loop. for (int s = 0; s < S; ++ s){ final int step = s; // Calculate next state of each cell. Parallel inner loop. execute (0, C-1, new IntegerForLoop(){ public IntegerSchedule schedule(){ return IntegerSchedule.guided(); } public void run (int first, int last){ for (int i = first; i <= last; ++ i){ nextCell[i].assign (currentCell[i]).add (currentCell[(i-1+C)%C]).add (currentCell[(i+1)%C]).mul (A).add (B).normalize().fracPart(); } },
21
CCASmp // Synchronize threads before next outer loop iteration. new BarrierAction(){ public void run() throws Exception{ // Write current CA state to image file. writeCurrentCell (step); // Advance one time step -- swap old and new cell // arrays. BigRational[] tmp = currentCell; currentCell = nextCell; nextCell = tmp; } });
22
Performance
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.