MPI Basic II

GraGrass·2024년 1월 23일
0

Chapter 4

MPI 통신

1. Sending and Receiving Message

Sender Process

  • 어디로 보낼까?
  • 무엇을 보낼까?
  • 얼마나 많이 보낼까?

Receiver Process

  • 어디에서 오는 메세지를 받을까?
  • 어디에 저장할까?
  • 얼마나 많이 받을 준비를 할까?

2. Communication Envelope

Data

  • 실제 전달하고자 하는 내용 (우편의 편지지에 해당)
  • Buffer: 전달/수신할 버퍼 지정
  • Count: 몇 개의 데이터를 보낼지 지정
  • Data type

Envelope

  • 실제 통신이 이루어지기 위한 부가 정보 (우편의 편지봉투에 해당)
  • Process identifier (source/destination rank)
  • Message tag: data를 여러 번 보낼 때, 각각을 구분하는 tag
  • communicator

MPI Data Type

MPI Functions

1. MPI_Send()

Data

  • buf: 송신 buffer의 시작 주소
  • count: 보내는 item의 개수
  • datatype: 보낼 item의 데이터 타입 지정

Envelope

  • dest: destination process의 rank
  • tag: message tag
  • comm: communicator

2. MPI_Recv()

Data

  • buf: 수신 buffer의 시작 주소
  • count: 받을 수 있는 최대 item 개수
    • MPI_Send()의 count보다 MPI_Recv()의 count가 적을 경우, 에러 발생 & truncate
  • datatype: 받을 item의 datatype

Envelope

  • src: source process의 rank
  • tag: message tag
    MPI_Send()와 tag가 동일해야 함
  • comn: communicator

Status

  • Status Information: 송신 process (rank), tag, Data size(MPI_GET_COUNT)
  • MPI_STATUS(ES)_IGNORE: status 필드 조사가 필요 없을 때 많이 사용

3. Example

C code

#include <stdio.h>
#include "mpi.h"

int main(void)
{
	int rank, i. count;
    float data[100], value[200];
    MPI_Status status;
    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    
    if (rank==1){
    	for(i=0; i<100; ++i) data[i]=i;
        MPI_Send(data,100,MPI_FLOAT, 0, 55, MPI_COMM_WORLD);
    }
    else {
    	MPI_Recv(value, 200, MPI_FLOAT, 1, 55, MPI_COMM_WORLD, &status);
        printf("P:%d Got data from processor %d\n", rank, status.MPI_SOURCE);
        MPI_Get_count(&status, MPI_FLOAT, &count);
        printf("P:%d Got %d elements\n",rank, count);
        printf("P:%d value[5]=%f\n", rank, value[5]);
    }
    MPI_Finalize();
    return 0;
}
  • MPI_Send(data,100,MPI_FLOAT, 0, 55, MPI_COMM_WORLD): data 배열의 요소 100개 모두 rank 0 process에 전송
  • rank 0, rank 1 두 개만 있는 example
  • 더 많은 process가 있는 상황에서 rank 1로 보내고 싶다면 else if(rank ==0)으로 지정해주어야 함

Result

profile
올해는 진짜 갓생 산다

0개의 댓글