Skip to content
Snippets Groups Projects
Commit 40d85e57 authored by vanydeasy's avatar vanydeasy
Browse files
parents 059e28e5 ec760ffa
No related merge requests found
No preview for this file type
/* Tifani Warnita (13513055) */ /* Tifani Warnita (13513055) Vanya Deasy Safrina (13513035) */
/* Nama File: bucket_sort.c */ /* Nama File: bucket_sort.c */
#include "mpi.h" #include "mpi.h"
...@@ -47,52 +47,89 @@ int *createRandomArray(int n) { ...@@ -47,52 +47,89 @@ int *createRandomArray(int n) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int n = atoi(argv[1]); //num array int n = atoi(argv[1]); //num array
MPI_Init(NULL, NULL);
// MPI things // MPI things
int world_rank; //process id int world_rank; //process id
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size; //total process int world_size; //total process
MPI_Comm_rank(MPI_COMM_WORLD, &world_size); MPI_Comm_size(MPI_COMM_WORLD, &world_size);
MPI_Status stat; MPI_Status stat;
MPI_Init(NULL, NULL);
// Parent process // Parent process
if (world_rank == 0) { if (world_rank == 0) {
// Create the random array // Create the random array
int *array = NULL; int *array = NULL;
array = createRandomArray(n); array = createRandomArray(n);
int num_bucket = n - 1; // COBA DULU YA
int num_element_in_bucket[n]; int num_bucket = world_size -1;
int num_element_in_bucket[num_bucket];
int i; int i;
for (i=0; i<n; i++) { // Initialize with 0 for (i=0; i<num_bucket; i++) { // Initialize with 0
num_element_in_bucket[i] = 0; num_element_in_bucket[i] = 0;
} }
// Print element
/* printf("Array Awal : [");
for (i=0; i<n-1; i++) {
printf("%d, ", array[i]);
}
printf("%d]\n", array[i]); */
// Timer (start)
double starttime = MPI_Wtime();
if (world_size == 1) {
insertionSort(array, n);
double endtime = MPI_Wtime();
printf("Waktu : %lf sekon\n", endtime-starttime);
return 0;
}
// Calculate max number in each bucket // Calculate max number in each bucket
for(i=0; i<n; i++) { for(i=0; i<n; i++) {
int position = array[i]/num_bucket; int position = array[i] / (n/num_bucket);
if (position==num_bucket)
position = num_bucket - 1;
// printf("element: %d | n/num_bucket :%d/%d | position: %d\n", array[i], n, num_bucket, position + 1);
// Send element // Send element
MPI_Send(&array[i], 1, MPI_INT, position+1, MPI_Send(&array[i], 1, MPI_INT, position+1,
ARRAY_ELEMENT, MPI_COMM_WORLD); ARRAY_ELEMENT, MPI_COMM_WORLD);
num_element_in_bucket[position]++;
} }
// printf("KESINI GA YA\n");
// Send max element in each bucket // Send max element in each bucket
for(i=1; i<=num_bucket; i++) { for(i=1; i<=num_bucket; i++) {
MPI_Send(&num_element_in_bucket[n], 1, MPI_INT, // printf("kemana: %d; isinya:%d\n", i, num_element_in_bucket[i-1]);
MPI_Send(&num_element_in_bucket[i-1], 1, MPI_INT,
i, BUCKET_SIZE, MPI_COMM_WORLD); i, BUCKET_SIZE, MPI_COMM_WORLD);
} }
// Get the result from all slaves // Get the result from all slaves
int a = 0; // Iterator for array element int a = 0; // Iterator for array element
for(i=1; i<=num_bucket; i++) { for(i=0; i<num_bucket; i++) {
// printf("proses 0 menunggu kiriman proses: %d sebanyak %d element\n",i,num_element_in_bucket[i]);
int j; int j;
for (j=0; j<num_element_in_bucket[i]; j++) { for (j=0; j<num_element_in_bucket[i]; j++) {
MPI_Recv(&array[a], 1, MPI_INT, num_bucket, MPI_Recv(&array[a], 1, MPI_INT, i+1,
RESULT, MPI_COMM_WORLD, &stat); RESULT, MPI_COMM_WORLD, &stat);
// printf("proses 0 menerima array: %d index: %d\n",array[a],a);
a++; a++;
} }
} // printf("proses 0 telah menerima kirim proses: %d\n",i);
}
// Timer (end)
double endtime = MPI_Wtime();
/* printf("Array Akhir: [");
for(i=0; i<n-1; i++) {
printf("%d, ", array[i]);
} */
printf("%d]\n", array[i]);
printf("Waktu : %lf sekon\n", endtime-starttime);
} else { // Slave process } else { // Slave process
int local_size; int local_size;
MPI_Recv(&local_size, 1, MPI_INT, 0, BUCKET_SIZE, MPI_Recv(&local_size, 1, MPI_INT, 0, BUCKET_SIZE,
...@@ -103,16 +140,19 @@ int main(int argc, char *argv[]) { ...@@ -103,16 +140,19 @@ int main(int argc, char *argv[]) {
for (i=0; i<local_size; i++) { for (i=0; i<local_size; i++) {
MPI_Recv(&local_array[i], 1, MPI_INT, 0, ARRAY_ELEMENT, MPI_Recv(&local_array[i], 1, MPI_INT, 0, ARRAY_ELEMENT,
MPI_COMM_WORLD, &stat); MPI_COMM_WORLD, &stat);
// printf("proses %d menerima elemen array %d\n",world_rank,local_array[i]);
} }
// printf("proses %d akan memasuki sort\n",world_rank);
insertionSort(local_array, local_size); insertionSort(local_array, local_size);
// printf("proses %d selesai sort\n",world_rank);
for (i=0; i<local_size; i++) { for (i=0; i<local_size; i++) {
// printf("proses %d mengirim array %d\n",world_rank,local_array[i]);
MPI_Send(&local_array[i], 1, MPI_INT, 0, RESULT, MPI_Send(&local_array[i], 1, MPI_INT, 0, RESULT,
MPI_COMM_WORLD); MPI_COMM_WORLD);
} }
} }
MPI_Finalize();
return 0; 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