I am trying to write a simple method to read a file in parallel where each process will read a number of ints from a file in order to split the data to each process, but I get a segmentation fault and I cannot understand why or how to fix it. Here is the code I wrote:
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#define NUM_INTS 5
int main (int argc, char** argv) {
MPI_Init(&argc, &argv);
int i;
int rank,processes,name_len;
const int root=0;
int *buf;
char *filename = "file.txt";
MPI_File fh;
MPI_Status status;
MPI_Offset offset;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &processes);
MPI_Get_processor_name(processor_name, &name_len);
MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
buf = malloc(NUM_INTS * sizeof(int));
MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *)NULL, MPI_INFO_NULL);
offset = rank * NUM_INTS;
MPI_File_read_at(fh, offset, buf, NUM_INTS, MPI_INT, &status);
MPI_Barrier(MPI_COMM_WORLD);
MPI_File_close(&fh);
for (i=0;i<NUM_INTS;i++)
printf("rank %d data[%d] = %d\n", rank, i, buf[i]);
free(buf);
MPI_Finalize();
return 0;
}
The file contains 10 ints that I have tried to split it over 2 processes. I think the problem is in MPI_File_read_at because all prints work up to that line
Thanks in advance