diff --git a/BucketSort_13513090_13514602.c b/BucketSort_13513090_13514602.c new file mode 100644 index 0000000000000000000000000000000000000000..43da417eba965e4687a2a4ad216b6c1f9b65842d --- /dev/null +++ b/BucketSort_13513090_13514602.c @@ -0,0 +1,203 @@ +// Copyright 2012 www.mpitutorial.com + +// Program yang menghitung rataÂrata dari array secara paralel menggunakanScatter dan Gather. +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <mpi.h> +#include <assert.h> +#include <string.h> + +int *create_rand_nums(int num_elements) { + int *rand_nums = (int *)malloc(sizeof(int) * num_elements); + assert(rand_nums != NULL); + int i; + + for (i = 0; i < num_elements; i++) { + rand_nums[i] = (rand() % 65536); + } + + return rand_nums; +} + +int getk(int n){ + int ans = 0; + while (n/2) { + ans = ans + 1; + n/=2; + } + return ans; +} + +void print_bit(int ans) { + while (ans > 0) { + printf("%d", ans&1); + ans /= 2; + } + + printf("\n"); +} + +int getmask(int k) { + int temp = 32768; + int ans = 0; + + //print_bit(temp); + + while (k--) { + ans = ans | temp; + temp = temp >> 1; + } + + return ans; +} + +int* insertion_sort(int *array){ + + int c, d; + + for (c = 2 ; c <= array[0]; c++) { + d = c; + + while ( d > 1 && array[d] < array[d-1]) { + int t = array[d]; + array[d] = array[d-1]; + array[d-1] = t; + + d--; + } + } + + return array; +} + + +int main(int argc, char** argv) { + + if (argc != 2) { + fprintf(stderr, "Usage: sister_sort num_elements\n"); + exit(1); + } + + int num_elements = atoi(argv[1]); + srand(time(NULL)); + + 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); + + MPI_Status Stat; + int *rand_nums = NULL; + int *buckets = NULL; + + double my_time = .0; + my_time -= MPI_Wtime(); + + int *sub_rand_nums = (int *)malloc(sizeof(int) * (num_elements + 1)); + assert(sub_rand_nums != NULL); + + if (world_rank == 0) { + printf("[init]\n"); + + rand_nums = create_rand_nums(num_elements * world_size); + + // get k + int k = getk(world_size); + int mask = getmask(k); + + printf("[allocating buckets]\n"); + buckets = (int*) malloc(sizeof(int) * (num_elements + 1) * world_size); + assert(buckets != NULL); + + memset(buckets, 0, sizeof(buckets)); + + int i; + for (i=0; i<num_elements; i++) { + unsigned indeks_num_element = ((rand_nums[i] & mask) >> (16-k)) * (num_elements + 1); + buckets[indeks_num_element]++; + buckets[buckets[indeks_num_element]+indeks_num_element]=rand_nums[i]; + } + + + for (i=world_size-1; i>0; i--) { + printf("[sending %d]\n", i); + int offset = (num_elements + 1)*i; + MPI_Send(buckets + offset, num_elements + 1, MPI_INTEGER, i, 0, MPI_COMM_WORLD); + } + + for (i=0; i<=buckets[0]; i++) { + sub_rand_nums[i] = buckets[i]; + } + + } + + if (world_rank!=0){ + MPI_Recv(sub_rand_nums, num_elements + 1, MPI_INTEGER, 0, 0, MPI_COMM_WORLD, &Stat); + } + + int* sorted = insertion_sort(sub_rand_nums); + + printf("[proc %d] i got %d item\n", world_rank, sub_rand_nums[0]); + + if (world_rank!=0){ + MPI_Send(sub_rand_nums, num_elements + 1, MPI_INTEGER, 0, 0, MPI_COMM_WORLD); + } + + + if (world_rank == 0) { + + int i; + for (i=0; i<=sub_rand_nums[0]; i++) { + buckets[i] = sub_rand_nums[i]; + } + + + for (i=1; i<world_size; i++) { + int offset = (num_elements + 1)*i; + MPI_Recv(buckets + offset, num_elements + 1, MPI_INTEGER, i, 0, MPI_COMM_WORLD, &Stat); + } + + } + + if (world_rank == 0) { + + int hasil[num_elements]; + int n_hasil = 0; + + int i; + for (i=0; i<world_size; i++) { + int offset = (num_elements + 1)*i; + int n = buckets[offset]; + printf("[print from bucket %d size %d]\n", i, n); + + int j; + for (j=0; j<n; j++){ + hasil[n_hasil++] = buckets[j+offset+1]; + } + } + + // untuk menampilkan hasil + + /*for (i=0; i<num_elements; i++) { + printf("%d ", hasil[i]); + }*/ + + my_time += MPI_Wtime(); + + printf( "Elapsed time is %.5lf\n", my_time); + + free(rand_nums); + free(buckets); + } + + free(sub_rand_nums); + + + MPI_Barrier(MPI_COMM_WORLD); + MPI_Finalize(); +} + diff --git a/BucketSort_13513090_13514602.pdf b/BucketSort_13513090_13514602.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e0a591116cf1554bfab5c4607381b7eb472c82bf Binary files /dev/null and b/BucketSort_13513090_13514602.pdf differ diff --git a/mpi_hostfile b/mpi_hostfile new file mode 100644 index 0000000000000000000000000000000000000000..7a7bc9d64bc62437b1c16d80f59e0a47e9efa3db --- /dev/null +++ b/mpi_hostfile @@ -0,0 +1,10 @@ +#daftar host +localhost +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