diff --git a/omp_bucket.c b/omp_bucket.c index 7d1ed96ff397964482f53c86c081a7087f83b227..455fdeaf75033f35e848e003eb967a19dc4f54e0 100644 --- a/omp_bucket.c +++ b/omp_bucket.c @@ -8,7 +8,7 @@ int thread_count; int getSize(int *array) { int n = 0; - + while (array[n]) { n++; } @@ -20,91 +20,93 @@ int *bucketSort(int *array, int n) { //n adalah jumlah 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 *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); - for(int i=0; i<n; i++) { + + 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]; } - - + + displs[0] = 0; for(int i=1; i<n; i++) { displs[i] = displs[i-1] + count[i-1]; } - + for(int i=0; i<n; i++) { memcpy(sendbuf + displs[i], buckets[i], count[i]; free(buckets[i]); } free(buckets); - + nextSort(sendbuf); - + return sendbuf; } 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]; } - + return tempBucket; } -void nextSort(int *array) { +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--; } } @@ -114,17 +116,17 @@ int main(int argc, char* argv[]) { int a; //jumlah elemen int *array; //array int *buckets; //bucket - + if (argc != 2) { fprintf(stderr, "usage: %s <number of threads>\n", argv[0]); exit(0); } - + thread_count = strtol(argv[1], NULL, 10); - + printf("masukkan jumlah elemen array"); scanf("%d", &a); - + // ngisi arrray array = malloc(sizeof(int*) * a); //Random nilai array @@ -132,7 +134,7 @@ int main(int argc, char* argv[]) { for(int i=0; i<a; i++) { array[i] = rand(); } - + buckets = bucketSort(array, thread_count); return 0; diff --git a/omp_bucketsort.c b/omp_bucketsort.c new file mode 100644 index 0000000000000000000000000000000000000000..90dc029f263cbe330326fa4c834e38ceb18004cd --- /dev/null +++ b/omp_bucketsort.c @@ -0,0 +1,117 @@ +#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); +}