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

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms The C Programming Language.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 1 Regression Verification for Multi-Threaded Programs Sagar Chaki, SEI-Pittsburgh Arie Gurfinkel, SEI-Pittsburgh Ofer Strichman, Technion-Haifa Originally.
11111 Functional Program Verification CS 4311 A. M. Stavely, Toward Zero Defect Programming, Addison-Wesley, Y. Cheon and M. Vela, A Tutorial on.
CS 284a, 11 November 97 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 11 November 1997.
CS Advanced C++ Exception Handling Topic #5.
CS 284a, 18 November 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 18 November, 1997.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Code Generation Mooly Sagiv html:// Chapter 4.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
CS 284a, 8 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 8 October, 1997.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
CS 284a Lecture Wednesday, 26 November, 1997
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 OpenMP -Example ICS 535 Design and Implementation.
CS 284a, 5 November 97Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 5 November 1997.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
CS 284a, 14 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 14 October, 1997.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
INTEL CONFIDENTIAL Confronting Race Conditions Introduction to Parallel Programming – Part 6.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Programming with Shared Memory Introduction to OpenMP
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
1 OpenMP Writing programs that use OpenMP. Using OpenMP to parallelize many serial for loops with only small changes to the source code. Task parallelism.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Compiler Construction
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
9-1 9 Variables and lifetime  Variables and storage  Simple vs composite variables  Lifetime: global, local, heap variables  Pointers  Commands 
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CS 284a, 29 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 29 October, 1997.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Fortran: Control Structures Session Three ICoCSIS.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
A Calculus of Atomic Actions Serdar Tasiran Koc University, Istanbul, Turkey Tayfun ElmasShaz Qadeer Koc University Microsoft Research.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Object Oriented Programming Lecture 2: BallWorld.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 3 of Programming Languages by Ravi Sethi
Introduction to OpenMP
Java Programming Language
Programming Languages and Paradigms
Introduction to OpenMP
Parallelizing Loops Moreno Marzolla
Programming Paradigms
Conditional Statements
Additional Control Structures
Structured Programming Taken from notes by Dr. Neil Moore
Homework Any Questions?.
Functional Program Verification
Introduction to OpenMP
ECE 103 Engineering Programming Chapter 18 Iteration
CS561 Computer Architecture Hye Yeon Kim
Corresponds with Chapter 5
Presentation transcript:

CS 284a, 21 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 21 October, 1997

CS 284a, 21 October 1997 Copyright (c) , John Thornley2 When Can I Use Pragma Multithreadable? #pragma multithreadable { x = n; y = y + 1; } #pragma multithreadable { a = b; c = a; } #pragma multithreadable { x = k; y = k; } #pragma multithreadable { { x = n; x = m; } { a = b; c = a; } } #pragma multithreadable { x = n; x = m; } Erroneous Sharing of VariablesPermissible Sharing of Variables

CS 284a, 21 October 1997 Copyright (c) , John Thornley3 When Can I Use Pragma Multithreadable? #pragma multithreadable { { acquire(lock); a = b; release(lock); } { acquire(lock); c = a; release(lock); } } Erroneous Sharing of Variables Permissible Sharing of Variables #pragma multithreadable { { a = b; set(flag); } { check(flag); c = a; } } #pragma multithreadable { { a = b; increment(count, 1); } { check(count, 1); c = a; } }

CS 284a, 21 October 1997 Copyright (c) , John Thornley4 When Can I Use Pragma Multithreadable? #pragma multithreadable { x = x + 1; if (i < j) goto finish; y = y + 1; } finish:... #pragma multithreadable { x = x + 1; if (i < j) goto incr_z; y = y + 1; incr_z: z = z + 1; } Erroneous Control FlowPermissible Control Flow #pragma multithreadable { { x = 1; loop: x = 2*x; if (x < n) goto loop; } for (i = 0; i < N; i++) { if (A[i] = target) break; } }

CS 284a, 21 October 1997 Copyright (c) , John Thornley5 Theorem Theorem:  Pre, Post, B : {Pre} B multi {Post}  {Pre} B seq {Post} and not bad_sharing(Pre, B seq ) and not bad_control_flow(Pre, B seq ). Where:B multi is the multithreaded interpretation of block B. B seq is the sequential interpretation of block B. Pre and Post are predicates on the program state. {Pre} B {Post} includes termination requirement. Note: Correctness of B multi defined solely in terms of B seq. bad_control_flow is actually a special kind of bad_sharing.

CS 284a, 21 October 1997 Copyright (c) , John Thornley6 Bad Sharing in Multithreadable Blocks B = {... S;... T;... } S and T both access (read/write) shared variable V. Permissible for S and T to both only read V. Erronenous for either (or both) S or T to write V... UNLESS: –Access to V in S is followed in S by set(f)/increment(c). –Access to V in T is preceded in T by check(f)/check(c).

CS 284a, 21 October 1997 Copyright (c) , John Thornley7 Example of Bad Sharing #pragma multithreadable { { a = b; set(aflag); } { x = k; y = k; } { check(aflag); c = a; } } Not Bad SharingBad Sharing #pragma multithreadable { { c = a; set(aflag); } { x = k; y = k; } { check(aflag); a = b; } } #pragma multithreadable { a = b; set(aflag); x = k; y = k; check(aflag); c = a; } #pragma multithreadable { { a = b; } { x = k; y = k; set(aflag); } { check(aflag); c = a; } }

CS 284a, 21 October 1997 Copyright (c) , John Thornley8 Bad Control Flow in Multithreadable Blocks B = {... S;... T;... } Erroneous for control jump between S and T. Erroneous for control jump from S or T out of B. OK for control jump within S or T. Examples of control jumps: goto, break, return, and continue statements.

CS 284a, 21 October 1997 Copyright (c) , John Thornley9 Proof Outline Consider B = { S; T; } (can trivially extend to more statements). B seq = S 1 S 2... S n T 1 T 2... T m (atomic actions). B multi  some interleaving of actions S 1 S 2... S n and T 1 T 2... T m. For example: B multi  T 1 S 1 S 2 T 2... S n... T m. Consider any “out-of-order” pair: B multi ... T i S j... Prove equivalence with actions swapped:... T i S j... ... S j T i... Consider action kinds: read(x), write(x), check(f), and set(f). All pairs can be swapped or imply error of some kind in B seq. Key: (partial) order of check and sets is same in B multi as B seq. Therefore: order of shared variable accesses is also preserved. By induction: B multi  S 1 S 2... S n T 1 T 2... T m.

CS 284a, 21 October 1997 Copyright (c) , John Thornley10 Multithreadable Regular for Loop (Pragma) #pragma multithreadable [chunk_size(c)] [mapping(m)] [num_threads(t)] [priority(p)] [stack_size(s)] for (i = initial; i |>= bound; i = i + step) statement c=number of iterations executed in each chunk (default = 1). m =mapping of statements onto threads (simple, dynamic, blocked, or interleaved) (default = simple). t =number of threads (default = number of statements). p =initial priority of threads (default = current priority of parent thread). s =stack size of threads (in bytes) (default = system-defined constant). optional arguments

CS 284a, 21 October 1997 Copyright (c) , John Thornley11 Multithreaded Regular for Loop (Sthreads) int sthread_regular_for_loop( void *chunk(int first, int last, int step, void *args), void *args, int initial, int condition, int bound, int step, int chunk_size, int mapping, int num_threads, int priority, int stack_size);  chunk:pointer to function that executes a chunk of loop iterations.  args:pointer to argument block passed to each chunk call.  initial:initial value of control variable  condition:condition tested between control variable and bound value  bound:bound value of control variable  step:step value of control variable  chunk_size:number of iterations executed in each chunk  mapping:mapping of statements onto threads.  num_threads:number of threads.  priority:priority of threads.  stack_size:stack size of threads (in bytes).

CS 284a, 21 October 1997 Copyright (c) , John Thornley12 A Simple Example: Array Summation void array_assignment(int n, int dest[], int source[], int c) { int i; #pragma multithreadable chunk_size(c) for (i = 0; i < n; i++) dest[i] = source[i]; } Constant variables: dest, source Modified variables: none

CS 284a, 21 October 1997 Copyright (c) , John Thornley13 Transformation: Pragma to Sthreads Define an “argument block” structure type: –One component for each constant variable. –One pointer component for each modified variable. Declare a function that executes a chunk of iterations: –Takes first, last, and step values and argument block. –Constant variable c replaced by (args->c). –Modified variable v replaced by (*args->v). Replace multithreadable block by block containing: –Declaration and creation of argument block variable. –Call to sthread_regular_for_loop() function. –Call to handle any error code returned.

CS 284a, 21 October 1997 Copyright (c) , John Thornley14 typedef struct { int *dest, *source; } loop_args; void chunk(int first, int last, int step, loop_args *args) { int i; for (i = first; i dest[i] = args->source[i]; } void array_assignment(int n, int dest[], int source[], int c) { loop_args args; int error_code; args.dest = dest; args.source = source; error_code = sthread_regular_for_loop( (void (*)(int, int, int, void *)) chunk, (void *) &args, 0, STHREAD_CONDITION_LT, n, 1, c, STHREAD_MAPPING_SIMPLE, 0, STHREAD_PRIORITY_PARENT, STHREAD_STACK_SIZE_DEFAULT); handle_sthread_error(error_code); } we know loop directionwe know step value