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 7710309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:05:40+00:00 2026-06-01T01:05:40+00:00

I am very new to MPI programming, I am trying to run a program

  • 0

I am very new to MPI programming, I am trying to run a program that should sort e.g. 8 or 16 or more numbers in parallel, I am getting segmentation error when I compiled and ran the program. Below is the code that I wan to compile. Any suggestion will be appreciated.

#include <stdio.h>

#include <stdlib.h> 

#include <mpi.h> 


/* The IncOrder function that is called by qsort is defined as follows */ 

int IncOrder(const void *e1, const void *e2) 

{ 
  return (*((int *)e1) - *((int *)e2)); 
} 

int main(int argc, char *argv[]){ 
     int n;         /* The total number of elements to be sorted */ 
     int npes;      /* The total number of processes */ 
     int myrank;    /* The rank of the calling process */ 
         int nlocal;    /* The local num of elements, and d array that stores */ 
     int *elmnts;   /* The array that stores the local elements */ 
         int *relmnts;  /* The array that stores the received elements */ 
     int oddrank;   /* The rank of d process during odd-phase comm */ 
     int evenrank;  /* The rank of the process during even-phase com */ 
     int *wspace;   /* Working space during the compare-split operation */ 
     int i; 
     MPI_Status status; 

     /* Initialize MPI and get system information */ 
     MPI_Init(&argc, &argv); 
     MPI_Comm_size(MPI_COMM_WORLD, &npes); 
     MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 

     n = atoi(argv[1]); 
     nlocal = n/npes; /* Compute the number of elements to be stored locally. */ 

     /* Allocate memory for the various arrays */ 
     elmnts  = (int *)malloc(nlocal*sizeof(int)); 
     relmnts = (int *)malloc(nlocal*sizeof(int)); 
     wspace  = (int *)malloc(nlocal*sizeof(int)); 

     /* Fill-in the elmnts array with random elements */ 
     srandom(myrank); 
     for (i=0; i<nlocal; i++) 
          elmnts[i] = random(); 

      /* Sort the local elements using the built-in quicksort routine */ 
          qsort(elmnts, nlocal, sizeof(int), IncOrder); 
              /* Determine the rank of the processors that myrank needs to com during 
      * ics/ccc.gifthe */ 
      /* odd and even phases of the algorithm */ 
      if (myrank%2 == 0) { 
          oddrank  = myrank-1; 
          evenrank = myrank+1; 
      } else { 
          oddrank  = myrank+1; 
          evenrank = myrank-1; 
                                  } 

      /* Set the ranks of the processors at the end of the linear */ 
      if (oddrank == -1 || oddrank == npes) 
          oddrank = MPI_PROC_NULL; 
      if (evenrank == -1 || evenrank == npes) 
          evenrank = MPI_PROC_NULL; 

      /* Get into the main loop of the odd-even sorting algorithm */ 
      for (i=0; i<npes-1; i++) { 
          if (i%2 == 1) /* Odd phase */ 
              MPI_Sendrecv(elmnts, nlocal, MPI_INT, oddrank, 1, relmnts, 
              nlocal, MPI_INT, oddrank, 1, MPI_COMM_WORLD, &status); 
          else /* Even phase */ 
              MPI_Sendrecv(elmnts, nlocal, MPI_INT, evenrank, 1, relmnts, 
              nlocal, MPI_INT, evenrank, 1, MPI_COMM_WORLD, &status); 

              CompareSplit(nlocal, elmnts, relmnts, wspace, myrank < status.MPI_SOURCE); 
       } 
MPI_Gather(elmnts,nlocal,MPI_INT,relmnts,n,MPI_INT,0,MPI_COMM_WORLD);


/* The master host display the sorted array */
int len = sizeof(elmnts)/sizeof(int);
if(myrank == 0) {

    printf("\nSorted array :\n");
    for (i=0;i<len;i++) {
        printf("%d; ",relmnts[i]); 
    }

    printf("\n");

}


          free(elmnts); free(relmnts); free(wspace); 
      MPI_Finalize(); 
} 

/* This is the CompareSplit function */ 
CompareSplit(int nlocal, int *elmnts, int *relmnts, int *wspace,  int keepsmall){ 
    int i, j, k; 

        for (i=0; i<nlocal; i++) 
            wspace[i] = elmnts[i]; /* Copy the elmnts array into the wspace array */ 

        if (keepsmall) { /* Keep the nlocal smaller elements */ 
         for (i=j=k=0; k<nlocal; k++) { 
            if (j == nlocal || (i < nlocal && wspace[i] < relmnts[j])) 
                elmnts[k] = wspace[i++]; 
            else 
                elmnts[k] = relmnts[j++]; 
         } 
     } else { /* Keep the nlocal larger elements */ 
            for (i=k=nlocal-1, j=nlocal-1; k>=0; k--) { 
              if (j == 0 || (i >= 0 && wspace[i] >= relmnts[j])) 
                  elmnts[k] = wspace[i--]; 
          else 
              elmnts[k] = relmnts[j--]; 
        } 
     } 
} 
  • 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-01T01:05:42+00:00Added an answer on June 1, 2026 at 1:05 am

    Your segmentation fault comes from this line

        n = atoi(argv[1]);
    

    Since argv[1] contains the string (null), n gets a zero value. Change n to a custom integer like: n=5 and everything will work fine.

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

Sidebar

Related Questions

Very new to PHP/programming in general, and I've been trying to run a PHP
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
Very new to Android development, and I am importing a bitmap that is 799
im very new in cocoa development and I'm trying to load a Window. I
Am very new to Android programming, so sorry if this is a simple problem.
very new to crystal reports.. Question is that - In our .net application we
Very new to programming for iOS and Cocoa so please take it easy on
Very new to web programming still and I don't really have a specific question
I'm still very new to programming and I want to write the cleanest code
im very new at javascipt (im php developer) so im really confused trying to

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.