• Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Book Chapter 8

  • 230 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
230
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
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. 第 11 章 並行アーキテクチャ
  • 2. 並行アーキテクチャ 概念 : アーキテクチャ : 並行システムにおけるプロセスが作る構造 モデル : Filter Pipeline Supervisor Worker Announcer-Listener 実践 :
  • 3. ソフトウェア・アーキテクチャとは
    • アーキテクチャとは
      • 建築物,建築学
      • 基本設計思想
    • コンピュータ関連
      • コンピュータ・アーキテクチャ
        • 狭義には命令セット
      • ネットワーク・アーキテクチャ
        • ISO/OSI の階層モデル
  • 4. アーキテクチャの役割
    • システム全体の構造を定めるもの
      • 部品合成としてのシステム組織
      • 大域的制御構造
      • 通信プロトコル
      • データベースとデータアクセス
      • 物理的分散配置
      • 処理規模と性能
      • 将来の発展方向の見通し
  • 5. ソフトウェア・アーキテクチャとは ( 続き )
    • 計算部品とそれらの相互作用という形でシステムを記述するもの
    • システム要求と構築されるシステムの要素との対応をつけ,設計判断に根拠を与える.
    • システム・レベルの性質として,容量,スループット,整合性,部品の適合性などを考慮.
    • 設計は,合成の規則と振舞いの規則を定める.
  • 6. 建築様式 (Architecture Style)
    • スタイル ( 様式 ) の例
      • データフロー
        • バッチ列
        • パイプとフィルタ
      • 呼び出し・復帰システム
        • メインとサブルーチン
        • OO システム
        • 層別
  • 7. パイプとフィルター アーキテクチャ 層別アーキテクチャ 黒板アーキテクチャ
  • 8. 建築様式(2)
      • 独立部品
        • 通信プロセス
        • 事象システム
      • バーチャルマシン
        • インタープリタ
        • ルールベース・システム
      • データ中心システム
        • データベース
        • ハイパーテキスト
        • 黒板システム
  • 9. アーキテクチャ記述言語 (ADL)
    • アーキテクチャの構造を記述するための言語
      • 構成要素:
        • 部品 (component)
        • 結合子 (connector)
    • ADL の例
      • Wright (CMU)
      • Darwin (Imperial College)
      • RAPIDE (Stanford U.)
      • GEN-VOCA (U. Texas)
  • 10. アーキテクチャに関連する概念
    • フレームワーク
      • 特定の領域向けにアーキテクチャを具体化した骨組み
        • 例:金融商品販売管理システム用フレームワーク
        • Web アプリケーション用フレームワーク
          • Struts, Ruby on Rails, JBoss
      • 特殊化すべき部分して実装
        • 「高温部 (hotspot) 」のみ継承などで追加
  • 11. アーキテクチャに関連する概念 (2)
    • 設計パターン
      • 設計上繰り返し現れる有用なパターン
      • アーキテクチャより小さい単位
      • 建築家 C. Alexander の「パターン言語」の影響
      • 例 : G4 による 23 の設計パターン
        • Observer, Mediator, Visitor, Proxy など
  • 12. 1 1 .1 フィルタのパイプライン フィルタ: ここでは 1 入力ストリームを 1 出力ストリームに変換 するプロセス(一般には多入力,多出力) パイプライン: フィルタをつなげて作る構造
  • 13. エラトステネスのふるいによる素数生成 234567 ... n => 23*5*7 ... n Figure 11.1 Primes Sieve process architecture.
  • 14. モデル 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. 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. 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. 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. 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. 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. 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. Primes Sieve Implementation
  • 22. Primes class diagram
  • 23. 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. 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. 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. 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. Why Use Buffering? 効率のため thread context switching を減らす
  • 28. 11.2 Supervisor-Worker
  • 29. Linda Tuple Space Data tuple in a tuple space has the form: (&quot;tag&quot;, value 1 ,...,value n ) Basic operations: out(&quot;tag&quot;, expr l ,...,expr n ) //input the matched tuple and remove it in(&quot;tag&quot;, field 1 ,...,field n ) //same as in except not removing rd(&quot;tag&quot;, field 1 ,...,field n ) inp, rdp //non-blocking version of in and rd, //return true when matching is found
  • 30. 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. LTS for TUPLE value any with N=2
  • 32. 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. Supervisor-Worker Model 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:: forall tasks: out(&quot;task&quot;,...) forall results: in(&quot;result&quot;,...) out(&quot;stop&quot;) Worker:: while not rdp(&quot;stop&quot;) do in(&quot;task&quot;,...) compute result out(&quot;result&quot;,...) const N = 2 set Tuples = {task,result,stop} set TupleAlpha ={{in,out,rd,rdp[Bool],inp[Bool]}.Tuples}
  • 34. Supervisor-Worker Model 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 = 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. Supervisor-Worker Model 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;} } } ATEND = (end->ENDED), ENDED = (ended->ENDED). ||SUPERVISOR_WORKER = (supervisor:SUPERVISOR || {redWork,blueWork}:WORKER || {supervisor,redWork,blueWork}::TUPLESPACE || ATEND )/{end/{supervisor,redWork,blueWork}.end}.
  • 36. 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. Revised Algorithms Supervisor:: forall tasks: out(&quot;task&quot;,...) forall results: in(&quot;result&quot;,...) out(&quot;task&quot;,stop) Worker:: while true do in(&quot;task&quot;,...) if value is stop then out(&quot;task&quot;,stop); exit compute result out(&quot;result&quot;,...)
  • 38. 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. Supervisor-Worker Implementation
  • 40. 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. 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. Announcer-Listener
  • 43. 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. LTS for a:REGISTER
  • 45. 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. Analysis property SAFE = (register[p:Pattern] -> SAFE[p]), SAFE[p:Pattern] = (event[p] -> SAFE[p] |deregister -> SAFE ). progress ANNOUNCE[p:Pattern] = {announce[p]}
  • 47. Announcer - Listener Implementation
  • 48. 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. レポート
    • 3 章から 10 章にある課題を解くこと
    • 少なくとも 3 題を解いて,それをレポートとして提出する.
      • 締切: 2 月 5 日 ( 金) 17:00
      • 提出方法: 玉井に直接提出するか学内便で送付 1 階事務の長森さんに出してもよい