CS 284a, 18 November 1997 Copyright (c) 1997-98, John Thornley1 CS 284a Lecture Tuesday, 18 November, 1997.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CS 284a, 11 November 97 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 11 November 1997.
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Computer Science 320 Reduction Variables and Operators.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
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.
CS 284a, 8 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 8 October, 1997.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 284a, 4 November 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 4 November, 1997.
CS 284a Lecture Wednesday, 26 November, 1997
CS 284a, 5 November 97Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 5 November 1997.
CS 284a, 14 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 14 October, 1997.
INTEL CONFIDENTIAL Confronting Race Conditions Introduction to Parallel Programming – Part 6.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
10/04/2011CS4961 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options.
Java Programming: From the Ground Up
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Concurrency, Mutual Exclusion and Synchronization.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
CS101 Computer Programming I Chapter 4 Extra Examples.
10/02/2012CS4230 CS4230 Parallel Programming Lecture 11: Breaking Dependences and Task Parallel Algorithms Mary Hall October 2,
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
CSCI-100 Introduction to Computing
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
9/22/2011CS4961 CS4961 Parallel Programming Lecture 9: Task Parallelism in OpenMP Mary Hall September 22,
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS 284a, 21 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 21 October, 1997.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Concurrency Idea. 2 Concurrency idea Challenge –Print primes from 1 to Given –Ten-processor multiprocessor –One thread per processor Goal –Get ten-fold.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Open[M]ulti[P]rocessing
CiS 260: App Dev I Chapter 4: Control Structures II.
Counted Loops.
CS4230 Parallel Programming Lecture 12: More Task Parallelism Mary Hall October 4, /04/2012 CS4230.
Arrays, For loop While loop Do while loop
Repeating Instructions And Advance collection
Example Problems for Exam#2 (covers chapters 5, 6, 7, 8, 9)
Synchronization Issues
Iteration: Beyond the Basic PERFORM
Parallel Computation Patterns (Reduction)
Subject : T0152 – Programming Language Concept
JavaScript: Control Statements II
Loops.
CSE 153 Design of Operating Systems Winter 19
ECE 103 Engineering Programming Chapter 18 Iteration
6.2 for Loops Example: for ( int i = 1; i
Looping and Repetition
Presentation transcript:

CS 284a, 18 November 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 18 November, 1997

CS 284a, 18 November 1997 Copyright (c) , John Thornley2 Flags for Mutual Exclusion and Ordering { sthread_flag flag[n + 1]; float sum; sum = 0.0; for (i = 0; i < n; i++) sthread_flag_intialize(&flag[i]); sthread_flag_set(&flag[0]); #pragma multithreadable for (i = 0; i < n; i++) { float subsum = f(i); sthread_flag_check(&flag[i]); sum = sum + subsum; sthread_flag_set(&flag[i + 1]); } for (i = 0; i < n; i++) sthread_flag_finalize(&flag[i]); } One flag for each synchronization operation.

CS 284a, 18 November 1997 Copyright (c) , John Thornley3 Need Fewer Counters Than Flags { sthread_counter counter; float sum; sum = 0.0; sthread_counter_intialize(&counter); #pragma multithreadable for (i = 0; i < n; i++) { float subsum = f(i); sthread_counter_check(&counter, i); sum = sum + subsum; sthread_counter_increment(&counter, 1); } sthread_counter_finalize(&counter); } One counter for all n synchronization operations.

CS 284a, 18 November 1997 Copyright (c) , John Thornley4 Synchronization Counter Operations typedef... sthread_counter; int sthread_counter_initialize(sthread_counter *counter); /* Initialize before any other operation on counter. */ /* Counter value is initialized to zero. */ int sthread_counter_finalize(sthread_counter *counter); /* Finalize after all other operations on counter. */ int sthread_counter_increment(sthread_counter *counter, int amount); /* Increment counter by amount. */ int sthread_counter_check(sthread_counter *counter, int value); /* Suspend until counter value reaches at least the given value. */ int sthread_counter_reset(sthread_counter *counter); /* Set counter value to zero. */ Return code is error value ( STHREAD_ERROR_NONE for no error detected).

CS 284a, 18 November 1997 Copyright (c) , John Thornley5 Example: LU Factorization A LU

CS 284a, 18 November 1997 Copyright (c) , John Thornley6 void LU_factorize(double A[N][N]) { int i, j, k; double sum; for (i = 0; i < N; i++) { for (j = 0; j < i; j++) { sum = 0.0; for (k = 0; k < j1; k++) sum = sum + A[i][k]*A[k][j]; A[i][j] = (A[i][j] - sum)/A[j][j]; } for (j = i; j < N; j++) { sum = 0.0; for (k = 0; k < i; k++) sum = sum +A[i][k]*A[k][j]; A[i][j] = A[i][j] - sum; } Sequential Program

CS 284a, 18 November 1997 Copyright (c) , John Thornley7 Data Dependencies LU 0 N-1 0

CS 284a, 18 November 1997 Copyright (c) , John Thornley8 Sequential Execution Order 0N-1 0 LU

CS 284a, 18 November 1997 Copyright (c) , John Thornley9 Goal: Multithreaded Execution Subject to Data Dependencies 0N-1 0 LU

CS 284a, 18 November 1997 Copyright (c) , John Thornley10 Fine-Grained Implementation Alternatives N 2 flags - one for every cell. 2N counters - one for every row and every column.

CS 284a, 18 November 1997 Copyright (c) , John Thornley11 void LU_factorize(double A[N][N]) { counter row_count[N], column_count[N]; int i; for (i = 0; i < n; i++) { sthread_counter_initialize(&row_count[i]); sthread_counter_initialize(&column_count[i]); } #pragma multithreadable for (i = 0; i < N; i++) { int j1, j2; #pragma multithreadable { #pragma multithreadable for (j1 = 0; j1 < i; j1++) { int k; double sum = 0.0; check(row_count[i], j1); check(column_count[j1], j1 + 1); for (k = 0; k < j1; k++) sum = sum + A[i][k]*A[k][j1]; A[i][j1] = (A[i][j1] - sum)/A[j1][j1]; increment(row_count[i]); } #pragma multithreadable for (j2 = i; j2 < N; j2++) { int k; double sum = 0.0; check(row_count[i], i); check(column_count[j2], i); for (k = 0; k < i; k++) sum = sum +A[i][k]*A[k][j2]; A[i][j2] = A[i][j2] - sum; increment(column_count[j2]); } for (i = 0; i < n; i++) { sthread_counter_finalize(&row_count[i]); sthread_counter_finalize(&column_count[i]); }

CS 284a, 18 November 1997 Copyright (c) , John Thornley12 Efficient Solution Synchronization on every cell is too fine-grained. Need to group cells into blocks. Need counter per row and column of blocks. Still too many threads? Use fewer threads than blocks. Which order of block execution? Which mapping of blocks onto threads?

CS 284a, 18 November 1997 Copyright (c) , John Thornley13 Rules for Using Flags: Did John Lie to Us? Want multithreaded  sequential. Multithreaded block {... S;... T;... } or multithreaded for loop with iterations... S;... T;... For each variable x such that either: –S writes x and T reads x, –S reads x and T writes x, or –S writes x and T writes x. Require flag x_ready such that: –x_ready is set after operation on x in S. –x_ready is checked before operation on x in T. Flag x_ready maintains order of operations on x.

CS 284a, 18 November 1997 Copyright (c) , John Thornley14 Question What are the rules for using counters to “guard” shared variables?