diff --git a/Laporan.txt b/Laporan.txt new file mode 100644 index 0000000000000000000000000000000000000000..b2ba5c98e71fd8e8244d2cd741cdf20835d299f3 --- /dev/null +++ b/Laporan.txt @@ -0,0 +1,41 @@ +Jumlah Proses = 1 + +N = 50000, Total time = 0.0008 +N = 100000, Total time = 0.0017 +N = 200000, Total time = 0.0155 +N = 400000, Total time = 0.0062 + +Jumlah Proses = 2 + +N = 50000, Total time = 0.8417 +N = 100000, Total time = 3.3689 +N = 200000, Total time = 14.0899 +N = 400000, Total time = 57.3434 + +Jumlah Proses = 4 + +N = 50000, Total time = 0.02372 +N = 100000, Total time = 2.0130 +N = 200000, Total time = 8.3548 +N = 400000, Total time = 23.6990 + +Jumlah Proses = 8 + +N = 50000, Total time = 0.3607 +N = 100000, Total time = 0.7510 +N = 200000, Total time = 2.0278 +N = 400000, Total time = 8.4307 + +Jumlah Proses = 16 + +N = 50000, Total time = 0.1267 +N = 100000, Total time = 0.3098 +N = 200000, Total time = 0.8227 +N = 400000, Total time = 1.8991 + +Jumlah Proses = 32 + +N = 50000, Total time = 0.0983 +N = 100000, Total time = 0.1815 +N = 200000, Total time = 0.4678 +N = 400000, Total time = 1.1503 \ No newline at end of file diff --git a/bucketsort.c b/bucketsort.c new file mode 100644 index 0000000000000000000000000000000000000000..b055e75583500707126e30a4f7f9c0d260aa3d9d --- /dev/null +++ b/bucketsort.c @@ -0,0 +1,122 @@ +// Program untuk melakukan sorting menggunakan Bucket Sort +// Apabila kompilasi gagal, tambahkan -lm berhubung kode menggunakan math.h + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <mpi.h> +#include <assert.h> + +int *create_Random_Numbers(int num_elements){ + time_t t; + srand((unsigned) time(&t)); + int *rand_nums = (int *) malloc (sizeof(int) * num_elements); + int i; + for(i=0; i<num_elements; i++){ + rand_nums[i] = (rand() % num_elements); + } + return rand_nums; +} + +int *sort(int* array, int num_elements){ + int c, d, t; + + for (c = 1 ; c <= num_elements - 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--; + } + } + + return array; +} + +typedef struct{ + + int* data; + +} SubUnsortedArray; + +int main(int argc, char** argv){ + if(argc!=2){ + fprintf(stderr,"Argument: num_elements of array\n"); + exit(1); + } + + int num_elements = atoi(argv[1]); + + MPI_Init(&argc, &argv); + + int world_rank; + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + int world_size; + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + + double startTime, endTime; + + int *unsortedArray = NULL; + if (world_rank == 0) { + unsortedArray = create_Random_Numbers(num_elements); + + SubUnsortedArray *buckets; + + startTime = MPI_Wtime(); + + int* bucketsNumOfElement; + buckets = (SubUnsortedArray *) malloc (sizeof(SubUnsortedArray) * world_size); + bucketsNumOfElement = (int *) malloc (sizeof(int*) * world_size); + + // Initialize buckets element + int i; + for(i=0; i<world_size; i++){ + buckets[i].data = (int *) malloc (sizeof(int) * num_elements); + bucketsNumOfElement[i] = 0; + } + + // Seperate elements of unsortedArray into buckets + for(i=0; i<num_elements; i++){ + int bucketNumber; + int numOfNumbersPerProcess; + numOfNumbersPerProcess = (int) ceil((float) num_elements / world_size); + bucketNumber = unsortedArray[i] / numOfNumbersPerProcess; + + int head = bucketsNumOfElement[bucketNumber]++; + buckets[bucketNumber].data[head] = unsortedArray[i]; + } + + // Sending number of elements for each process + for(i=1; i<world_size; i++){ + MPI_Send(&bucketsNumOfElement[i], 1, MPI_INT, + i, 0, MPI_COMM_WORLD); + } + + // Sending appropriate subArray to a process + for(i=1; i<world_size; i++){ + MPI_Send(buckets[i].data, bucketsNumOfElement[i], MPI_INT, i, + 0, MPI_COMM_WORLD); + } + } + else{ + int numOfElementsThisProcess; + MPI_Recv(&numOfElementsThisProcess, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); + + int *subArray = (int *) malloc (sizeof(int) * numOfElementsThisProcess); + MPI_Recv(subArray, numOfElementsThisProcess, MPI_INT, 0, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); + + subArray = sort(subArray, numOfElementsThisProcess); + } + + MPI_Finalize(); + + if(world_rank == 0){ + endTime = MPI_Wtime(); + printf("total time: %.4lf\n", endTime - startTime); + } + + return 0; +}