一、安装mpi
apt-get update
apt-get intall mpich2
二、编写程序测试
vi hello.c
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
#include<time.h>
int main(int argc, char* argv[])
{
int myid, numprocs, namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv); // starts MPI
MPI_Comm_rank(MPI_COMM_WORLD, &myid); // get current process id
MPI_Comm_size(MPI_COMM_WORLD, &numprocs); // get number of processes
MPI_Get_processor_name(processor_name, &namelen);
if (myid == 0) printf("number of processes: %d\n...", numprocs);
printf("%s: Hello world from process %d \n", processor_name, myid);
MPI_Finalize();
return 0;
}
编译
mpicc -o hello hello.c
运行
mpirun -n 4 ./hello
或者
mpiexec -n 4 ./hello
结果