Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:19:33+00:00 2026-06-17T22:19:33+00:00

I am trying to find a maximum element of an array using MPI in

  • 0

I am trying to find a maximum element of an array using MPI in C language. I have to compare the time it takes to send and calculation of the maximum using vs MPI_Scatter functions. MPI_Send: Here’ the code for the MPI_Scatter function it 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 with the MPI_Send function because I calculated the maximum array elements, 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 wasted some hours watching Where is the fault but I can not see it … Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:19:35+00:00Added an answer on June 17, 2026 at 10:19 pm

    you are missing the MPI_Recv call on the other end of your MPI_Send call, these kind of functions are more low level as opposed to the collective scatter, gather, reduce and broadcast functions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find top 4 maximum value from integer array input. For
I'm trying to find a linear-time algorithm using recursion to solve the diameter problem
Trying to find an example that has css rollover using sprites & sliding door
Trying to find a way to send a POST HTTPS request from Python to
I'm tring to find the maximum weight subsequence of an array of positive integers
I'm trying to find the maximum number within a list, then do something with
I am trying to find out the maximum value for an integer (signed or
I am trying to write pre and post conditions to find the maximum value
I have a map: typedef map<string, float> my_map; my_map maxmap; I'm trying to find
I am trying, unsucessfully, to query my database to find the maximum 'area number',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.