MPI分区数组分为块和发送

时间:2013-01-29 00:06:21

标签: c arrays mpi hpc scatter

我试图在C语言中使用MPI找到数组的最大元素。我必须比较使用vs MPI_Scatter函数发送和计算最大值所需的时间。 MPI_Send:这里'MPI_Scatter函数的代码很好用:

#include "mpi.h"
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#define lim 20

//returns "a-b" in seconds
double timeval_diff(struct timeval *a, struct timeval *b)
{
  return
    (double)(a->tv_sec + (double)a->tv_usec/1000000) -
    (double)(b->tv_sec + (double)b->tv_usec/1000000);
}

//Array to be divided among the processes
int buf[lim]= 
{27,24,3,8,45,10,50,15,10,11,9,48,69,25,19,29,61,72,93,20};
int buf2[lim];
int buf3[lim];
int max;

int main(int argc, char *argv[])
{    
    struct timeval t_ini, t_fin;
    double secs;            
    int  n, myid, numprocs, i,j;
    int  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,"Process %d in %s\n",myid, processor_name);

/*Check Border Conditions */    
    n=lim/numprocs;
    gettimeofday(&t_ini, NULL); //take the time before sending the buffer with Scatter
    MPI_Scatter(buf,n, MPI_INT,buf2,n,MPI_INT, 0, MPI_COMM_WORLD);
    gettimeofday(&t_fin, NULL);//take the time to complete the send routine        
    secs = timeval_diff(&t_fin, &t_ini);      
    MPI_Reduce(buf2,buf3,n, MPI_INT, MPI_MAX, 0,MPI_COMM_WORLD);
    if (myid == 0)
    { max = buf3[0];
      for (i=1; i<n ; i++)
              if (max < buf3[i])  max = buf3[i];
          for (i=0; i<n ; i++)
          printf("Buf3[%d]= %d \n", i, buf3[i]);
          printf("Max number of the array is:  %d \n", max);
     }
     for (i=0; i<n ; i++){
       printf("%d,Buf2[%d]= %d \n",myid, i,buf2[i]);}
       printf("%.16g milliseconds\n", secs * 1000.0);         
    MPI_Finalize();
    return 0;
}

问题来自于我尝试使用MPI_Send函数执行相同的过程,因为我计算了最大数组元素,我做错了什么?:

#include "mpi.h"
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#define lim 20

//returns "a-b" in seconds
double timeval_diff(struct timeval *a, struct timeval *b)
{
  return
    (double)(a->tv_sec + (double)a->tv_usec/1000000) -
    (double)(b->tv_sec + (double)b->tv_usec/1000000);
}

//Array to be divided among the processes
int buf[lim]= 
{27,24,3,8,45,10,50,15,10,11,9,48,69,25,19,29,61,72,93,20};
int buf2[lim];
int buf3[lim];
int max;

int main(int argc, char *argv[])
{

    struct timeval t_ini, t_fin;
    double secs;           
    int  n, myid, numprocs, i,j;
    int  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,"Process %d in %s\n",myid, processor_name);

/*Check Border Conditions */    
    n=lim/numprocs;
    gettimeofday(&t_ini, NULL); //take the time before sending the buffer with Scatter
    for (j=0;j<n;j++){
    MPI_Send(buf, lim, MPI_INT, 1, 111, MPI_COMM_WORLD);
    }
    gettimeofday(&t_fin, NULL);//take the time to complete the send routine
    secs = timeval_diff(&t_fin, &t_ini);
    if (myid == 0)
    { max = buf3[0];
      for (i=1; i<n ; i++)
              if (max < buf3[i])  max = buf3[i];
          for (i=0; i<n ; i++)
          printf("Buf3[%d]= %d \n", i, buf3[i]);
          printf("Max number of the array is:  %d \n", max);
     }
     for (i=0; i<n ; i++){
       printf("%d,Buf2[%d]= %d \n",myid, i,buf2[i]);}
       printf("%.16g milliseconds\n", secs * 1000.0);   

    MPI_Finalize();
    return 0;
}

我浪费了几个小时看着哪里有错,但我看不到它......有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

您错过了MPI_Recv来电另一端的MPI_Send来电,这些功能与集体分散,收集,缩小和广播功能相比更低级别