diff --git a/bucketsort b/bucketsort new file mode 100755 index 0000000000000000000000000000000000000000..ee64757457fee41b229fb2cd763c25f103ed3b3c Binary files /dev/null and b/bucketsort differ diff --git a/bucketsort.c b/bucketsort.c index abf4d3c1807f6d74a8a6fe8e1cc557c7fcac2bef..8d835c7c9e5aa0423fd28cee50fba1d4866e0b34 100644 --- a/bucketsort.c +++ b/bucketsort.c @@ -1,5 +1,7 @@ -include "mpi.h" +#include "mpi.h" #include <stdlib.h> +#include <stdio.h> +#include <time.h> void insertionSort(int n, int* array) { @@ -49,30 +51,107 @@ int main() { double time_bucket= 0.0; double time_insert= 0.0; + int* bucket; + int* array; + float div; + float upperbound; + int n, c, d, t; if(rank==0) { - printf("Enter number of elements\n"); - scanf("%d", &n); - - printf("Enter %d integers\n", n); - int *array = (int*)malloc(sizeof(int)*n); - - for (c = 0; c < n; c++) { - scanf("%d", &array[c]); + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + array =(int*)malloc(sizeof(int)*n); + srand(time(NULL)); + for (c = 0; c < n; c++) { + array[c] = rand()%100; + } } - float div = (float)(max(array, n) - min(array, n)/(float)size); - printf("%f", div); - - - insertionSort(n, array); - printf("Sorted list in ascending order:\n"); - - for (c = 0; c <= n - 1; c++) { - printf("%d\n", array[c]); + MPI_Barrier(MPI_COMM_WORLD); + time_bucket -= MPI_Wtime(); + MPI_Bcast(&n, 1, MPI_INT, 0 , MPI_COMM_WORLD); + + + if(rank!=0) { + array=(int*)malloc(sizeof(int)*n); } + + + MPI_Bcast(array, n, MPI_INT, 0 , MPI_COMM_WORLD); + + + + bucket = (int*)malloc(sizeof(int)*n); + + int i; + + div = (float)(max(array, n) - min(array, n))/(float)size; + if(div==0) div=1; + int j=0; + for(i=0; i<n; i++) { + int bucket_no = (int)((array[i]-min(array,n))/div); + if(rank==bucket_no) { + bucket[j]=array[i]; + j++; + } + else if (bucket_no>=size) { + if (rank==size-1){ + bucket[j]=array[i]; + j++; + } + } + } + + insertionSort(j,bucket); + + int *result; + if(rank==0) { + result = (int*)malloc(sizeof(int) * n); + } + + int *counts = (int*) malloc (sizeof(int) * size); + + // Each process tells the root how many elements it holds + MPI_Gather(&j, 1, MPI_INT, counts, 1, MPI_INT, 0, MPI_COMM_WORLD); + + // Displacements in the receive buffer for MPI_GATHERV + int *disps = (int*) malloc (sizeof(int) * size); + // Displacement for the first chunk of data - 0 + for (i = 0; i < size; i++) + disps[i] = (i > 0) ? (disps[i-1] + counts[i-1]) : 0; + + // Collect everything into the root + MPI_Gatherv(bucket, j, MPI_INT, + result, counts, disps, MPI_INT, 0, MPI_COMM_WORLD); + + MPI_Barrier(MPI_COMM_WORLD); + time_bucket += MPI_Wtime(); + + if (rank==0) { + + printf("Sorted list in ascending order with BucketSort:\n"); + + for (c = 0; c <= n - 1; c++) { + printf("%d ", result[c]); + } + printf("\nTime : %lf\n", time_bucket); } + + if(rank==0) { + time_insert -= MPI_Wtime(); + insertionSort(n, array); + time_insert+= MPI_Wtime(); + printf("Sorted list in ascending order with Insertion Sort:\n"); + + for (c = 0; c <= n - 1; c++) { + printf("%d ", result[c]); + } + printf("\nTime : %lf\n", time_insert); + } + MPI_Finalize(); -return 0; + return 0; } diff --git a/mpi_hostfile b/mpi_hostfile new file mode 100644 index 0000000000000000000000000000000000000000..1aab781c29869ebddbe342b46de587fe30d3c24d --- /dev/null +++ b/mpi_hostfile @@ -0,0 +1,7 @@ +#daftar host +localhost +167.205.35.26 +167.205.35.28 +167.205.35.29 +167.205.35.30 +167.205.35.31