Skip to main content
Sign in
Snippets Groups Projects
Commit 205799e6 authored by 13513044's avatar 13513044
Browse files

dikit lagi

parent 4388a23b
Branches
No related tags found
No related merge requests found
No preview for this file type
File added
#include "mpi.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
int main( int argc, char* argv[] )
{
double starttime, endtime;
int t, i, j, c, d;
int proceso_id;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int namelen;
int numprocsused;
// Intiating parallel part
MPI_Status stat;
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &numprocsused);
MPI_Comm_rank(MPI_COMM_WORLD, &proceso_id);
MPI_Get_processor_name(processor_name, &namelen);
unsigned int receivedElement;
if(proceso_id == 0) {
// if it is main process
int size;
scanf("%d", &size);
unsigned int *array = malloc(sizeof(unsigned int) * size);
for (i=0;i<size;i++)
array[i] = rand() % size;
// starting time calculation of the sort
starttime = MPI_Wtime();
// min and max values are got
unsigned int min = array[0];
unsigned int max = array[0];
for(i=0; i < size; i++) {
if(array[i] < min) { min = array[i]; }
if(array[i] > max) { max = array[i]; }
}
// calculating how many numbers each bucket/process will get numbers
int *elementQtyArray = malloc (sizeof(int)*numprocsused);
// default values
for(d=1; d < numprocsused; d++) {
elementQtyArray[d] = 0;
}
int pridetas;
for(d=0; d < size; d++) {
int increaseOf = max/(numprocsused-1);
int iteration = 1;
pridetas = 0;
for(j = increaseOf; j <= max; j = j + increaseOf) {
if(array[d] <= j) {
elementQtyArray[iteration]++;
pridetas = 1;
break;
}
iteration++;
}
if (!pridetas)
elementQtyArray[iteration-1]++;
}
for(i=1; i<numprocsused; i++) {
MPI_Send(&elementQtyArray[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
// doing the same, this time sending the numbers
for(d=0; d < size; d++) {
int increaseOf = max/(numprocsused-1);
int iteration = 1;
int issiunte = 0;
for (j = increaseOf; j <= max; j = j + increaseOf) {
if(array[d] <= j) {
MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration, 1, MPI_COMM_WORLD);
issiunte = 1;
break;
}
iteration++;
}
if (!issiunte) {
MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration-1, 1, MPI_COMM_WORLD);
}
}
// Getting back results and adding them to one array
int lastIndex = 0; int indexi = 0;
for(i=1; i < numprocsused; i++) {
unsigned int * recvArray = malloc (sizeof(unsigned int)*elementQtyArray[i]);
MPI_Recv(&recvArray[0], elementQtyArray[i], MPI_UNSIGNED, i, 2, MPI_COMM_WORLD, &stat);
if(lastIndex == 0) {
lastIndex = elementQtyArray[i];
}
for(j=0; j<elementQtyArray[i]; j++) {
array[indexi] = recvArray[j];
indexi++;
}
}
// stoping the time
endtime = MPI_Wtime();
// showing results in file
for(c = 0 ; c < size ; c++) {
printf("%d\n", array[c]);
}
// sorting results
printf("it took %f seconds \n", endtime-starttime);
printf("Numbers: %d \n", size);
printf("Processes: %d \n", numprocsused);
} else {
// if child process
int elementQtyUsed; // kiek elementu si gija gauja is tevinio proceso
// --- getting the number of numbers in the bucket
MPI_Recv(&elementQtyUsed, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat);
unsigned int *localArray = malloc (sizeof(unsigned int)*elementQtyUsed); // initiating a local bucket
int li;
// --- getting numbers from the main process
for(li = 0; li < elementQtyUsed; li++) {
MPI_Recv(&receivedElement, 1, MPI_UNSIGNED, 0, 1, MPI_COMM_WORLD, &stat);
localArray[li] = receivedElement;
}
// --- sorting the bucket
for (c = 1 ; c <= elementQtyUsed - 1; c++) {
d = c;
while ( d > 0 && localArray[d] < localArray[d-1]) {
t = localArray[d];
localArray[d] = localArray[d-1];
localArray[d-1] = t;
d--;
}
}
// --- sending back sorted array
MPI_Send(localArray, elementQtyUsed, MPI_UNSIGNED, 0, 2, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
#daftar host
localhost
167.205.35.25
167.205.35.26
167.205.35.28
167.205.35.29
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment