diff --git a/bucket b/bucket new file mode 100755 index 0000000000000000000000000000000000000000..27646b19477ad447d515fcadb644199e46484036 Binary files /dev/null and b/bucket differ diff --git a/bucket.c b/bucket.c new file mode 100644 index 0000000000000000000000000000000000000000..85af08c5fea8c4c6766dfa471debf8992704e553 --- /dev/null +++ b/bucket.c @@ -0,0 +1,132 @@ +#include "mpi.h" +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <time.h> +#include <assert.h> + +int main( int argc, char* argv[] ) +{ + double starttime, endtime; + int proceso_id; + int num_element; + int n[1000]; + int m; + int i,d,li,c; + unsigned int j; + char processor_name[MPI_MAX_PROCESSOR_NAME]; + int namelen; + int numprocsused; + // Intiating parallel part + MPI_Status stat; + MPI_Init(NULL,NULL); + + MPI_Comm_size(MPI_COMM_WORLD, &numprocsused); + MPI_Comm_rank(MPI_COMM_WORLD,&proceso_id); + MPI_Get_processor_name(processor_name, &namelen); + unsigned int receivedElement; + if(proceso_id == 0) { + // if it is main process + printf("Enter number of elements\n"); + scanf("%d",&num_element); + printf("Enter %d integers\n",num_element); + for (i= 0; i < num_element; i++){ + scanf("%d", &n[i]); + } + + // starting time calculation of the sort + starttime = MPI_Wtime(); + + // min and max values are got + unsigned int min = n[0]; + unsigned int max = n[0]; + for(i=0; i < num_element; i++) { + if(n[i] < min) { min = n[i]; } + if(n[i] > max) { max = n[i]; } + } + + // calculating how many numbers each bucket/process will get numbers + int elementQtyArray[numprocsused]; + // default values + for(d=1; d < numprocsused; d++) { + elementQtyArray[d] = 0; + } + for(d=0; d < num_element; d++) { + int increaseOf = max/(numprocsused-1); + int iteration = 1; + int pridetas = 0; + for(j = increaseOf; j <= max; j = j + increaseOf) { + if(n[d] <= j) { + elementQtyArray[iteration]++; + pridetas = 1; + break; + } + iteration++; + } + if (pridetas == 1) { + elementQtyArray[iteration-1]++; + } + } + + // Sending how many each process/bucket will get numbers + for(i=1; i<numprocsused; i++) { + MPI_Send(&elementQtyArray[i], 1, MPI_INT, i, -2, MPI_COMM_WORLD); + } + + // doing the same, this time sending the numbers + for(d=0; d < num_element; d++) { + int increaseOf = max/(numprocsused-1); + int iteration = 1; + int issiunte = 0; + for (j = increaseOf; j <= max; j = j + increaseOf) { + if(n[d] <= j) { + MPI_Send(&n[d], 1, MPI_UNSIGNED, iteration, -4, MPI_COMM_WORLD); + issiunte = 1; + break; + } + iteration++; + } + if (issiunte == 1) { + MPI_Send(&n[d], 1, MPI_UNSIGNED, iteration-1, -4, MPI_COMM_WORLD); + } + } + + // Getting back results and adding them to one array + int lastIndex = 0; int indexi = 0; + for(i=1; i < numprocsused; i++) { + unsigned int recvArray[elementQtyArray[i]]; + MPI_Recv(&recvArray[0], elementQtyArray[i], MPI_UNSIGNED, i, 1000, MPI_COMM_WORLD, &stat); + + if(lastIndex == 0) { + lastIndex = elementQtyArray[i]; + + } + for(j=0; j<elementQtyArray[i]; j++) { + n[indexi] = recvArray[j]; + + indexi++; + + } + + } + + + + // stoping the time + + endtime = MPI_Wtime(); + + + + // showing results in array + for(c = 0; c<num_element; c++){ + printf("Hasil Sorting index - %d : %d ",c,n[c]); + } + // sorting results + printf("it took %f seconds \n", endtime-starttime); + printf("Numbers: %d \n", num_element); + printf("Processes: %d \n", numprocsused); + } + MPI_Finalize(); + return 0; +}