diff --git a/bucketsort.c b/bucketsort.c index 59e18275307755f78840df7353c88970e7611cbe..330e20d96e863c594c14a593ab27a4883c984a49 100644 --- a/bucketsort.c +++ b/bucketsort.c @@ -6,25 +6,33 @@ #include <time.h> #include <assert.h> +typedef struct{ + int* content; + int size; + int nContent; +} Bucket; + +typedef struct{ + Bucket* tables; +} arrayBucket; + +Bucket bucketsort(arrayBucket localArray, int nBucket, int size); -int* bucket(int* localArray, int n, ); -using namespace std; int main( int argc, char* argv[] ) { - double start_time, end_time; - int i, j, a, b, temp; - + double start_time, nContent_time; + if (argc != 2) { fprintf(stderr, "usage: %s <number of threads>\n", argv[0]); exit(0); } - - thread_count = strtol(argv[1], NULL, 10); - + + int thread_count = strtol(argv[1], NULL, 10); + int size; scanf("%d", &size); - int *array = malloc(sizeof( int) * size); - for (i=0;i<size;i++) + int *array = (int*)malloc(sizeof( int) * size); + for (int i=0;i<size;i++) array[i] = rand() % size; // starting time calculation of the sort @@ -32,28 +40,28 @@ int main( int argc, char* argv[] ) // min and max values are got int min = array[0]; int max = array[0]; - for(i=0; i < size; i++) { - if(array[i] < min) + for(int i=0; i < size; i++) { + if(array[i] < min) min = array[i]; - if(array[i] > max) + if(array[i] > max) max = array[i]; } - + // calculating how many numbers each bucket/process will get numbers - int *elementQtyArray = malloc (sizeof(int)*thread_count); + int *elementQtyArray = (int*)malloc (sizeof(int)*thread_count); // default values - - for(int b=1; b < thread_count; b++) { + + for(int b=0; b < thread_count; b++) { elementQtyArray[b] = 0; } - + if(thread_count>1){ int boundary1; for(int b=0; b < size; b++) { int increaseOf = max/(thread_count-1); int k = 1; boundary1 = 0; - for(j = increaseOf; j <= max; j += increaseOf) { + for(int j = increaseOf; j <= max; j += increaseOf) { if(array[b] <= j) { elementQtyArray[k]++; boundary1 = 1; @@ -65,32 +73,114 @@ int main( int argc, char* argv[] ) elementQtyArray[k-1]++; } - #pragma omp parallel num_threads(thread_count) { - - } - } - -return 0; -} + Bucket localArray[thread_count]; + for (int i = 0; i < thread_count; i++) { + /* code */ + localArray[i].size = elementQtyArray[i]; + localArray[i].content = (int*)malloc (sizeof(int)* localArray[i].size); + localArray[i].nContent = 0; + } + int increaseOf = max/(thread_count-1); + for(int b=0; b < size; b++) { + int k = 1; + int boundary1 = 0; + for(int j = increaseOf; j <= max; j += increaseOf) { + if(array[b] <= j) { + int idx=localArray[k].nContent; + localArray[k].content[idx]=array[b]; + localArray[k].nContent++; + boundary1 = 1; + break; + } + k++; + } + if (!boundary1){ + int idx=localArray[k-1].nContent; + localArray[k-1].content[idx]=array[b]; + localArray[k-1].nContent++; + } + }/* + for(int i=0; i<size; i++){ + printf("%d ", array[i]); + } + printf("\n"); + for(int i=0; i<thread_count; i++){ + printf("%d ", elementQtyArray[i]); + } + + printf("\n"); + for(int i=0; i<thread_count; i++){ + for(int j=0; j<localArray[i].size; j++){ + printf("%d ", localArray[i].content[j]); + } + } + */ + + + arrayBucket localArrayB; + localArrayB.tables = localArray; + + Bucket sortenArray; + + sortenArray = bucketsort(localArrayB, thread_count, size); + + printf("\nsortir:\n"); + for(int x=0; x<size; x++){ + printf("%d %d\n", array[x], sortenArray.content[x]); + } + +} else { -int* bucket(int* localArray, int n ){ - // --- sorting the bucket - int arraytemp[n]; - - int a, b, temp; - for (a = 1 ; a <= n - 1; a++) { - b = a; - while ( b > 0 && localArray[b] < localArray[b-1]) { - temp = localArray[b]; - localArray[b] = localArray[b-1]; - localArray[b-1] = temp; - b--; - } - } - for (a=0; a<n; a++){ - arraytemp[a] = localArray[a]; - } - return arraytemp; } +return 0; +} + + +Bucket bucketsort(arrayBucket localArrayB, int nBucket,int size){ + Bucket sortenArray; + sortenArray.content= (int*)malloc (sizeof(int)* size); + sortenArray.nContent=0; + sortenArray.size=size; + + + int a, b, temp, idx, i, idxAfter; + int bucket_num; + int my_rank; + # pragma omp parallel for schedule(static) default(none) \ + shared(sortenArray, localArrayB, nBucket) private(my_rank, i,a, b, bucket_num, temp, idx, idxAfter) num_threads(nBucket) + for(bucket_num=0; bucket_num<nBucket; bucket_num++){ + my_rank = omp_get_thread_num(); + if(bucket_num==my_rank){ + printf("oke %d %d\n", bucket_num, my_rank); + for (a = 1 ; a <localArrayB.tables[bucket_num].size; a++) { + b = a; + while ( b > 0 && localArrayB.tables[bucket_num].content[b] < localArrayB.tables[bucket_num].content[b-1]) { + temp = localArrayB.tables[bucket_num].content[b]; + localArrayB.tables[bucket_num].content[b] = localArrayB.tables[bucket_num].content[b-1]; + localArrayB.tables[bucket_num].content[b-1] = temp; + b++; + } + } + if(bucket_num!=0){ + idx= localArrayB.tables[bucket_num-1].size; + } else { + idx=0; + } + + idxAfter=idx+localArrayB.tables[bucket_num].size; + + for(a=idx; a<idxAfter; a++){ + i=0; + sortenArray.content[a]=localArrayB.tables[bucket_num].content[i]; + sortenArray.nContent++; + printf("%d %d\n", localArrayB.tables[bucket_num].content[i],sortenArray.content[a]); + i++; + } + } + } + + # pragma omp barrier + return sortenArray; +} diff --git a/omp_trap.c b/omp_trap.c index c0779f2235a8fcceed3d46d842c7a60024760e9f..94fae60acb56363f4deeb6666151771d40b1a731 100644 --- a/omp_trap.c +++ b/omp_trap.c @@ -1,7 +1,7 @@ -/* +/* * Copyrights http://www.cs.usfca.edu/~peter/cs625/code/trap/omp_trap.c * File: omp_trap.c - * Purpose: Calculate definite integral using trapezoidal + * Purpose: Calculate definite integral using trapezoidal * rule. * * Input: a, b, n @@ -12,7 +12,7 @@ * gcc -g -Wall -fopenmp -o omp_trap omp_trap.c * Usage: ./omp_trap <number of threads> * - * Notes: + * Notes: * 1. The function f(x) is hardwired. * 2. This version uses OpenMP's parallel for with variable * scope specified, and static partitioning. @@ -41,7 +41,7 @@ int main(int argc, char* argv[]) { printf("Enter a, b, and n\n"); scanf("%lf %lf %d", &a, &b, &n); - + /* OpenMP starts from here */ integral = Trap(a, b, n); @@ -55,7 +55,7 @@ int main(int argc, char* argv[]) { /*------------------------------------------------------------------ * Function: Trap * Purpose: Use trapezoidal rule to compute definite integral - * Input args: + * Input args: * a: left endpoint * b: right endpoint * n: number of trapezoids @@ -63,7 +63,7 @@ int main(int argc, char* argv[]) { */ double Trap(double a, double b, int n) { double h, x, integral = 0.0; - int i; + int i; h = (b-a)/n; integral += (f(a) + f(b))/2.0; diff --git a/test b/test new file mode 100644 index 0000000000000000000000000000000000000000..0510b595b0934d2b1fcf6f91c7d1f0865be0aa18 Binary files /dev/null and b/test differ