diff --git a/bucketsort.c b/bucketsort.c index fed7cebf12389eb9f3ba5f5b9c0314a30428826b..c2a7bb89b2ac021e96b8c235f8125a03b6e27970 100644 --- a/bucketsort.c +++ b/bucketsort.c @@ -9,7 +9,7 @@ int *create_rand_nums(int num_elements) { assert(rand_nums != NULL); int i; for (i = 0; i < num_elements; i++) { - rand_nums[i] = rand(); + rand_nums[i] = rand() % num_elements; } return rand_nums; } @@ -37,6 +37,8 @@ int main(int argc, char** argv) { int bucket[world_size][total_elements]; int *rand_nums = NULL; + double time_start, time_stop; + if (world_rank == 0) { rand_nums = create_rand_nums(total_elements); for (i = 0; i < total_elements; i++) { @@ -46,6 +48,7 @@ int main(int argc, char** argv) { min = rand_nums[i]; } range = (max - min) / world_size + ((max - min) % world_size > 0); + time_start = MPI_Wtime(); for (i = 0; i < world_size; i++) { k = 0; for (j = 0; j < total_elements; j++) { @@ -59,8 +62,14 @@ int main(int argc, char** argv) { } rc = MPI_Send(bucket[i], total_elements+1, MPI_INT, i, 1, MPI_COMM_WORLD); } - for (i = 1 ; i < total_elements; i++) { - j = i; + for (i = 1 ; i < total_elements-1; i++)// { + for (j = i; j < total_elements; j++) + if (bucket[0][i] > bucket[0][j]) { + int temp = bucket[0][i]; + bucket[0][i] = bucket[0][j]; + bucket[0][j] = temp; + } + /*j = i; while ( j > 0 && bucket[0][j] < bucket[0][j-1]) { t = bucket[0][j]; @@ -69,11 +78,18 @@ int main(int argc, char** argv) { j--; } - } + }*/ } else { rc = MPI_Recv(inmsg, total_elements+1, MPI_INT, 0, 1, MPI_COMM_WORLD, &Stat); //Sort - for (i = 1 ; i < total_elements; i++) { + for (i = 0; i < total_elements-1; i++) + for (j = i+1; j < total_elements; j++) + if (inmsg[i] > inmsg[j]) { + int temp = inmsg[i]; + inmsg[i] = inmsg[j]; + inmsg[j] = temp; + } + /* for (i = 1 ; i < total_elements; i++) { j = i; while ( j > 0 && inmsg[j] < inmsg[j-1]) { @@ -83,7 +99,7 @@ int main(int argc, char** argv) { j--; } - } + } */ rc = MPI_Send(inmsg, total_elements+1, MPI_INT, 0, 1, MPI_COMM_WORLD); } if (world_rank == 0) { @@ -103,9 +119,12 @@ int main(int argc, char** argv) { } } } + time_stop = MPI_Wtime() - time_start; + } if (world_rank == 0) { free(rand_nums); + printf("%d processes, %d elements, %.2lf seconds", world_size, total_elements, time_stop); } MPI_Barrier(MPI_COMM_WORLD); diff --git a/bucksort b/bucksort new file mode 100755 index 0000000000000000000000000000000000000000..b71729d411794a3d0ec9381cb955583b22b93f79 Binary files /dev/null and b/bucksort differ diff --git a/mpi_hostfile b/mpi_hostfile new file mode 100644 index 0000000000000000000000000000000000000000..5cc7fe57ffe73fbb47bc80be880b227eec918e23 --- /dev/null +++ b/mpi_hostfile @@ -0,0 +1,9 @@ +#daftar host +167.205.35.26 +167.205.35.28 +167.205.35.29 +167.205.35.30 +#167.205.35.31 +#167.205.35.32 +#167.205.35.33 +#167.205.35.34