[TOC] --- ## MPI程序基本框架与常用函数 ### 任务描述 本关任务:练习`MPI`程序的基本框架与常用函数,包括`MPI_Init`、`MPI_Comm_size`、`MPI_Comm_rank`、`MPI_Send`、`MPI_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. ```c 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. ```c 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. ```c 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. ```c 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. ```c 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. ```c int MPI_Finalize() ``` OUTPUT PARAMETER - `ierror`: Fortran only: Error status (integer). #### MPI程序编译运行 ```bash $ mpicc -g -Wall -o mpi_hello mpi_hello.c $ mpiexec -n 4 ./mpi_hello ``` ### 编程要求 根据提示,在右侧编辑器补充六处代码。启动四个进程分别将问候语发送给进程`0`,由进程`0`将接收到的问候语打印输出出来。 ### 测试说明 平台会对你编写的代码进行测试: 测试输入:无; 预期输出: ```bash Greetings from process 0 of 4! Greetings from process 1 of 4! Greetings from process 2 of 4! Greetings from process 3 of 4! ``` --- 开始你的任务吧,祝你成功!