Skip to content
Snippets Groups Projects
Commit fb07ca54 authored by 13513063's avatar 13513063
Browse files

Finish Bucket Sort

parent 51486e90
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <mpi.h>
#include <assert.h>
int *create_rand_nums(int num_elements) {
int *rand_nums = (int *)malloc(sizeof(int) * (num_elements+5));
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 insertionSort(int *arr, int size){
int i,j;
int value;
for(i=1;i<size;i++)
{
value=arr[i];
if(value == -1) break;
j=i-1;
while(j>=0 && value<arr[j])
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=value;
}
}
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "Usage: avg num_elements\n");
exit(1);
}
int num_elements = atoi(argv[1]);
srand(time(NULL));
MPI_Init(NULL, NULL);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int *rand_nums = NULL;
if (world_rank == 0) {
rand_nums = create_rand_nums(num_elements);
assert(rand_nums != NULL);
}
time_t start_t;
int bucketSize = num_elements;
int *bucket = NULL;
int * indeks = NULL;
int range = (bucketSize+world_size)/world_size;
if (world_rank == 0){
start_t = clock();
bucket = (int *)malloc(sizeof(int) * (world_size+5) * bucketSize);
indeks = (int *)malloc(sizeof(int) * (world_size+5));
memset(bucket,-1,sizeof(int) * world_size * bucketSize);
int i;
for(i = 0; i < world_size; i++) {
indeks[i] = i*bucketSize;
}
for(i = 0; i < bucketSize; i++) {
int no_group = rand_nums[i]/range;
bucket[indeks[no_group]++] = rand_nums[i];
}
}
int *sub_rand_nums = (int *)malloc(sizeof(int) * bucketSize);
assert(sub_rand_nums != NULL);
MPI_Scatter(bucket, bucketSize, MPI_INT, sub_rand_nums, bucketSize, MPI_INT, 0, MPI_COMM_WORLD);
insertionSort(sub_rand_nums,bucketSize);
int *nums_sorted = NULL;
if (world_rank == 0) {
nums_sorted = (int *)malloc(sizeof(int) * (world_size+5) * bucketSize);
assert(nums_sorted != NULL);
}
MPI_Gather(sub_rand_nums, bucketSize, MPI_INT, nums_sorted, bucketSize, MPI_INT, 0,
MPI_COMM_WORLD);
if (world_rank == 0) {
int i;
for(i = 0; i < bucketSize*world_size; ++i){
if(nums_sorted[i] >= 0) printf("%d\n",nums_sorted[i]);
}
time_t finish_t = clock();
printf("Waktu eksekusi %d data : %d process : %.5f ms\n", bucketSize, world_size, (double)(finish_t-start_t)*1000/CLOCKS_PER_SEC);
}
if (world_rank == 0) {
free(rand_nums);
free(bucket);
free(nums_sorted);
free(indeks);
}
free(sub_rand_nums);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
Perbandingan waktu kinerja program
1)
N = 50.000, M = 1
t = 2050 ms
N = 50.000, M = 4
t = 370 ms
N = 50.000, M = 8
t = 250 ms
N = 50.000, M = 16
t = 270 ms
N = 50.000, M = 32
t = 180 ms
2)
N = 100.000, M = 1
t = 8070 ms
N = 100.000, M = 4
t = 890 ms
N = 100.000, M = 8
t = 410 ms
N = 100.000, M = 16
t = 370 ms
N = 100.000, M = 32
t = 310
3)
N = 200.000, M = 1
t = 32880 ms
N = 200.000, M = 4
t = 2500 ms
N = 200.000, M = 8
t = 1070 ms
N = 200.000, M = 16
t = 900 ms
N = 200.000, M = 32
t = 760
4)
N = 400.000, M = 1
t = 128580 ms
N = 400.000, M = 4
t = 9230 ms
N = 400.000, M = 8
t = 3190 ms
N = 400.000, M = 16
t = 1970 ms
N = 400.000, M = 32
t = 1770
main 0 → 100755
File added
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