Skip to content
Snippets Groups Projects
Commit b1cd708f authored by Chairuni Aulia Nusapati 's avatar Chairuni Aulia Nusapati
Browse files

add new Bucket Sort

parent 6bad8b92
No related merge requests found
File added
/*
* 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
<?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>
File added
omp_hello 0 → 100755
File added
#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
<?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>
File added
omp_trap 0 → 100755
File added
File added
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment