diff --git a/bucketsort b/bucketsort index cbcd28d15484dcf1d2d7b7a390342bc030783776..0658450b509c0971b80878d62b958bad4af401ba 100755 Binary files a/bucketsort and b/bucketsort differ diff --git a/bucketsort.c b/bucketsort.c index 00a81bdb4959b0f869d71b00477c7d31129af74d..d5c914afca2cc226451e6bc5e54107808a3bc96b 100644 --- a/bucketsort.c +++ b/bucketsort.c @@ -86,12 +86,6 @@ int *insertionSort(int *array,int n){ } } - printf("Sorted list in ascending order:\n"); - - for (c = 0; c <= n - 1; c++) { - printf("%d\n", array[c]); - } - return array; } @@ -103,9 +97,16 @@ void printBucket (Bucket b) { } } +void printArray (int *array, int size) { + int i; + for (i = 0; i < size; i++) { + printf("[%d]", array[i]); + } +} + int main(int argc, char *argv[]) { - MPI_Init(&argc, &argv); + MPI_Init(NULL, NULL); int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); @@ -113,9 +114,10 @@ int main(int argc, char *argv[]) { int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); - int array_size = 10; + int array_size = atoi(argv[1]); int *array = NULL; Bucket *buckets = NULL; + if (world_rank == 0) { // Ini kalo proses utama jadinya dia yang bikin masalah trus disebar // ke proses lainnya buat diselesaikan @@ -124,9 +126,67 @@ int main(int argc, char *argv[]) { // Bikin bucket buat memecah pecah masalah jadi lebih kecil buckets = createBucket(array, searchMax(array,array_size), array_size, world_size); - printBucket(buckets[0]); + //printBucket(buckets[0]); + + int i = 0; + + int *sorted_all[world_size]; + int sorted_size[world_size]; + + double start_time = 0.0; + double end_time = 0.0; + + start_time = MPI_Wtime(); + + int *sorted_zero = insertionSort(buckets[0].content, buckets[0].size); + sorted_all[0] = intdup(sorted_zero, buckets[0].size); + sorted_size[0] = buckets[0].size; + + for (i = 1; i < world_size; i++) { + // Ukuran + MPI_Send(&buckets[i].size, 1, MPI_INT, i, 0, MPI_COMM_WORLD); + // Data + MPI_Send(buckets[i].content, buckets[i].size, MPI_INT, i, 1, MPI_COMM_WORLD); + + } + + for (i = 1; i < world_size; i++) { + int rec_size = 0; + int *received = (int *) malloc(sizeof(int) * array_size); + + MPI_Recv(&rec_size, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE ); + MPI_Recv(received, rec_size, MPI_INT, i, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE ); + + sorted_all[i] = intdup(received, rec_size); + sorted_size[i] = rec_size; + } + + end_time = MPI_Wtime(); + + printf("Time allocated to sort: %G\n", end_time-start_time); + + for (i = 0; i < world_size; i++) { + printArray(sorted_all[i], sorted_size[i]); + } + + } else { + + int rec_size = 0; + int *received = (int *) malloc(sizeof(int) * array_size); + + MPI_Recv(&rec_size, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE ); + MPI_Recv(received, rec_size, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE ); + + int *sorted = insertionSort(received, rec_size); + + // Kirim Balik + + // Ukuran + MPI_Send(&rec_size, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); + // Data + MPI_Send(sorted, rec_size, MPI_INT, 0, 1, MPI_COMM_WORLD); + } - MPI_Finalize(); }