diff --git a/bucketsort b/bucketsort index 43c2ec39025535235c61f0b85ba2dc5f2d95769d..c54f61a6881ccdd9643cba5d292017137f35cdbb 100755 Binary files a/bucketsort and b/bucketsort differ diff --git a/bucketsort.c b/bucketsort.c index c8615420c13a29a3a5557a4aaa26e3a2512cb6d9..5d11082d405bf17f219e61f8422b46d141cd4527 100644 --- a/bucketsort.c +++ b/bucketsort.c @@ -8,86 +8,90 @@ int main( int argc, char* argv[] ) { - double starttime, endtime; - int t, i, j, c, d; - int proceso_id; + double start_time, end_time; + int i, j, a, b, temp; + int world_rank; char processor_name[MPI_MAX_PROCESSOR_NAME]; int namelen; - int numprocsused; + int nprocess; // 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_Comm_size(MPI_COMM_WORLD, &nprocess); + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Get_processor_name(processor_name, &namelen); unsigned int receivedElement; - if(proceso_id == 0) { + if(world_rank == 0) { // if it is main process - + int size; scanf("%d", &size); unsigned int *array = malloc(sizeof(unsigned int) * size); for (i=0;i<size;i++) array[i] = rand() % size; - + // starting time calculation of the sort - starttime = MPI_Wtime(); - + start_time = MPI_Wtime(); + // min and max values are got unsigned int min = array[0]; unsigned int max = array[0]; for(i=0; i < size; i++) { - if(array[i] < min) { min = array[i]; } - if(array[i] > max) { max = array[i]; } + if(array[i] < min) + min = array[i]; + if(array[i] > max) + max = array[i]; } - + // calculating how many numbers each bucket/process will get numbers - int *elementQtyArray = malloc (sizeof(int)*numprocsused); + int *elementQtyArray = malloc (sizeof(int)*nprocess); // default values - for(d=1; d < numprocsused; d++) { - elementQtyArray[d] = 0; + for(b=1; b < nprocess; b++) { + elementQtyArray[b] = 0; } - int pridetas; - for(d=0; d < size; d++) { - int increaseOf = max/(numprocsused-1); - int iteration = 1; - pridetas = 0; - for(j = increaseOf; j <= max; j = j + increaseOf) { - if(array[d] <= j) { - elementQtyArray[iteration]++; - pridetas = 1; + if(nprocess>1){ + + int boundary1; + for(b=0; b < size; b++) { + int increaseOf = max/(nprocess-1); + int k = 1; + boundary1 = 0; + for(j = increaseOf; j <= max; j += increaseOf) { + if(array[b] <= j) { + elementQtyArray[k]++; + boundary1 = 1; break; } - iteration++; + k++; } - if (!pridetas) - elementQtyArray[iteration-1]++; + if (!boundary1) + elementQtyArray[k-1]++; } - for(i=1; i<numprocsused; i++) { + for(i=1; i<nprocess; i++) { MPI_Send(&elementQtyArray[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD); } // doing the same, this time sending the numbers - for(d=0; d < size; d++) { - int increaseOf = max/(numprocsused-1); - int iteration = 1; - int issiunte = 0; - for (j = increaseOf; j <= max; j = j + increaseOf) { - if(array[d] <= j) { - MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration, 1, MPI_COMM_WORLD); - issiunte = 1; + for(b=0; b < size; b++) { + int increaseOf = max/(nprocess-1); + int k = 1; + int boundary2 = 0; + for (j = increaseOf; j <= max; j += increaseOf) { + if(array[b] <= j) { + MPI_Send(&array[b], 1, MPI_UNSIGNED, k, 1, MPI_COMM_WORLD); + boundary2 = 1; break; } - iteration++; + k++; } - if (!issiunte) { - MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration-1, 1, MPI_COMM_WORLD); + if (!boundary2) { + MPI_Send(&array[b], 1, MPI_UNSIGNED, k-1, 1, MPI_COMM_WORLD); } } - + // Getting back results and adding them to one array int lastIndex = 0; int indexi = 0; - for(i=1; i < numprocsused; i++) { + for(i=1; i < nprocess; i++) { unsigned int * recvArray = malloc (sizeof(unsigned int)*elementQtyArray[i]); MPI_Recv(&recvArray[0], elementQtyArray[i], MPI_UNSIGNED, i, 2, MPI_COMM_WORLD, &stat); if(lastIndex == 0) { @@ -98,52 +102,60 @@ int main( int argc, char* argv[] ) indexi++; } } - + } else { + for (a = 0 ; a <size; a++) { + b = a; + while ( b > 0 && array[b] < array[b-1]) { + temp = array[b]; + array[b] = array[b-1]; + array[b-1] = temp; + b--; + } + } + } // stoping the time - endtime = MPI_Wtime(); - + end_time = MPI_Wtime(); + // showing results in file - for(c = 0 ; c < size ; c++) { - printf("%d\n", array[c]); + for(a = 0 ; a < size ; a++) { + printf("%d\n", array[a]); } // sorting results - printf("it took %f seconds \n", endtime-starttime); + printf("Time : %f seconds \n", end_time-start_time); printf("Numbers: %d \n", size); - printf("Processes: %d \n", numprocsused); + printf("Processes: %d \n", nprocess); } else { // if child process int elementQtyUsed; // kiek elementu si gija gauja is tevinio proceso // --- getting the number of numbers in the bucket MPI_Recv(&elementQtyUsed, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat); - + unsigned int *localArray = malloc (sizeof(unsigned int)*elementQtyUsed); // initiating a local bucket - int li; + int li; // --- getting numbers from the main process for(li = 0; li < elementQtyUsed; li++) { - MPI_Recv(&receivedElement, 1, MPI_UNSIGNED, 0, 1, MPI_COMM_WORLD, &stat); - localArray[li] = receivedElement; + MPI_Recv(&receivedElement, 1, MPI_UNSIGNED, 0, 1, MPI_COMM_WORLD, &stat); + localArray[li] = receivedElement; } - + // --- sorting the bucket - for (c = 1 ; c <= elementQtyUsed - 1; c++) { - d = c; - - while ( d > 0 && localArray[d] < localArray[d-1]) { - t = localArray[d]; - localArray[d] = localArray[d-1]; - localArray[d-1] = t; - - d--; - } - } - + for (a = 1 ; a <= elementQtyUsed - 1; a++) { + b = a; + while ( b > 0 && localArray[b] < localArray[b-1]) { + temp = localArray[b]; + localArray[b] = localArray[b-1]; + localArray[b-1] = temp; + b--; + } + } + // --- sending back sorted array MPI_Send(localArray, elementQtyUsed, MPI_UNSIGNED, 0, 2, MPI_COMM_WORLD); } - + MPI_Finalize(); - + return 0; }