Skip to content
Snippets Groups Projects
Commit f03ca917 authored by Adin Baskoro's avatar Adin Baskoro
Browse files

fix time benchmark

parent 50a41c17
Branches
No related merge requests found
No preview for this file type
......@@ -11,7 +11,11 @@ void insertion_sort(int* sorted_array, int size, int element_num);
int main(int argc, char *argv[]) {
int i;
time_t time_count;
// Timer
struct timeval start, end;
float delta;
// Num of threads
int thread_count = atoi(argv[1]);
// Num of elements
......@@ -31,11 +35,14 @@ int main(int argc, char *argv[]) {
memset (sorted_array, 0, (sizeof(int) * element_num));
// Do bucket sort
time_count = clock();
gettimeofday(&start, NULL);
#pragma omp parallel num_threads(thread_count)
bucket_sort(sorted_array, element_num);
float final_time = (float) (clock()-time_count) / CLOCKS_PER_SEC *1000;
gettimeofday(&end, NULL);
delta = ((end.tv_sec - start.tv_sec) * 1000000u +
end.tv_usec - start.tv_usec) / 1.e6;
// Print result
// printf("\n");
// for (i=0;i<element_num;i++){
......@@ -43,7 +50,7 @@ int main(int argc, char *argv[]) {
// }
// printf("\n");
printf("Time : %f ms\n", final_time);
printf("Time : %f s\n", delta);
return 0;
}
......
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define MAX_VALUE 10000
void bucket_sort(int* sorted_array, int element_num); /* Thread function */
void insertion_sort(int* sorted_array, int size);
int main(int argc, char *argv[]) {
int i;
// Num of threads
int thread_count = atoi(argv[1]);
// Num of elements
int element_num = atoi(argv[2]);
int range = MAX_VALUE / thread_count;
// Seed
srand(time(NULL));
// Bucket
int* bucket = (int *)malloc(sizeof(int) * element_num);
memset (bucket, 0, (sizeof(int) * element_num));
// Sorted Array
int* sorted_array = (int *)malloc(sizeof(int) * element_num);
memset (sorted_array, 0, (sizeof(int) * element_num));
// Do bucket sort
#pragma omp parallel num_threads(thread_count)
bucket_sort(sorted_array, element_num);
for (i=0;i<element_num;i++){
printf("%d\n", sorted_array[i]);
}
return 0;
}
void insertion_sort(int* sorted_array, int size)
{
int n, array[size], c, d, t;
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
for (c = 0; c < n; c++) {
array[c] = rand() % (size*(thread_count+1));
}
// Sorting
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
memcpy(&array, sorted_array+(my_rank * size), size);
}
void bucket_sort(int* sorted_array, int element_num) {
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
int bucket_size = element_num / thread_count;
insertion_sort(sorted_array, bucket_size);
}
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