Array of MPI sections into blocks and Send

I'm trying to find the maximum element of an array using MPI in C. I need to compare the time it takes to send and calculate the maximum value using vs MPI_Scatter functions. MPI_Send: Here's the code for the MPI_Scatter function works great:

#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;
}

      

The problem comes when I try to do the same procedure using the MPI_Send function because I have calculated the maximum elements of the array, what am I doing wrong?

#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;
}

      

I spent a few hours looking at Where is the error but I can't see it ... Any help?

+3


source to share


1 answer


you don't need a call MPI_Recv

on the other end of your call MPI_Send

, this kind of functions are lower, not collective scatter, collection, reduction and broadcast of functions.



+2


source







All Articles