From 4107b99fda569e234bedd0d494fac159277d6f23 Mon Sep 17 00:00:00 2001 From: 13513044 <13513044@ld5-01.if.itb.ac.id> Date: Fri, 12 Feb 2016 17:28:24 +0700 Subject: [PATCH] Final --- bucketsort | Bin 13246 -> 13246 bytes bucketsort.c | 150 +++++++++++++++++++++++++++------------------------ 2 files changed, 81 insertions(+), 69 deletions(-) diff --git a/bucketsort b/bucketsort index 43c2ec39025535235c61f0b85ba2dc5f2d95769d..c54f61a6881ccdd9643cba5d292017137f35cdbb 100755 GIT binary patch delta 1253 zcmZ`&T}TvB6ux(MwQY5s+1(LMO|9HA%byG+v&1cL+)BbC$jTt1=m-0uq#v*}mo27o zYvWc3Az2SmJw&wz2BK-D{XqFrgb{4z!%A<Ww)D_+?l`-yg#&x<%y-Ur&bi;Ww|js0 ze*d7gE*a@hUGmkTZbz*=`Bf`d=r6T0dw_@Jwe42-!ORm!PPK)p|82!l?CQ)3v<iMf zFw23wxUhLP0s<ZAJ?0E7u}T=%vsDR8=G1GN=6x=D=fopKeO~%U)2PzllPw3D5_V!y z^&SAk6UO4@A`?P^FNw1-zR7;r?g-)E>|)Y(+{V5oRa@$Qa~|<CyO_*2iFx=VJ0tos zMn1rZ17w&FI?@4)N`@5@{CP+vLAE*3DJ!G}UTG`!$zGmBRq}|n+fx=iqWHCn6k#&z z3E|ibiu+X3!oJ!wSia4&k@NM8#ApqjUD_u$*TDHaWEiXZ(Mrj%*s>a+(3-HOjf!?} zE<dn3xhbTHWlHlK7l2Tme40=eWKCl?yd4!K(ljd8l6yUicpm0?zkX@jWCzf$EJ_dR zFjo-Z^-=f@z$5Q~2!dDg{=Vzgd{g8c_p|nF)+43o-qNXhw3YS-1*!~$z5qrQ?`fES zfOPQzf7UQfTGqIZCzGyN8TBl#ledbu7d)g3G~o40NmoM~c)|xC>4(Me<m5j_7!?M= zh5t;7R@c2__wBYu1DeUSDMkwnb2x}SXM^Zm`Aj{f7~dnVPIchMu8SJW59LV>;W9AS zmkY4^RM$<_3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5xnRq~xw^Va$b{#C-mg*G4eN^-rA}A}*2!(b&eX@q zh+tzX*T{lUm=%HGEdxXy8Kyqwq%m1?>vY!*{`|~$dq{icGMV4+QlDf)-q&QTCkLz* zsRG=tIs~3HTJ4-AzO}?3`Z@9pNIRZ;$ZAjZ#YsNizpt=Ie>S0^3B@NJVo^eDH*P`+ zx!pL2KyRTWhM6vbgvy2(6Hj`}hoD|kV*6fI6gN#I6~i5HJ52QPKn>t#WJE4Asj7IB Rq8$f>9uY@H)<|VQ_ZPEqNk;$x diff --git a/bucketsort.c b/bucketsort.c index c861542..5d11082 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; } -- GitLab