diff --git a/omp_bucketSort b/omp_bucketSort new file mode 100755 index 0000000000000000000000000000000000000000..40538c133d664a192acf04803d51ca38f0b6bf10 Binary files /dev/null and b/omp_bucketSort differ diff --git a/omp_bucketSort.c b/omp_bucketSort.c index 858364baca4be8758d70d32616e121218058f4f5..4a4baaa0e9617200917f6e53bbdfcb550b6b6ca8 100644 --- a/omp_bucketSort.c +++ b/omp_bucketSort.c @@ -1,154 +1,93 @@ -/* - * 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. - */ +//file: omp_bucketSort.c +//praktikan: 13513008 Muhammad Ridwan, 13513054 Chairuni Aulia Nusapati #include <stdio.h> +#include <time.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--; - } +int thread_count; // thread number, 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; // random numbers + + if(argc != 3){ + fprintf(stderr, "usage: %s <number of threads> <number of elements>\n", argv[0]); + exit(0); + } + + thread_count = atoi(argv[1]); + n = atoi(argv[2]); + + // randomize + rand_nums = create_rand_nums(n, n); + + //max 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; + + // 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("Your random elements: \n"); + for (int i = 0; i < n; i++){ + printf("%d\n", rand_nums[i]); + } + + printf("\n"); + + //sort using OpenMP + clock_t begin, end; + begin = clock(); + int my_rank; + #pragma omp parallel num_threads(thread_count) shared(buckets, thread_count) private(my_rank) + { + my_rank = omp_get_thread_num(); + int c, d, t; + int numel = buckets[my_rank][0]; + for (c = 1; c <= numel; c++){ + d = c; + while(d>1 && buckets[my_rank][d] < buckets[my_rank][d-1]){ + t = buckets[my_rank][d]; + buckets[my_rank][d] = buckets[my_rank][d-1]; + buckets[my_rank][d-1] = t; + d--; + } } - } + } + end = clock(); + printf("Your %d elements sorted by %d threads: \n", n, thread_count); + for (int i = 0; i < thread_count; i++){ + for (int j = 1; j <= buckets[i][0]; j++){ + printf("%d\n", buckets[i][j]); + } + } + printf("execution time: %f\n", (double)(end-begin)/CLOCKS_PER_SEC); - - -/*------------------------------------------------------------------ - * 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 */ - + return 0; +} /* main */ \ No newline at end of file diff --git a/omp_bucketSort.dSYM/Contents/Info.plist b/omp_bucketSort.dSYM/Contents/Info.plist new file mode 100644 index 0000000000000000000000000000000000000000..a73cd32b76c58b109ad8de99d6ae083e6a721487 --- /dev/null +++ b/omp_bucketSort.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> + <dict> + <key>CFBundleDevelopmentRegion</key> + <string>English</string> + <key>CFBundleIdentifier</key> + <string>com.apple.xcode.dsym.omp_bucketSort</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundlePackageType</key> + <string>dSYM</string> + <key>CFBundleSignature</key> + <string>????</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + </dict> +</plist> diff --git a/omp_bucketSort.dSYM/Contents/Resources/DWARF/omp_bucketSort b/omp_bucketSort.dSYM/Contents/Resources/DWARF/omp_bucketSort new file mode 100644 index 0000000000000000000000000000000000000000..38133ddb6d823ea384d5b4280eb7048611255a44 Binary files /dev/null and b/omp_bucketSort.dSYM/Contents/Resources/DWARF/omp_bucketSort differ diff --git a/omp_hello b/omp_hello new file mode 100755 index 0000000000000000000000000000000000000000..6621cb5057a6a504e9db9bb12e366f994ee00744 Binary files /dev/null and b/omp_hello differ diff --git a/omp_hello.c b/omp_hello.c new file mode 100644 index 0000000000000000000000000000000000000000..cdf1defeffcb13fcf3c0b932ad3505f577177560 --- /dev/null +++ b/omp_hello.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdlib.h> +#include <omp.h> + +void Hello(void); /* Thread function */ + +int main(int argc, char* argv[]){ + /* Get number of threads form command line */ + int thread_count = strtol(argv[1], NULL, 10); + +# pragma omp parallel num_threads(thread_count) + Hello(); + + return 0; +} /* main */ + +void Hello(void){ + int my_rank = omp_get_thread_num(); + int thread_count = omp_get_num_threads(); + + printf("Hello from thread %d of %d\n", my_rank, thread_count); +} /* Hello */ \ No newline at end of file diff --git a/omp_hello.dSYM/Contents/Info.plist b/omp_hello.dSYM/Contents/Info.plist new file mode 100644 index 0000000000000000000000000000000000000000..ae70f05edd8e18497b76be74ca58a609067be423 --- /dev/null +++ b/omp_hello.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> + <dict> + <key>CFBundleDevelopmentRegion</key> + <string>English</string> + <key>CFBundleIdentifier</key> + <string>com.apple.xcode.dsym.omp_hello</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundlePackageType</key> + <string>dSYM</string> + <key>CFBundleSignature</key> + <string>????</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + </dict> +</plist> diff --git a/omp_hello.dSYM/Contents/Resources/DWARF/omp_hello b/omp_hello.dSYM/Contents/Resources/DWARF/omp_hello new file mode 100644 index 0000000000000000000000000000000000000000..7e526962fe8201b03f7e06e43e9808eb64320d3c Binary files /dev/null and b/omp_hello.dSYM/Contents/Resources/DWARF/omp_hello differ diff --git a/omp_trap b/omp_trap new file mode 100755 index 0000000000000000000000000000000000000000..5581549b923575a932f7dc16a245b21f3d3a902f Binary files /dev/null and b/omp_trap differ diff --git a/omp_trap_basic b/omp_trap_basic new file mode 100755 index 0000000000000000000000000000000000000000..37aa306554eff49241aff44a880d7596b48da3c0 Binary files /dev/null and b/omp_trap_basic differ