diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..6ca538a12828e0c0aff855e41c2a5537dd20ea24 Binary files /dev/null and b/a.out differ diff --git a/bucket-sort b/bucket-sort new file mode 100755 index 0000000000000000000000000000000000000000..1d8380a78c46d92a12372eee7333ec7643aa54e0 Binary files /dev/null and b/bucket-sort differ diff --git a/bucket.c b/bucket.c new file mode 100644 index 0000000000000000000000000000000000000000..bdfcbd8925b6a770344d26c4cc055d6536efdb29 --- /dev/null +++ b/bucket.c @@ -0,0 +1,177 @@ +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <mpi.h> +#include <assert.h> + +#define ERR_NO_NUM -1; +#define ERR_NO_MEM -2; +#define MAX_PROCESS 1000 + +int get_unique_rand_num(int max) { + int i, n; + static int numNums = 0; + static int *numArr = NULL; + + if (max >= 0) { + if (numArr != NULL) + free(numArr); + if((numArr = malloc (sizeof(int) * max)) == NULL) + return ERR_NO_MEM; + for (i = 0; i < max; i++) + numArr[i] = i; + numNums = max; + } + + if (max == 0) + return ERR_NO_NUM; + + n = (rand() % numNums); + i = numArr[n]; + + numArr[n] = numArr[numNums - 1]; + numNums--; + if (numNums == 0) { + free (numArr); + numArr = 0; + } + + return i; +} + +int *create_rand_nums(int num_elements) { + int *rand_nums = (int *)malloc(sizeof(int) * num_elements); + assert(rand_nums != NULL); + + int first = get_unique_rand_num(num_elements); + rand_nums[0] = first; + + for (int i = 1; i < num_elements; i++) { + rand_nums[i] = get_unique_rand_num(-1); + } + + return rand_nums; +} + + + +int main(int argc, char** argv) { + if (argc != 2) { + fprintf(stderr, "Usage: avg num_elements_per_proc\n"); + exit(1); + } + int NbElmts = atoi(argv[1]); //ukuran array utama + + double total_times = 0.0; + + MPI_Init(NULL, NULL); + + int world_rank; + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + int world_size; + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + int bucket_size = NbElmts / world_size; + + //ARRAY UTAMA + int *rand_nums = NULL; + int *rand_all = NULL; + + + if (world_rank == 0) { + srand(time(NULL)); + rand_nums = create_rand_nums(NbElmts); + + int sub_rand_nums_all[world_size][bucket_size]; + + //used to save last sub_rand_nums_all[] index + int last_idx_all[world_size]; + + for(int i=0; i<world_size; i++) { + last_idx_all[i] = 0; + } + + //mengelompokkan rand_nums pada sub_rand_nums_all[][] + for(int i = 0; i < NbElmts; i++) { + for (int j = 0; j < world_size; j++) { + if (rand_nums[i] < (j + 1) * bucket_size) { + sub_rand_nums_all[j][last_idx_all[j]] = rand_nums[i]; + last_idx_all[j] ++; + break; + } + } + } + + rand_all = malloc(NbElmts * sizeof(int)); + for(int i=0;i<world_size;i++) { + memcpy(rand_all + (i * bucket_size), &sub_rand_nums_all[i], sizeof(int) * bucket_size); + } + } + + int *sub_rand_nums = malloc(sizeof(int) * bucket_size); + + double total_time = 0.0; + + MPI_Barrier(MPI_COMM_WORLD); + + /* Start timer */ + total_time -= MPI_Wtime(); + MPI_Scatter(rand_all, bucket_size, MPI_INT, sub_rand_nums, + bucket_size, MPI_INT, 0, MPI_COMM_WORLD); + + + int swap; + + // Buble Sort + for (int c = 0 ; c < ( bucket_size - 1 ); c++) + { + for (int d = 0 ; d < bucket_size - c - 1; d++) + { + if (sub_rand_nums[d] > sub_rand_nums[d+1]) /* For decreasing order use < */ + { + swap = sub_rand_nums[d]; + sub_rand_nums[d] = sub_rand_nums[d+1]; + sub_rand_nums[d+1] = swap; + } + } + } + + total_time += MPI_Wtime(); + total_times += total_time; + /* End timer */ + + int *all_new = NULL; + + if (world_rank == 0){ + all_new = malloc(sizeof(int) * NbElmts); + } + MPI_Gather(sub_rand_nums, bucket_size, MPI_INT, all_new, bucket_size, MPI_INT, 0, MPI_COMM_WORLD); + + if (world_rank == 0) { + assert(all_new != NULL); + printf("Old Array : \n"); + for(int i=0;i<NbElmts;i++) { + printf("%d, ",rand_nums[i]); + } + + printf("\n\n"); + printf("Sorted Array : \n"); + for(int i=0;i<NbElmts;i++) { + printf("%d, ",all_new[i]); + } + printf("\n\n"); + } + + MPI_Barrier(MPI_COMM_WORLD); + + if (world_rank == 0) { + free(all_new); + free(rand_nums); + } + + free(sub_rand_nums); + + + MPI_Finalize(); + return 0; +} diff --git a/mpi_hostfile b/mpi_hostfile new file mode 100644 index 0000000000000000000000000000000000000000..7fda33e8f04b8ff759d051d225e3adb1858bfeab --- /dev/null +++ b/mpi_hostfile @@ -0,0 +1,10 @@ +#daftar host +localhost +167.205.35.25 +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 diff --git a/performance b/performance new file mode 100644 index 0000000000000000000000000000000000000000..b81adfd2759c8dc4b67c7e503b63ae9f9d87c2ff --- /dev/null +++ b/performance @@ -0,0 +1 @@ +0 = 0.060081 ms