所以我尝试使用MPI实现一个在C中执行并行向量向量加法的函数。
不幸的是,当我尝试运行它时,我收到一个很大的错误,表明存在分段错误。
这是系统内存回溯后的输出。
===================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 12581 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.
这是我的函数代码:
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;
}
我发现问题在于MPI_Gather调用,因为当我发表评论时,没有错误。但是,我显然需要使用MPI_Gather函数。
我也尝试使用gdb调试代码,但gdb完全没有看错。当我在gdb上运行它并使用printf()输出结果向量时,它会输出正确的结果!
我该怎么做才能阻止错误发生?
提前致谢