MPI Basic I

GraGrass·2024년 1월 22일
0

Chapter 3

MPI 기본 함수

1. MPI_Init

개요

int MPI_Init(int *argc, char ***argv)
  • MPI 실행을 위해 시스템을 준비
  • MPI_Init 호출 전에 MPI 함수 호출 불가
  • C에서 MPI_Init 호출은 argument update 가능 but implementation에 의존적

실행

  • MPI_Init 실행 시 MPI_COMM_WORLD라는 기본 Communicator 생성
  • MPI_COMM_WORLD: 사용하는 모든 프로세스를 의미

MPI Communicator

  • 서로 통신할 수 있는 프로세스들의 그룹에 해당하는 handle
  • 모든 MPI 통신 호출은 communicator를 인자로 가짐

2. MPI_Finalize

int MPI_Finalize(void)
  • MPI에 의해 할당된 모든 메모리를 해제하는 종료문
  • MPI_Finalize 호출 이후 MPI 함수 호출 불가
  • 프로세스 하나라도 종료문에 도달하지 못하면 프로그램은 종료되지 않음

3. MPI_Comm_size

int MPI_Comm_size(MPI_Comm comm, int *size)
  • Communicator Size
  • Communicator에 포함된 프로세스의 개수

4. MPI_Comm_rank

int MPI_Comm_rank(MPI_Comm comm, int *rank)
  • Processor Rank == Processor ID
  • Communicator 내에서 Process ID number
  • 지정된 communicator 내에서 호출 프로세스의 rank return
  • 프로세스들은 0~N-1까지의 rank를 가짐

실습 (Hello world code)

1. C code

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

int main(int argc, char *argv[])
{
	int ver, subver;
    int nRank, nProcs;
    char procName[MPI_MAX_PROCESSOR_NAME];
    int nNameLen;
    
    // MPI Start
    MPI_Init(NULL, NULL);
    // Get Current processor rank id
    MPI_Comm_rank(MPI_COMM_WORLD, &nRank);
    // Get number of processors
    MPI_Comm_size(MPI_COMM_WORLD, &nProcs); 
    
    // MPI Version Information
    MPI_Get_version(&ver, &subver));
    if (nRank==0) printf("MPI Version %d.%d\n",ver,subver);
    MPI_Get_processor_name(procName, &nNameLen);
    
    printf("Hello World. (Process name=%s, nRank=%d, nProcs=%d)\n",procName,nRank,nProcs);
    MPI_Finalize();
    return 0;
}

2. Result

  • 4개의 프로세스가 하나의 노드 node8103에서 실행됨
  • NODEFILE의 상위 4개 노드 사용
  • -np 16을 지정했다면, node8104도 사용되었을 것
profile
올해는 진짜 갓생 산다

0개의 댓글