My code below attempts to map a set of integers in an array with multiple processors in parallel. I am confused why it keeps getting a segmentation fault. I am using Ubuntu 17.10. Any help would be greatly appreciated.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define IN 16 //input size
int main(int argc, char** argv){
// Initialize the MPI environment
MPI_Init(&argc, &argv);
MPI_Win win;
// Find out rank, size
int id; //process id
MPI_Comm_rank(MPI_COMM_WORLD, &id);
int p; //number of processes
MPI_Comm_size(MPI_COMM_WORLD, &p);
srand(time(0));
int mapper[IN];
int toMap[IN];
int result[IN];
if(id==0){
for(int n=0; n<IN; n++){ //predecided map values
toMap[n] = rand()%IN;
mapper[n] = rand()%101;
printf("[%d, %d]", n, mapper[n]);
}
printf("\n");
}
int d = IN/p;
int i = id*d;
while(i<id*d+d && i<IN){
result[i] = mapper[toMap[i]];
i++;
}
MPI_Barrier(MPI_COMM_WORLD);
if(id == 0){
for(int n=0; n<IN; n++){ //map results
printf("[%d -> %d]\n", toMap[n], result[n]);
}
}
MPI_Finalize();
return 0;
}
When I execute the program using:
mpiexec -np 2 parallelMap
I get the error:
[sanjiv-Inspiron-5558:00943] *** Process received signal ***
[sanjiv-Inspiron-5558:00943] Signal: Segmentation fault (11)
[sanjiv-Inspiron-5558:00943] Signal code: Address not mapped (1)
[sanjiv-Inspiron-5558:00943] Failing at address: 0x7ffecfc33a90
[sanjiv-Inspiron-5558:00943] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x13150)[0x7f8c74400150]
[sanjiv-Inspiron-5558:00943] [ 1] parallelMap(+0xbf2)[0x5652d5561bf2]
[sanjiv-Inspiron-5558:00943] [ 2] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7f8c7402e1c1]
[sanjiv-Inspiron-5558:00943] [ 3] parallelMap(+0x99a)[0x5652d556199a]
[sanjiv-Inspiron-5558:00943] *** End of error message ***
--------------------------------------------------------------------------
mpiexec noticed that process rank 1 with PID 0 on node sanjiv-Inspiron-5558 exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------