// file name="hello-mpi.c"
#include "mpi.h"
#include<stdio.h>
#include<math.h>
int main(int argc, char *argv[])
{
int myid,numprocs;
/* 整型变量myid和numprocs分别用来记录某一个
并行执行进程的标识和所有参加计算的进程的个数*/
int namelen;
/* namelen是实际得到的机器名字的长度*/
char processor_name[MPI_MAX_PROCESSOR_NAME];
/*MPI_MAX_PROCESSOR_NAME宏:某一MPI的具体实现中允许机器名字
的最大长度,机器名放在变量processor_name中; */
MPI_Init(&argc, &argv);
/* MPI程序的初始化*/
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
/*得到当前正在运行的进程的标识号,放在myid中*/
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
/*得到所有参加运算的进程的个数,放在numprocs中;*/
MPI_Get_processor_name(processor_name,&namelen);
/* MPI_Get_processor_name得到本进程运行的机器的名称,
结果放在processor_name中,长度放在namelen中;*/
fprintf(stderr,"Hello World!Process %d of %d on %s\n",myid,numprocs,processor_name);
/*MPI程序的结束*/
return 0;
}
mpicc hello-mpi.c -o hello-mpi
mpirun -np 5 ./hello-mpi
air:~ wkl$ mpirun -np 5 ./mpi-try
Hello World!Process 0 of 5 on air.local
Hello World!Process 2 of 5 on air.local
Hello World!Process 3 of 5 on air.local
Hello World!Process 1 of 5 on air.local
Hello World!Process 4 of 5 on air.local