Computer Science 320 Reduction Variables and Operators.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
CSE 303 Lecture 16 Multi-file (larger) programs
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
ITC 240: Web Application Programming
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS150 Introduction to Computer Science 1
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
P Fig. 6-1, p. 193 Fig. 6-2, p. 193 Fig. 6-3, p. 195.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS0007: Introduction to Computer Programming Introduction to Arrays.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
Scope.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Computer Science 320 Load Balancing for Hybrid SMP/Clusters.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Concurrency, Mutual Exclusion and Synchronization.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© David Kirk/NVIDIA and Wen-mei W. Hwu ECE408/CS483/ECE498al, University of Illinois, ECE408 Applied Parallel Programming Lecture 12 Parallel.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Parallel Algorithms Patrick Cozzi University of Pennsylvania CIS Spring 2012.
Design Issues. How to parallelize  Task decomposition  Data decomposition  Dataflow decomposition Jaruloj Chongstitvatana 2 Parallel Programming: Parallelization.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Computer Science 320 Introduction to Hybrid SMP/Clusters.
Computer Science 320 Reduction. Estimating π Throw N darts, and let C be the number of darts that land within the circle quadrant of a unit circle Then,
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 320 Load Balancing with Clusters.
© David Kirk/NVIDIA and Wen-mei W. Hwu, University of Illinois, CS/EE 217 GPU Architecture and Parallel Programming Lecture 11 Parallel Computation.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Computer Science 320 Parallel Image Generation. The Mandelbrot Set.
Computer Science 320 Random Numbers for Parallel Programs.
Computer Science 320 A First Program in Parallel Java.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Computer Science 320 Barrier Actions. 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.
Computer Science 320 Reduction. Estimating π Throw N darts, and let C be the number of darts that land within the circle quadrant of a unit circle Then,
Computer Science 320 Cache Interference. Unexpected Performance The testing of the partial key search SMP program produced anomalous results In particular,
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Perkins Honors Precalculus Day 1 Section Write the first 5 terms for each sequence. Set of terms sequence. Calculator: LIST : OPS : seq( expression.
© David Kirk/NVIDIA and Wen-mei W. Hwu, University of Illinois, CS/EE 217 GPU Architecture and Parallel Programming Lecture 12 Parallel Computation.
Test 2 Review Outline.
CSC111 Quick Revision.
ECE Application Programming
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Parallel Computation Patterns (Scan)
Introduction to Computer Science
Presentation transcript:

Computer Science 320 Reduction Variables and Operators

Get a Histogram of Mandelbrot Set Print to a file the number of points generated on each iteration Same inputs as before, but the output text file will hold a table whose first column runs from 0 to N and whose second column includes the number of points for each I

MSHistogramSeq Maintains an array of N + 1 integers Increments the array element at position i after exiting the nested loop Prints the array indexes and contents to a text file // Histogram (array of counters indexed by pixel value). static int[] histogram; … // Create histogram. histogram = new int [maxiter + 1]; … while (i < maxiter && zmagsqr <= 4.0){ // i will be the pixel “value” when the loop exits } // Increment histogram counter for pixel value. ++ histogram[i];

Parallelize without Reduction All threads write to the same shared array Use edu.rit.reduction.SharedIntegerArray for multiple-thread-safe incrementAndGet Only minor changes to the structure of the Seq program

MSHistogramSmp import edu.rit.pj.reduction.SharedIntegerArray; // Histogram (array of counters indexed by pixel value). static SharedIntegerArray histogram; // Create histogram. histogram = new SharedIntegerArray (maxiter + 1); … execute (0, height-1, new IntegerForLoop() … while (i < maxiter && zmagsqr <= 4.0){ // i will be the pixel “value” when the loop exits } // Increment histogram counter for pixel value. histogram.incrementAndGet(i);

Reduction Pattern Shared variable to hold the complete result of the whole program Each thread has its own variable to hold the partial result it computes The partial results are combined in the shared variable as each thread finishes

Reduction Variables and Operators A reduction variable holds the complete result A reduction operator combines this variable with a partial result The variable and operator are multiple-thread-safe

Using a Reduction Operator // Import integer and operator classes import edu.rit.pj.reduction.SharedInteger; import edu.rit.pj.reduction.IntegerOp; // Initialize an integer SharedInteger x = new SharedInteger(0); // Thread-safe increment x by 4 and return new value of x to result int result = x.reduce(4, IntegerOp.SUM); // Definition of SUM in IntegerOp public static final IntegerOp SUM = new IntegerOp(){ public in op(int x, int y){ return x + y; } }; (x op y) = x.reduce(y, op)

Reduction Operator for Arrays // Import shared integer array and operator classes import edu.rit.pj.reduction.SharedIntegerArray; import edu.rit.pj.reduction.IntegerOp; // Initialize a shared integer array and a regular integer array SharedIntegerArray x = new SharedIntegerArray(1000); int[] y = new int[1000]; // Thread-safe increment each x[i] by y[i] x.reduce(y, IntegerOp.SUM); x.length <= y.length

Threads Reducing with an Array

MSHistogramSmp2 // In the IntegerForLoop // Reduce per-thread histogram into global histogram. public void finish(){ histogram.reduce(thr_histogram, IntegerOp.SUM); }

MSHistogramSmp2 // In the ParallelRegion // Per-thread histogram, plus extra padding. int[] thr_histogram = new int [maxiter ]; long p0, p1, p2, p3, p4, p5, p6, p7; long p8, p9, pa, pb, pc, pd, pe, pf; // In the IntegerForLoop // Reduce per-thread histogram into global histogram. public void finish(){ histogram.reduce(thr_histogram, IntegerOp.SUM); }

Critical Sections Sometimes you have to do more than appl;y a binary reduction operator to combine results You have to execute a more complex block of code that is multiple-thread-safe Critical sections support the needed synchronization

Critical Sections When a thread is executing code in a critical section and another thread enters the same critical section, the second thread blocks until the first thread completes the section And so on, for all the other threads Guarantees mutual exclusion within a critical section: only one thread at a time can be executing in the section

Critical Sections in Parallel Java new ParallelTeam().execute(new ParallelRegion(){ public void run(){ // Non-multiple-thread-safe code critical(new ParallelSection(){ public void run(){ // Code for the critical section (multiple-thread-safe) } }); // Non-multiple-thread-safe code } });

MSHistogramSmp3 // Create histogram. histogram = new int [maxiter + 1]; // Reduce per-thread histogram into global histogram. public void finish() throws Exception{ region().critical(new ParallelSection(){ public void run(){ for (int i = 0; i <= maxiter; ++ i){ histogram[i] += thr_histogram[i]; } }); } Shared histogram is now a standard array, made thread-safe by being accessed within a critical section