So I need to implement a vector addition function in parallel using MPI in C. Unfortunately, when I run it, it prints a trace of lots of memory locations and then this message:
==================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 2419 RUNNING AT hbaum-pc
= EXIT CODE: 6
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Aborted (signal 6)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
Here is my function code:
double* vector_vector_addition_parallel(double* a, double* b, int length)
{
int rank, size, error;
double* result = (double*)malloc(sizeof(double)*length);
error = MPI_Init(NULL,NULL);
error = MPI_Comm_size(MPI_COMM_WORLD,&size);
error = MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int sublist_length = (int)(length/size);
double* sub_a = (double*)malloc(sizeof(double) * sublist_length);
double* sub_b = (double*)malloc(sizeof(double) * sublist_length);
error = MPI_Scatter(a, sublist_length, MPI_DOUBLE, sub_a, sublist_length, MPI_DOUBLE, 0, MPI_COMM_WORLD);
error = MPI_Scatter(b, sublist_length, MPI_DOUBLE, sub_b, sublist_length, MPI_DOUBLE, 0, MPI_COMM_WORLD);
double* buffer = (double*)malloc(sizeof(double)*sublist_length);
for(int i = 0; i < sublist_length; i++)
{
buffer[i] = sub_a[i] + sub_b[i];
}
error = MPI_Gather(buffer,sublist_length,MPI_DOUBLE,result,length,MPI_DOUBLE,0,MPI_COMM_WORLD);
error = MPI_Finalize();
return result;
}
And here is the code that calls it:
int main(int argc, char** argv)
{
double a[8] = {1.0,3.0,5.0,7.0,9.0,11.0,13.0,15.0};
double b[8] = {2.0,4.0,6.0,8.0,10.0,12.0,14.0,16.0};
double* vec = vector_vector_addition_parallel(a,b,8);
return 0;
}
I compile it with
mpicc <source_file>
and run it with
mpiexec -n 4 <path_to_executable>
I also tried using gdb to debug the code but according to gdb, there's no problem and it's absolutely fine. When I use printf to output the vector, it even prints the correct vector when I run the program via gdb.
I think there's something wrong with how I've used MPI_Gather as when I comment it out, the code will run without a segmentation fault although obviously not the correct answer as I need to use MPI_Gather in order to get a result.
How can I improve my code in order to not get a segmentation fault?
Indeed, your
MPI_Gather
is the issue. The receive count parameter should be the number of elements received from any single process. Thus, you should passsublist_length
instead oflength
for therecvcount
, ie: