44
Parallel Programming & MPI 박필성 수원대학교 IT대학 컴퓨터학과

Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Programming& MPI

박 필 성

수원대학교 IT대학 컴퓨터학과

Page 2: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

목차

Why Parallel?

Parallel Computers

Parallel Processing

Parallel Programming

MPI의 기본 개념

MPI 표준

MPI 프로그램 및 실행

Related Topics & Questions

Page 3: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Why Parallel? (1)

Why parallel computing?

• 전통적인 compute-intensive applications기상 예보, 전산 유체역학, 화학, 천문학, 다양한 공학문제

• 새로운 data-intensive applicationsvideo servers, data mining

• 미래의 high performance applicationsVR, 협업 환경, CAD

• Save time and/or money.

• Solve large problems.

• Provide concurrency.

• Use of non-local resources

• Limits to serial computing

• http://en.wikipedia.org/wiki/Parallel_computer

• http://www.top500.org/

Page 4: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Why Parallel? (2) Atmosphere, Earth, Environment

Physics - applied, nuclear, particle, condensed matter, high pressure, fusion, photonics

Bioscience, Biotechnology, Genetics

Chemistry, Molecular Sciences

Geology, Seismology

Mechanical Engineering - from prosthetics to spacecraft

Electrical Engineering, Circuit Design, Microelectronics

Computer Science, Mathematics

Page 5: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Why Parallel? (3) Databases, data mining

Oil exploration

Web search engines, web based business services

Medical imaging and diagnosis

Pharmaceutical design

Management of national and multi-national corporations

Financial and economic modeling

Advanced graphics and virtual reality, particularly in the entertainment industry

Networked video and multi-media technologies

Collaborative work environments

Page 6: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Why Parallel? (4)

Computer의 물리적 한계(계속)• 현재 CPU의 clock speed는 대략 3GHz

- 발열 문제- 반도체 소자의 문제

• 1 Tera Hz(1 x 10^12 Hz)의 computer는 가능한가?광속 c = 3 x 10^8 m/sec = 3 x 10^11 mm/secCPU와 memory 사이의 거리를 r이라고 하면

r < c / 10^12 = 0.3 mm !!

• 1 Tera byte(=10^6 x 10^6 bytes)의 memory 가지려면1 byte는 3A x 3A 이내에 저장되어야 !!

Computer의 성능 높아질수록 가격은 기하급수적으로 상승

하나의 고가 시스템보다 여러 개의 저가 시스템을 사용하여병렬 처리하는 것이 대안

Page 7: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Why Parallel? (5)

Computer의 물리적 한계• 2004년까지 SW 개선 없이도 HW 발전에 따라 수혜

• CPU clock의 증가 : 2000년대 초반까지 기하급수적으로 증가

• CPU 실행시간 최적화 : CPU 명령어의 순차실행 최적화(pipeline, 분기예측, out-of-order execution 등)

• Cache 크기 증가

• “The free lunch is over: A fundamental turn toward concurrency in SW” (Hurb Sutter, 2005)• 전력 소모 및 발열 문제 : clock 속도의 제곱(세제곱?)

• Clock 속도 증가 짧은 도선 CPU의 신뢰도 하락

• 2004년부터 CPU 제조사들은 clock 속도 경쟁 포기

• CPU : single core multi-core GPU : many-core

Page 8: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Computers (1)

Parallel computer• 정의

* 다수의 CPU가 다수의 프로그램 혹은 분할된 프로그램을 동시에처리하는 컴퓨터

* 다수의 CPU를 결합하여 단일 CPU 성능의 한계를 극복하기 위한컴퓨터 구조http://giyyon.tistory.com/44?srchid=BR1http%3A%2F%2Fgiyyon.tistory.com%2F44

• 다양한 분류 방법이 있으나, 메모리 공유에 따른 분류는* SMP(Symmetric Multi Processing)* MPP(Massively Parallel Processing)* NUMA(Non-Uniform Memory Access)

** Cluster computer (workstation, PC, …)

MPI는 이 모든 구조에서 작동

Page 9: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Computers (2)

Cluster computer• http://pantheonjk.tistory.com/142?srchid=BR1http%3A%2F%2Fpantheonjk.tistory.com%2F142

• 정의개인 PC나 소형 server 등을 network 장비를 사용하여 다수 연결하여 구성한 일종의 병렬처리용 supercomputer

• 특징* 저렴한 가격 : 상용 supercomputer의 1/10* 확장성* 유연성

• 2002년 6월 Top500에 따른 HPC 시장의 architecture구성비

Page 10: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Computers (3)

2011년 11월 Top500에 따른 HPC 시장의 구성비

Page 11: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Computers (3)

2011년 11월 Top500에 따른 HPC 시장의 구성비

Page 12: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Computers (4)

2011년 11월 Top500에 따른 HPC 시장의 구성비

Page 13: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Computers (5)

PC Cluster• 소형 PC나 PC 서버를 수십 대에서 수천 대까지 병렬 네트워크로 연결

해 슈퍼컴퓨터에 상응하는 고성능 컴퓨팅(HPC)을 구현하는 기술

• 최초 NASA CESDIS Beowulf : 16-node Intel DX4 processorshttp://www.phy.duke.edu/~rgb/brahma/Resources/beowulf/

Page 14: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Processing (1)

병렬 처리(parallel processing)란?• 복수의 처리 장치를 사용하여, 모든 처리 장치가 하나의 프로그

램상의 서로 다른 태스크를 동시에 처리함으로써 처리의 부하를분담하여 처리 속도를 향상시키는 방법

• 여러 개의 프로그램을 동시에 병렬 처리하는 다중 처리( multiprocessing)와는 다름

Page 15: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Processing (2)

병렬 처리의 장단점• 장점 : 고속 연산 가능

• 단점- 추가적인 overhead 소요- 프로그래밍이 어려움- 모든 문제에 효율적으로 적용되는 것이 아님

병렬 처리의 예• 일상생활/회사에서의 예

• 벽에 페인트 칠하기 병렬 처리 쉬움

• 고도의 수학 문제 풀기 병렬 처리 어려움/불가능/무의미

Page 16: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Programming (1)

병렬 처리 문제 : 1부터 n까지의 합을 구하라.• p : 전체 worker의 수

각 worker는 p와 자신이 몇 번째 worker인지(자신의 “rank”) 알아야.

• Algorithm1. master worker가 n을 입력받는다.2. master worker가 고르게 작업을 나누고

각자 담당할 작업 범위를 slave worker들에게 알린다.3. 각자 주어진 범위의 부분합을 독립적으로 계산한다.4. slave worker는 각기 master worker에게

자신이 계산한 부분합을 보고한다.5. master worker는 자신을 포함한 모든 worker의 부분합을

취합하여 답을 낸다.

2 대신 master worker는 n 값을 slave worker들에게 broadcast하고모든 worker는 각자 자신의 rank에 따라 자신의 작업 범위를 파악한다.

Page 17: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Programming (2)

병렬 프로그래밍에서 고려되어야 할 사항• 고른 작업 분배가 중요 : 모두가 동시에 작업이 끝나야

• I/O 문제 : 입출력은 하나의 worker가 담당해야

• 모든 worker의 작업 및 진도를 어떻게 control? 동기화(synchronization)의 문제

• 어떻게 worker 간에 정보(즉 data)를 주고 받을 것인가? shared memory vs. distributed memory

• program을 어떻게 작성할 것인가?- SPMD(single program multiple data)

모든 worker가 같은 프로그램 사용- MPMD(multiple program multiple data)

worker가 각기 다른 프로그램 사용(보통은 역할에 따라 소수 개의 프로그램 작성)

• 어떻게 각 worker에게 program을 제공할 것인가?

Page 18: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Programming (3)

Shared memory vs. distributed memory

• Shared memory system- 모든 CPU는 메모리를 공유 한 CPU가 다른 CPU에 data를 전달하려면

단순히 공유 메모리에 write 다른 CPU가 read programming이 쉽다.- memory contention CPU 개수에 한계

• Distributed memory system- 모든 CPU는 각자 독립된 메모리 사용 한 CPU가 다른 CPU에 data를

전달하려면 명시적인 통신 필요 programming 어렵다.- memory contention 없으므로 시스템 확장 용이

Page 19: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Programming (4)

SPMD vs. MPMD [1/2]• SPMD(single program multiple data)

worker 모두가 같은 프로그램 사용,단, 각 worker의 rank에 따라 역할을 분류

Algorithm master worker의 rank=0을 가정1. If my rank==0,

n을 입력받고, slave worker들의 작업범위를 결정하고, 각자에게 통보한다.else

master worker로부터 작업 범위를 통보 받는다.(그 때까지 대기)2. 각자 독립적으로 자신이 맡은 범위의 수를 더해 부분합을 구한다.3. If my rank==0,

모든 slave worker로부터 부분합을 전달받아 총 합을 계산한다.else

각자 master worker에게 자신의 부분합을 보고한다.4. If my rank==0,

총합을 출력한다.

Page 20: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Parallel Programming (5)

SPMD vs. MPMD [2/2]• MPMD(multiple program multiple data)

역할에 따라 2개 이상의 프로그램 사용,

Algorithm (master worker)1. n을 입력받고, 각 slave worker의 작업범위를 결정하고, 각자에게 통보한다.2. 자신이 맡은 범위의 수를 더해 부분합을 구한다.3. 모든 다른 worker로부터 부분합을 전달받아 총 합을 계산한다.4. 총합을 출력한다.

Algorithm (slave worker)1. master worker로부터 자신의 작업범위를 통보받는다.2. 각자 독립적으로 자신이 맡은 범위의 수를 더해 부분합을 구한다.3. 각자 master worker에게 자신의 부분합을 보고한다.

Page 21: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI의 기본 개념 (1)

Message Passing

지역적으로 독립된 메모리를 가지는 process들이 data를 공유하기 위해message(data)를 송수신하여 통신하는 방식

• 병렬화를 위한 작업할당, data 분배, 통신의 운용 등 모든 것을 프로그래머가 담당 : 어렵지만 효율 높다.

• 다양한 hardware platform에서 구현 가능

• 분산 메모리 다중 processor system

• 공유 메모리 다중 processor system

• 단일 processor system

MPI란 무엇인가?• Message Passing Interface de facto standard

• Message passing 병렬 프로그래밍을 위해 표준화된 데이터 통신 라이브러리(Message Passing Library)의 표준을 정의한 것

• 목적 : 이식성(portability), 효율성(efficiency), 기능성(functionality)* hardware vendor가 자신의 hardware에 최적화된 library 제공 가능

Page 22: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI의 기본 개념 (2)

MPI의 역사• 1980년대 – 1990년대 초까지, 다양한 분산 메모리 병렬 컴퓨팅 SW 등장.

• MPI Forum : 표준 마련의 필요성에서 정부, 학계, 산업체 등, 1992 시작

• MPI-1 표준 마련(MPI Forum) : 1994년http://www.mcs.anl.gov/mpi/index.html

• MPI-2 발표 : 1997년

• 현재 MPI-2.2 : 2009년http://www.mpi-forum.org/docs/docs.html

• MPI 표준에 맞추어 개발한 MPI LibraryMPICH, CHIMP, LAM/MPI, OpenMPI, …,기타 각 hardware vendor들의 MPI

OpenMPI sitehttp://www.open-mpi.org/

Page 23: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI의 기본 개념 (3)

Process와 processor• MPI는 process 기준으로 작업 할당

• Processor : process = 1:1 or 1:many

Communicator• 서로간에 통신이 허용되는

모든 process들의 집합

Process rank• 동일한 communicator 내의 process들을

식별하기 위한 식별자

• 만일 p개의 process가 연산에 참여한다면 rank는0, 1, …, p-1

Page 24: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI의 기본 개념 (4)

Message [ = data + envelope ]• 어느 process가 보내는가

• 어디에 있는 data를 보내는가

• 어떤 data를 보내는가

• Data를 얼마나 보내는가

• 어느 process가 수신할 것인가

• 어디에 저장할 것인가

• 얼마나 받을 준비를 해야 하는가

Tag (꼬리표)• Message의 matching과 구분에 이용

• 순서대로 메시지 도착을 처리할 수 있음 message buffer 사용

• wild card 사용 가능ex. “누군가 message를 보내오면”

Page 25: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI의 기본 개념 (5)

점 대 점 통신 (point to point communication)• 두 개의 process 사이의 통신

• 하나의 송신 process에 하나의 수신 process가 대응

집합 통신(collective communication)• 동시에 여러 개의 process가 통신에 참여

• 일 대 다, 다 대 일, 다 대 다 대응 가능

• 여러 번의 점 대 점 통신 사용을 하나의 집합통신으로 대체

• 프로그래밍이 쉽고 간단하다.

• 오류의 가능성이 적다.

• 최적화되어 일반적으로 빠르다.

Page 26: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (1)

MPI(Message Passing Interface)• http://enc.daum.net/dic100/contents.do?query1=20XX221466

• In the definition by Gropp et al 96, MPI "is a message passing application programmer interface, together with protocol and semantic specifications for how its features must behave in any implementation", "MPI includes point-to-point message passing and collective (global) operations, all scoped to a user-specified group of processes.“

• MPI is a language-independent communications protocol used to program parallel computers.

• MPI-1은 127개의 함수로 구성

• 대부분의 MPI implementation은 Fortran, C, C++에서 호출가능한library function으로 구성 보통의 Fortran, C, C++ 프로그램에서 다른 process로 통신이 필요시 적절한 함수만 호출하면 됨

• 그 외 Python, Ocaml, Java에서도 사용하도록 노력중

Page 27: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (2)

MPI 프로그램의 구조

• Include MPI header file- MPI 함수의 prototype 선언- macro, MPI 관련 인수, data type 정의

• 변수 선언- MPI 함수의 prototype 선언

• MPI 환경의 초기화- MPI_Init()- MPI_Comm_rank()- MPI_Comm_size()

… MPI 통신 함수 호출하며 연산 수행

• MPI 환경 해제- MPI_Finalize()

Page 28: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (3)

MPI 함수에 대하여

• MPI 함수의 이름과 형태MPI_Xxxxxx(parameter, …);

MPI_ 로 시작그 다음 첫 글자 X는 대문자

• MPI 함수의 호출과 return 값

• 호출 예err = MPI_Init(&argc, &argv);if (err == MPI_SUCCESS) {

:}

혹은MPI_Init(&argc, &argv);

• Return 값첫 예의 경우, “err”로 return.

호출이 성공적이면 MPI_SUCCESS 값 가짐

Page 29: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (4)

int MPI_Init(&argc, &argv);

• MPI 환경 초기화

• MPI 루틴 중 가장 먼저 오직 한번 반드시 호출되어야 함

• MPI_COMM_WORLD라는 communicator가 정의됨

• 호출 예MPI_Init(&argc, &argv);

int MPI_Comm_rank(MPI_COMM comm, int *rank);

• 같은 communicator comm에 속한 process의 rank를 할당- p개의 process를 사용할 경우, 0부터 p-1의 값을 할당

• 호출 예MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

Page 30: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (5)

int MPI_Comm_size(MPI_COMM comm, int *size);

• Communicator comm에 포함된 process들의 총 개수 가져오기

• 호출 예MPI_Comm_size(MPI_COMM_WORLD, &p);

int MPI_Finalize( );

• 모든 MPI 자료구조 정리

• 모든 process들이 마지막으로 한 번 호출되어야 함

• Process를 종료시키는 것은 아님

• 호출 예MPI_Finalize();

Page 31: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (6)

MPI 메시지 = data + 봉투(envelope)

• Data

• Buffer : 수신(송신) data의 변수 이름

• 개수 : 수신(송신) data의 개수

• Data type : 수신(송신) data의 data 유형

• 봉투• 수신자(송신자) : 수신(송신) process의 rank

• Tag(꼬리표) : 송신(수신) data를 나타내는 고유한 정수

• Communicator : 송신, 수신 process들이 포함된 process group

• MPI data type

• 기본 type과 유도 type(derived type)

• 유도 type은 마음대로 만들 수 있다.

• 송신과 수신 data type은 반드시 일치해야 한다.

Page 32: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (7)

MPI 기본 data type

Page 33: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (8)

점대점 통신(point to point communication) [1/3]

• 반드시 두 개의 process만 참여

• communicator 내에서만 이루어짐

• 송신/수신 process의 확인을 위해 communicator와 rank 사용

• 통신의 완료

• 메시지 전송에 이용된 메모리 위치에 안전하게 접근할 수 있음을 의미

• Blocking 통신과 non-blocking 통신

• Blocking : 통신이 완료된 후 루틴으로부터 return됨

• Non-blocking : 통신이 시작되면 완료와 관계없이 return, 이후 완료여부검사

• 통신 완료에 요구되는 조건에 따라통신 모드 분류

Page 34: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (9)

점대점 통신(point to point communication) [2/3]

• 통신 모드

Page 35: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (10)

점대점 통신(point to point communication) [3/3]

• int MPI_Send(void *message, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)

• Datatype 형태의 자료 message를 count 개수만큼 dest rank의 process에게 전송

• 반환값 : error codeex. MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag,

MPI_COMM_WORLD);

• int MPI_Recv(void *message, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status)

• Source process로부터 tag 태그를 사용해서 보내온 메시지를 받음

• Source : process rank 혹은 MPI_ANY_SOURCE 사용

• Tag : 전송자가 사용한 tag 혹은 MPI_ANY_TAG 사용 가능ex. MPI_Recv(message, 100, MPI_CHAR, source, tag,

MPI_COMM_WORLD, &status);

• 이외 많은 점대점 통신 함수가 있음

Page 36: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (11)

집합 통신(Collective communication) [1/3]

• 한 그룹의 process가 참여

• 점대점 통신 기반

• 점대점 통신을 이용한 구현보다 편리하고 성능면에서 유리

• 집합통신 루틴

• Communicator 내의 모든 process 호출

• 동기화가 보장되지 않음

• Non-blocking 루틴 없음

• Tag 없음

Page 37: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (12)

집합 통신(Collective communication) [2/3]

Page 38: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 표준 (13)

집합 통신(Collective communication) [3/3]

• int MPI_Bcast(void *message, int count, MPI_Datatype datatype, int root, MPI_Comm comm)

• 전송자와 수신자는 모두 같은 명령을 사용

• root라는 rank를 가진 process가 송신하며 다른 모든 process는 수신Ex. MPI_Bcast(&uplimit,1,MPI_LONG,0,MPI_COMM_WORLD);

• Int MPI_Reduce(void *operand, void *result, int count, MPI_Datatype datatype, MPI_Op operator, int root, MPI_COMM comm)

• Operand에 주어진 자료에 대해 operator로 지정된 연산을 수행한 결과를 result로 반환함

• Operator는 MPI_MAX, MPI_MIN, MPI_SUM, MPI_PROD 등 많은 종류의 연산이 가능Ex. MPI_Reduce(&sum,&gsum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);

• 이외 여러 집합 통신 함수가 있음

Page 39: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 프로그램 및 실행 (1)

간단한 MPI 프로그램의 예 [1/2]• “hello, world” program (Pacheco)

/* "Hello World" Type MPI Test Program */ #include <stdio.h> #include “mpi.h”

int main(int argc, char **argv) {

int my_rank; /* rank of process */int p; /* number of processes */int source; /* rank of sender */int dest; /* rank of receiver */int tag=50; /* tag for messages */char message[100]; /* storage for the message */MPI_Status status; /* return status for receive */

MPI_Init(&argc, &argv);MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);MPI_Comm_size(MPI_COMM_WORLD, &p);

Page 40: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 프로그램 및 실행 (2)

간단한 MPI 프로그램의 예 [2/2]• “hello, world” program (Pacheco, 계속)

if (my_rank != 0) {sprintf(message, “Greetings from process %d !”, my_rank);dest = 0;MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag,

MPI_COMM_WORLD);} else { /* my_rank == 0 */

for (source = 1; source < p; source++) {MPI_Recv(message, 100, MPI_CHAR, source, tag,

MPI_COMM_WORLD, &status);printf(“%s\n”, message);

}}

MPI_Finalize();} /* main */

Page 41: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 프로그램 및 실행 (3)

프로그램의 실행 [1/2]• LAM/MPI에서의 comile & run

• LAM 환경 조성 및 process 지정$ lamboot –v lamhosts

• 병렬 program compile$ hcc –o helloworld helloworld.c

• 병렬로 run하기$ mpirun –np 4 helloworld

• LAM 환경 해제$ wipe –v lamhosts

• “lamhosts” file 이름은 임의로 줄 수 있음- 병렬 연산하는 데 사용되는 host의 이름(IP도 OK)을 나열- 멀리 떨어진 host와도 병렬 연산 가능

ex. psmp 혹은 psmppsmp psmptoshiba 211.221.225.179

Page 42: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 프로그램 및 실행 (4)

프로그램의 실행 [2/2]

[pspark@psmp MPI]$ cat lamhostspsmppsmppsmppsmp[pspark@psmp MPI]$ lamboot -v lamhosts

LAM 7.1.1/MPI 2 C++/ROMIO - Indiana University

n-1<24783> ssi:boot:base:linear: booting n0 (psmp)n-1<24783> ssi:boot:base:linear: finished[pspark@psmp MPI]$ hcc -o helloworld helloworld.c[pspark@psmp MPI]$ mpirun -np 4 helloworldGreetings from process 1 !Greetings from process 2 !Greetings from process 3 ![pspark@psmp MPI]$ wipe -v lamhosts

LAM 7.1.1/MPI 2 C++/ROMIO - Indiana University

n-1<24800> ssi:boot:base:linear: booting n0 (psmp)n-1<24800> ssi:boot:base:linear: finished[pspark@psmp MPI]$

Page 43: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

MPI 프로그램 및 실행 (5)

1부터 n까지 더하는 프로그램serial program#include <stdio.h>

int main(int argc, char* argv[]){long int i,last;double sum;

printf("This program computes the sum from 1 to a given number.\n");printf("Enter a big number : ");scanf("%ld",&last);

sum=0.;for (i=1; i<=last; i++) {sum=sum+i;

}printf("\nSum from 1 to %ld is %f\n",last,sum);

}

parallel program

http://pspark.suwon.ac.kr/Linux/LectNote/mpisum.c

Page 44: Parallel Programming & MPIvmlab.suwon.ac.kr/mwlee/data2/file/병렬표준_v3.pdf · 2012-05-08 · Why Parallel? (1) Whyparallel computing? • 전통적인compute-intensive applications

Related Topics & Questions

Related Topics GPU computing

GPGPU(위키백과) 한글 http://enc.daum.net/dic100/contents.do?query1=10XX236094영문 http://enc.daum.net/dic100/contents.do?query1=20X1268939

CUDA미루웨어 http://www.miruware.com/한국 CUDA 사용자 그룹(KCUG) http://cafe.daum.net/KCUG

OpenCL http://enc.daum.net/dic100/contents.do?query1=10XX281093

OpenMP http://enc.daum.net/dic100/contents.do?query1=20XX381842

Questions?