url: https://hpc-tutorials.llnl.gov/mpi/exercise_1/
https://hpc-tutorials.llnl.gov/mpi/exercise_1/
这个参考链接包含大量 MPI 例子程序可供参考:
部分代码还有串行版本作为对照

看一下练习要求:

很简单,实现代码如下:
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define MASTER 0
int main (int argc, char *argv[])
{
int numtasks, taskid, len;
char hostname[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
MPI_Get_processor_name(hostname, &len);
printf ("Hello from task %d on %s!\n", taskid, hostname);
if (taskid == MASTER)
printf("MASTER: Number of MPI tasks is: %d\n",numtasks);
MPI_Finalize();
}
编译运行方式:
mpicc -w -o hello myhello.c
mpirun -np 4 ./hello
预期输出:
Hello from task 3 on <hostname>
Hello from task 0 on <hostname>
MASTER: Number of MPI tasks is: 4
Hello from task 1 on <hostname>
Hello from task 2 on <hostname>

被折叠的 条评论
为什么被折叠?



