the_martian
the_martian

Reputation: 634

MPI debug, segmentation fault issue

I'm trying to adapt this program to use MPI, at this stage I am just trying to set up a couple of basic things. But with what I have now I'm running into a segmentation fault error.

Here is the code:

#include <mpi.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main(int argc, char *argv[]) {
  int id;               /* Process rank */
  int p;                /* Number of processes */

  double stop = (double)atol(argv[1]);

  MPI_Init (&argc, &argv);
  MPI_Comm_rank (MPI_COMM_WORLD, &id);
  MPI_Comm_size (MPI_COMM_WORLD, &p);

  assert(stop >= 1.0);

  int result = 0;

  for (double x = 0.0; x < stop; x += 1.0) {
    double tmp = sin(x);
    double tmp2 = tmp*tmp;
    int z = (int)(tmp2*10000.0);

    result = (result + z)%10000; // 0<=result<10000

  }
 MPI_Finalize();

}

The error:

I run it like this:

mpicc -o pin pin.c -lm
mpiexec -n 2 ./pin

And get this

mpiexec noticed that process rank 1 with PID 7280 on node (my-computer) exited on signal 11 (Segmentation fault).

Upvotes: 0

Views: 88

Answers (1)

Gilles Gouaillardet
Gilles Gouaillardet

Reputation: 8395

You submit like this

mpiexec -n 2 ./pin

and then your program

double stop = (double)atol(argv[1]);

at that time, your program crashes because argv[1] is NULL. You should first double check argc > 1, and to be on the safe side, you should do that after MPI_Init(&argc, &argv);

Meanwhile, you can run your application as intended

mpiexec -n 2 ./pin 4

Upvotes: 1

Related Questions