Click here to Skip to main content
15,946,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Help! I have a code, but I don't know where array numbers come to the programm. I want to create array of random numbers that in programm sort, how I can do this?

C++
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <iostream>
#include <windows.h>

//#define N 2

int compare(const void *, const void *);
void mergeArrays(int *, int *, int *, int, int);
int computeNeighbor(int, int, int);

int main(int argc, char ** argv)
{
	setlocale(LC_ALL, "Russian");

	int i, j, n, N, rank, size;
	MPI_Status status;

	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	do {
		std::cout << "Enter mass lenght: ";
		std::cin >> N;
	} while (N > 5000000);

	int numElements = N / size;
	int * arr = (int *)malloc(sizeof(int)*numElements);
	int * temp = (int *)malloc(sizeof(int)*numElements * 2);
	int * recvArr = (int *)malloc(sizeof(int)*numElements);
	int * fullArr = NULL;

	if (rank == 0)
		fullArr = (int *)malloc(sizeof(int)*N);

	//Fill array in descending order
	int start = rank*numElements;
	int end = start + numElements;
	for (i = 0, j = start; i < numElements; i++, j++)
		arr[i] = N - j;

	//Sort array with odd-even sort

	//Sort local values
	qsort(arr, numElements, sizeof(int), compare);

	//Begin iterations
	for (n = 0; n < size; n++) {
		MPI_Barrier(MPI_COMM_WORLD);
		int neighbor = computeNeighbor(n, rank, size);

		if (neighbor >= 0 && neighbor < size)
		{
			//Send my values to my neighbor and receive values from my neighbor
			MPI_Sendrecv(arr, numElements, MPI_INT, neighbor, n,
				recvArr, numElements, MPI_INT, neighbor, n,
				MPI_COMM_WORLD, &status);

			//If my rank < my neighbor's rank, keep the smaller values
			if (rank < neighbor) {
				mergeArrays(arr, recvArr, temp, numElements, 1);
				//Else keep the larger values
			}
			else {
				mergeArrays(arr, recvArr, temp, numElements, 0);
			}
		}
	}

	MPI_Barrier(MPI_COMM_WORLD);
	MPI_Gather(arr, numElements, MPI_INT, fullArr, numElements, MPI_INT, 0, MPI_COMM_WORLD);

	if (rank == 0)
	{
		for (i = 0; i < N; i++)
			printf("%d ", fullArr[i]);
		printf("\n");
	}

	free((void *)arr);
	free((void *)recvArr);
	free((void *)temp);

	MPI_Finalize();
	system("pause");
	return 0;
}

int compare(const void * a, const void * b)
{
	if (*((int *)a) <  *((int *)b)) return -1;
	else if (*((int *)a) == *((int *)b)) return 0;
	else return 1;
}

int computeNeighbor(int phase, int rank, int size)
{
	int neighbor;
	if (phase % 2 != 0) {  //odd phase
		if (rank % 2 != 0) {  //odd rank
			neighbor = rank + 1;
		}
		else {  //even rank
			neighbor = rank - 1;
		}
	}
	else {  //even phase
		if (rank % 2 != 0) {  //odd rank
			neighbor = rank - 1;
		}
		else {  //even rank
			neighbor = rank + 1;
		}
	}

	if (neighbor < 0 || neighbor >= size)
		neighbor = -1;
	return neighbor;
}

void mergeArrays(int * arr, int * neighborArr, int * temp, int size, int low_or_high)
{
	int i, j, k;
	i = j = k = 0;

	//Assume arrays are already sorted
	for (i = 0, j = 0, k = 0; i < size * 2; i++)
	{
		if (j < size && k < size)
		{
			if (arr[j] < neighborArr[k])
			{
				temp[i] = arr[j];
				j++;
			}
			else {
				temp[i] = neighborArr[k];
				k++;
			}
		}
		else if (j < size) {
			temp[i] = arr[j];
			j++;
		}
		else {
			temp[i] = neighborArr[k];
			k++;
		}
	}

	if (low_or_high % 2 != 0)
		for (i = 0; i < size; i++)
			arr[i] = temp[i];
	else
		for (i = size, j = 0; j < size; i++, j++)
			arr[j] = temp[i];
}


What I have tried:

I don't know where in this code create's array.
Posted
Updated 27-Nov-16 10:21am

1 solution

There are four arrays created. Right after the line that calculates numElements there are four mallocs and those are allocating the arrays in memory.
 
Share this answer
 
Comments
Member 12855422 27-Nov-16 18:20pm    
Thank you! But I don't understand where numbers are creating. In this programm I see: "1 2 3 4 5 6" if N = 6, or "1 2 3" if N = 3, where can I change it to: "421 432 123" if N = 3 and etc?
Member 12855422 27-Nov-16 18:27pm    
Nevermind. Thank you!

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