diff --git a/omp_bucketSort.c b/omp_bucketSort.c new file mode 100644 index 0000000000000000000000000000000000000000..858364baca4be8758d70d32616e121218058f4f5 --- /dev/null +++ b/omp_bucketSort.c @@ -0,0 +1,154 @@ +/* + * Copyrights http://www.cs.usfca.edu/~peter/cs625/code/trap/omp_trap.c + * File: omp_bucketSort.c + * Purpose: omp_bucketSort + * + * Input: a, b, n + * Output: estimate of integral from a to b of f(x) + * using n trapezoids. + * + * Compile: Using gcc + * gcc -g -Wall -fopenmp -o omp_trap omp_trap.c + * Usage: ./omp_trap <number of threads> + * + * Notes: + * 1. The function f(x) is hardwired. + * 2. This version uses OpenMP's parallel for with variable + * scope specified, and static partitioning. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <assert.h> +#include <omp.h> + +int thread_count; /* also number of buckets */ + + +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[]) { + int n; /* number of elements */ + int *rand_nums = NULL; + + //double integral; /* Store result in integral */ + //double a, b; /* Left and right endpoints */ + //int n; /* Number of trapezoids */ + + if (argc != 3) { + fprintf(stderr, "usage: %s <number of threads>\n", argv[0]); + exit(0); + } + thread_count = strtol(argv[1], NULL, 10); + n = strtol(argv[2], NULL, 10); + + //printf("Enter a, b, and n\n"); + //scanf("%lf %lf %d", &a, &b, &n); + + //randomize + rand_nums = create_rand_nums(n, n); + + //maks buckets + int buckets[thread_count][n]; + + //assign initial buckets capacity to buckets + for(int i = 0; i < thread_count; i++){ + buckets[i][0] = 0; + } + + //determine interval + int range = n/thread_count; //bisa jadi thread_count - 1 + + //assign to buckets + for(int i = 0; i < n; i++){ + int temp = rand_nums[i]; + buckets[temp/range][buckets[temp/range][0]+1] = temp; + buckets[temp/range][0] += 1; + } + + printf("BEFORE:\n"); + for(int i = 0; i < n; i++){ + printf("%d\n",rand_nums[i]); + } + + + printf("IN BUCKETS:\n"); + for(int i = 0; i < thread_count; i++){ + for (int j = 1; j <= buckets[i][0]; j++){ + printf("%d, ",buckets[i][j]); + } + printf("\n"); + } + + sort(buckets); + + printf("AFTER:\n"); + for(int i = 0; i < thread_count; i++){ + for (int j = 1; j <= buckets[i][0]; j++){ + printf("%d\n",buckets[i][j]); + } + } + /* OpenMP starts from here */ + //integral = Trap(a, b, n); + + return 0; +} /* main */ + +void sort(int** buckets){ + //int c, d, t; +//# pragma omp parallel for schedule(static) default(none) \ +// shared(buckets) private(c, d, t) \ num_threads(thread_count) +#pragma omp parallel num_threads(thread_count) + //sort + + for (int c = 1 ; c <= buckets[omp_get_thread_num()][0]; c++) { + int d = c; + while ( d > 0 && buckets[omp_get_thread_num()][d] < buckets[omp_get_thread_num()][d-1]) { + int t = buckets[omp_get_thread_num()][d]; + buckets[omp_get_thread_num()][d] = buckets[omp_get_thread_num()][d-1]; + buckets[omp_get_thread_num()][d-1] = t; + d--; + } + } + } + + + + +/*------------------------------------------------------------------ + * Function: Trap + * Purpose: Use trapezoidal rule to compute definite integral + * Input args: + * a: left endpoint + * b: right endpoint + * n: number of trapezoids + * Return value: Estimate of Integral from a to b of f(x) + */ +/*double Trap(double a, double b, int n) { + double h, x, integral = 0.0; + int i; + + h = (b-a)/n; + integral += (f(a) + f(b))/2.0; +# pragma omp parallel for schedule(static) default(none) \ + shared(a, h, n) private(i, x) \ + reduction(+: integral) num_threads(thread_count) + for (i = 1; i <= n-1; i++) { + x = a + i*h; + integral += f(x); + } + + integral = integral*h; + + return integral; +}*/ /* Trap */ +