在C中使用MPI进行细分错误? [英] Segmentation fault error using MPI in C?
问题描述
因此,我需要在C中使用MPI并行实现矢量加法功能. 不幸的是,当我运行它时,它会打印出很多内存位置的痕迹,然后显示以下消息:
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
这是我的功能代码:
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;
}
这是调用它的代码:
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;
}
我用
mpicc <source_file>
并使用
mpiexec -n 4 <path_to_executable>
我也尝试使用gdb调试代码,但是根据gdb,这没有问题,而且绝对可以.当我使用printf输出矢量时,当我通过gdb运行程序时,它甚至会打印正确的矢量.
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.
我认为我使用MPI_Gather的方式有问题,因为当我注释掉它时,代码将运行而不会出现分段错误,尽管显然不是正确的答案,因为我需要使用MPI_Gather来获得结果.
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?
推荐答案
确实,您的MPI_Gather
是问题所在.接收计数参数应为从任何单个进程接收的元素数.因此,对于recvcount
,您应该传递sublist_length
而不是length
,即:
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 pass sublist_length
instead of length
for the recvcount
, ie:
MPI_Gather(buffer,sublist_length,MPI_DOUBLE,result,sublist_length,MPI_DOUBLE,0,MPI_COMM_WORLD);
这篇关于在C中使用MPI进行细分错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!