给定一个数据文件data.txt,存放有N个整型数据(测试数据为13个,1-13),要求统计素数个数。
#include "mpi.h" #include <stdio.h> #include <math.h> #define SIZE 10000 int isPrime(int num){ if(num<2){ return 0; }else if(num==2 || num==3){ return 1; }else{ int primeFlag=1; for(int i=2;i<(int)sqrt(num)+1;i++){ if(num%i==0){ primeFlag=0; break; } } return primeFlag; } } int main(int argc,char* argv[]) { int number[SIZE]; FILE *fp=fopen("data.txt","r"); if(fp==NULL) { printf("can not open data.txt!\n"); } else { int totalNum=0; while(!feof(fp)) { if(totalNum<SIZE-1) { int num=0; fscanf(fp,"%d",&num); num?number[totalNum++]=num:0; } else { break; } } int rank,size; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if(rank==0){ int result=0; int total=0; for(int i=1;i<size;i++){ MPI_Recv(&result,sizeof(int),MPI_INT,i,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE); total+=result; } printf("total number:%d\n",total); }else{ int start=totalNum/(size-1)*(rank-1); int tail=(rank==(size-1))?totalNum-1:start+totalNum/(size-1)-1; int mySize=0; for(int i=start;i<=tail;i++){ printf("[rank %d] judge number:%d\n",rank,number[i]); mySize=isPrime(number[i])?mySize+1:mySize; } printf("[rank %d]total prime number:%d\n",rank,mySize); MPI_Send(&mySize,sizeof(int),MPI_INT,0,0,MPI_COMM_WORLD); } MPI_Finalize(); } return 0; }
执行编译命令:
mpicc prime_test.cpp -lm -o prime_test
注意-lm参数表示需要链接数学库,编译环境为mpich2-1.0.2p1 + ubuntu14.04 b4bit
然后执行:
mpiexec -n 4 ./prime_test
运行截图如下:
原文地址:http://blog.csdn.net/hzylmf/article/details/45130197