我使用openMPI在c编程.我的代码发布在下面.发生的事情是每次运行此程序时都会收到分段错误错误.我相信我已经通过使用这些printf语句来解决问题.分段似乎发生在MPI_Finalize()之后.任何帮助是极大的赞赏.
我收到的错误是:
[linuxscc003:10019] *** Process received signal ***
[linuxscc003:10019] Signal: Segmentation fault (11)
[linuxscc003:10019] Signal code: Address not mapped (1)
Run Code Online (Sandbox Code Playgroud)
和我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char** argv)
{
int i = 0; //index
int comm_sz, my_rank;
int part_sum = 0;
//size of the array is hard-coded in,
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if(my_rank == 0)
{
int* array;
//generate the array of n elements
printf("There are %d array elements\n", comm_sz);
array = (int*)malloc(sizeof(int*)*comm_sz);
for(i = 0; i < comm_sz; i++)
{
//we don't want to count zero in here
//nobody likes zero
array[i] = (i+1);
}
for(i = 1; i < comm_sz; i++)
{
MPI_Send(&array[i], sizeof(int*), MPI_INT, i, 0, MPI_COMM_WORLD);
}
//part_sum = 1;
free(array);
printf("freed array!\n");
}
if(my_rank != 0)
{
MPI_Recv(&part_sum, sizeof(int*), MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("proc %d out of %d, I have received %d!\n", my_rank, comm_sz, part_sum);
}
printf("proc %d signing off!\n",my_rank);
MPI_Finalize();
printf("proc %d signed off!\n",my_rank);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这一行:
array = (int*)malloc(sizeof(int*)*comm_sz);
Run Code Online (Sandbox Code Playgroud)
sizeof(int*)应该是sizeof(int).
在这一行:
MPI_Send(&array[i], sizeof(int*), MPI_INT, i, 0, MPI_COMM_WORLD);
Run Code Online (Sandbox Code Playgroud)
sizeof(int*)应该是1.您指定MPI_INT要发送多少s(而不是多少字节). sizeof(int)可能是4或8,所以你正在重读你的缓冲区.
在这一行:
MPI_Recv(&part_sum, sizeof(int*), MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
Run Code Online (Sandbox Code Playgroud)
sizeof(int*) 应该是1,同样的事情.
段错误可能MPI_Finalize()在某个进程之前发生,您认为它发生在其他进程已完成之后MPI_Finalize().或者有可能因为你part_sum在堆栈上覆盖了被破坏的堆栈,直到MPI_Finalize()被调用才会导致问题.
| 归档时间: |
|
| 查看次数: |
3124 次 |
| 最近记录: |