Skip to content
Snippets Groups Projects
Commit afde91bb authored by 13513073's avatar 13513073
Browse files

add bucket_sort OpenMP and laporan

parent 729bc0e6
Branches master
No related merge requests found
Moch Ginanjar Busiri 13513041
Wiwit Rifa'i 13513073
Laporan Perbandingan Waktu Bucket Sort OpenMP :
Thread : 1
Data 50000 : 3690 ms
Data 100000 : 14260 ms
Data 200000 : 61810 ms
Data 400000 : 222600 ms
Thread : 8
Data 50000 : 590 ms
Data 100000 : 2170
Data 200000 : 8140 ms
Data 400000 : 30600 ms
Thread : 16
Data 50000 : 390 ms
Data 100000 : 1020 ms
Data 200000 : 4190 ms
Data 400000 : 15900 ms
Thread : 32
Data 50000 : 360 ms
Data 100000 : 1020 ms
Data 200000 : 2540 ms
Data 400000 : 11810 ms
Thread : 64
Data 50000 : 500 ms
Data 100000 : 1020 ms
Data 200000 : 2690 ms
Data 400000 : 8560 ms
Thread : 128
Data 50000 : 60 ms
Data 100000 : 290 ms
Data 200000 : 1260 ms
Data 400000 : 3360 ms
bucket 0 → 100755
File added
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <omp.h>
#include <assert.h>
int *create_rand_nums(int num_elements) {
int *rand_nums = (int *)malloc(sizeof(int) * num_elements);
assert(rand_nums != NULL);
int i;
for (i = 0; i < num_elements; i++) {
rand_nums[i] = (abs((int)rand()) % num_elements);
}
return rand_nums;
}
void insertion_sort(int *array, int a, int b)
{
int c, d, t;
for (c = a ; c < b; c++) {
d = c;
while ( d > a && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
}
int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "Usage: bucket num_threads num_elements\n");
exit(1);
}
int num_elements = atoi(argv[2]);
int num_threads = atoi(argv[1]);
int range_size = (num_elements + num_threads - 1) / num_threads;
srand(time(NULL));
int *rand_nums = NULL;
rand_nums = create_rand_nums(num_elements);
int * bucket_nums;
bucket_nums = (int *) malloc(sizeof(int) * num_elements);
int *str, *fsh;
str = (int *)malloc(sizeof(int) * num_threads);
fsh = (int *)malloc(sizeof(int) * num_threads);
memset(fsh, 0, sizeof(int) * num_threads);
time_t start_time = clock();
int i;
for(i = 0; i<num_elements; i++) {
fsh[rand_nums[i]/range_size]++;
}
int now = 0;
for(i = 0; i<num_threads; i++) {
str[i] = now;
now += fsh[i];
fsh[i] = str[i];
}
for(i = 0; i<num_elements; i++) {
int no_bucket = rand_nums[i]/range_size;
bucket_nums[fsh[no_bucket]++] = rand_nums[i];
}
// Implementasi bucket sort dengan OMP
#pragma omp parallel for num_threads(num_threads) private(i) shared(bucket_nums,str, fsh)
for (i = 0; i < num_threads; i++)
{
insertion_sort(bucket_nums, str[i], fsh[i]);
}
time_t finish_time = clock();
for(i = 0; i<num_elements; i++)
printf("%d\n", bucket_nums[i]);
printf("Bucket sort of %d with %d thread need time : %.5lf ms\n", num_elements, num_threads, (double)(finish_time-start_time)*1000/CLOCKS_PER_SEC);
free(str);
free(fsh);
free(rand_nums);
free(bucket_nums);
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