Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley1 CS 284a Lecture Tuesday, 11 November 1997

2 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley2 Independent Concurrency Multithreaded blocks/loops can only express a limited form of data-dependence: independent statements/iterations.

3 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley3 Independent Concurrency is Not Enough { a = f(x); b = f(y); c = g(a); d = h(a, b); } sequential code data dependencies a = f(x);b = f(y); c = g(a);d = h(a, b); Cannot express the full concurrency inherent in these data-dependencies using only multithreaded blocks/loops.

4 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley4 Independent Concurrency is Not Enough { produce(&x, &y); consume(x); consume(y); } sequential code Cannot express the concurrent overlap of a producer and consumers using only multithreaded blocks/loops.

5 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley5 Independent Concurrency is Not Enough { produce(&stream); consume(&stream); } sequential code consumer producer Cannot express the concurrent overlap of a producer and consumer of a dynamic data structure using only multithreaded blocks/loops. rear front

6 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley6 Additional Synchronization Multithreaded blocks/loops go a long way. But they cannot express all concurrency patterns. Many synchronization constructs are known: locks, semaphores, monitors, etc. But these do not preserve the (deterministic) sequential interpretation of the program. We introduce flags and counters. Flags and counters can be used so that: multithreaded  sequential.

7 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley7 Synchronization Flag Operations typedef... sthread_flag; int sthread_flag_initialize(sthread_flag *flag); /* Initialize before any other operation on flag. */ /* Flag status is initialized to “not set”. */ int sthread_flag_finalize(sthread_flag *flag); /* Finalize after all other operations on flag. */ int sthread_flag_set(sthread_flag *flag); /* Return STHREAD_ERROR_FLAGSET if flag status is “set”. */ /* Otherwise, change flag status to “set”. */ int sthread_flag_check(sthread_flag *flag); /* Suspend until flag status is “set”. */ int sthread_flag_reset(sthread_flag *flag); /* Set flag status to “not set”. */ Return code is error value ( STHREAD_ERROR_NONE for no error detected).

8 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley8 Example of Flags sthread_flag a_ready; sthread_flag_initialize(&a_ready); #pragma multithreadable { { a = f(x); sthread_flag_set(&a_ready); c = g(a); } { b = f(y); sthread_flag_check(&a_ready); d = h(a, b); } sthread_flag_finalize(&a_ready); data dependencies a = f(x);b = f(y); c = g(a);d = h(a, b);

9 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley9 Rules for Using Flags to “Guard” Shared Variables 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.

10 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley10 Examples of Shared Variables That Need Flags #pragma multithreadable { {... x = a;... }... {... b = x;... } } #pragma multithreadable { {... a = x;... }... {... x = b;... } } #pragma multithreadable { {... x = a;... }... {... x = b;... } }

11 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley11 Shared Variables Made Safe Using Flags #pragma multithreadable { {... x = a; sthread_flag_set(&x_ready);... }... {... sthread_flag_check(&x_ready); b = x;... } } #pragma multithreadable { {... a = x; sthread_flag_set(&x_ready);... }... {... sthread_flag_check(&x_ready); x = b;... } } #pragma multithreadable { {... x = a; sthread_flag_set(&x_ready);... }... {... sthread_flag_check(&x_ready); x = b;... } }

12 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley12 Example: Simple Producer-Consumer sthread_flag x_ready, y_ready; sthread_flag_initialize(&x_ready); sthread_flag_initialize(&y_ready); #pragma multithreadable { produce(&x, &x_ready, &y, &y_ready); { sthread_flag_check(&x_ready); consume(x); } { sthread_flag_check(&y_ready); consume(y); } } sthread_flag_finalize(&x_ready); sthread_flag_finalize(&y_ready);

13 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley13 Example: Synchronized Stream typedef struct node *link; typedef struct node { item data; link next; sthread_flag next_ready; } node; typedef struct { link rear; /* write to the rear */ link front; /* read from the front */ } stream; consumerproducer rear front

14 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley14 Operations on a Synchronized Stream void put(stream *s, item data) { link new_rear; new_rear = (link) malloc(sizeof(node)); sthread_flag_initialize(new_rear->next_ready); new_rear->data = data; s->rear->next = new_rear; sthread_flag_set(&s->rear->next_ready); s->rear = new_rear; } void get(stream *s, item *data) { link new_front; sthread_flag_check(&s->front->next_ready); sthread_flag_finalize(&s->front->next_ready); new_front = s->front->next; free(s->front); s->front = new_front; *data = new_front->data; }

15 CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley15 When To Reset a Flag Never reset a flag in your program DESIGN. Reset and reuse a flag in your IMPLEMENTATION to avoid repeated flag initialization and finalization. for (i = 0; i < n; i++) { sthread_flag a_ready; sthread_flag_initialize(&a_ready); #pragma multithreadable { { a = f(x); sthread_flag_set(&a_ready); c = g(a); } { b = f(y); sthread_flag_check(&a_ready); d = h(a, b); } } sthread_flag_finalize(&a_ready); }


Download ppt "CS 284a, 11 November 97 Copyright (c) 1997-98, John Thornley1 CS 284a Lecture Tuesday, 11 November 1997."

Similar presentations


Ads by Google