[英]MPI MergeSort Signal: Abort trap: 6 (6)
I am new to MPI.我是 MPI 的新手。 My sequential version of the code is the same of this, except for the MPI functions and the part about the rank.
我的代码顺序版本与此相同,除了 MPI 函数和有关排名的部分。 I was trying to implement a parallel version of merge sorting.
我试图实现合并排序的并行版本。 My parallel implementation is the following:
我的并行实现如下:
//MPI MERGE SORT
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<mpi.h>
void merge(int arr[], int indexA, int indexB, int end, int arrOut[]);
void mergeSort(int arr[], int inf, int sup, int arrOut[]);
int main(int argc, char** argv){
int rank, n_ranks;
int size;
int *sub_array, *temp;
int N = 10;
int my_array[N];
int outputArray[N];
int length = sizeof(my_array) / sizeof(my_array[0]);
srand(time(NULL));
int i;
for (i=0; i<N; i++){
my_array[i]=rand()%100 + 1;
}
//print the array
for (i=0; i<N; i++){
printf("%d ", my_array[i]);
}
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &n_ranks);
size=N/n_ranks;
sub_array=malloc(size*sizeof(int));
temp=malloc(size*sizeof(int));
MPI_Scatter(my_array, size, MPI_INT, sub_array, size, MPI_INT, 0, MPI_COMM_WORLD);
mergeSort(sub_array, 0, length-1, temp);
MPI_Gather(sub_array, size, MPI_INT, outputArray, size, MPI_INT, 0, MPI_COMM_WORLD);
if(rank==0){
int *temp_array=malloc(N*sizeof(int));
mergeSort(outputArray, 0, length-1, temp_array);
for(i=0; i<N; i++){
printf("%d ", temp_array[i]);
}
free(temp_array);
}
free(my_array);
free(sub_array);
free(temp);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
void merge(int arr[], int indexA, int indexB, int end, int arrOut[]){
int i=indexA, j=indexB, k=indexA;
while(i<=indexB-1 && j<=end){
if(arr[i]<arr[j]){
//i=i+1;
arrOut[k]=arr[i++];
}
else{
//j=j+1;
arrOut[k]=arr[j++];
}
k++;
}
while(i<=indexB-1){
//i++;
arrOut[k]=arr[i++];
k++;
}
while(j<=end){
//j++;
arrOut[k]=arr[j++];
k++;
}
for(i=indexA; i<=end; i++)
arr[i]=arrOut[i];
}
void mergeSort(int arr[], int inf, int sup, int arrOut[]){
int medium;
if(inf<sup){
medium=(inf+sup)/2;
mergeSort(arr, inf, medium, arrOut);
mergeSort(arr, medium+1, sup, arrOut);
merge(arr, inf, medium+1, sup, arrOut);
}
}
It seems working, in the sense that the array is sorted, but I have these errors after the printing of the array:从数组已排序的意义上讲,它似乎有效,但在打印数组后出现这些错误:
[Brunos-MBP-4:03022] Signal: Abort trap: 6 (6)
[Brunos-MBP-4:03022] Signal code: (0)
[Brunos-MBP-4:03022] [ 0] 0 libsystem_platform.dylib 0x00007fff2036ad7d _sigtramp + 29
[Brunos-MBP-4:03022] [ 1] 0 ??? 0x000000000000400a 0x0 + 16394
[Brunos-MBP-4:03022] [ 2] 0 libsystem_c.dylib 0x00007fff20279720 abort + 120
[Brunos-MBP-4:03022] [ 3] 0 libsystem_malloc.dylib 0x00007fff2015a430 has_default_zone0 + 0
[Brunos-MBP-4:03022] [ 4] 0 libsystem_malloc.dylib 0x00007fff2015d4c8 malloc_report + 151
[Brunos-MBP-4:03022] [ 5] 0 a.out 0x000000010be0fc1f main + 831
[Brunos-MBP-4:03022] [ 6] 0 libdyld.dylib 0x00007fff20341631 start + 1
[Brunos-MBP-4:03022] [ 7] 0 ??? 0x0000000000000001 0x0 + 1
[Brunos-MBP-4:03022] *** End of error message ***
Your design does not make sense.你的设计没有意义。 You scatter
my_array
into sub_array
, but then everyone sorts my_array
.您将
my_array
分散到sub_array
中,然后每个人都对my_array
进行排序。 And then you gather sub_array
, which hasn't changed, into output_array
, so the gather/scatter basically does a copy from my_array
into output_array
.然后您将未更改的
output_array
sub_array
,因此收集/分散基本上是从my_array
复制到output_array
。 And then you sort the whole array on rank zero.然后你将整个数组排序为零。
sub_array
?sub_array
进行排序? So your parallelism does not gain you anything.因此,您的并行性不会为您带来任何好处。 Sorting the subarray is a good first step, but then instead of a gather, do a recursive merge.
对子数组进行排序是一个很好的第一步,但随后不是聚集,而是进行递归合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.