Skip to content
Snippets Groups Projects
Commit 346b6fa9 authored by Rosi Lutfiana's avatar Rosi Lutfiana
Browse files

new

parent 2a5fda05
No related merge requests found
...@@ -8,7 +8,7 @@ int thread_count; ...@@ -8,7 +8,7 @@ int thread_count;
int getSize(int *array) { int getSize(int *array) {
int n = 0; int n = 0;
while (array[n]) { while (array[n]) {
n++; n++;
} }
...@@ -20,91 +20,93 @@ int *bucketSort(int *array, int n) { //n adalah jumlah bucket ...@@ -20,91 +20,93 @@ int *bucketSort(int *array, int n) { //n adalah jumlah bucket
int k; //rentang nilai isi bucket int k; //rentang nilai isi bucket
int arrsize; //panjang array int arrsize; //panjang array
int count; //Untuk menentukan nomor cell pada bucket tertentu 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 *sendcounts;
int *displs; int *displs;
sendcounts = malloc(sizeof(int*) * n); sendcounts = malloc(sizeof(int*) * n);
displs = malloc(sizeof(int*) * n); displs = malloc(sizeof(int*) * n);
arrsize = getSize(array); arrsize = getSize(array);
count = malloc(sizeof(int *) * n); count = malloc(sizeof(int *) * n);
buckets = 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; count[i] = 0;
buckets[i] = malloc(sizeof(int *) * arrsize); buckets[i] = malloc(sizeof(int *) * arrsize);
} }
//Menghitung rentang nilai bucket //Menghitung rentang nilai bucket
if(((max-min+1)%n) > 0) { if(((max-min+1)%n) > 0) {
k = ((max-min+1)/n) + 1; k = ((max-min+1)/n) + 1;
} else { } else {
k = ((max-min+1)/n); k = ((max-min+1)/n);
} }
# pragma omp parallel for schedule(static) default(none) \ # pragma omp parallel for schedule(static) default(none) \
shared(array, n) private(i, m) \ shared(array, n) private(i, m) \
reduction(+: sendbuf) num_threads(thread_count) reduction(+: sendbuf) num_threads(thread_count)
//Mengisi array ke bucket yang sesuai //Mengisi array ke bucket yang sesuai
for (int i = 0; i<arrsize; i++) { for (int i = 0; i<arrsize; i++) {
int m; //nomor bucket int m; //nomor bucket
m = array[i]/k; m = array[i]/k;
buckets[m][count[m]] = array[i]; buckets[m][count[m]] = array[i];
count[m]++; count[m]++;
} }
for(int i=0; i<n; i++) { for(int i=0; i<n; i++) {
sendcounts[i] = count[i]; sendcounts[i] = count[i];
} }
displs[0] = 0; displs[0] = 0;
for(int i=1; i<n; i++) { for(int i=1; i<n; i++) {
displs[i] = displs[i-1] + count[i-1]; displs[i] = displs[i-1] + count[i-1];
} }
for(int i=0; i<n; i++) { for(int i=0; i<n; i++) {
memcpy(sendbuf + displs[i], buckets[i], count[i]; memcpy(sendbuf + displs[i], buckets[i], count[i];
free(buckets[i]); free(buckets[i]);
} }
free(buckets); free(buckets);
nextSort(sendbuf); nextSort(sendbuf);
return sendbuf; return sendbuf;
} }
int *bucket(int **array, int i, int count) { //i adalah nomor bucket, count adalah jumlah isi bucket int *bucket(int **array, int i, int count) { //i adalah nomor bucket, count adalah jumlah isi bucket
int *tempBucket; int *tempBucket;
tempBucket = malloc(sizeof(int*) * count); tempBucket = malloc(sizeof(int*) * count);
for(int j=0; j<count; j++) { for(int j=0; j<count; j++) {
tempBucket[j] = array[i][count]; tempBucket[j] = array[i][count];
} }
return tempBucket; return tempBucket;
} }
void nextSort(int *array) { void nextSort(int *array) {
int n = getSize(array); int n = getSize(array);
int d, t; int d, t;
for (int i=1 ; i <=n-1; i++) { for (int i=1 ; i <=n-1; i++) {
d = i; d = i;
while ((d > 0) && (array[d] < array[d-1])) { while ((d > 0) && (array[d] < array[d-1])) {
t = array[d]; t = array[d];
array[d] = array[d-1]; array[d] = array[d-1];
array[d-1] = t; array[d-1] = t;
d--; d--;
} }
} }
...@@ -114,17 +116,17 @@ int main(int argc, char* argv[]) { ...@@ -114,17 +116,17 @@ int main(int argc, char* argv[]) {
int a; //jumlah elemen int a; //jumlah elemen
int *array; //array int *array; //array
int *buckets; //bucket int *buckets; //bucket
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "usage: %s <number of threads>\n", argv[0]); fprintf(stderr, "usage: %s <number of threads>\n", argv[0]);
exit(0); exit(0);
} }
thread_count = strtol(argv[1], NULL, 10); thread_count = strtol(argv[1], NULL, 10);
printf("masukkan jumlah elemen array"); printf("masukkan jumlah elemen array");
scanf("%d", &a); scanf("%d", &a);
// ngisi arrray // ngisi arrray
array = malloc(sizeof(int*) * a); array = malloc(sizeof(int*) * a);
//Random nilai array //Random nilai array
...@@ -132,7 +134,7 @@ int main(int argc, char* argv[]) { ...@@ -132,7 +134,7 @@ int main(int argc, char* argv[]) {
for(int i=0; i<a; i++) { for(int i=0; i<a; i++) {
array[i] = rand(); array[i] = rand();
} }
buckets = bucketSort(array, thread_count); buckets = bucketSort(array, thread_count);
return 0; return 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);
}
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