Your SlideShare is downloading. ×
0
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Book Chapter 8
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Book Chapter 8

239

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
239
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Concurrency: message passing 1 ©Magee/Kramer 第 11 章 並行アーキテクチャ
  • 2. Concurrency: message passing 2 ©Magee/Kramer 並行アーキテクチャ 概念 : アーキテクチャ: 並行システムにおけるプロセスが作る構造 モデル : Filter Pipeline Supervisor Worker Announcer-Listener 実践 :
  • 3. Concurrency: message passing 3 ©Magee/Kramer ソフトウェア・アーキテクチャとは アーキテクチャとは 建築物,建築学 基本設計思想 コンピュータ関連 コンピュータ・アーキテクチャ 狭義には命令セット ネットワーク・アーキテクチャ ISO/OSI の階層モデル 3
  • 4. Concurrency: message passing 4 ©Magee/Kramer アーキテクチャの役割 システム全体の構造を定めるもの 部品合成としてのシステム組織 大域的制御構造 通信プロトコル データベースとデータアクセス 物理的分散配置 処理規模と性能 将来の発展方向の見通し 4
  • 5. Concurrency: message passing 5 ©Magee/Kramer ソフトウェア・アーキテクチャとは ( 続き ) 計算部品とそれらの相互作用という形でシステムを 記述するもの システム要求と構築されるシステムの要素との対応 をつけ,設計判断に根拠を与える. システム・レベルの性質として,容量,スループッ ト,整合性,部品の適合性などを考慮. 設計は,合成の規則と振舞いの規則を定める. 5
  • 6. Concurrency: message passing 6 ©Magee/Kramer 建築様式 (Architecture Style) スタイル ( 様式 ) の例 データフロー バッチ列 パイプとフィルタ 呼び出し・復帰システム メインとサブルーチン OO システム 層別 6
  • 7. パイプとフィルター アーキテクチャ 層別アーキテクチャ 黒板アーキテクチャ 7
  • 8. Concurrency: message passing 8 ©Magee/Kramer 建築様式(2) 独立部品 通信プロセス 事象システム バーチャルマシン インタープリタ ルールベース・システム データ中心システム データベース ハイパーテキスト 黒板システム 8
  • 9. Concurrency: message passing 9 ©Magee/Kramer アーキテクチャ記述言語 (ADL) アーキテクチャの構造を記述するための言語 構成要素:  部品 (component)  結合子 (connector) ADL の例 Wright (CMU) Darwin (Imperial College) RAPIDE (Stanford U.) GEN-VOCA (U. Texas)
  • 10. Concurrency: message passing 10 ©Magee/Kramer アーキテクチャに関連する概念 フレームワーク 特定の領域向けにアーキテクチャを具体化した骨 組み 例:金融商品販売管理システム用フレームワーク Web アプリケーション用フレームワーク Struts, Ruby on Rails, JBoss 特殊化すべき部分して実装 「高温部 (hotspot) 」のみ継承などで追加 10
  • 11. Concurrency: message passing 11 ©Magee/Kramer アーキテクチャに関連する概念 (2) 設計パターン 設計上繰り返し現れる有用なパターン アーキテクチャより小さい単位 建築家 C. Alexander の「パターン言語」の影響 例 : G4 による 23 の設計パターン Observer, Mediator, Visitor, Proxy など 11
  • 12. Concurrency: message passing 12 ©Magee/Kramer 11.1 フィルタのパイプライン フィルタ: ここでは 1 入力ストリームを 1 出力スト リームに変換 するプロセス(一般には多入力,多出力) パイプライン: フィルタをつなげて作る構造
  • 13. Concurrency: message passing 13 ©Magee/Kramer エラトステネスのふるいによる素数生成 234567 ... n => 23*5*7 ... n Figure 11.1 Primes Sieve process architecture.
  • 14. Concurrency: message passing 14 ©Magee/Kramer モデル canst MAX = 9 range NUM =2..MAX set S ={[NUM],eos} PIPE = (put[x:S]->get[x]->PIPE). GEN = GEN[2], GEN[x:NUM] = (out.put[x] -> if x<MAX then GEN[x+1] else (out.put.eos -> end ->GEN) ). PIPE はアーキテクチャの要素としてはコンポーネントをつな ぐ結合子 (connector) の役割を果たす.
  • 15. Concurrency: message passing 15 ©Magee/Kramer Filter FILTER = (in.get[p:NUM]->prime[p]->FILTER[P] |in.get.eos->ENDFILTER ), FILTER[p:NUM] = (in.get[x:NUM] -> if x%p!=O then (out.put[x]->FILTER[p]) else FILTER[P] |in.get.eos->ENDFILTER ), ENDFILTER = (out.put.eos->end->FILTER).
  • 16. Concurrency: message passing 16 ©Magee/Kramer Primes ||PRIMES(N=4) = ( gen:GEN ||pipe[O..N-1]:PIPE ||filter[O..N-1]:FILTER )/{pipe[O]/gen.out, pipe[i:O..N-1]/filter[i].in, pipe[i:1..N-1]/filter[i-1].out, end/{filter[O..N-1].end,gen.end} }@{filter[O..N-1].prime,end}.
  • 17. Concurrency: message passing 17 ©Magee/Kramer Unbuffered Pipes ||PRIMESUNBUF(N=4) = (gen:GEN || filter[O..N-1]:FILTER) /{pipe[O]/gen.out.put, pipe[i:O..N-1]/filter[i].in.get, pipe[i:1..N-1]/filter[i-1].out.put, end/{filter[O..N-1].end,gen.end} }@{filter[O..N-1].prime,end}.
  • 18. Concurrency: message passing 18 ©Magee/Kramer Abstracting from Application Detail ||AGEN = GEN/{out.put/out.put[NUM)}. ||AFILTER = FILTER/{out.put/out.put[NUM], in.get/in.get.[NUM], prime/prime[NUM] }. ||APIPE = PIPE/{put/put[NUM],get/get[NUM]}.
  • 19. Concurrency: message passing 19 ©Magee/Kramer Multi-Slot Pipes ||MPIPE(B=4) = if B==1 then APIPE else (APIPE/{mid/get} || MPIPE(B-1)/{mid/put}) @{put,get}. ||APRIMES(N=4,B=3) = (gen:AGEN || PRIMEP(N) || pipe[O..N-1]:MPIPE(B) || filter[O..N-1]:AFILTER) /{pipe[O]/gen.out, pipe[i:O..N-1]/filter[i].in, pipe[i:1..N-1]/filter[i-1].out, end/{filter[O..N-1].end,gen.end} }.
  • 20. Concurrency: message passing 20 ©Magee/Kramer Architectural Property Analysis progress END = {end} property PRIMEP(N=4) = PRIMEP[0], PRIMEP[i:0..N] = (when (i<N) filter[i].prime->PRIMEP[i+1] |end -> PRIMEP ).
  • 21. Concurrency: message passing 21 ©Magee/Kramer Primes Sieve Implementation
  • 22. Concurrency: message passing 22 ©Magee/Kramer Primes class diagram
  • 23. Concurrency: message passing 23 ©Magee/Kramer Java による実装 class PrimesCanvas extends Canvas { // display val in an upper box numbered index // boxes are numbered from the left synchronized void print(int index, int val){...} // display val in a lower box numbered index // the lower box indexed by 0 is not displayed synchronized void prime(int index, int val){...} // clear all boxes synchronized void clear{) {...} }
  • 24. Concurrency: message passing 24 ©Magee/Kramer Java による実装 class Generator extends Thread { private PrimesCanvas display; private Pipe<Integer> out; static int MAX = 50; Generator(Pipe<Integer> c, PrimesCanvas d) {out=c; display = d;} public void run() { try { for (int i=2;i<=MAX;++i) { display.print(0,i); out.put(i); sleep(500);} display.print(0,Primes.EOS); out.put(Primes.EOS); } catch (InterruptedException e){} }}
  • 25. Concurrency: message passing 25 ©Magee/Kramer Java による実装 class Filter extends Thread { private PrimesCanvas display; private Pipe<Integer> in,out; private int index; Filter(Pipe<Integer> i, Pipe<Integer> o, int id, PrimesCanvas d) {in = i; out = o; display = d; index = id;} public void run() { int i,p; try { p = in.get(); display.prime(index, p); if (p==Primes.EOS && out!=null) { out.put(p); return;} while(true) { i= in.get(); display.print(index,i); sleep(1000); if (i==Primes.EOS) { if (out!=null) out.put(i); break; } else if (i%p!=0 && out!=null) out.put(i);} } catch (InterruptedException e){} }}
  • 26. Concurrency: message passing 26 ©Magee/Kramer Java による実装 public interface Pipe<T> { public void put(T o) throws InterruptedException; public T get() throws InterruptedException } // Buffered pipe implementation public class PipeImplBuf<T> implements Pipe<T> { Buffer<T> buf = new BufferImpl<T>(10); public void put(T o) throws InterruptedException { buf.put(o);} public T get() throws InterruptedException { return buf.get();} }
  • 27. Concurrency: message passing 27 ©Magee/Kramer Why Use Buffering? 効率のため thread context switching を減らす
  • 28. Concurrency: message passing 28 ©Magee/Kramer 11.2 Supervisor-Worker
  • 29. Concurrency: message passing 29 ©Magee/Kramer Linda Tuple Space Data tuple in a tuple space has the form: ("tag", value1,...,valuen) Basic operations: out("tag", exprl,...,exprn) //input the matched tuple and remove it in("tag", field1,...,fieldn) //same as in except not removing rd("tag", field1,...,fieldn) inp, rdp //non-blocking version of in and rd, //return true when matching is found
  • 30. Concurrency: message passing 30 ©Magee/Kramer Tuple Space Model const N = ... set Tuples = {...} const False = 0 const True = 1 range Bool = False..True TUPLE(T=‘any) = TUPLE[0], TUPLE[i:0..N] = (out[T] -> TUPLE[i+1] |when (i>0) in[T] -> TUPLE[i-1] |when (i>0) inp[True][T] -> TUPLE[i-1] |when (i==0) inp[False][T] -> TUPLE[i] |when (i>0) rd[T] -> TUPLE[i] |rdp[i>0][T] -> TUPLE[i] ). ||TUPLESPACE = forall [t:Tuples] TUPLE(t).
  • 31. Concurrency: message passing 31 ©Magee/Kramer LTS for TUPLE value any with N=2
  • 32. Concurrency: message passing 32 ©Magee/Kramer Tuple Space Implementation public interface TupleSpace { // deposits data in tuple space public void out(String tag, Object data); // extracts object with tag from tuple space, // blocks if not available public Object in(String tag) throws InterruptedException; // reads object with tag from tuple space, // blocks if not available public Object rd(String tag) throws InterruptedException; // extracts object if available, // return null if not available public Object inp(String tag); //reads object if available, return null if not available public Object rdp(String tag); }
  • 33. Concurrency: message passing 33 ©Magee/Kramer public class Entry extends Port { private CallMsg cm; public Object call(Object req) throws InterruptedException { Channel clientChan = new Channel(); send(new CallMsg(req,clientChan)); return clientChan.receive(); } public Object accept()throws InterruptedException { cm = (CallMsg) receive(); return cm.request; } public void reply(Object res) throws InterruptedException { cm.replychan.send(res); } private class CallMsg { Object request; Channel replychan; CallMsg(Object m, Channel c) {request=m; replychan=c;} } } Supervisor-Worker Model アルゴリズムのスケッチ Supervisor:: forall tasks: out("task",...) forall results: in("result",...) out("stop") Worker:: while not rdp("stop") do in("task",...) compute result out("result",...) const N = 2 set Tuples = {task,result,stop} set TupleAlpha ={{in,out,rd,rdp[Bool],inp[Bool]}.Tuples}
  • 34. Concurrency: message passing 34 ©Magee/Kramer public class Entry extends Port { private CallMsg cm; public Object call(Object req) throws InterruptedException { Channel clientChan = new Channel(); send(new CallMsg(req,clientChan)); return clientChan.receive(); } public Object accept()throws InterruptedException { cm = (CallMsg) receive(); return cm.request; } public void reply(Object res) throws InterruptedException { cm.replychan.send(res); } private class CallMsg { Object request; Channel replychan; CallMsg(Object m, Channel c) {request=m; replychan=c;} } } Supervisor-Worker Model SUPERVISOR = TASK[1], TASK[i:1..N] = (out.task -> if i<N then TASK[i+1] else RESULT[1]), RESULT[i:1..N] = (in.result -> if i<N then RESULT[i+1] else FINISH), FINISH = (out.stop -> end -> STOP) + TupleAlpha. WORKER = (rdp[b:Bool].stop-> if (!b) then (in.task -> out.result -> WORKER) else (end -> STOP) )+TupleAlpha.
  • 35. Concurrency: message passing 35 ©Magee/Kramer public class Entry extends Port { private CallMsg cm; public Object call(Object req) throws InterruptedException { Channel clientChan = new Channel(); send(new CallMsg(req,clientChan)); return clientChan.receive(); } public Object accept()throws InterruptedException { cm = (CallMsg) receive(); return cm.request; } public void reply(Object res) throws InterruptedException { cm.replychan.send(res); } private class CallMsg { Object request; Channel replychan; CallMsg(Object m, Channel c) {request=m; replychan=c;} } } Supervisor-Worker Model ATEND = (end->ENDED), ENDED = (ended->ENDED). ||SUPERVISOR_WORKER = (supervisor:SUPERVISOR || {redWork,blueWork}:WORKER || {supervisor,redWork,blueWork}::TUPLESPACE || ATEND )/{end/{supervisor,redWork,blueWork}.end}.
  • 36. Concurrency: message passing 36 ©Magee/Kramer Analysis Safety analysis reveals the following deadlock: Trace to DEADLOCK: supervisor.out.task supervisor.out.task redWork.rdp.0.stop - rdp returns false redWork.in.task redWork.out.result supervisor.in.result redWork.rdp.0.stop - rdp returns false redWork.in.task redWork.out.result supervisor.in.result redWork.rdp.0.stop - rdp returns false supervisor.out.stop blueWork.rdp.1.stop - rdp returns true
  • 37. Concurrency: message passing 37 ©Magee/Kramer Revised Algorithms Supervisor:: forall tasks: out("task",...) forall results: in("result",...) out("task",stop) Worker:: while true do in("task",...) if value is stop then out("task",stop); exit compute result out("result",...)
  • 38. Concurrency: message passing 38 ©Magee/Kramer Revised Model set Tuples = {task,task.stop,result} SUPERVISOR = TASK[1], TASK[i:1..N] = (out.task -> if i<N then TASK[i+1] else RESULT[1]), RESULT[i:1..N] = (in.result -> if i<N then RESULT[i+1] else FINISH), FINISH = (out.task.stop -> end -> STOP) + TupleAlpha. WORKER = (in.task -> out.result -> WORKER |in.task.stop -> out.task.stop -> end ->STOP ) + TupleAlpha.
  • 39. Concurrency: message passing 39 ©Magee/Kramer Supervisor-Worker Implementation
  • 40. Concurrency: message passing 40 ©Magee/Kramer Program class SupervisorCanvas extends Canvas { // display rectangle slice i with color c, add a to area field synchronized void setSlice( int i,double a,Color c) {...} // reset display to clear rectangle slices and draw curve for f synchronized void reset (Function f) {...} } class WorkerCanvas extends Panel { // display current task number val synchronized void setTask(int val) {...); } interface Function { double fn(double x); }
  • 41. Concurrency: message passing 41 ©Magee/Kramer Speedup and Efficiency speedup: time of sequential program / time of parallel program efficiency: speedup / no. of processors e.g. 12 seconds by sequential program, 4 seconds by parallel program with 6 processors => speedup: 3, efficiency: 0.5 (50%) The integral example will not speed up, because tasks by workers take very small amount of time. The Supervisor-Worker architecture is successfully applied to the problem like image rendering using ray-tracing techniques.
  • 42. Concurrency: message passing 42 ©Magee/Kramer Announcer-Listener
  • 43. Concurrency: message passing 43 ©Magee/Kramer Announcer-Listener Model (Event Manager) set Listeners = {a,b,c,d} set Pattern = {pat1,pat2} REGISTER =IDLE, IDLE = (register[p:Pattern] -> MATCH[p] |announce[Pattern] -> IDLE ), MATCH[p:Pattern] = (announce[a:Pattern] -> if (a==p) then (event[a] -> MATCH[p] |deregister -> IDLE) else MATCH[p] |deregister -> IDLE). ||EVENTMANAGER = (Listeners:REGISTER) /{announce/Listeners.announce}.
  • 44. Concurrency: message passing 44 ©Magee/Kramer LTS for a:REGISTER
  • 45. Concurrency: message passing 45 ©Magee/Kramer Announcer-Listener Model (Announcer, Listener) ANNOUNCER = (announce[Pattern] -> ANNOUNCER). LISTENER(P='pattern) = (register[P] -> LISTENING), LISTENING = (compute -> LISTENING |event[P] -> LISTENING |event[P] -> deregister -> STOP )+{register[Pattern]}. ||ANNOUNCER_LISTENER = ( a:LISTENER('pat1) ||b:LISTENER('pat2) ||c:LISTENER('pat1) ||d:LISTENER('pat2) ||EVENTMANAGER ||ANNOUNCER ||Listeners:SAFE).
  • 46. Concurrency: message passing 46 ©Magee/Kramer Analysis property SAFE = (register[p:Pattern] -> SAFE[p]), SAFE[p:Pattern] = (event[p] -> SAFE[p] |deregister -> SAFE ). progress ANNOUNCE[p:Pattern] = {announce[p]}
  • 47. Concurrency: message passing 47 ©Magee/Kramer Announcer - Listener Implementation
  • 48. Concurrency: message passing 48 ©Magee/Kramer BoxCanvas class class BoxCanvas extends Canvas { // clear all boxes synchronized void reset() {...} // draw colored box id at position x,y synchronized void moveBox(int id, int x, int y) {...} // draw black box id at position x,y synchronized void blackBox(int id, int x, int y) {...} } class BoxMover extends Thread {... synchronized void isHit(int mx, int my) { hit = (mx>x && mx<x+display.BOXSIZE && my>y && my<y+display.BOXSIZE); if (hit) notify();} ... class MyListener extends MouseAdapter { public void mousePressed(MouseEvent e) { isHit(e.getX(),e.getY());}}
  • 49. Concurrency: message passing 49 ©Magee/Kramer レポート 3 章から 10 章にある課題を解くこと  少なくとも 3 題を解いて,それをレポートとして提 出する.  締切: 2 月 5 日 ( 金) 17:00  提出方法: 玉井に直接提出するか学内便で送付 1 階事務の長森さんに出してもよい

×