1
我是mpi編程的新手。我剛剛在c中使用mpi_scatter嘗試了一個並行搜索程序。我想知道我的程序是否正確。但是當我執行一個沒有MPI_SCATTER的程序,即線性搜索時,與並行程序相比,執行時間更少。爲什麼會發生這種情況?使用MPI_Scatter的並行搜索
#include<stdio.h>
#include<time.h>
#include<mpi.h>
main(int argc,char *argv[])
{
clock_t tic = clock();
int rank,size,a[10]={1,2,3,4,5,6,7,8,9,10},b[10],search=6,flag=0;
long int i;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Scatter(&a,5,MPI_INT,&b,5,MPI_INT,0,MPI_COMM_WORLD);
if(rank==0)
{
for(i=0;i<5;i++)
{
if(b[i]==search)
{
printf("\nNumber found!\t\t%d\t\t%d",rank,i);
flag=1;
}
printf("\n%d\t\t%d",b[i],rank);
}
}
if(rank==1)
{
for(i=0;i<5;i++)
{
if(b[i]==search)
{
printf("\nNumber found!\t\t%d\t\t%d",rank,i);
flag=1;
}
printf("\n%d\t\t%d",b[i],rank);
}
}
MPI_Finalize();
clock_t toc=clock();
printf("\n\nElapsed: %f seconds\n", (double)(toc - tic)/CLOCKS_PER_SEC);
}
OUTPUT:
[[email protected] mpipgms]$ /usr/lib/openmpi/bin/mpicc my_pgm2.c -o my_pgm2
[[email protected] mpipgms]$ /usr/lib/openmpi/bin/mpirun -np 2 my_pgm2
1 0
2 0
3 0
4 0
Number found! 1 0
6 1
7 1
8 1
9 1
5 0
Elapsed: 0.070000 seconds
10 1
Elapsed: 0.080000 seconds