-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpiFindMedian.c
711 lines (680 loc) · 25.2 KB
/
mpiFindMedian.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
The MIT License (MIT)
Copyright (c) 2014
Athanassios Kintsakis
Contact
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
MPI_Status Stat;
void partition (int *array, int elements, int pivot, int **arraysmall, int **arraybig, int *endsmall, int *endbig);
int selection(int *array,int number);
/***Kills processes that have no values left in their arrays****/
void removeElement(int *array, int *size, int element)
{
int i;
int flag=0;
for(i=0;i<*size;i++)
{
if(flag==1)
array[i]=array[i+1];
if(array[i]==element&& flag==0)
{
array[i]=array[i+1];
flag=1;
}
}
*size=*size-1;
}
/***Calculate Lengths and Send them to the corresponding Node***/
void sendLengths(int size,int noProcesses)
{
int i,partLength;
if(size%noProcesses!=0)
{
int left=size-(size/noProcesses)*noProcesses; //Split the size in as close to equal as possible parts
partLength=(size/noProcesses)+1;
for(i=1;i<left;i++) //start from 1 because we create the zero one through the main function
MPI_Send(&partLength,1,MPI_INT,i,1,MPI_COMM_WORLD);
partLength-=1;
for(i=left;i<noProcesses;i++)
MPI_Send(&partLength,1,MPI_INT,i,1,MPI_COMM_WORLD);
}
else
{
partLength=size/noProcesses;
for(i=1;i<noProcesses;i++)
MPI_Send(&partLength,1,MPI_INT,i,1,MPI_COMM_WORLD);
}
}
/****Swaps two values in an array****/
void swap_values(int *array,int x,int y)
{
int temp;
temp=array[x];
array[x]=array[y];
array[y]=temp;
}
/*****Send random numbers to every node.*****/
void generateNumbers(int *numberPart,int partLength, int cal)
{
srand((cal+1)*time(NULL)); //Generate number to fill the array
int i;
for(i=0; i<partLength; i++)
numberPart[i]=rand()-rand();
}
/***Validates the stability of the operation****/
void validation(int median,int partLength,int size,int *numberPart,int processId)
{
MPI_Bcast(&median,1,MPI_INT,0,MPI_COMM_WORLD);
int countMin=0;
int countMax=0;
int countEq=0;
int sumMax,sumMin,sumEq,i;
for(i=0;i<partLength;i++)
{
if(numberPart[i]>median)
countMax++;
else if(numberPart[i]<median)
countMin++;
else
countEq++;
}
MPI_Reduce(&countMax,&sumMax,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
MPI_Reduce(&countMin,&sumMin,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
MPI_Reduce(&countEq,&sumEq,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
if(processId==0)
{
if((sumMax<=size/2)&&(sumMin<=size/2)) //Checks if both the lower and higher values occupy less than 50% of the total array.
printf("VALIDATION PASSED!\n");
else
printf("VALIDATION FAILED!\n");
printf("Values greater than median: %d\n",sumMax);
printf("Values equal to median: %d\n",sumEq);
printf("Values lower than median: %d\n",sumMin);
}
}
/***Validates the stability of the operation (Single Threaded)****/
void validationST(int median,int size,int *numberPart)
{
int countMin=0;
int countMax=0;
int countEq=0;
int i;
for(i=0;i<size;i++)
{
if(numberPart[i]>median)
countMax++;
else if(numberPart[i]<median)
countMin++;
else
countEq++;
}
if((countMax<=size/2)&&(countMin<=size/2)) //Checks if both the lower and higher values occupy less than 50% of the total array.
printf("VALIDATION PASSED!\n");
else
printf("VALIDATION FAILED!\n");
printf("Values greater than median: %d\n",countMax);
printf("Values equal to median: %d\n",countEq);
printf("Values lower than median: %d\n",countMin);
}
/****Part executed only by the Master Node****/
int masterPart(int noProcesses,int processId,int size,int partLength,int *numberPart) //MASTER NODE CODE
{
int elements,i,keepBigSet,sumSets,finalize,median,randomNode,pivot,k,tempPivot;
int endSmall=0;
int dropoutFlag=0;
int endBig=0;
int *arraySmall,*arrayBig,*arrayToUse,*activeNodes;
int activeSize=noProcesses;
int stillActive=1;
int oldSumSets=-1;
int checkIdentical=0;
int useNewPivot=0;
int *pivotArray;
k=(int)size/2+1; //It is done so in order to find the right median in an even numbered array.
elements=partLength;
activeNodes=(int *)malloc(noProcesses*sizeof(int)); //we create the array that contains the active nodes.
arrayToUse=numberPart;
pivotArray=(int*)malloc(noProcesses*sizeof(int)); //Used for special occasions to gather values different than the pivot.
for(i=0;i<activeSize;i++)
{
activeNodes[i]=i;
}
int randomCounter=0;
int randomCounter2=0;
struct timeval first, second, lapsed;
struct timezone tzp;
gettimeofday(&first, &tzp);
for(;;) //Begin the infinite loop until the median is found.
{
int counter=0;
useNewPivot=0;
if(stillActive==1&&checkIdentical!=0) //If i still have values in my array and the Sumed Big Set is identical to the previous one, check for identical values.
{
for(i=0;i<elements;i++)
{
if(pivot==arrayToUse[i])
counter++;
else
{
useNewPivot=1;
tempPivot=arrayToUse[i];
break;
}
}
}
if(checkIdentical!=0)
{
int useNewPivotMax=0;
MPI_Reduce(&useNewPivot,&useNewPivotMax,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD); //FIRST(OPTIONAL) REDUCE : MAX useNewPivot
if(useNewPivotMax!=1) //That means that the only values left are equal to the pivot!
{
median=pivot;
finalize=1;
MPI_Bcast(&finalize,1,MPI_INT,0,MPI_COMM_WORLD); //FIRST(OPTIONAL) BROADCAST : WAIT FOR FINALIZE COMMAND OR NOT
gettimeofday(&second, &tzp);
if(first.tv_usec>second.tv_usec)
{
second.tv_usec += 1000000;
second.tv_sec--;
}
lapsed.tv_usec = second.tv_usec - first.tv_usec;
lapsed.tv_sec = second.tv_sec - first.tv_sec;
printf("Time elapsed: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
validation(median,partLength,size,numberPart,processId);
MPI_Finalize();
free(pivotArray);
return median;
}
else
{
finalize=0;
int useit=0;
randomCounter2++;
MPI_Bcast(&finalize,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Gather(&useNewPivot, 1, MPI_INT, pivotArray, 1, MPI_INT, 0, MPI_COMM_WORLD); //Gather every value and chose a node to change the pivot.
for(i=0;i<activeSize;i++)
{
if(pivotArray[i]==1)
{
if((randomCounter2>1)&&(randomNode!=activeNodes[i])) //Check if the same node has already been used in a similar operation.
{
useit=1;
randomNode=activeNodes[i];
randomCounter2=0;
break;
}
else if(randomCounter2<2)
{
useit=1;
randomNode=activeNodes[i];
break;
}
}
}
if(useit!=0)
useNewPivot=1;
else
useNewPivot=0;
}
}
if(useNewPivot!=0)
MPI_Bcast(&randomNode,1,MPI_INT,0,MPI_COMM_WORLD); //THIRD(OPTIONAL) BROADCAST : BROADCAST THE SPECIAL NODE
if(useNewPivot==0) //if we didnt choose a special Node, choose the node that will pick the pivot in a clockwise manner. Only selects one of the active nodes.
{
if(randomCounter>=activeSize)
randomCounter=0; //Fail safe
randomNode=activeNodes[randomCounter];
randomCounter++; //Increase the counter
MPI_Bcast(&randomNode,1,MPI_INT,0,MPI_COMM_WORLD); //FIRST BROADCAST : SENDING randomnode, who will chose
}
if(randomNode==processId) //If i am to choose the pivot.....
{
if(useNewPivot==0)
{
srand(time(NULL));
pivot=arrayToUse[rand() % elements];
MPI_Bcast(&pivot,1,MPI_INT,0,MPI_COMM_WORLD); //SECOND BROADCAST : SENDING PIVOT k ton stelnw sto lao
}
else
{
MPI_Bcast(&tempPivot,1,MPI_INT,0,MPI_COMM_WORLD); //SECOND BROADCAST : SENDING PIVOT k ton stelnw sto lao
pivot=tempPivot;
}
}
else //If not.. wait for the pivot to be received.
MPI_Bcast(&pivot,1,MPI_INT,randomNode,MPI_COMM_WORLD); // SECOND BROADCAST : RECEIVING PIVOT
if(stillActive==1) //If i still have values in my array.. proceed
{
partition(arrayToUse,elements,pivot,&arraySmall,&arrayBig,&endSmall,&endBig); //I partition my array // endsmall=number of elements in small array, it may be 0
// endbig=number of elements in big array, it may be 0
//arraysmall = Points to the position of the small array.NULL if the array is empty
//Same for arraybig
}
else //If i'm not active endBig/endSmall has zero value.
{
endBig=0;
endSmall=0;
}
sumSets=0;
//We add the bigSet Values to decide if we keep the small or the big array
MPI_Reduce(&endBig,&sumSets,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD); //FIRST REDUCE : SUM OF BIG
MPI_Bcast(&sumSets,1,MPI_INT,0,MPI_COMM_WORLD);
if(oldSumSets==sumSets)
checkIdentical=1;
else
{
oldSumSets=sumSets;
checkIdentical=0;
}
//hmetabliti keepBigSet 0 h 1 einai boolean k me autin enimerwnw ton lao ti na kratisei to bigset h to smallset
if(sumSets>k) //an to sumofbigsets > k tote krataw to big SET
{
keepBigSet=1; //to dilwnw auto gt meta tha to steilw se olous
if(endBig==0)
dropoutFlag=1; //wraia.. edw an dw oti to bigset mou einai 0.. alla prepei na kratisw to bigset sikwnw auti ti simaia pou simainei tha ginw inactive ligo pio katw tha to deis
else
{
arrayToUse=arrayBig; //thetw ton neo pinaka na einai o big
elements=endBig; //thetw arithmo stoixeiwn iso me tou big
}
}
else if(sumSets<k) //antistoixa an to sumofbigsets < k tote krataw to small set
{
keepBigSet=0;
k=k-sumSets;
if(endSmall==0)
dropoutFlag=1; //antistoixa koitaw an tha ginw inactive..
else
{
arrayToUse=arraySmall; //dinw times..
elements=endSmall;
}
}
else //edw simainei k=sumofbigsetes ara briskw pivot k telos
{
median=pivot;
finalize=1; //dilwnw finalaize =1
MPI_Bcast(&finalize,1,MPI_INT,0,MPI_COMM_WORLD); //to stelnw se olous, oi opoioi an laboun finalize =1 tote kaloun MPI finalize k telos
gettimeofday(&second, &tzp);
if(first.tv_usec>second.tv_usec)
{
second.tv_usec += 1000000;
second.tv_sec--;
}
lapsed.tv_usec = second.tv_usec - first.tv_usec;
lapsed.tv_sec = second.tv_sec - first.tv_sec;
printf("Time elapsed: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
validation(median,partLength,size,numberPart,processId);
MPI_Finalize();
free(pivotArray);
return median;
}
finalize=0; //an den exw mpei sta if den exw steilei timi gia finalize.. oi alloi omws perimenoun na laboun kati, stelnw loipon to 0 pou simainei sunexizoume
MPI_Bcast(&finalize,1,MPI_INT,0,MPI_COMM_WORLD); //SECOND BROADCAST : WAIT FOR FINALIZE COMMAND OR NOT
//edw tous stelnw to keepbigset gia na doun ti tha dialeksoun
MPI_Bcast(&keepBigSet,1,MPI_INT,0,MPI_COMM_WORLD); //THIRD BROADCAST: SEND keepBigset boolean
if(dropoutFlag==1 && stillActive==1) //edw sumfwna me to dropoutflag pou orisame prin an einai 1 kalw tin sinartisi pou me petaei apo ton pinaka. episis koitaw na eimai active gt an me exei idi petaksei se proigoumeni epanalispi tote den xreiazetai na me ksanapetaksei
{
stillActive=0;
removeElement(activeNodes, &activeSize, 0);
}
int flag;
//edw perimenw na akousw apo ton kathena an sunexizei active h oxi.. an oxi ton petaw.. an einai idi inactive apo prin stelnei kati allo (oxi 1)k den ton ksanapetaw
for(i=0;i<activeSize;i++)
{
if(activeNodes[i]!=0)
{
MPI_Recv(&flag,1,MPI_INT,activeNodes[i],1,MPI_COMM_WORLD,&Stat); //FIRST RECEIVE : RECEIVE active or not
if(flag==1)
removeElement(activeNodes, &activeSize, activeNodes[i]);
}
}
}
}
/***Executed only by Slave nodes!!*****/
void slavePart(int processId,int partLength,int *numberPart,int size) //code here is for the cheap slaves :P
{
int dropoutflag,elements,i,sumSets,finalize,keepBigSet,pivot,randomNode,tempPivot;
int endSmall=0;
int endBig=0;
int *arraySmall,*arrayBig,*arrayToUse;
arrayToUse=numberPart;
elements=partLength;
int stillActive=1;
int *pivotArray;
int oldSumSets=-1;
int checkIdentical=0;
int useNewPivot;
for(;;)
{
finalize=0;
int counter=0;
useNewPivot=0;
if(stillActive==1&&checkIdentical!=0) //If i still have values in my array.. If the Sumed Big Set is identical to the previous one, check for identical values.
{
for(i=0;i<elements;i++)
{
if(pivot==arrayToUse[i])
counter++;
else
{
useNewPivot=1;
tempPivot=arrayToUse[i];
break;
}
}
}
if(checkIdentical!=0)
{
int useNewPivotMax=0;
MPI_Reduce(&useNewPivot,&useNewPivotMax,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
MPI_Bcast(&finalize,1,MPI_INT,0,MPI_COMM_WORLD);//an o master apo to keepbigset k apo to count apofasisei oti teleiwsame mou stelnei 1, alliws 0 sunexizoume
if(finalize==1)
{
int median=0;
validation(median,partLength,size,numberPart,processId);
MPI_Finalize();
return ;
}
else
{
MPI_Gather(&useNewPivot, 1, MPI_INT, pivotArray, 1, MPI_INT, 0, MPI_COMM_WORLD);
}
}
MPI_Bcast(&randomNode,1,MPI_INT,0,MPI_COMM_WORLD); //FIRST BROAD CAST : RECEIVING RANDOM NODE, perimenw na dw poios einaito done
if(randomNode!=processId) //means I am not the one to chose pivot.. so I wait to receive the pivot
MPI_Bcast(&pivot,1,MPI_INT,randomNode,MPI_COMM_WORLD); //SECOND BROADCAST : RECEIVING PIVOT
else if(randomNode==processId) //I am choosing suckers
{
if(useNewPivot==0)
{
srand(time(NULL));
pivot=arrayToUse[rand() % elements];
MPI_Bcast(&pivot,1,MPI_INT,processId,MPI_COMM_WORLD); //SECOND BROADCAST : SENDING PIVOT k ton stelnw sto lao
}
else
{
MPI_Bcast(&tempPivot,1,MPI_INT,processId,MPI_COMM_WORLD); //SECOND BROADCAST : SENDING PIVOT k ton stelnw sto lao
pivot=tempPivot;
}
}
if(stillActive==1) //an eksakolouthw na eimai active, trexw tin partition.. k to count kommati to opio eimape kapou exei problima
{
partition(arrayToUse,elements,pivot,&arraySmall,&arrayBig,&endSmall,&endBig);
}
else
{
endBig=0;
endSmall=0;
}
//an eimai inactive stelnw endbig=0 gia to bigset pou den epireazei
sumSets=0;
MPI_Reduce(&endBig,&sumSets,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD); //FIRST REDUCE : SUM OF BIG, stelnw ola ta bigset gia na athroistoun sotn master
MPI_Bcast(&sumSets,1,MPI_INT,0,MPI_COMM_WORLD);
if(oldSumSets==sumSets)
checkIdentical=1;
else
{
oldSumSets=sumSets;
checkIdentical=0;
}
MPI_Bcast(&finalize,1,MPI_INT,0,MPI_COMM_WORLD);//an o master apo to keepbigset k apo to count apofasisei oti teleiwsame mou stelnei 1, alliws 0 sunexizoume
if(finalize==1)
{
int median=0;
validation(median,partLength,size,numberPart,processId);
MPI_Finalize();
return ;
}
MPI_Bcast(&keepBigSet,1,MPI_INT,0,MPI_COMM_WORLD);//THIRD BROADCAST: Receive keepBigset boolean, edw lambanw an krataw to mikro i megalo set.
//afou elaba ton keepbigset an eimai active krataw enan apo tous duo pinake small h big.. alliws den kanw tpt
//edw antistoixa allazw tous pointers, k eksetazw an exw meinei xwris stoixeia tin opoia periptwsi sikwnw to dropoutflag k pio katw tha dilwsw na ginw inactive
if(stillActive==1)
{
if(keepBigSet==1)
{
if(endBig==0)
dropoutflag=1;
else
{
arrayToUse=arrayBig;
elements=endBig;
}
}
else if(keepBigSet==0)
{
if(endSmall==0)
dropoutflag=1;
else
{
arrayToUse=arraySmall;
elements=endSmall;
}
}
}
//edw einai ligo periploka grammeno, isws exei perita mesa alla, an eimai active k thelw na ginw inactive einai i prwti periptwsi, h deuteri einai eimai inactive hdh k i triti einai sunexizw dunamika
if(dropoutflag==1 && stillActive==1)
{
MPI_Send(&dropoutflag,1,MPI_INT,0,1,MPI_COMM_WORLD); //FIRST SEND : send active or not;
stillActive=0;
}
else if(stillActive==0)
{
dropoutflag=-1;
MPI_Send(&dropoutflag,1,MPI_INT,0,1,MPI_COMM_WORLD); //FIRST SEND : send active or not;
}
else
{
dropoutflag=0;
MPI_Send(&dropoutflag,1,MPI_INT,0,1,MPI_COMM_WORLD); //FIRST SEND : send active or not;
}
}
}
/*****MAIN!!!!!!!!!!*****/
int main (int argc, char **argv)
{
int processId,noProcesses,size,partLength,median;
int *numberPart;
size=atoi(argv[1]);
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &processId); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &noProcesses); /* get number of processes */
if(processId==0)
{
printf("size: %d processes: %d\n",size,noProcesses);
if(noProcesses>1)
{
if(size%noProcesses==0)
partLength=(size/noProcesses);
else
partLength=(size/noProcesses)+1;
sendLengths(size,noProcesses);
numberPart=(int*)malloc(partLength*sizeof(int));
generateNumbers(numberPart,partLength,processId);
}
else
{
numberPart=(int*)malloc(size*sizeof(int));
generateNumbers(numberPart,size,processId);
struct timeval first, second, lapsed;
struct timezone tzp;
gettimeofday(&first, &tzp);
median=selection(numberPart,size);
gettimeofday(&second, &tzp);
if(first.tv_usec>second.tv_usec)
{
second.tv_usec += 1000000;
second.tv_sec--;
}
lapsed.tv_usec = second.tv_usec - first.tv_usec;
lapsed.tv_sec = second.tv_sec - first.tv_sec;
validationST(median,size,numberPart);
printf("Time elapsed: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
printf("Median: %d\n",median);
free(numberPart);
MPI_Finalize();
return 0;
}
}
else
{
MPI_Recv(&partLength,1,MPI_INT,0,1,MPI_COMM_WORLD,&Stat);
numberPart=(int*)malloc(partLength*sizeof(int));
generateNumbers(numberPart,partLength,processId);
}
if(processId==0)
{
median=masterPart(noProcesses,processId,size,partLength,numberPart);
printf("Median: %d\n",median);
}
else
slavePart(processId,partLength,numberPart,size);
free(numberPart);
return 0;
}
/*========================FIND MEDIAN FUNCTIONS====================================
* ================================================================================
* ================================================================================
*/
/****Partitions the Array into larger and smaller than the pivot values****/
void partition (int *array, int elements, int pivot, int **arraysmall, int **arraybig, int *endsmall, int *endbig)
{
int right=elements-1;
int left=0;
int pos;
if(elements==1)
{
if(pivot>array[0])
{
*endsmall=1; //One value in the small part
*endbig=0; //Zero on the big one
*arraysmall=array; //There is no big array therefore NULL value
*arraybig=NULL;
}
else if(pivot<=array[0])
{
*endsmall=0; //The exact opposite of the above actions.
*endbig=1;
*arraysmall=NULL;
*arraybig=array;
}
}
else if(elements>1)
{
while(left<right)
{
while(array[left]<pivot)
{
left++;
if(left>=elements)
{
break;
}
}
while(array[right]>=pivot)
{
right--;
if(right<0)
{
break;
}
}
if(left<right)
{
swap_values(array,left,right);
}
}
pos=right;
if(pos<0) //Arrange the arrays so that they are split into two smaller ones.
{ //One containing the small ones. And one the big ones.
*arraysmall=NULL; //However these arrays are virtual meaning that we only save the pointer values of the beging and end
} //of the "real" one.
else
{
*arraysmall=array;
}
*endsmall=pos+1;
*arraybig=&array[pos+1];
*endbig=elements-pos-1;
}
}
/***==============================================***/
/***==============================================***/
/***=============SERIAL SELECTION==============***/
/***==============================================***/
/***==============================================***/
int selection(int *array,int number)
{
int *arraybig;
int *arraysmall;
int endsmall=0;
int endbig=0;
int *arraytobeused;
int i;
int counter=0;
int k;
int pivot;
int median;
k=(int)number/2+1;
arraytobeused=array;
for(;;)
{
pivot=arraytobeused[rand() % number];
partition(arraytobeused,number,pivot,&arraysmall,&arraybig,&endsmall,&endbig);
if(endbig>k)
{
number=endbig;
arraytobeused=arraybig;
for(i=0;i<endbig;i++)
{
if(pivot==arraybig[i])
counter++;
else
break;
}
if(counter==endbig)
{
median=arraybig[0];
break;
}
else
counter=0;
//end of count equals
}
else if(endbig<k)
{
number=endsmall;
arraytobeused=arraysmall;
k=k-endbig;
}
else
{
median=pivot;
break;
}
}
return median;
}