diff --git a/omp_bucket.c b/omp_bucket.c index 455fdeaf75033f35e848e003eb967a19dc4f54e0..90dc029f263cbe330326fa4c834e38ceb18004cd 100644 --- a/omp_bucket.c +++ b/omp_bucket.c @@ -1,143 +1,117 @@ #include <stdio.h> #include <stdlib.h> -#include <math.h> +#include <string.h> +#include <time.h> #include <omp.h> +#include <assert.h> +#include <math.h> -int thread_count; - - -int getSize(int *array) { - int n = 0; - - while (array[n]) { - n++; - } - return n; -} - -int *bucketSort(int *array, int n) { //n adalah jumlah bucket - int **buckets; //Isi bucket - int k; //rentang nilai isi bucket - int arrsize; //panjang array - int count; //Untuk menentukan nomor cell pada bucket tertentu - - - int *sendbuf; //bucket yang sudah disortir dan akan di-return - int *sendcounts; - int *displs; - - sendcounts = malloc(sizeof(int*) * n); - displs = malloc(sizeof(int*) * n); - - arrsize = getSize(array); - - - - count = malloc(sizeof(int *) * n); - buckets = malloc(sizeof(int *) * n); - - int i; - for(i=0; i<n; i++) { - count[i] = 0; - buckets[i] = malloc(sizeof(int *) * arrsize); - } - - //Menghitung rentang nilai bucket - if(((max-min+1)%n) > 0) { - k = ((max-min+1)/n) + 1; - } else { - k = ((max-min+1)/n); - } - - # pragma omp parallel for schedule(static) default(none) \ - shared(array, n) private(i, m) \ - reduction(+: sendbuf) num_threads(thread_count) - - //Mengisi array ke bucket yang sesuai - for (int i = 0; i<arrsize; i++) { - int m; //nomor bucket - - m = array[i]/k; - buckets[m][count[m]] = array[i]; - - count[m]++; - } - - for(int i=0; i<n; i++) { - sendcounts[i] = count[i]; +int *random(int elmtNum, int* counts, int* displace, int bucketNum) +{ + int *randNums = (int *)malloc(sizeof(int) * elmtNum); + assert(randNums != NULL); + int bucketSize = RAND_MAX / bucketNum; + int i, j; + int** buckets; + buckets = (int **)malloc(sizeof(int*) * bucketNum); + for(i = 0; i < bucketNum; i++) + { + buckets[i] = (int *)malloc(sizeof(int) * elmtNum); + counts[i] = 0; + displace[i] = 0; } - - - displs[0] = 0; - for(int i=1; i<n; i++) { - displs[i] = displs[i-1] + count[i-1]; + for (i = 0; i < elmtNum; i++) + { + int randNum = (rand() % RAND_MAX); + int bucket = randNum / bucketSize; + buckets[bucket][counts[bucket]] = randNum; + counts[bucket]++; + for(j = bucket+1; j < bucketNum; j++) + { + displace[j]++; + } } - - for(int i=0; i<n; i++) { - memcpy(sendbuf + displs[i], buckets[i], count[i]; + for(i = 0; i < bucketNum; i++) + { + memcpy(randNums + displace[i], buckets[i], counts[i]); free(buckets[i]); } free(buckets); - - nextSort(sendbuf); - - return sendbuf; + return randNums; } -int *bucket(int **array, int i, int count) { //i adalah nomor bucket, count adalah jumlah isi bucket - int *tempBucket; - tempBucket = malloc(sizeof(int*) * count); - - for(int j=0; j<count; j++) { - tempBucket[j] = array[i][count]; +int countBucket(int bucket, int *array, int elmtNum, int bucketNum) +{ + int bucketSize = RAND_MAX / bucketNum; + int i = 0; + int counter = 0; + + for(i = 0; i < elmtNum; i++) + { + if((array[i]/bucketSize) == bucket) + { + counter++; + } } - - return tempBucket; + return counter; } -void nextSort(int *array) { - int n = getSize(array); - int d, t; - for (int i=1 ; i <=n-1; i++) { - d = i; - - while ((d > 0) && (array[d] < array[d-1])) { - t = array[d]; - array[d] = array[d-1]; - array[d-1] = t; - - d--; - } - } +void sort(int *array, int n) +{ + int i, j, k; + for (i = 1 ; i <= n - 1; i++) + { + j = i; + while ( j > 0 && array[j] < array[j-1]) + { + k = array[j]; + array[j] = array[j-1]; + array[j-1] = k; + j--; + } + } } -int main(int argc, char* argv[]) { - int a; //jumlah elemen - int *array; //array - int *buckets; //bucket +int main(int argc, char** argv) +{ + int* sendcounts; + int* displace; - if (argc != 2) { - fprintf(stderr, "usage: %s <number of threads>\n", argv[0]); - exit(0); - } - - thread_count = strtol(argv[1], NULL, 10); + if (argc != 2) + { + fprintf(stderr, "Usage: number of elements\n"); + exit(1); + } - printf("masukkan jumlah elemen array"); - scanf("%d", &a); + int elmtNum = atoi(argv[1]); + srand(time(NULL)); - // ngisi arrray - array = malloc(sizeof(int*) * a); - //Random nilai array - srand(time(NULL)); - for(int i=0; i<a; i++) { - array[i] = rand(); - } + int threadNum = omp_get_thread_num(); + int threadCount = omp_get_num_threads(); - buckets = bucketSort(array, thread_count); + int *randNums = NULL; + sendcounts = (int *)malloc(sizeof(int)*threadCount); + displace = (int *)malloc(sizeof(int)*threadCount); + if (threadNum == 0) + { + randNums = random(elmtNum, sendcounts, displace, threadCount); + printf("Thread count : %d\n", threadCount); + } + printf("Thread number : %d\n", threadNum); - return 0; -} + int *subRand = (int *)malloc(sizeof(int) * elmtNum); + assert(subRand != NULL); + #pragma omp parallel num_threads(threadCount) + printf("Jumlah thread : %d\n", countBucket(threadNum, subRand, elmtNum, threadCount)); + sort(subRand, countBucket(threadNum, subRand, elmtNum, threadCount)); + free(subRand); + if (threadNum == 0) + { + free(randNums); + } + free(sendcounts); + free(displace); +} diff --git a/omp_bucketsort.c b/omp_bucketsort.c deleted file mode 100644 index 90dc029f263cbe330326fa4c834e38ceb18004cd..0000000000000000000000000000000000000000 --- a/omp_bucketsort.c +++ /dev/null @@ -1,117 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> -#include <omp.h> -#include <assert.h> -#include <math.h> - -int *random(int elmtNum, int* counts, int* displace, int bucketNum) -{ - int *randNums = (int *)malloc(sizeof(int) * elmtNum); - assert(randNums != NULL); - int bucketSize = RAND_MAX / bucketNum; - int i, j; - int** buckets; - buckets = (int **)malloc(sizeof(int*) * bucketNum); - for(i = 0; i < bucketNum; i++) - { - buckets[i] = (int *)malloc(sizeof(int) * elmtNum); - counts[i] = 0; - displace[i] = 0; - } - for (i = 0; i < elmtNum; i++) - { - int randNum = (rand() % RAND_MAX); - int bucket = randNum / bucketSize; - buckets[bucket][counts[bucket]] = randNum; - counts[bucket]++; - for(j = bucket+1; j < bucketNum; j++) - { - displace[j]++; - } - } - for(i = 0; i < bucketNum; i++) - { - memcpy(randNums + displace[i], buckets[i], counts[i]); - free(buckets[i]); - } - free(buckets); - return randNums; -} - -int countBucket(int bucket, int *array, int elmtNum, int bucketNum) -{ - int bucketSize = RAND_MAX / bucketNum; - int i = 0; - int counter = 0; - - for(i = 0; i < elmtNum; i++) - { - if((array[i]/bucketSize) == bucket) - { - counter++; - } - } - return counter; -} - - -void sort(int *array, int n) -{ - int i, j, k; - for (i = 1 ; i <= n - 1; i++) - { - j = i; - while ( j > 0 && array[j] < array[j-1]) - { - k = array[j]; - array[j] = array[j-1]; - array[j-1] = k; - j--; - } - } -} - -int main(int argc, char** argv) -{ - int* sendcounts; - int* displace; - - if (argc != 2) - { - fprintf(stderr, "Usage: number of elements\n"); - exit(1); - } - - int elmtNum = atoi(argv[1]); - srand(time(NULL)); - - int threadNum = omp_get_thread_num(); - int threadCount = omp_get_num_threads(); - - int *randNums = NULL; - sendcounts = (int *)malloc(sizeof(int)*threadCount); - displace = (int *)malloc(sizeof(int)*threadCount); - if (threadNum == 0) - { - randNums = random(elmtNum, sendcounts, displace, threadCount); - printf("Thread count : %d\n", threadCount); - } - printf("Thread number : %d\n", threadNum); - - int *subRand = (int *)malloc(sizeof(int) * elmtNum); - assert(subRand != NULL); - #pragma omp parallel num_threads(threadCount) - printf("Jumlah thread : %d\n", countBucket(threadNum, subRand, elmtNum, threadCount)); - sort(subRand, countBucket(threadNum, subRand, elmtNum, threadCount)); - - free(subRand); - - if (threadNum == 0) - { - free(randNums); - } - free(sendcounts); - free(displace); -}