I am writing a program with C ++ that uses MPI. A simplified version of my code
#include <iostream> #include <fstream> #include <cstdlib> #include <mpi.h> #define RNumber 3000000 //Number of loops to go using namespace std; class LObject { /*Something here*/ public: void FillArray(long * RawT){ /*Does something*/ for (int i = 0; i < RNumber; i++){ RawT[i] = i; } } }; int main() { int my_rank; int comm_sz; MPI_Init(NULL, NULL); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &comm_sz); LObject System; long rawT[RNumber]; long * Times = NULL; if (my_rank == 0) Times = (long*) malloc(comm_sz*RNumber*sizeof(long)); System.FillArray(rawT); if (my_rank == 0) { MPI_Gather(rawT, RNumber, MPI_LONG, Times, RNumber, MPI_LONG, 0, MPI_COMM_WORLD); } else { MPI_Gather(rawT, RNumber, MPI_LONG, Times, RNumber, MPI_LONG, 0, MPI_COMM_WORLD); } MPI_Finalize(); return 0; };
The program compiles fine, but when the error occurs, segmentation errors. Message
================================================================================= = BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES = EXIT CODE: 11 = CLEANING UP REMAINING PROCESSES = YOU CAN IGNORE THE BELOW CLEANUP MESSAGES ================================================================================= APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
When I decrease RNumber , the program works fine. Maybe someone can explain what exactly is going wrong? Am I trying to allocate too much space for an array? If this happens, will this problem be solved by storing the results in a file instead of an array?
If possible, could you give broad comments on what I am doing wrong.
Thank you for your time and effort!