使用MPI C ++进行代码合并排序时出错
问题描述:
我有一个代码 - 在c ++中合并排序数组,但它不起作用。
我的代码:
I have a code - merge sort array in c++, but it doesn't work.
My code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>
#include <mpi.h>
void merge(int *, int *, int, int, int);
void mergeSort(int *, int *, int, int);
void* malloc(size_t);
int main(int argc, char** argv) {
/********** Create and populate the array **********/
int n = atoi(argv[1]);
int *original_array = malloc(n * sizeof(int));
int c;
srand(time(NULL));
printf("This is the unsorted array: ");
for (c = 0; c < n; c++) {
original_array[c] = rand() % n;
printf("%d ", original_array[c]);
}
printf("\n");
printf("\n");
/********** Initialize MPI **********/
int world_rank;
int world_size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
/********** Divide the array in equal-sized chunks **********/
int size = n / world_size;
/********** Send each subarray to each process **********/
int *sub_array = malloc(size * sizeof(int));
MPI_Scatter(original_array, size, MPI_INT, sub_array, size, MPI_INT, 0, MPI_COMM_WORLD);
/********** Perform the mergesort on each process **********/
int *tmp_array = malloc(size * sizeof(int));
mergeSort(sub_array, tmp_array, 0, (size - 1));
/********** Gather the sorted subarrays into one **********/
int *sorted = NULL;
if (world_rank == 0) {
sorted == malloc(n * sizeof(int));
}
MPI_Gather(sub_array, size, MPI_INT, sorted, size, MPI_INT, 0, MPI_COMM_WORLD);
/********** Make the final mergeSort call **********/
if (world_rank == 0) {
int *other_array = malloc(n * sizeof(int));
mergeSort(sorted, other_array, 0, (n - 1));
/********** Display the sorted array **********/
printf("This is the sorted array: ");
for (c = 0; c < n; c++) {
printf("%d ", sorted[c]);
}
printf("\n");
printf("\n");
/********** Clean up root **********/
free(sorted);
free(other_array);
}
/********** Clean up rest **********/
free(original_array);
free(sub_array);
free(tmp_array);
/********** Finalize MPI **********/
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
/********** Merge Function **********/
void merge(int *a, int *b, int l, int m, int r) {
int h, i, j, k;
h = l;
i = l;
j = m + 1;
while ((h <= m) && (j <= r)) {
if (a[h] <= a[j]) {
b[i] = a[h];
h++;
}
else {
b[i] = a[j];
j++;
}
i++;
}
if (m < h) {
for (k = j; k <= r; k++) {
b[i] = a[k];
i++;
}
}
else {
for (k = h; k <= m; k++) {
b[i] = a[k];
i++;
}
}
for (k = l; k <= r; k++) {
a[k] = b[k];
}
}
/********** Recursive Merge Function **********/
void mergeSort(int *a, int *b, int l, int r) {
int m;
if (l < r) {
m = (l + r) / 2;
mergeSort(a, b, l, m);
mergeSort(a, b, (m + 1), r);
merge(a, b, l, m, r);
}
}
错误:
Error:
Severity Code Description Project File Line Suppression State
Error (active) a value of type "void *" cannot be used to initialize an entity of type "int *" ConsoleApplication2 c:\Users\Андрей\Desktop\ЛАБАТРИ\ConsoleApp2\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp 16
在这一行:
At this line:
int *original_array = malloc(n * sizeof(int));
请帮助!
什么我试过了:
我试图用另一种方式编写数组,但它不起作用。我试着编写像
Help please!
What I have tried:
I tried to write array another way but it doesn't work. I tried to write like
void* malloc(size_t);
但它不起作用。
答
使用C ++,你必须强制转换void *
返回值因为C ++使用强类型系统:
With C++, you must cast thevoid*
return value because C++ uses a strong type system:
int *original_array = reinterpret_cast<int*>(malloc(n * sizeof(int)));
但是,对于C ++,你应该使用 new
和 delete
代替:
However, with C++ you should use new
and delete
instead:
int *original_array = new int[n];