diff --git a/laporan.txt b/laporan.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/omp_bucket.c b/omp_bucket.c index 8a9b800655f6081b67abf87da93b925a7ad37b9b..ea3c453d26ad805649403567d6913c7b9ca3b102 100644 --- a/omp_bucket.c +++ b/omp_bucket.c @@ -1,90 +1,95 @@ -#include <stdio.h> -#include <stdlib.h> -#include <omp.h> +#include <stdio.h> +#include <stdlib.h> +#include <omp.h> +#include <time.h> #define MAX_VALUE 10000 -void bucket_sort(int* sorted_array, int element_num); /* Thread function */ -void insertion_sort(int* sorted_array, int size); +void bucket_sort(int* sorted_array, int element_num); /* Thread function */ +void insertion_sort(int* sorted_array, int size, int element_num); -int main(int argc, char *argv[]) { - +int main(int argc, char *argv[]) { + int i; - + time_t time_count; // Num of threads - int thread_count = atoi(argv[1]); + int thread_count = atoi(argv[1]); // Num of elements int element_num = atoi(argv[2]); - + int range = MAX_VALUE / thread_count; - + // Seed srand(time(NULL)); - + // Bucket //int* bucket = (int *)malloc(sizeof(int) * element_num); //memset (bucket, 0, (sizeof(int) * element_num)); - + // Sorted Array int* sorted_array = (int *)malloc(sizeof(int) * element_num); memset (sorted_array, 0, (sizeof(int) * element_num)); - + // Do bucket sort - #pragma omp parallel num_threads(thread_count) - bucket_sort(sorted_array, element_num); - - for (i=0;i<element_num;i++){ - printf("%d ", sorted_array[i]); - } - return 0; -} - -void insertion_sort(int* sorted_array, int size) + time_count = clock(); + #pragma omp parallel num_threads(thread_count) + bucket_sort(sorted_array, element_num); + float final_time = (float) (clock()-time_count) / CLOCKS_PER_SEC *1000; + + // Print result + // printf("\n"); + // for (i=0;i<element_num;i++){ + // printf("%d ", sorted_array[i]); + // } + // printf("\n"); + + printf("Time : %f ms\n", final_time); + return 0; +} + +void insertion_sort(int* sorted_array, int size, int element_num) { int n, array[size], c, d, t; - - - int my_rank = omp_get_thread_num(); - int thread_count = omp_get_num_threads(); + + + int my_rank = omp_get_thread_num(); + int thread_count = omp_get_num_threads(); n = thread_count+1; - - printf("%d\n", my_rank); - + for (c = 0; c < size; c++) { array[c] = rand() % (size) + (my_rank)*size; - printf("before sort : %d\n", array[c]); } - + // Sorting for (c = 1 ; c <= size - 1; c++) { d = c; - + while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; - + d--; } - } - - for (c=0;c<size;c++){ - printf("%d",array[c]); - -} - - memcpy(&array, sorted_array+(my_rank * size), size); + } + +// for (c=0;c<size;c++){ +// printf("%d",array[c]); +// +// } + // printf("\nSize : %d\n", size); + memcpy(sorted_array+(my_rank * size), &array, size*sizeof(int)); } -void bucket_sort(int* sorted_array, int element_num) { - int my_rank = omp_get_thread_num(); +void bucket_sort(int* sorted_array, int element_num) { + int my_rank = omp_get_thread_num(); int thread_count = omp_get_num_threads(); - + int bucket_size = element_num / thread_count; - - insertion_sort(sorted_array, bucket_size); - - - -} + + insertion_sort(sorted_array, bucket_size, element_num); + + + +}