分段故障:11 Mpi Mac OS

时间:2017-03-09 18:21:47

标签: c macos segmentation-fault mpi

我尝试在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和/或地址和指针,因此它可能很简单。

很抱歉代码的数量,但我认为提供所有代码以便进行适当的调试会更好。

1 个答案:

答案 0 :(得分:0)

这里的原因是缺少numbers.txt文件。

确保创建一些输入文件&#34; numbers.txt&#34;代码所在的内部主管。

例如,输入以下内容:

> cat numbers.txt
1
2
3
4
5
6
7
8

在你的numbers.txt里面。之后。开始了。结果在这里。

> ./sample
yess
2

通常,您可以从其他人那里获得很好的建议,以熟悉调试器。我会帮你的。看看这里:

run
Process 2641 launched: '.../tmp/sample' (x86_64)
yess
Process 2641 stopped
* thread #1: tid = 0x144bc, 0x00007fffb89ff6a0 libsystem_c.dylib`flockfile + 4, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x68)
    frame #0: 0x00007fffb89ff6a0 libsystem_c.dylib`flockfile + 4
libsystem_c.dylib`flockfile:
->  0x7fffb89ff6a0 <+4>:  movq   0x68(%rdi), %rdi
    0x7fffb89ff6a4 <+8>:  addq   $0x8, %rdi
    0x7fffb89ff6a8 <+12>: popq   %rbp
    0x7fffb89ff6a9 <+13>: jmp    0x7fffb8a4898e            ; symbol stub for: pthread_mutex_lock
(lldb) bt
* thread #1: tid = 0x144bc, 0x00007fffb89ff6a0 libsystem_c.dylib`flockfile + 4, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x68)
  * frame #0: 0x00007fffb89ff6a0 libsystem_c.dylib`flockfile + 4
    frame #1: 0x00007fffb8a019d0 libsystem_c.dylib`fscanf + 156
    frame #2: 0x0000000100001399 sample`read(size=1, rank=0, part=0x00007fff5fbf5d80) + 121 at sample.c:31
    frame #3: 0x00000001000015a3 sample`main(argc=1, argv=0x00007fff5fbff9f0) + 179 at sample.c:52
    frame #4: 0x00007fffb898d255 libdyld.dylib`start + 1

感谢在调试器中运行代码,您可以轻松找到出错的地方 - &gt; sample.c第31行,您尝试从文件中读取数据。

所以,要坚强并开始学习如何使用gdb / lldb调试代码;)

玩得开心!