You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.3 KiB

[TOC]


MPI程序基本框架与常用函数

任务描述

本关任务:练习MPI程序的基本框架与常用函数,包括MPI_InitMPI_Comm_sizeMPI_Comm_rankMPI_SendMPI_Recv以及MPI_Finalize的使用。

Open MPI 文档:https://docs.open-mpi.org/en/main/

相关知识

为了完成本关任务你需要掌握1.MPI程序的基本框架2.MPI常用函数。

MPI常用函数


MPI_Init

MPI_Init — Initializes the MPI execution environment.

int MPI_Init(int *argc, char ***argv)

INPUT PARAMETERS

  • argc: C only: Pointer to the number of arguments.
  • argv: C only: Argument vector.

OUTPUT PARAMETER

  • ierror: Fortran only: Error status (integer).

MPI_Comm_size

MPI_Comm_size — Returns the size of the group associated with a communicator.

int MPI_Comm_size(MPI_Comm comm, int *size)

INPUT PARAMETER

  • comm: Communicator (handle).

OUTPUT PARAMETERS

  • size: Number of processes in the group of comm (integer).

MPI_Comm_rank

MPI_Comm_rank — Determines the rank of the calling process in the communicator.

int MPI_Comm_rank(MPI_Comm comm, int *rank)

INPUT PARAMETERS

  • comm: Communicator (handle).

OUTPUT PARAMETERS

  • rank: Rank of the calling process in group of comm (integer).
  • ierror: Fortran only: Error status (integer).

MPI_Send

MPI_Send — Performs a standard-mode blocking send.

int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest,
     int tag, MPI_Comm comm)

INPUT PARAMETERS

  • buf: Initial address of send buffer (choice).
  • count: Number of elements in send buffer (nonnegative integer).
  • datatype: Datatype of each send buffer element (handle).
  • dest: Rank of destination (integer).
  • tag: Message tag (integer).
  • comm: Communicator (handle).

OUTPUT PARAMETER

  • ierror: Fortran only: Error status (integer).

MPI_Recv

MPI_Recv — Performs a standard-mode blocking receive.

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

INPUT PARAMETERS

  • count: Maximum number of elements to receive (integer).
  • datatype: Datatype of each receive buffer entry (handle).
  • source: Rank of source (integer).
  • tag: Message tag (integer).
  • comm: Communicator (handle).

OUTPUT PARAMETERS

  • buf: Initial address of receive buffer (choice).
  • status: Status object (status).
  • ierror: Fortran only: Error status (integer).

MPI_Finalize

MPI_Finalize — Terminates MPI execution environment.

int MPI_Finalize()

OUTPUT PARAMETER

  • ierror: Fortran only: Error status (integer).

MPI程序编译运行

$ mpicc -g -Wall -o mpi_hello mpi_hello.c
$ mpiexec -n 4 ./mpi_hello

编程要求

根据提示,在右侧编辑器补充六处代码。启动四个进程分别将问候语发送给进程0,由进程0将接收到的问候语打印输出出来。

测试说明

平台会对你编写的代码进行测试:

测试输入:无; 预期输出:

Greetings from process 0 of 4!
Greetings from process 1 of 4!
Greetings from process 2 of 4!
Greetings from process 3 of 4!

开始你的任务吧,祝你成功!