Skip to content
Snippets Groups Projects
Commit ee514bba authored by 13512054's avatar 13512054
Browse files

belum selesai

parent fb28200d
Branches master
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <assert.h>
#include <string.h>
#include <time.h>
void Hello(void); /* Thread function */
int *create_rand_nums(int num_elements, int* counts, int* displs, int num_buckets) {
int *rand_nums = (int *)malloc(sizeof(int) * num_elements);
assert(rand_nums != NULL);
srand(time(NULL));
int bucket_size = RAND_MAX / num_buckets;
int i, j;
int** buckets;
buckets = (int **)malloc(sizeof(int*) * num_buckets);
for(i = 0; i < num_buckets; i++){
counts[i] = 0;
displs[i] = 0;
buckets[i] = (int *)malloc(sizeof(int) * num_elements);
}
for (i = 0; i < num_elements; i++) {
int random = (rand() % RAND_MAX);
int bucket = random / bucket_size;
buckets[bucket][counts[bucket]] = random;
counts[bucket]++;
for(j = bucket+1; j < num_buckets; j++){
displs[j]++;
}
}
for (i = 0; i < num_buckets; i++){
memcpy(rand_nums + displs[i], buckets[i], counts[i]);
//printf("count %d\n", counts[i]);
free(buckets[i]);
}
free(buckets);
return rand_nums;
}
void sort(int *array, int n) {
int c, d, t;
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--;
}
}
}
int main(int argc, char *argv[]) {
int *array;
int *displacement;
int *count;
int thread_count = strtol(argv[1], NULL, 10);
int num_element = strtol(argv[2], NULL, 10);
displacement = malloc(thread_count*sizeof(int));
count = malloc(thread_count*sizeof(int));
array = create_rand_nums(num_element, count, displacement, thread_count);
for(int i = 0; i < num_element; i++){
printf("%d\n", array[i]);
}
#pragma omp parallel for shared(array, displacement, count) num_threads(thread_count)
for(int i = 0; i < thread_count; i++)
{
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
sort(array+displacement[i], count[i]);
}
for(int i = 0; i < num_element; i++){
printf("%d\n", array[i]);
}
free(array);
free(displacement);
free(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