3 12 1 Flynn Flynn SISDSingleinstruction Singledata SIMDSingleinstruction

  • Slides: 45
Download presentation

3 12. 1 병렬 처리 소개 • 컴퓨터 시스템을 분류 - Flynn의 분류 –

3 12. 1 병렬 처리 소개 • 컴퓨터 시스템을 분류 - Flynn의 분류 – 스트림 • 하나의 프로세서에 의하여 순서대로 처리되는 일련의 명령어 들과 데이터들의 흐름 – Flynn에 의한 네 가지 분류 • • SISD(Single-instruction Single-data) SIMD(Single-instruction Multiple-data) MISD(Multiple-instruction Single-data) MIMD(Multiple-instruction Multiple-data)

10 12. 2 병렬 처리와 프로그래밍 언어 • 대표적 예제 – 유한 버퍼 문제(the

10 12. 2 병렬 처리와 프로그래밍 언어 • 대표적 예제 – 유한 버퍼 문제(the bounded buffer problem) – 병렬 행렬 곱셈(parallel matrix multiplication) • 정수형 행렬 선언 VAR a, b, c:ARRAY[n, n] OF INTEGER FOR i : = 1 TO n DO FOR j : = 1 TO n DO c[i, j] : = 0; FOR k : = 1 TO n DO c[i, j] : = c[i, j] + a[i, k] * b[k, j]; END;

12 12. 2 병렬 처리와 프로그래밍 언어 표 12. 1 병렬 처리를 제공하는 라이브러리

12 12. 2 병렬 처리와 프로그래밍 언어 표 12. 1 병렬 처리를 제공하는 라이브러리 사용의 예 #include <parallel/paralle. h> #define SIZE 100 #define NUMPROCS 10 shared int a[SIZE], b[SIZE], c[SIZE]; void main(void) { int err; int multiply(); m_set_procs(NUMPROCS); m_fork(multiply); m_kill_procs(); } void multiply(void) { int i, j, k; for(i = m_get_myid(); i < SIZE; i += NUMPROCS) for(j = 0; j < SIZE; ++j) for(k = 0; k < SIZE; ++k) c[i][j] += a[i][k] * b[k][j]; }

17 12. 2 병렬 처리와 프로그래밍 언어 • 명령어 수준의 병렬성 parbegin S 1;

17 12. 2 병렬 처리와 프로그래밍 언어 • 명령어 수준의 병렬성 parbegin S 1; S 2;. . . Sn; parend for i : = 1 to n do parallel begin for j : = 1 to n do begin forc[i, j] j : =: =10; to n do begin c[i, j] : = 10; for k : = to n do begin for k : = 1 to n do begin c[i, j] : = c[i, j] + a[i, k] * b[k, j]; end; end; - 명령어 수준의 병렬성은 VHDL과 같은 하드웨어 기술 언어

20 12. 2 병렬 처리와 프로그래밍 언어 표 12. 2 Fork 구조를 나타내는 C코드

20 12. 2 병렬 처리와 프로그래밍 언어 표 12. 2 Fork 구조를 나타내는 C코드 예 #define SIZE 100 #define NUMPROCS 10 int a[SIZE], b[SIZE], c[SIZE]; void main(void) { int myid; for (myid = 0; myid < NUMPROC; ++myid ) if(fork( ) == 0) { multiply(myid); exit(0); } for (myid = 0; myid < NUMPROCS; ++myid) wait(0); /* code to output c goes here */ } void multiply(int myid) { int i, j, k; for( i=myid; i < SIZE; i += NUMPROCS) for ( j=0; j < SIZE; ++j) { c[i][j] = 0; for(k = 0; k<SIZE; ++k) c[i][j] += a[i][k] * b[k][j]; } }

22 12. 3 세마포어 • Process – Terminate (정해진 시간 안에 임계구역 실행 끝냄)

22 12. 3 세마포어 • Process – Terminate (정해진 시간 안에 임계구역 실행 끝냄) – Fair scheduling ( 한정된 시간 안에 임계구역 들어감) • 임계구역 (critical region) – mutual exclusion 요구 • 세마포어 (semaphor) - Dijkstra 개발, Algol 68 – Binary valued variable : s – 두 연산 wait(s), signal(s) • Queue, fair scheduling 요구 wait(s) : if s = 1 then s : = 0 else 프로세스 P를 큐에 저장 signal(s) : if queue empty then 대기중인 프로세스 준비 else s: = 1

23 12. 3 세마포어 표 12. 3 Mutual Exclusion Using a Binary Semaphore procedure

23 12. 3 세마포어 표 12. 3 Mutual Exclusion Using a Binary Semaphore procedure READER begin. . . wait(mutex) DB에서 자료 읽음 signal(mutex). . . end procedure WRITER begin. . . wait(mutex) DB에 기록 signal(mutex). . . end

25 12. 3 세마포어 • Counting semaphore : ALGOL 68 (sema) – 선언문 :

25 12. 3 세마포어 • Counting semaphore : ALGOL 68 (sema) – 선언문 : sema mutex Down mutex : if mutex = 0 then 실행이 블록됨 else mutex : = mutex – 1 Up mutex : = mutex + 1, mutex 때문에 블록된 프로그램 을 실행 제개함 • Collateral 문 – ( , ) 로 분리, begin – end 또는 ( ) 로 쌓임 begin BEGIN S 1 , S 2 , … , Sn END S 1 S 2 … Sn end 그림 12. 5 Algol 68의 collateral statement

26 12. 3 세마포어 • 병렬절(parallel clause) 표 12. 5 Algol 68에서 해결한 생산자-소비자

26 12. 3 세마포어 • 병렬절(parallel clause) 표 12. 5 Algol 68에서 해결한 생산자-소비자 문제 Begin int buffer[50] ; int I : = 0, j : = 0; sema numberin : = level 0, openspots : = level 50 ; par (do down openspots ; I : = I mod 50 + 1 read(buffer[I]) ; up numberin ; od do down numberin ; j : = j mod 50 + 1; print(buffer[j]) ; up openspots ; od) end

28 12. 4 모니터 • 어느 주어진 시간에는 한 프로세스만이 모니터 프로시저를 실행시 킴

28 12. 4 모니터 • 어느 주어진 시간에는 한 프로세스만이 모니터 프로시저를 실행시 킴 • Queue 구성 자동 • Wait – signal • Concurrent Pascal (Brinch Hansen) Modula (Wirth) Mesa (Xerox) CSP/K (Holt) – Concurrent SP/K

29 12. 4 모니터 • CSP/K – 선언문 name : procedure options (concurrent) •

29 12. 4 모니터 • CSP/K – 선언문 name : procedure options (concurrent) • Monitor 선언 (monitor안에 entry선언) • Process선언

30 12. 4 모니터 • 표 12. 6 CSP/K 예 EXAMPLE: PROCEDURE OPTIONS(CONCURRENT) CRITICAL:

30 12. 4 모니터 • 표 12. 6 CSP/K 예 EXAMPLE: PROCEDURE OPTIONS(CONCURRENT) CRITICAL: MONITOR DECLARE (SUM) BINARY FIXED; DO; SUM=0; END; COUNT: ENTRY; SUM=SUM+1; END; WATCH: ENTRY; PUT SKIP LIST(SUM); SUM=0; END CRITICAL; COUNTER: PROCESS; . . . CALL COUNT; . . . END COUNTER; WATCHER: PROCESS; . . . CALL WATCH; . . . END WATCHER; END EXAMPLE;

34 12. 4 모니터 표 12. 7 Concurrent-Pascal의 생산자-소비자 문제 type buffer = array

34 12. 4 모니터 표 12. 7 Concurrent-Pascal의 생산자-소비자 문제 type buffer = array (1. . 80) of char, producer = process var card : buffer begin card(1) : = ‘N’ while card(1) <> ‘E’ do begin read(card); spool(card) end; consumer = process var line : buffer begin line : = ‘N’ while line <> ‘E’ do begin unspool(line); write(line) end;

35 12. 4 모니터 cbuffer = monitor var pool : array(1. . 5) of

35 12. 4 모니터 cbuffer = monitor var pool : array(1. . 5) of buffer front, read, nobuf, noful : interger procedure entry spool(contents : buffer) begin if noful = nobuf then delay; pool(rear) : = contents rear : = rear mod nobuf +1; noful : = noful +1; continue end; procedure entry unspool(contents : buffer) begin if noful = 0 then delay; contents : = pool(front); front : = front mod nobuf +1; noful : = noful – 1; continue end; begin front : = 1; rear : = 1; nobuf : = 5; oful : = 0 end var x: producer, y : consumer, z : cbuffer begin init x, y, z end.

43 임계 구역 (critical section) 용어 국제 표준 규격 15. 04. 02 critical section

43 임계 구역 (critical section) 용어 국제 표준 규격 15. 04. 02 critical section 임계 구역 A portion of a task during the execution of which other parts of this or other tasks are prohibited from execution. 어떤 태스크의 일부분으로 이 부분을 실행 시에는 이 태스크의 다른 부분이나 다른 태스크들의 실행이 금지된다.

44 세마포어 (semaphore) 용어 국제 표준 규격 15. 07. 06 semaphore 세마포어 A data

44 세마포어 (semaphore) 용어 국제 표준 규격 15. 07. 06 semaphore 세마포어 A data structure for controlling, by means of a queue, access to the resources that are available to more than one task, but only to one task at a time. 두 개 이상의 태스크가 사용 가능한 자원에 접근을 큐로써 제어하기 위한 자료 구조이다. 그러나 어느 한 시간에 오직 하나의 태스크만이 사용 가능하다.

45 모니터 (monitor) 용어 국제 표준 규격 15. 07 monitor (in programming language) 모니터(프로그래밍

45 모니터 (monitor) 용어 국제 표준 규격 15. 07 monitor (in programming language) 모니터(프로그래밍 언어에서) A shared data object together with a set of operations that may manipulate the data object in order to control requests for resources or access to the resources that are available to parallel processes, but only to one process at a time. 자원 요청을 제어하기 위하여, 또는 병렬 프로세스들이 한번에 한 개씩 이용할 수 있게 자원에 접근하도록, 자료 객체를 다룰 수 있는 연산들의 집합을 가진 공유 자료 객체.