Skip to content
Snippets Groups Projects
Commit 0e2ed630 authored by 13513044's avatar 13513044
Browse files

Final + laporan

parent faced224
Branches master
No related merge requests found
Jumlah Thread = 1
N = 50000, Total time = 3.720000
N = 100000, Total time = 14.730000
N = 200000, Total time = 56.090000
N = 400000, Total time = 225.590000
Jumlah Thread = 8
N = 50000, Total time = 1.480000
N = 100000, Total time = 5.060000
N = 200000, Total time = 17.44000
N = 400000, Total time = 66.13000
Jumlah Thread = 16
N = 50000, Total time = 1.030000
N = 100000, Total time = 2.800000
N = 200000, Total time = 9.390000
N = 400000, Total time = 36.090000
Jumlah Thread = 32
N = 50000, Total time = 0.800000
N = 100000, Total time = 2.430000
N = 200000, Total time = 5.980000
N = 400000, Total time = 19.910000
File added
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>
#include <time.h>
void Hello(void); /* Thread function */
int **bucket;
int main(int argc, char *argv[]) {
int thread_count = strtol(argv[1], NULL, 10);
bucket = malloc(sizeof(int*) * thread_count-1);
#pragma omp parallel num_threads(thread_count);
int my_rank = omp_get_thread_num();
if (my_rank = 0) {
int **bucket = malloc(sizeof(int*) * thread_count-1);
int i, j;
int size;
scanf("%d", &size);
int *array = malloc (sizeof(int) * size);
int i, j;
int *res = malloc (sizeof(int*) * size);
for (i=0;i<thread_count-1;i++) {
bucket[i] = malloc(sizeof(int) * size);
}
for (i=0;i<size;i++)
array[i] = rand() % size;
time_t start_time, end_time;
start_time = time(0);
clock_t start_time, end_time;
start_time = clock();
int min = array[0];
int max = array[0];
......@@ -34,77 +35,75 @@ int main(int argc, char *argv[]) {
max = array[i];
}
int *bucketsize = malloc (sizeof(int) * thread_count);
for (i=1;i<thread_count;i++)
int *bucketsize = malloc (sizeof(int) * thread_count-1);
for (i=0;i<thread_count-1;i++)
bucketsize[i] = 0;
if (thread_count > 1) {
int interval = max/(thread_count-1);
int interval = (int)ceil((float)(max-min+1)/(float)(thread_count-1));
for (i=0;i<size;i++) {
int boundary1 = 0;
int k = 0;
for (j=interval; j<=max; j += interval) {
if (array[i] <= j) {
bucketsize[k]++;
boundary1 = 1;
break;
}
k++;
}
if (!boundary1)
bucketsize[k-1]++;
}
for (i=0;i<thread_count;i++) {
*bucket = malloc (sizeof(int*) * bucketsize[i]);
int index = (array[i]-min)/interval;
bucketsize[index]++;
bucket[index][bucketsize[index]-1] = array[i];
}
int interval = max/(thread_count-1);
for (i=0;i<size;i++) {
/*for (i=0;i<thread_count-1;i++) {
for (j=0;j<bucketsize[i];j++) {
printf("Bucket %d : %d\n", i, bucket[i][j]);
}
}*/
#pragma omp parallel num_threads(thread_count)
{
int p, q;
#pragma omp for
for (p=1;p<thread_count;p++) {
int temp;
for (q = 1 ; q<bucketsize[p-1]; q++) {
int a = q;
while ( a > 0 && bucket[p-1][a] < bucket[p-1][a-1]) {
temp = bucket[p-1][a];
bucket[p-1][a] = bucket[p-1][a-1];
bucket[p-1][a-1] = temp;
a--;
}
}
}
int boundary1 = 0;
int k = 1;
for (j=interval; j<=max; j += interval) {
if (array[i] <= j) {
bucketsize[k]++;
boundary1 = 1;
break;
}
}
int k = 0;
for (i=0;i<thread_count-1;i++) {
for (j=0;j<bucketsize[i];j++) {
res[k] = bucket[i][j];
k++;
}
if (!boundary1)
bucketsize[k-1]++;
}
for (i=0;i<thread_count;i++) {
*bucket = malloc (sizeof(int) * bucketsize[i]);
}
int *a = malloc (sizeof(int) * thread_count);
for (i=0;i<size;i++) {
a[i] = 0;
}
for (i=0;i<size;i++) {
int k=1;
bucket[array[i]/interval][a[array[i]/interval]] = array[i];
a[array[i]/interval]++;
}
}
else {
int temp;
for (j = 1 ; j < size; j++) {
int a = j;
while ( a > 0 && array[a] < array[a-1]) {
temp = array[a];
array[a] = array[a-1];
array[a-1] = temp;
a--;
}
}
for (j=0;j<size;j++) {
res[j] = array[j];
}
}
printf("\n==============================================\n");
for (i=0;i<size;i++) {
printf("Sorted %d : %d\n", omp_get_thread_num(), res[i]);
}
Hello();
end_time = time(0);
printf("Time : %f seconds\n", end_time-start_time);
end_time = clock();
printf("Time : %f seconds\n", (double)(end_time-start_time)/CLOCKS_PER_SEC);
printf("Numbers : %d \n", size);
printf("Processes : %d \n", nprocess);
return 0;
}
void Hello(void) {
int thread_count = omp_get_num_threads();
printf("Hello from thread %d of %d\n",
my_rank, thread_count);
printf("Processes : %d \n", thread_count);
return 0;
}
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