1 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ CORRECTNESS ¡CRITERIA ¡FOR ¡ CONCURRENCY ¡& ¡ PARALLELISM ¡
2 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Contracts ¡– ¡Correctness ¡for ¡SequenGal ¡Code ¡
3 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Contracts ¡– ¡Correctness ¡for ¡SequenGal ¡Code ¡ • AsserGons ¡ • A ¡predicate ¡expected ¡to ¡hold ¡at ¡a ¡parGcular ¡program ¡point ¡ • PrecondiGon ¡ • A ¡predicate ¡expected ¡to ¡hold ¡at ¡a ¡funcGon ¡call ¡ • A ¡failure ¡can ¡be ¡blamed ¡on ¡the ¡caller ¡ • PostcondiGon ¡ • A ¡predicate ¡expected ¡to ¡hold ¡at ¡a ¡funcGon ¡return ¡ • A ¡failure ¡can ¡be ¡blamed ¡on ¡the ¡callee ¡
4 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Code ¡Contracts ¡for ¡.Net ¡ ¡ ¡int ¡Divide( int ¡n, ¡ int ¡d) ¡{ ¡ ¡ ¡ ¡ ¡ ¡return ¡n/d; ¡ ¡ ¡} ¡
5 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ PrecondiGons ¡using ¡Requires ¡ ¡ ¡int ¡Divide( int ¡n, ¡ int ¡d) ¡{ ¡ ¡ ¡ ¡ ¡Contract.Requires( ¡0 ¡!= ¡d ¡); ¡ ¡ ¡ ¡ ¡return ¡n/d; ¡ ¡ ¡} ¡
6 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ PrecondiGons ¡using ¡Ensures ¡ ¡ ¡int ¡Divide( int ¡n, ¡ int ¡d) ¡{ ¡ ¡ ¡ ¡ ¡Contract.Requires( ¡0 ¡!= ¡d ¡); ¡ ¡ ¡ ¡ ¡Contract.Ensures( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Contract.Result<int>() ¡* ¡d ¡<= ¡n ¡&& ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Contract.Result<int>() ¡* ¡d ¡> ¡n-‑d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡); ¡ ¡ ¡ ¡ ¡return ¡n/d; ¡ ¡ ¡} ¡
7 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Example: ¡Library ¡APIs ¡
8 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Example: ¡System ¡Call ¡API ¡
9 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Correctness ¡for ¡Concurrency ¡& ¡Parallelism ¡ • Reuse ¡contracts ¡wriQen ¡for ¡sequenGal ¡code ¡ • Relate ¡correctness ¡of ¡concurrent/parallel ¡execuGons ¡ to ¡correctness ¡of ¡appropriate ¡sequenGal ¡execuGons ¡
10 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Coin ¡SorGng ¡Example ¡ Vending ¡ Machine ¡ Sort ¡ Coins ¡ reject ¡ accept ¡
11 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Use ¡Contracts ¡for ¡Correctness ¡Criteria ¡ • SortCoins ¡accepts ¡a ¡set ¡of ¡coins ¡and ¡returns ¡a ¡set ¡of ¡bad ¡ones ¡ • Parallelizing ¡SortCoins ¡should ¡not ¡change ¡the ¡contract ¡ SortCoins(…) ¡{ ¡ SortCoins(…) ¡{ ¡ ¡ ¡ ¡ ¡Contract.Requires(…); ¡ ¡ ¡ ¡ ¡Contract.Requires(…); ¡ ¡ ¡ ¡ ¡Contract.Ensures(…); ¡ ¡ ¡ ¡ ¡Contract.Ensures(…); ¡ SequenGal ¡ Parallel ¡ ImplementaGon ¡ ImplementaGon ¡ } ¡ } ¡
12 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Parallelizing ¡can ¡someGmes ¡produce ¡correct ¡ but ¡different ¡outputs ¡ SequenIal ¡ Parallel ¡ SortCoins ¡ SortCoins ¡ reject ¡ reject ¡ accept ¡ accept ¡ The ¡order ¡of ¡coins ¡can ¡change ¡
13 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ RestricGve ¡Contracts ¡Can ¡Limit ¡Parallelism ¡ • The ¡order ¡of ¡coins ¡returned ¡by ¡SortCoins ¡might ¡be ¡ different ¡from ¡the ¡input ¡order ¡ • Do ¡care ¡about ¡the ¡total ¡amount ¡returned ¡ • If ¡the ¡contracts ¡enforce ¡the ¡ordering, ¡resulGng ¡ parallelizaGon ¡might ¡be ¡unacceptably ¡slow ¡ • Design ¡interfaces ¡in ¡such ¡a ¡way ¡that ¡contracts ¡are ¡not ¡ restricGve ¡
14 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Strategies ¡For ¡Reasoning ¡About ¡Parallel ¡Code ¡ • How ¡do ¡we ¡know ¡if ¡a ¡parallel ¡loop ¡saGsfies ¡its ¡ contract? ¡ ¡ • Reasoning ¡about ¡parallel ¡execuGons ¡is ¡hard ¡ General ¡Strategy: ¡ 1. Make ¡sure ¡that ¡every ¡parallel ¡behavior ¡is ¡equal ¡to ¡ some ¡sequenGal ¡behavior ¡ 2. Convince ¡that ¡the ¡sequenGal ¡behavior ¡saGsfies ¡the ¡ contract ¡ ¡
15 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Independent ¡Loops ¡ • Let ¡m(1) ¡… ¡m(n) ¡be ¡the ¡loop ¡iteraGons ¡ • Two ¡iteraGons ¡m(j) ¡and ¡m(k) ¡(j ¡≠ ¡k) ¡are ¡ dependent ¡ if ¡they ¡ access ¡the ¡same ¡memory ¡locaGon ¡and ¡at ¡least ¡one ¡of ¡ them ¡is ¡a ¡write. ¡ • Loop ¡iteraGons ¡are ¡ independent ¡ if ¡no ¡two ¡of ¡them ¡are ¡ dependent ¡
16 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Dependent ¡Loops ¡ • Dependencies ¡need ¡to ¡be ¡made ¡explicit ¡ • No ¡data ¡races ¡ • Reason ¡that ¡order ¡of ¡dependent ¡operaGons ¡don’t ¡ maQer ¡ • e.g. ¡These ¡operaGons ¡are ¡commutaGve ¡and ¡associaGve ¡ • Recall: ¡reduce/scan ¡
17 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Determinism: ¡ A ¡New ¡Correctness ¡Criteria ¡ • Pre ¡and ¡post ¡condiGons ¡do ¡two ¡things ¡ • Specify ¡how ¡the ¡funcGon ¡behaves ¡sequenGally ¡ • Enforce ¡the ¡same ¡behavior ¡ ¡when ¡parallelized ¡ int ComputeSum ( IEnumerable<int> input) { Contract.Requires ( input != null); Contract.Ensures ( Contract.Result<int>() == input.Sum(i => i) )); //implementation }
18 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Determinism ¡Contract ¡ • Allows ¡you ¡to ¡check ¡parallel ¡correctness ¡ • Without ¡having ¡to ¡specify ¡the ¡sequenGal ¡contract ¡ • The ¡output ¡of ¡the ¡funcGon ¡does ¡not ¡depend ¡on ¡task ¡ interleavings ¡for ¡a ¡given ¡input ¡ int ComputeSum ( IEnumerable<int> input) { Contract.Requires ( input != null); IsDeterministic ( Contract.Result<int>(), input ); //implementation }
19 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Determinism ¡Checking ¡ Contract.IsDeterministic ( output, {input1, input2, … }) Is ¡same ¡as ¡saying ¡ Contract.Ensures ( output == F ( input1, input2, … ) ) For ¡some ¡determinisGc ¡funcGon ¡F ¡ Very ¡useful ¡when ¡specifying ¡F ¡is ¡tedious ¡
20 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Determinism ¡Checking ¡ Contract.IsDeterministic ( output, {input1, input2, … }, comp) Is ¡same ¡as ¡saying ¡ Contract.Ensures ( comp(output, F ( input1, input2, … ) ) For ¡some ¡determinisGc ¡funcGon ¡F ¡
21 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Strategies ¡for ¡Checking ¡Determinism ¡
22 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Concurrent ¡Objects ¡ • Can ¡be ¡called ¡concurrently ¡by ¡many ¡threads ¡ • Examples ¡ • Work ¡Stealing ¡Queue ¡
23 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Concurrent ¡Objects ¡ • Can ¡be ¡called ¡concurrently ¡by ¡many ¡enGGes ¡ • Examples ¡ • Work ¡Stealing ¡Queue ¡ • C ¡RunGme ¡library ¡ • OperaGng ¡System ¡ ¡ • Data ¡bases ¡
24 ¡ 6/16/2010 ¡ Correctness ¡Criteria ¡for ¡Parallelism ¡& ¡Concurrency ¡ Correctness ¡Criteria ¡ • Informally ¡called ¡“thread ¡safety” ¡ • What ¡does ¡“thread ¡safety” ¡mean ¡to ¡you? ¡
Recommend
More recommend