Skip to content
Snippets Groups Projects
Commit 0626913e authored by 13512501's avatar 13512501
Browse files

sudah bisa sort. belum testing waktu

parent 729bc0e6
No related merge requests found
Readme Praktikum II IF3230 Sistem Paralel dan Terdistribusi Semester II 2015/2016
13512501 Muhammad Nizami
13513039 Ivan Andrianto
Laporan:
laporan.txt
Source code:
bucket_sort.c random.c insertion_sort_func.c
Mengkompilasi:
gcc bucket_sort_openmp.c -o bucket_sort_openmp
PERHATIAN! jangan coba-coba melakukan linking dengan random.c maupun insertion_sort_func.c karena keduanya diinclude
Menjalankan:
./bucket_sort_openmp <thread_count>
INPUT: satu integer desimal menandakan banyak bilangan
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include "random.c"
#include "insertion_sort_func.c"
void sort(int * arr, int len){
insertion_sort(arr,len);
}
int main(int argc, char ** argv){
if (argc<2){
fprintf(stderr, "bucket_sort_openmp <thread_count>\n");
exit(1);
}
int thread_count =strtol(argv[1],NULL,10);
int * compl_arr;
int i,j;
int num_elmts;
//input
scanf("%d",&num_elmts);
compl_arr = (int*) malloc(num_elmts*sizeof(int));
//random elemen
randomarr(compl_arr,num_elmts);
for (i=0;i<num_elmts;i++){
printf("%d ", compl_arr[i]);
}
printf("\n");
//bagi-bagi
int * num_elmts_in_buckets;
int * buckets_bin;
int ** buckets;
num_elmts_in_buckets = (int*) malloc(thread_count*sizeof(int));
buckets = (int**) malloc(thread_count*sizeof(int*));
buckets_bin = (int*) malloc(thread_count*sizeof(int));
for (i=0;i<thread_count;i++) buckets_bin[i]=num_elmts*(i+1)/thread_count;
for (i=0;i<thread_count;i++){ num_elmts_in_buckets[i]=0; buckets[i]=malloc(num_elmts*sizeof(int));}
for (i=0;i<num_elmts;i++){
for (j=0;j<thread_count;j++){
if (compl_arr[i]<=buckets_bin[j]){
buckets[j][num_elmts_in_buckets[j]]=compl_arr[i];
num_elmts_in_buckets[j]++;
break;
}
}
}
//buat reduksinya
int * bucket_offset = malloc(thread_count*sizeof(int));
int curbuck_offset = 0;
for (i=0;i<thread_count;i++){
bucket_offset[i]=curbuck_offset;
curbuck_offset+=num_elmts_in_buckets[i];
}
#pragma omp parallel for num_threads(thread_count)
for (i=0;i<thread_count;i++)
{
sort(buckets[i], num_elmts_in_buckets[i]);
//copy
memcpy(&compl_arr[bucket_offset[i]],buckets[i],num_elmts_in_buckets[i]*sizeof(int));
}
for (i=0;i<num_elmts;i++){
printf("%d ", compl_arr[i]);
}
printf("\n");
free(bucket_offset);
free(num_elmts_in_buckets);
for (i=0;i<thread_count;i++)
free(buckets[i]);
free(buckets);
free(buckets_bin);
free(compl_arr);
}
/* Copyrights http://www.programmingsimplified.com/c/source-code/c-program-insertion-sort */
/* insertion sort ascending order */
//insertion sort
void insertion_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--;
}
}
}
random.c 0 → 100644
#include <stdlib.h>
void swap(int * a, int * b){
int c = *b;
*b = *a;
*a = c;
}
void randomarr(int vektor [], int n) {
int i;
for (i=0;i<n;)
vektor[i]=++i;
int max;
int r;
for (max=n-1;max>=0;max--){
r = rand()%n;
swap(&vektor[r],&vektor[max]);
}
}
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