例如
#include “mpi.h”
#include<stdio.h>
#include<math.h>
void main( argc, argv)
int argc;
char *argv[];
{
int myid, numprocs, namelen;
char processor_name[MPI_MAX _PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_ WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM _WORLD, &myid);
MPI_Get_processor_name(processor_name, &namelen);
fprintf(stderr,"hello World! Process %d of %d on %s\n”, myid, numprocs, processor_name);
MPI_Finalize();
}
正常C语言方式书写代码,保存代码
编译 $mpicc -o mpi_hello helloworld.c
执行 $mpirun -np 4 mpi_hello
结果打印为
Hello World!Process 1 of 4 on ubuntu
Hello World!Process 2 of 4 on ubuntu
Hello World!Process 3 of 4 on ubuntu
Hello World!Process 0 of 4 on ubuntu