-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVantagePoint_v_0.4.3.c
2073 lines (1829 loc) · 73.9 KB
/
VantagePoint_v_0.4.3.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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
The MIT License (MIT)
Copyright (c) 2014
Athanassios Kintsakis
Contact
athanassios.kintsakis@gmail.com
akintsakis@issel.ee.auth.gr
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>
#include <unistd.h>
#include <float.h>
struct ELEMENT{
float *coordinates;
};
struct NODE{
struct ELEMENT Vantage_Point;
float median;
struct NODE *Father_Node;
struct NODE *Left_Node;
struct NODE *Right_Node;
};
struct SIMPLE_NODE{
struct ELEMENT Vantage_Point;
float median;
};
struct knn{
float *distances;
struct ELEMENT *Elements;
float t;
int k;
struct ELEMENT query;
int added;
int largest_distance_index;
int *send_array;
};
MPI_Status Stat;
int dimensions;
int MAX_level;
void partition ( float *array, int elements, float pivot, float **arraysmall, float **arraybig, int *endsmall, int *endbig, struct ELEMENT *ElementsArray);
float selection(float *array,int number, struct ELEMENT *ElementsArray);
int correction( float *array, struct ELEMENT *ElementsArray, int length, float pivot);
void create_VP_tree( struct NODE *root, int processId, int noProcesses, struct ELEMENT *ElementsArray, int partLength, int level, MPI_Comm Communicator);
void rearrange( int processId, int noProcesses, int partLength, int FirstBigElementPosition, struct ELEMENT *ElementsArray, MPI_Comm Communicator);
void create_local_VP_tree( struct NODE *root, int length, struct ELEMENT *ElementsArray);
void inform_master( struct SIMPLE_NODE **commonTree, struct NODE *root, int level, int processId);
struct NODE *who_is_my_local_Node( int processId, int noProcesses, struct NODE *root);
void inform_other_nodes( struct SIMPLE_NODE **commonTree, int processId, int level);
void initialize_knn( struct knn *VP_knn, struct ELEMENT Element, int k);
void validate_knn( struct ELEMENT *ElementsArray, int partLength, struct knn *VP_knn);
void update_Node( struct NODE *root, struct SIMPLE_NODE **commonTree, int row_level, int fathers_position);
void search_knn( struct NODE *root, struct knn *VP_knn);
void search_global_knn( struct knn *VP_knn, struct SIMPLE_NODE **commonTree, int level, int position);
void knn( struct NODE *root, struct ELEMENT *ElementsArray, int partLength, struct SIMPLE_NODE **commonTree, int k, struct NODE *local_NODE, struct timeval *fourth, struct timezone *tzp);
/** calculates distance between two elements **/
float dist( float *x, float *y){
int j;
float distance = 0;
for( j = 0; j < dimensions; j++)
distance = distance + powf( x[j] - y[j], 2);
distance = sqrtf( distance);
return distance;
}
/***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;
}
/****Swaps two values in an array****/
void swap_values(float *array,int x,int y, struct ELEMENT *ElementsArray)
{
float temp;
struct ELEMENT tempElement = ElementsArray[x];
temp=array[x];
array[x]=array[y];
ElementsArray[x] = ElementsArray[y];
array[y]=temp;
ElementsArray[y] = tempElement;
}
/*****Send random numbers to every node.*****/
void generateElements( struct ELEMENT *ElementsArray, int partLength, int cal)
{
srand((cal+1)*time(NULL)); //Generate number to fill the array
int i, j;
float a = 100; /* Maximum Number that can be generated */
for(i=0; i<partLength; i++){
ElementsArray[i].coordinates = ( float*)malloc( dimensions * sizeof(float));
for( j = 0; j < dimensions; j++){
ElementsArray[i].coordinates[j] = ((float)rand()/(float)(RAND_MAX)) * a;
}
}
}
void calculate_distances( float *distances, struct ELEMENT *ElementsArray, int partLength, float *VP_coordinates){
int i, j;
float distance;
for( i = 0; i < partLength; i++){
distance = 0;
for( j = 0; j < dimensions; j++)
distance = distance + powf( ElementsArray[i].coordinates[j] - VP_coordinates[j], 2);
distance = sqrtf( distance);
distances[i] = distance;
}
}
/***Validates the stability of the operation****/
void validation(float median,int partLength,int size, float *numberPart,int processId, MPI_Comm Communicator)
{
MPI_Bcast(&median,1,MPI_INT,0,Communicator);
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,Communicator);
MPI_Reduce(&countMin,&sumMin,1,MPI_INT,MPI_SUM,0,Communicator);
MPI_Reduce(&countEq,&sumEq,1,MPI_INT,MPI_SUM,0,Communicator);
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);
}
void validate_Distance_Position( struct ELEMENT *ElementsArray, float *distances, int partLength, float *coordinates){
int i,Flag = 0;
float *new_distances;
new_distances = (float*)malloc( sizeof(float)*partLength);
calculate_distances( new_distances, ElementsArray, partLength, coordinates);
for( i = 0; i < partLength; i++){
if( distances[i] != new_distances[i]){
printf("ELEMENTS REARRAGNMENT INSIDE NODE: FAILED\n");
Flag = 1;
break;
}
}
if(Flag != 1)
printf("ELEMENTS REARRAGNMENT INSIDE NODE: PASSED \n");
}
void validations_Global_rearrangment( int processId , int noProcesses, struct NODE *root, int partLength, struct ELEMENT *ElementsArray){
float *distances;
int Flag = 0, i;
distances = (float*)malloc( partLength * sizeof(float));
calculate_distances( distances, ElementsArray, partLength, root->Vantage_Point.coordinates);
if( processId < noProcesses/2){
for( i = 0; i < partLength; i++){
if( distances[i] > root->median){
Flag = 1;
break;
}
}
}
else{
for( i = 0; i < partLength; i++){
if( distances[i] <= root->median){
Flag = 1;
break;
}
}
}
if( Flag == 0)
printf("VALIDATION PASSED.THREAD %d: Elements Rearranged globally correctly\n", processId);
else
printf("VALIDATION FAILED distance %f > %f: GLOBAL REARRAGNMENT FAILED \n", distances[i], root->median);
}
/****Part executed only by the Master Node****/
float masterPart( int noProcesses, int processId, int size, int partLength, float *distances, struct ELEMENT *ElementsArray, MPI_Comm Communicator)
{
int elements,i,keepBigSet,sumSets,finalize,randomNode,k;
int endSmall=0;
int dropoutFlag=0;
int endBig=0;
int *activeNodes;
int activeSize=noProcesses;
int stillActive=1;
int oldSumSets=-1;
int checkIdentical=0;
int useNewPivot=0;
float *arraySmall,*arrayBig,*arrayToUse, *pivotArray;
float median, pivot, tempPivot;
struct ELEMENT *ElementsArrayToUse;
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=distances;
ElementsArrayToUse = ElementsArray;
pivotArray=(float*)malloc(noProcesses*sizeof(float)); //Used for special occasions to gather values different than the pivot.
ptrdiff_t diff;
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,Communicator); //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,Communicator); //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,distances,processId, Communicator);
//MPI_Finalize();
free(pivotArray);
return median;
}
else
{
finalize=0;
int useit=0;
randomCounter2++;
MPI_Bcast(&finalize,1,MPI_INT,0,Communicator);
MPI_Gather(&useNewPivot, 1, MPI_INT, pivotArray, 1, MPI_FLOAT, 0, Communicator); //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,Communicator); //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,Communicator); //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_FLOAT,0,Communicator); //SECOND BROADCAST : SENDING PIVOT k ton stelnw sto lao
}
else
{
MPI_Bcast(&tempPivot,1,MPI_FLOAT,0,Communicator); //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_FLOAT,randomNode,Communicator); // SECOND BROADCAST : RECEIVING PIVOT
if(stillActive==1) //If i still have values in my array.. proceed
{
partition(arrayToUse,elements,pivot,&arraySmall,&arrayBig,&endSmall,&endBig, ElementsArrayToUse); //I partition my array // endsmall=number of elements in small array, it may be 0
}
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,Communicator); //FIRST REDUCE : SUM OF BIG
MPI_Bcast(&sumSets,1,MPI_INT,0,Communicator);
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
ElementsArrayToUse = &ElementsArrayToUse[endSmall];
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..
ElementsArrayToUse = ElementsArrayToUse;
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,Communicator); //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,distances,processId, Communicator);
//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,Communicator); //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,Communicator); //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,Communicator,&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, float *distances, int size, struct ELEMENT *ElementsArray, MPI_Comm Communicator)
{
int dropoutflag,elements,i,sumSets,finalize,keepBigSet,randomNode;
int endSmall=0;
int endBig=0;
float *arraySmall,*arrayBig,*arrayToUse;
arrayToUse=distances;
elements=partLength;
int stillActive=1;
float *pivotArray;
int oldSumSets=-1;
int checkIdentical=0;
int useNewPivot;
float pivot, tempPivot;
struct ELEMENT *ElementsArrayToUse;
ElementsArrayToUse = ElementsArray;
ptrdiff_t diff;
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,Communicator);
MPI_Bcast(&finalize,1,MPI_INT,0,Communicator);//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,distances,processId, Communicator);
//MPI_Finalize();
return ;
}
else
{
MPI_Gather(&useNewPivot, 1, MPI_INT, pivotArray, 1, MPI_FLOAT, 0, Communicator);
}
}
MPI_Bcast(&randomNode,1,MPI_INT,0,Communicator); //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_FLOAT,randomNode,Communicator); //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_FLOAT,processId,Communicator); //SECOND BROADCAST : SENDING PIVOT k ton stelnw sto lao
}
else
{
MPI_Bcast(&tempPivot,1,MPI_FLOAT,processId,Communicator); //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, ElementsArrayToUse);
}
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,Communicator); //FIRST REDUCE : SUM OF BIG, stelnw ola ta bigset gia na athroistoun sotn master
MPI_Bcast(&sumSets,1,MPI_INT,0,Communicator);
if(oldSumSets==sumSets)
checkIdentical=1;
else
{
oldSumSets=sumSets;
checkIdentical=0;
}
MPI_Bcast(&finalize,1,MPI_INT,0,Communicator);//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,distances,processId, Communicator);
//MPI_Finalize();
return ;
}
MPI_Bcast(&keepBigSet,1,MPI_INT,0,Communicator);//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;
ElementsArrayToUse = &ElementsArrayToUse[endSmall];
elements=endBig;
}
}
else if(keepBigSet==0)
{
if(endSmall==0)
dropoutflag=1;
else
{
arrayToUse=arraySmall;
ElementsArrayToUse = ElementsArrayToUse;
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,Communicator); //FIRST SEND : send active or not;
stillActive=0;
}
else if(stillActive==0)
{
dropoutflag=-1;
MPI_Send(&dropoutflag,1,MPI_INT,0,1,Communicator); //FIRST SEND : send active or not;
}
else
{
dropoutflag=0;
MPI_Send(&dropoutflag,1,MPI_INT,0,1,Communicator); //FIRST SEND : send active or not;
}
}
}
/*****MAIN!!!!!!!!!!*****/
int main (int argc, char **argv){
int processId,noProcesses,size,partLength, power, i, j, k, q;
struct ELEMENT *ElementsArray;
struct timeval first, second, third, fourth, lapsed;
struct timezone tzp;
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 */
dimensions = atoi(argv[2]);
/* CONSTRAINTS */
if(processId==0){
/* Execution Constraint */
if( argc != 4){
printf("ERROR:Execution requires exact 1 argument\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
/* Number Of Processes Constraint */
/* if( noProcesses%2 != 0 || noProcesses == 1){
printf("ERROR: Number Of Processes must be multiple of 2\n");
MPI_Abort(MPI_COMM_WORLD, 2);
} */
/* Dimensions Constraints */
if( dimensions < 1){
printf("ERROR: Dimensions non-positive\n");
MPI_Abort(MPI_COMM_WORLD, 2);
}
}
/* END OF CONSTRAINTS */
power = atoi(argv[1]);
MAX_level = logf(noProcesses)/logf(2);
size = 1 << power; /* size of Elements 2^power */
partLength = size/noProcesses; /* Length of Array of Elements in each Node */
ElementsArray = ( struct ELEMENT*)malloc( partLength * sizeof( struct ELEMENT)); /* Allocate space for partLength Elements */
if(processId==0)
{
printf("size: %d processes: %d\n",size,noProcesses);
if(noProcesses>1)
{
generateElements( ElementsArray, partLength, processId); /* Generate Elements */
}
else
{
ElementsArray = ( struct ELEMENT*)malloc( partLength * sizeof( struct ELEMENT)); /* Allocate space for partLength Elements */
generateElements( ElementsArray, partLength, processId);
struct timeval first, second, lapsed;
struct timezone tzp;
gettimeofday(&first, &tzp);
struct NODE root;
root.Father_Node = NULL;
create_local_VP_tree( &root, partLength, ElementsArray);
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,distances);
//printf("Time elapsed: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
// printf("Median: %d\n",median);
//free(distances);
// MPI_Finalize();
//return 0;
k = 1<<atoi(argv[3]);
MPI_Barrier(MPI_COMM_WORLD);
if(processId == 0){
gettimeofday(&third, &tzp);
}
struct knn *VP_knn = (struct knn*)malloc(partLength * sizeof(struct knn));
//knn( &root, ElementsArray, partLength, commonTree, k + 1, local_NODE, &fourth, &tzp);
for( i = 0; i < partLength; i++){
initialize_knn( &VP_knn[i], ElementsArray[i], k+1);
search_knn( &root, &VP_knn[i]);
//search_global_knn( &VP_knn[i], commonTree, 0, 0);
}
MPI_Barrier(MPI_COMM_WORLD);
if(processId == 0){
gettimeofday(&fourth, &tzp);
}
if(processId == 0){
sleep(2);
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 of Tree Creation: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
if(third.tv_usec>fourth.tv_usec)
{
second.tv_usec += 1000000;
second.tv_sec--;
}
lapsed.tv_usec = fourth.tv_usec -third.tv_usec;
lapsed.tv_sec = fourth.tv_sec - third.tv_sec;
printf("Time elapsed of All-knn search: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
}
MPI_Finalize();
free( ElementsArray);
return 0;
}
}
else
{
generateElements( ElementsArray, partLength, processId); /* Generate Elements */
}
/* print arrays of all Nodes */
/* for( int i = 0; i < noProcesses; i++){
if( processId == i){
printf("THREAD %d: \n", processId);
for( int j = 0; j < partLength; j++){
printf("[");
for( int k = 0; k < dimensions; k++)
printf("%.2f ", ElementsArray[j].coordinates[k]);
printf("]");
}
printf("\n");
}
MPI_Barrier(MPI_COMM_WORLD);
} */
MPI_Barrier(MPI_COMM_WORLD);
if(processId == 0){
gettimeofday(&first, &tzp);
}
struct NODE root;
root.Father_Node = NULL;
if( processId == 0){
create_VP_tree( &root, processId, noProcesses, ElementsArray, partLength, MAX_level, MPI_COMM_WORLD);
}
else{
create_VP_tree( &root, processId, noProcesses, ElementsArray, partLength, MAX_level, MPI_COMM_WORLD);
}
//MPI_Barrier(MPI_COMM_WORLD);
//sleep(1);
/* Allocate a 2D array which represents common tree */
/* array[0][1] root *
* array[1][2] Left Node Right Node *
* array[2][4] Left Node Right Node Left Node Right Node *
* array[3][8] /\ /\ /\ /\ */
struct SIMPLE_NODE **commonTree;
commonTree = (struct SIMPLE_NODE**)malloc((MAX_level ) * sizeof( struct SIMPLE_NODE*));
for( i = 0; i < MAX_level + 1; i++){
commonTree[i] = (struct SIMPLE_NODE*)malloc(pow(2, i) * sizeof(struct SIMPLE_NODE));
}
inform_master( commonTree, &root, MAX_level, processId);
//MPI_Barrier(MPI_COMM_WORLD);
//sleep(1);
struct NODE *local_NODE = who_is_my_local_Node( processId, noProcesses, &root);
//MPI_Barrier(MPI_COMM_WORLD);
//sleep(1);
/* for( i = 0; i < noProcesses; i++){
if( processId == i){
printf("THREAD %d: VANTAGE POINT is", processId);
for( k = 0; k < dimensions; k++)
printf(" %f", local_NODE->Vantage_Point.coordinates[k]);
printf(" Median is %f\n", local_NODE->median);
}
MPI_Barrier(MPI_COMM_WORLD);
} */
inform_other_nodes( commonTree, processId, MAX_level);
//MPI_Barrier(MPI_COMM_WORLD);
//sleep(1);
/* for( q = 0; q < noProcesses; q++){
if( processId == q){
for( i = 0; i < MAX_level; i++){
for( j = 0; j < pow(2, i); j++){
printf("[%d][%d] Vantage Point :", i, j);
for( k = 0; k < dimensions; k++)
printf(" %f", commonTree[i][j].Vantage_Point.coordinates[k]);
printf(", Median is %f\n", commonTree[i][j].median);
}
}
}
MPI_Barrier( MPI_COMM_WORLD);
} */
MPI_Barrier(MPI_COMM_WORLD);
if(processId == 0){
gettimeofday(&second, &tzp);
}
k = 1<<atoi(argv[3]);
MPI_Barrier(MPI_COMM_WORLD);
if(processId == 0){
gettimeofday(&third, &tzp);
}
struct knn *VP_knn = (struct knn*)malloc(partLength * sizeof(struct knn));
//knn( &root, ElementsArray, partLength, commonTree, k + 1, local_NODE, &fourth, &tzp);
for( i = 0; i < partLength; i++){
initialize_knn( &VP_knn[i], ElementsArray[i], k+1);
search_knn( &root, &VP_knn[i]);
//search_global_knn( &VP_knn[i], commonTree, 0, 0);
}
MPI_Barrier(MPI_COMM_WORLD);
if(processId == 0){
gettimeofday(&fourth, &tzp);
}
if(processId == 0){
sleep(2);
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 of Tree Creation: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
if(third.tv_usec>fourth.tv_usec)
{
second.tv_usec += 1000000;
second.tv_sec--;
}
lapsed.tv_usec = fourth.tv_usec -third.tv_usec;
lapsed.tv_sec = fourth.tv_sec - third.tv_sec;
printf("Time elapsed of All-knn search: %lu, %lu s\n", lapsed.tv_sec, lapsed.tv_usec);
}
MPI_Finalize();
free( ElementsArray);
return 0;
}
/*---------CREATE GLOBAL-LOCAL VANTAGE POINT TREE FUNCTIONS-----------*/
/*** Create Vantage Point tree ***/
void create_VP_tree( struct NODE *root, int processId, int noProcesses, struct ELEMENT *ElementsArray, int partLength, int level, MPI_Comm Communicator){
int VP_index, size, i, j;
int FirstBigElementPosition;
float temp, median;
float *distances;
root->Left_Node = NULL;
root->Right_Node = NULL;
if(level > 0) size = pow(2, level)*partLength;
//printf("THREAD %d: creating Vantage Point tree : LEVEL %d \n", processId, level);
root->Vantage_Point.coordinates = ( float*)malloc( dimensions * sizeof(float)); /* Allocate Memory for Coordinates */
if( level > 0){
if( processId == 0){ /* If i am the Master */
srand( time(NULL)); /* I select Vantage Point randomly*/
VP_index = rand() % partLength; /* Vantage Point index */
for( i = 0; i < dimensions; i++) /* Store Vantage Point Coordinates */
root->Vantage_Point.coordinates[i] = ElementsArray[VP_index].coordinates[i];
printf("THREAD %d: Vantage Point is:", processId); /*Print Vantage Point */
for( i = 0; i < dimensions; i++){
//printf(" %.2f", root->Vantage_Point.coordinates[i]);
//if( i == dimensions - 1) printf("\n");
}
for( i = 0; i < dimensions; i++){ /* BROADCAST Vantage Point: ANNOUNCE */
temp = root->Vantage_Point.coordinates[i];
MPI_Bcast( &temp, 1, MPI_FLOAT, 0, Communicator);
}
}
else{ /* if I am not the master */
//sleep(1); //Dimitris: This sleep is used to synchronize printfs ***REMOVE IT***
//printf("THREAD %d: Waiting Vantage Point to be announced\n", processId);
for( i = 0; i < dimensions; i++){ /* BROADCAST Vantage Point: RECEIVE */
MPI_Bcast( &temp, 1, MPI_FLOAT, 0, Communicator);
root->Vantage_Point.coordinates[i] = temp;
}
//printf("THREAD %d: Vantage Point is:", processId); /*Print Vantage Point */
for( i = 0; i < dimensions; i++){
//printf(" %.2f", root->Vantage_Point.coordinates[i]);
//if( i == dimensions - 1) printf("\n");
}
}
distances = ( float*)malloc( partLength * sizeof(float));
calculate_distances( distances, ElementsArray, partLength, &(root->Vantage_Point.coordinates[0]));
/* print arrays of all Nodes */
/* for( int i = 0; i < noProcesses; i++){
if( processId == i){
printf("THREAD %d: \n", processId);
for( int j = 0; j < partLength; j++){
printf("%.2f ", distances[j]);
}
printf("\n");
}
MPI_Barrier( Communicator);
sleep(1);
} */
if( processId == 0){
median = masterPart( noProcesses, processId, size, partLength, distances, ElementsArray, Communicator);
//printf("Median: %f\n", median);
}
else{
slavePart( processId, partLength, distances, size, ElementsArray, Communicator);
}
MPI_Bcast( &median, 1, MPI_FLOAT, 0, Communicator); /* Inform All the THREADS about median */
//MPI_Barrier(Communicator);
root->median = median;
FirstBigElementPosition = correction( distances,ElementsArray, partLength, median);
if(FirstBigElementPosition == partLength)
printf("THREAD: %d: NO BIG ELEMENTS\n", processId);
else
printf("THREAD: %d FirstBigElementPosition: %d is with value %f \n",processId, FirstBigElementPosition,distances[FirstBigElementPosition]);
//printf("THREAD %d: FBE: %d\n", processId, FirstBigElementPosition);
/* print arrays of all Nodes */
/* for( int i = 0; i < noProcesses; i++){
if( processId == i){
printf("THREAD %d: \n", processId);
for( int j = 0; j < partLength; j++){
printf("%.2f ", distances[j]);
}
printf("\n");
}
MPI_Barrier( Communicator);
} */
//MPI_Barrier(Communicator);
validate_Distance_Position( ElementsArray, distances, partLength,&(root->Vantage_Point.coordinates[0])); /* This Function Validates that Elements have been rearranged according to distances */
//MPI_Barrier(Communicator);
//sleep(1);
rearrange( processId, noProcesses, partLength, FirstBigElementPosition, ElementsArray, Communicator);
//MPI_Barrier(Communicator);
//sleep(1);
//printf("Before Val \n");
validations_Global_rearrangment( processId , noProcesses, root, partLength, ElementsArray);
free(distances);
//MPI_Barrier(Communicator);
//sleep(1);
/* Create New Communicator */
/* Split the old Communicator in two teams */
int color = processId/(noProcesses/2);
MPI_Comm newCommunicator;
MPI_Comm_split(Communicator, color, processId, &newCommunicator);
int newProcessId;
int newSize;
MPI_Comm_rank( newCommunicator, &newProcessId);
MPI_Comm_size( newCommunicator, &newSize);
//MPI_Barrier(MPI_COMM_WORLD);
//sleep(1);
if( processId < noProcesses/2){