【发布时间】:2017-07-30 20:58:20
【问题描述】:
我尝试在 Mac OS Sierra 上运行此代码,但始终出现分段错误 11 错误。我最近开始学习 Mpi 和 C。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
void merge_sort(int l,int r,int part[]){
if ( !(l<r) ) return ;
int m = l + (r - l) / 2;
merge_sort(l,m,part);
merge_sort(m+1,r,part);
int i = l, j = m+1, k = 0;
int a[10000];
while (i<=m && j<=r) {
if (part[i]>part[j]) {a[k] = part[j];j++;}
else {a[k] = part[i];i++;}
k++;
}
while (i<=m) {a[k] = part[i];i++;k++;}
while (j<=r) {a[k] = part[j];j++;k++;}
for (i=0;i<k;i++) part[i+l] = a[i];
}
int read(int size,int rank,int part[]){
int local_sz=0;
int n;
int data[1000000];
if (rank==0){
FILE *f = fopen("numbers.txt","r");
fscanf(f,"%d",&n);
int i;
for (i=0;i<n;i++)fscanf(f,"%d",&data[i]);
local_sz = n/size;
}
MPI_Bcast(&local_sz,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Scatter(data,local_sz,MPI_INT,part,local_sz,MPI_INT,0,MPI_COMM_WORLD);
return local_sz;
}
int main(int argc,char* argv[]){
int comm_sz,my_rank,local_sz;
int part[10000];
int *other_part[10000];
int i,j;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
printf("yess\n");
local_sz=read(comm_sz,my_rank,part);
merge_sort(0,local_sz-1,part);
for (i=0;i<local_sz;i++)
printf("%d ", *(part+i));
printf("\n");
for (i=0;i<comm_sz;i++){
if ( i%2 == 0 ){
if ( my_rank % 2 == 0 ){
if(my_rank+1<comm_sz){
MPI_Recv(other_part,local_sz,MPI_INT,my_rank+1,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
int b[local_sz*2];
for (j = 0; j < local_sz; j++ ){
b[j]=part[j];
b[j+local_sz]=other_part[j];
}
merge_sort(0,local_sz*2-1,b);
for (j = 0; j < local_sz; j++ ){
part[j] = b[j];
other_part[j] = b[j+local_sz];
}
free(b);
MPI_Send(other_part,local_sz,MPI_INT,my_rank+1,0,MPI_COMM_WORLD);
}
}
else{
if(my_rank-1 >= 0 ){
MPI_Send(part,local_sz,MPI_INT,my_rank-1,0,MPI_COMM_WORLD);
MPI_Recv(part,local_sz,MPI_INT,my_rank-1,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}
}
}
else {
if ( my_rank % 2 == 1){
if(my_rank+1<comm_sz){
MPI_Recv(other_part,local_sz,MPI_INT,my_rank+1,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
int b[local_sz*2];
for (j = 0; j < local_sz; j++ ){
b[j]=part[j];
b[j+local_sz]=other_part[j];
}
merge_sort(0,local_sz*2-1,b);
for (j = 0; j < local_sz*2; j++)printf("%d ", b[j]);
printf("\n");
for (j = 0; j < local_sz; j++ ){
part[j] = b[j];
other_part[j] = b[j+local_sz];
}
free(b);
MPI_Send(other_part,local_sz,MPI_INT,my_rank+1,0,MPI_COMM_WORLD);
}
}
else {
if(my_rank-1>=0){
MPI_Send(part,local_sz,MPI_INT,my_rank-1,0,MPI_COMM_WORLD);
MPI_Recv(part,local_sz,MPI_INT,my_rank-1,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}
}
}
}
FILE *file;
char name[100]="",t[2];
t[0] = ((char)my_rank+48);
t[1] = '\0';
strcat(name,"sorted");
strcat(name,t);
strcat(name,".txt");
file = fopen(name,"w");
fprintf(file,"Process %d: ", my_rank);
for (i=0;i<local_sz;i++)fprintf(file, "%d ", part[i]);
MPI_Finalize();
return 0;
}
我对 C 不是很熟悉,很可能是我错误地使用了 malloc 和/或地址和指针,因此它可能很简单。
抱歉代码量太大,但我认为最好提供所有代码以进行适当的调试。
【问题讨论】:
-
这样做:逐步删除部分代码,直到您没有任何分段错误并且您可能会找到它的原因。
-
学习调试器会更有效。但是,您确实需要按照@nbro 的建议使用minimal reproducible example 提出正确的问题。
标签: c macos segmentation-fault mpi