diff --git a/Laporan.txt b/Laporan.txt new file mode 100644 index 0000000000000000000000000000000000000000..fca7ce72cc80174274c1aedd470db283efaf918e --- /dev/null +++ b/Laporan.txt @@ -0,0 +1,22 @@ +Jumlah Processor | Jumlah Elemen | Waktu (s) +------------------------------------------------ + 1 | 50000 | 3.520 + 1 | 100000 | 12.534 + 1 | 200000 | 48.514 + 1 | 400000 | 195.055 + 4 | 50000 | 0.936808 + 4 | 100000 | 2.355405 + 4 | 200000 | 5.009665 + 4 | 400000 | 10.523 + 8 | 50000 | 0.893685 + 8 | 100000 | 1.976507 + 8 | 200000 | 4.407416 + 8 | 400000 | 10.041354 + 16 | 50000 | 0.803780 + 16 | 100000 | 1.626734 + 16 | 200000 | 4.009986 + 16 | 400000 | 8.169488 + 32 | 50000 | 0.719767 + 32 | 100000 | 1.098951 + 32 | 200000 | 3.454022 + 32 | 400000 | 8.042038 diff --git a/mpiBucketSort.c b/mpiBucketSort.c new file mode 100644 index 0000000000000000000000000000000000000000..c6cfd0b289b8085500387faf5f319902eb20d2d9 --- /dev/null +++ b/mpiBucketSort.c @@ -0,0 +1,170 @@ +//praktikan: 13513052 Levanji Prahyudy, 13513054 Chairuni Aulia Nusapati +// Copyright www.computing.llnl.gov +// Copyright www.mpitutorial.com +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#include <mpi.h> +#include <assert.h> + +int *create_rand_nums(int num_elements, int MAX_RAND) { + 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()%MAX_RAND; + } + return rand_nums; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Tidak ada argumen\n"); + exit(1); + } + if (atoi(argv[1]) <=0){ + fprintf(stderr, "Jumlah elemen harus > 1\n"); + exit(1); + } + + //argv[1] = banyak elemen yang akan diurutkan + int numprocs, rank, dest, source, rc, count, tag=1; + int inmsg, outmsg=10; + int buckets[atoi(argv[1])]; + + MPI_Status Stat; + + MPI_Init(NULL,NULL); + MPI_Comm_size(MPI_COMM_WORLD, &numprocs); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + + + + int *rand_nums = NULL; + if (rank == 0) {//master + + + rand_nums = create_rand_nums(atoi(argv[1]), atoi(argv[1])); + clock_t begin, end; + double time_spent; + begin = clock(); + + //mencari max dan min nilai element + int min = rand_nums[0]; + int max = rand_nums[0]; + for(int i = 1; i < atoi(argv[1]); i++){ + if(rand_nums[i] < min){ + min = rand_nums[i]; + } + if(rand_nums[i] > max){ + max = rand_nums[i]; + } + } + + //mencari range tiap bucket + int range = (max - min)/(numprocs - 1); + + //send + dest = 1; + source = 1; + for(int i = 1; i <numprocs-1; i++){ + + dest = i; + source = i; + + int count = 0; + for(int j = 0; j < atoi(argv[1]); j++){ + + if(rand_nums[j] >= (min + (i-1)*range) && rand_nums[j] < (min + (i*range))){ + count++; + } + } + + rc = MPI_Send(&count, 1, MPI_INT, dest, tag, MPI_COMM_WORLD); + for(int j = 0; j < atoi(argv[1]); j++){ + + if(rand_nums[j] >= (min + (i-1)*range) && rand_nums[j] < (min + (i*range))){ + rc = MPI_Send(&rand_nums[j], 1, MPI_INT, dest, tag, MPI_COMM_WORLD); + } + } + } + + dest = numprocs-1; + source = numprocs-1; + int count = 0; + for(int j = 0; j < atoi(argv[1]); j++){ + + if(rand_nums[j] >= min + ((numprocs-2)*range)) + count++; + } + + rc = MPI_Send(&count, 1, MPI_INT, dest, tag, MPI_COMM_WORLD); + for(int j = 0; j < atoi(argv[1]); j++){ + //printf("loop dalam sender"); + if(rand_nums[j] >= min + ((numprocs-2)*range)) + rc = MPI_Send(&rand_nums[j], 1, MPI_INT, dest, tag, MPI_COMM_WORLD); + } + + + //receive, langsung menggabungkan + int countelement = 0; + for(int i = 1; i <numprocs; i++){ + + dest = i; + source = i; + int numelproc; + int dummy; + rc = MPI_Recv(&numelproc, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); + for(int j = 0; j < numelproc; j++){ + //printf("loop dalam sender lagi receive"); + rc = MPI_Recv(&buckets[countelement], 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); + countelement++; + } + } + + end = clock(); + + + //print + for(int i = 0 ; i < countelement; i++){ + printf("%d\n",buckets[i]); + } + printf("Execution time : %f\n", (double)(end - begin)/CLOCKS_PER_SEC); + + + } + else if (rank > 0 && rank < atoi(argv[1])) { //buckets + + dest = 0; + source = 0; + int numelproc; + + rc = MPI_Recv(&numelproc, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); + int bucket[inmsg]; + for(int i = 0; i < numelproc; i++){ + + rc = MPI_Recv(&bucket[i], 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); + } + + //sort + int c, d, t; + for (c = 1 ; c <= numelproc - 1; c++) { + d = c; + while ( d > 0 && bucket[d] < bucket[d-1]) { + t = bucket[d]; + bucket[d] = bucket[d-1]; + bucket[d-1] = t; + d--; + } + } + + //kembalikan ke master + rc = MPI_Send(&numelproc, 1, MPI_INT, dest, tag, MPI_COMM_WORLD); + for(int i = 0; i < numelproc; i++){ + + rc = MPI_Send(&bucket[i], 1, MPI_INT, dest, tag, MPI_COMM_WORLD); + } + } + MPI_Finalize(); +}