I have a code of merge sort at MPI:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>
#include "mpi.h"
#pragma comment (lib, "msmpi.lib")
using namespace std;
void merge(int *, int *, int, int, int);
void mergeSort(int *, int *, int, int);
int main(int argc, char** argv) {
int n = atoi(argv[1]);
int *original_array = reinterpret_cast<int*>(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");
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);
int size = n / world_size;
int *sub_array = reinterpret_cast<int*>(malloc(n * sizeof(int)));
MPI_Scatter(original_array, size, MPI_INT, sub_array, size, MPI_INT, 0, MPI_COMM_WORLD);
int *tmp_array = reinterpret_cast<int*>(malloc(n * sizeof(int)));
mergeSort(sub_array, tmp_array, 0, (size - 1));
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);
if (world_rank == 0) {
int *other_array = reinterpret_cast<int*>(malloc(n * sizeof(int)));
mergeSort(sorted, other_array, 0, (n - 1));
printf("This is the sorted array: ");
for (c = 0; c < n; c++) {
printf("%d ", sorted[c]);
}
printf("\n");
printf("\n");
delete[] sorted;
delete[] other_array;
}
delete[] original_array;
delete[] sub_array;
delete[] tmp_array;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
system("pause");
}
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];
}
}
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);
}
}
But when I start the programm it shutdown and it is all. How I can fix that?
What I have tried:
system("pause"); - don't work, console appear in 1-2 sec.
MPI connect.