将MPI分割数组为块并发送

3

我正在尝试使用C语言中的MPI查找数组的最大元素。我必须比较使用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个回答

2

你在另一端缺少MPI_Recv调用,这些函数更低级,与集体散射、聚集、规约和广播函数相反。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接