编译:mpicc filename.c -o filename
运行:mpirun -np 4 filename
1.Hello World
#include
#include
int main (int argc, char **argv) {
int rank, num, i;
MPI_Init (&argc, &argv);
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
MPI_Comm_size (MPI_COMM_WORLD, &num);
printf ("Hello from process %i of %i\n", rank, num);
MPI_Finalize ();
return 0;
}
2.PingPong 主机向其他人发送Ping,收到Pong
#include
#include
#include
int main(int argc,char **argv){
MPI_Init(&argc, &argv);
int rank,num,i,source;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num);
if(rank == 0){
char sendMess[] = "Ping";
int len = strlen(sendMess) + 1;
for(i = 1; i < num;i++){
MPI_Send(sendMess,len,MPI_CHAR,i,99,MPI_COMM_WORLD);
}
MPI_Status status;
char recvMess[100];
for (source = 1; source < num; source++) {
MPI_Recv(recvMess, 100, MPI_CHAR, source, 99,MPI_COMM_WORLD, &status);
printf("%i received %s from %i\n",rank, recvMess,source);
}
}else{
char mess[100];
MPI_Status status;
MPI_Recv(mess,100,MPI_CHAR,0,99,MPI_COMM_WORLD,&status);
printf("%i received %s\n",rank, mess);
char sendMess[] = "Pong";
int len = strlen(sendMess) + 1;
MPI_Send(sendMess,len,MPI_CHAR,0,99,MPI_COMM_WORLD);
}
MPI_Finalize();
}
3.矩阵乘法
#include
#include "mpi.h"
#include
#include
#include
#include
int **genMatrix(int size)
{
int num = 0,m;
int **matrix;
matrix = (int **)malloc(sizeof(int *) * size);
for(m = 0; m < size; m++)
matrix[m] = (int *)malloc(sizeof(int) * size);
int i,j;
srand(time(NULL) + rand());
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
matrix[i][j]= rand() % 20;
}
}
return matrix;
}
void showMatrx(int **a,int size)
{
int i,j;
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
int * Multiplication(int **a,int b[],int size){
int *result;
result = (int *)malloc(sizeof(int) * size);
int i,m,n,sum = 0;
for(m = 0; m < size; m++){
for(n = 0; n < size; n++){
sum += a[n][m] * b[n];
}
result[m] = sum;
sum = 0;
}
return result;
}
int main(int argc,char **argv){
int size,rank,dest;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(comm,&size);
MPI_Comm_rank(comm,&rank);
int **matrix1;
int **matrix2;
int send_buff[size*size];
matrix1 = genMatrix(size);
matrix2 = genMatrix(size);
if(rank == 0){
printf("matrix1 is :\n");
showMatrx((int **)matrix1,size);
printf("matrix2 is :\n");
showMatrx((int **)matrix2,size);
int j,k,tmp = 0;
for(j = 0; j < size; j++)
for(k = 0; k < size; k++){
send_buff[tmp] = matrix1[j][k];
tmp++;
}
}
int rbuf[size];
int final_buff[size];
int *result;
result = (int *)malloc(sizeof(int) * size);
MPI_Scatter(send_buff,size,MPI_INT,rbuf,size,MPI_INT,0,comm);
result = Multiplication((int **)matrix2,rbuf,size);
MPI_Barrier(comm);
int *recv_buff;
if(rank == 0)
recv_buff = (int*)malloc(sizeof(int)*size*size);
MPI_Barrier(comm);
MPI_Gather(result,size,MPI_INT,recv_buff,size,MPI_INT,0,comm);
if(rank == 0){
printf("\nresult is :\n");
int m,n,tmp = 0;
for(m = 0; m < size; m++){
for(n = 0;n < size;n++){
printf("%d ",recv_buff[tmp]);
tmp++;
}
printf("\n");
}
printf("\n");
}
MPI_Finalize();
return 0;
}