diff --git a/Laporan.txt b/Laporan.txt new file mode 100644 index 0000000000000000000000000000000000000000..6042a8247e1e1a96ee98d9024f066483bef5ff9e --- /dev/null +++ b/Laporan.txt @@ -0,0 +1,46 @@ +Berikut adalah hasil perbandingan waktu untuk masing-masing kombinasi dari jumlah elemen pada array dan jumlah elemen proses : + + N = Jumlah elemen pada array + M = Jumlah thread + W = Waktu, dalam detik + + +Untuk N = 50.000 + + M = 1 W = 0.903954 + M = 8 W = 0.212713 + M = 16 W = 0.211205 + M = 32 W = 0.211240 + M = 64 W = 0.214821 + M = 128 W = 0.219549 + +Untuk N = 100.000 + + M = 1 W = 3.168612 + M = 8 W = 0.751038 + M = 16 W = 0.755312 + M = 32 W = 0.755973 + M = 64 W = 0.756779 + M = 128 W = 0.733448 + +Untuk N = 200.000 + + M = 1 W = 13.197695 + M = 8 W = 2.637561 + M = 16 W = 2.638672 + M = 32 W = 2.636348 + M = 64 W = 2.700888 + M = 128 W = 2.686167 + +Untuk N = 400.000 + + M = 1 W = 49.165459 + M = 8 W = 10.203683 + M = 16 W = 10.187924 + M = 32 W = 10.457075 + M = 64 W = 10.231239 + M = 128 W = 10.303235 + + +M. Fauzan Naufan / 13513062 +Faisal Prabowo / 13513094 diff --git a/bucketsort b/bucketsort index 30c779d81839b6096c28280ea3e417c2bfde64cc..e0ef26825401e5d6489069668658bd9ec6fff400 100755 Binary files a/bucketsort and b/bucketsort differ diff --git a/bucketsort.c b/bucketsort.c index 1c403faf86355f3ab1ef7abeceb210c5705ac201..c338d42d0e0da513b5ac0d2cc791df3bd238acbe 100644 --- a/bucketsort.c +++ b/bucketsort.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include <omp.h> #include <assert.h> -#include <time.h> +#include <sys/time.h> float *create_rand_nums(int num_elements) { float *rand_nums = (float *)malloc(sizeof(float) * num_elements); @@ -19,7 +19,7 @@ float * Sort(float * unsorted) { int size = unsorted[0]; float t; - for (c=1;c<size; c++) { + for (c=1;c<size + 1; c++) { d = c; while (d > 1 && unsorted[d] < unsorted[d-1]) { @@ -35,15 +35,19 @@ float * Sort(float * unsorted) { } float * * Scatter (float * input, int size) { - int * min_limit = (int *)malloc(sizeof(int) * 5); - int * max_limit = (int *)malloc(sizeof(int) * 5); - float * * output = (float **)malloc(sizeof(float*) * 400000); + float * min_limit = (float *)malloc(sizeof(float) * 5); + float * max_limit = (float *)malloc(sizeof(float) * 5); + float * * output = malloc(sizeof(float*) * 5); + + for (int i = 0; i < 5 ; i++) { + output[i] = malloc(sizeof(float) * 400000); + } //define the border limit int i; for (i = 0 ;i < 5; i++) { - min_limit[i] = 10 * i + 0 ; - max_limit[i] = 10 * i + 9 ; + min_limit[i] = i * 0.2 ; + max_limit[i] = (i+1) * 0.2 ; } //define the size of every bucket, the first index of the array is the size @@ -54,23 +58,23 @@ float * * Scatter (float * input, int size) { //scatter the input into 5 bucket int index =0; for ( i = 0; i < size ; i++) { - if (input[i] < max_limit[0]) { + if (input[i] <= max_limit[0]) { output[0][0]++; index = output[0][0]; - output[0][index] = input[i]; - } else if ((input[i] < max_limit[1]) && (input[i] > min_limit[1])) { + output[0][index] = input[i]; + } else if ((input[i] <= max_limit[1]) && (input[i] > min_limit[1])) { output[1][0]++; index = output[1][0]; output[1][index] = input[i]; - } else if ((input[i] < max_limit[2]) && (input[i] > min_limit[2])) { + } else if ((input[i] <= max_limit[2]) && (input[i] > min_limit[2])) { output[2][0]++; index = output[2][0]; output[2][index] = input[i]; - } else if ((input[i] < max_limit[3]) && (input[i] > min_limit[3])) { + } else if ((input[i] <= max_limit[3]) && (input[i] > min_limit[3])) { output[3][0]++; index = output[3][0]; output[3][index] = input[i]; - } else if (input[i] > min_limit[41]) { + } else { output[4][0]++; index = output[4][0]; output[4][index] = input[i]; @@ -81,26 +85,54 @@ float * * Scatter (float * input, int size) { } float** SortAllBucket(float* *arrayfloat, int M) { - float* *sortedarray; - int i=0; + int i=0; + + float * * sortedarray = malloc(sizeof(float*) * 5); + + for (int i = 0; i < 5 ; i++) { + sortedarray[i] = malloc(sizeof(float) * 400000); + } + #pragma omp parallel for shared(sortedarray, arrayfloat) private (i) num_threads(M) for (i=0;i<5;i++) { //Bucketnya ada 5 sortedarray[i] = Sort(arrayfloat[i]); } + + return sortedarray; } -float BucketSort(float* arrayfloat, int N, int M) { +float BucketSort(float *arrayfloat, int N, int M) { struct timeval start,end; - gettimeofday(&start, NULL); + gettimeofday(&start, NULL); float** array1 = Scatter(arrayfloat,N); - /*float** sortedarray = SortAllBucket(array1, M);*/ + float** sortedarray = SortAllBucket(array1, M); + + //gather all bucket + int element = 0; + float * gathered = malloc(sizeof(float) * 400000); + for (int i = 0; i < 5; i++) { + for (int j = 1; j <= sortedarray[i][0]; j++) { + gathered[element] = sortedarray[i][j]; + element++; + } + } + + int error = 0; + float temp = 0; + + for (int i = 0; i < N ; i++) { + if (gathered[i] < temp) error++; + temp = gathered[i]; + } + printf("Error : %d\n",error); gettimeofday(&end, NULL); float delta = ((end.tv_sec - start.tv_sec)*1000000u + end.tv_usec - start.tv_usec) / 1.e6; + return delta; } diff --git a/omp_trap b/omp_trap deleted file mode 100755 index 6dd9157fc98d0e2800bebc67c612c7cd6f75be1a..0000000000000000000000000000000000000000 Binary files a/omp_trap and /dev/null differ