Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (4)
File added
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 start_time, end_time;
int i, j, a, b, temp;
int world_rank;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int namelen;
int nprocess;
// Intiating parallel part
MPI_Status stat;
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &nprocess);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Get_processor_name(processor_name, &namelen);
unsigned int receivedElement;
if(world_rank == 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
start_time = 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)*nprocess);
// default values
for(b=1; b < nprocess; b++) {
elementQtyArray[b] = 0;
}
if(nprocess>1){
int boundary1;
for(b=0; b < size; b++) {
int increaseOf = max/(nprocess-1);
int k = 1;
boundary1 = 0;
for(j = increaseOf; j <= max; j += increaseOf) {
if(array[b] <= j) {
elementQtyArray[k]++;
boundary1 = 1;
break;
}
k++;
}
if (!boundary1)
elementQtyArray[k-1]++;
}
for(i=1; i<nprocess; i++) {
MPI_Send(&elementQtyArray[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
// doing the same, this time sending the numbers
for(b=0; b < size; b++) {
int increaseOf = max/(nprocess-1);
int k = 1;
int boundary2 = 0;
for (j = increaseOf; j <= max; j += increaseOf) {
if(array[b] <= j) {
MPI_Send(&array[b], 1, MPI_UNSIGNED, k, 1, MPI_COMM_WORLD);
boundary2 = 1;
break;
}
k++;
}
if (!boundary2) {
MPI_Send(&array[b], 1, MPI_UNSIGNED, k-1, 1, MPI_COMM_WORLD);
}
}
// Getting back results and adding them to one array
int lastIndex = 0; int indexi = 0;
for(i=1; i < nprocess; 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++;
}
}
} else {
for (a = 0 ; a <size; a++) {
b = a;
while ( b > 0 && array[b] < array[b-1]) {
temp = array[b];
array[b] = array[b-1];
array[b-1] = temp;
b--;
}
}
}
// stoping the time
end_time = MPI_Wtime();
// showing results in file
for(a = 0 ; a < size ; a++) {
printf("%d\n", array[a]);
}
// sorting results
printf("Time : %f seconds \n", end_time-start_time);
printf("Numbers: %d \n", size);
printf("Processes: %d \n", nprocess);
} 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 (a = 1 ; a <= elementQtyUsed - 1; a++) {
b = a;
while ( b > 0 && localArray[b] < localArray[b-1]) {
temp = localArray[b];
localArray[b] = localArray[b-1];
localArray[b-1] = temp;
b--;
}
}
// --- sending back sorted array
MPI_Send(localArray, elementQtyUsed, MPI_UNSIGNED, 0, 2, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
/* Copyrights http://www.programmingsimplified.com/c/source-code/c-program-insertion-sort */
/* insertion sort ascending order */
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}
\ No newline at end of file
N = Element array
M = Banyak proses
1) N = 50.000
______________________
| M | Waktu (seconds)|
|____|________________|
| 1 | 6.856739 |
| 4 | 0.672057 |
| 8 | 0.185040 |
| 16 | 0.391889 |
| 32 | 0.390784 |
|____|________________|
2) N = 100.000
______________________
| M | Waktu (seconds)|
|____|________________|
| 1 | 14.909016 |
| 4 | 2.744831 |
| 8 | 0.440783 |
| 16 | 0.294712 |
| 32 | 0.686711 |
|____|________________|
3) N = 200.000
______________________
| M | Waktu (seconds)|
|____|________________|
| 1 | 54.440212 |
| 4 | 6.701494 |
| 8 | 1.613141 |
| 16 | 0.631648 |
| 32 | 2.975302 |
|____|________________|
4) N = 400.000
______________________
| M | Waktu (seconds)|
|____|________________|
| 1 | 219.474459 |
| 4 | 25.008242 |
| 8 | 7.521053 |
| 16 | 3.224539 |
| 32 | 3.344244 |
|____|________________|
#daftar host
localhost
167.205.35.25
167.205.35.26
167.205.35.28
167.205.35.29
167.205.35.30
167.205.35.31