私はこのコードをMac OS Sierraで実行しようとしていますが、ここではセグメンテーションフォールトエラーが常に発生します。私は最近MpiとCを学び始める。セグメンテーションフォールト:11 Mpi Mac OS
#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
デバッガを学ぶ方がずっと効果的です。しかし、あなたは[mcve]で適切な質問を出すために@nbroが提案するものを行う必要があります。 – Zulan