Click here to Skip to main content
15,894,050 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a code of merge sort at MPI:

C++
#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) {

	/********** Create and populate the array **********/

	int n = atoi(argv[1]);
	//int *original_array = malloc(n * sizeof(int));
	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");

	/********** 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 = reinterpret_cast<int*>(malloc(n * 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 = reinterpret_cast<int*>(malloc(n * 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 = reinterpret_cast<int*>(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);
		delete[] sorted;
		delete[] other_array;
		//free(other_array);

	}

	/********** Clean up rest **********/
	//free(original_array);
	//free(sub_array);
	//free(tmp_array);
	delete[] original_array;
	delete[] sub_array;
	delete[] tmp_array;

	/********** Finalize MPI **********/
	MPI_Barrier(MPI_COMM_WORLD);
	MPI_Finalize();

	system("pause");

}

/********** 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);

	}

}


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.
Posted
Updated 18-Nov-16 2:24am
Comments
jeron1 17-Nov-16 15:16pm    
Sounds like it's a good time to learn how to use a debugger. Where you can step through the code and see exactly where it fails.
Member 12855422 17-Nov-16 15:20pm    
You are right! I found some mistakes when debug code, it's LNK2019. How can I fix it?
jeron1 17-Nov-16 16:00pm    
You were not actually debugging because no exe was built, what you're seeing is a linker error, something that happens during the build process. Try posting the whole error.
[no name] 17-Nov-16 16:17pm    
https://msdn.microsoft.com/en-us/library/799kze2z.aspx
Philippe Mori 17-Nov-16 16:48pm    
Don't mix C and C++ memory allocation. If you code in C++, then use new and if you code in C, then don't use any C++...

In particular, it is undefined behavior if you call delete or allocation made with malloc.

1 solution

At first: dont mix malloc and delete. Use new and delete, because they have the modern commands for C++.

Second: your arrays for sorting arent initialized.

Third and last: learn to use the debuggeron your IDE. Watch some videos on Youtube for learning it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900