Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 284a, 21 October 1997 Copyright (c) 1997-98, John Thornley1 CS 284a Lecture Tuesday, 21 October, 1997."— Presentation transcript:

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

2 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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

3 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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; } }

4 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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; } }

5 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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.

6 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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).

7 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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; } }

8 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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.

9 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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.

10 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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

11 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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).

12 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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

13 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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.

14 CS 284a, 21 October 1997 Copyright (c) 1997-98, 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


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

Similar presentations


Ads by Google