-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shellings_model.c
964 lines (802 loc) · 40.3 KB
/
Shellings_model.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
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100 //Indica il numero di righe e colonne che la matrice avrà
#define TOLLERANCE 51 //Indica la tolleranza che devono avere gli elementi della matrice
#define PERC_O 50 //Viene indicata la percentuale di elementi 'O' all'interno della matrice
#define PERC_X 100-PERC_O //Viene indicata la percentuale di elementi 'X' all'interno della matrice
#define WHITE_SPACES 30 //Viene indicata la percentuale di caselle vuote all'interno della matrice
#define ITERATIONS 100 //Viene indicato il numero massimo di iterazioni che l'algoritmo esegue
#define RANDOM_MATRIX 1 //Con 0 viene generata una matrice costante, con 1 una matrice casuale
#define PRINT_MATRIX 0 //Con 1 viene stampata la matrice iniziale e finale, con 0 la stampa viene omessa
typedef struct
{
int x;
int y;
}Coord; //La struttura Coord viene utilizzata per salvare la posizione dei vari elementi
typedef struct {
int array_border[2][SIZE]; //array per salvare le righe dei processi vicini
int **array_op; //array per andare a salvare le righe che verranno utilizzate durante la computazione da ciascun processo
Coord *array_empty_coord; //array per salvare le posizioni di tutti gli elementi vuoti presenti all'interno della matrice
Coord *array_local_empty_coord; //array per salvare le posizioni degli elementi vuoti appartenenente ad un processo
int rows_op; //utilizzato per tenere traccia del quantitativo di righe che un processo possiede
int initial_row; //utilizzato per tenere traccia la posizione della riga iniziale della porzione di matrice appartenente ad un processo
int dim_array_local_empty; //utilizzato per tenere traccia del numero di celle vuote appartenenti ad un processo.
} MatElements;
typedef struct
{
int rank;
int val;
int x;
int y;
int prev_x;
int prev_y;
int changed;
} Element; //Questa struttura viene utilizzata per tenere traccia degli elementi scambiati dai diversi processori
MatElements sarray;
Element *elements;
int size_elements=0;
int stop=1;
int numb_white_spaces=SIZE*SIZE*WHITE_SPACES/100;
int array_completo[SIZE][SIZE];
int check(int array_border[],int size); //Controlla se l'elemento rispetta i parametri di tolleranza all'interno della matrice
void swap(int i,int j,int world_rank,int rows,int num_it); //Effettua lo swap dell'elemento all'interno della matrice
void print_matrix_complete(); //Viene stampata la matrice completa
void print_matrix_op(); //Viene stampata la matrice appartenente al singolo processo
void def_array_border(int world_rank,int world_size); //Permette lo scambio di righe di bordo tra i vari processi
void find_places(int world_rank, int world_size,int num_it); //Utilizzata per iterare gli elementi all'interno della matrice, controllando se ogni elemento è soddisfatto
int main(int argc, char** argv) {
MPI_Init(NULL, NULL);
MPI_Status stat;
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Request request[world_size];
int send_counts[world_size];
int displs[world_size];
MPI_Datatype elementswap,oldtypes[1];
int blockcounts[1]={7};
MPI_Aint offsets[1], lb, extent;
offsets[0]=0;
oldtypes[0] = MPI_INT;
blockcounts[0]=7;
MPI_Type_create_struct(1, blockcounts, offsets, oldtypes, &elementswap);
MPI_Type_commit(&elementswap);
MPI_Datatype coordswap;
blockcounts[0]=2;
MPI_Type_create_struct(1, blockcounts, offsets, oldtypes, &coordswap);
MPI_Type_commit(&coordswap);
int char_arr[]={88,79};
elements=malloc(sizeof(Element));
sarray.array_empty_coord=malloc(sizeof(Coord)*numb_white_spaces);
int all_int_info[world_size*2+numb_white_spaces*2];
int count_stop=0;
int numb_o=(SIZE*SIZE-numb_white_spaces)*PERC_O/100;
int numb_x=SIZE*SIZE-numb_white_spaces-numb_o;
int count_o=0;
int count_x=0;
int initial_row[world_size];
if(SIZE<world_size) //Viene fatto un controllo se il numero dei processori sia almeno uguale al numero di righe della matrice
{
if(world_rank == 0)
printf("Dare almeno 1 processore per riga di matrice\n");
MPI_Abort(MPI_COMM_WORLD,MPI_ERR_OP);
}
//Il rank 0 provvede a riempire in modo randomico la matrice con i parametri associati agli elementi
if(world_rank == 0)
{
int counter_for_empty=0;
if(RANDOM_MATRIX == 0)
{
int char_array[]={79,32,88};
for(int i=0;i<SIZE;i++)
for(int j=0;j<SIZE;j++)
{
srand(i*j);
int random_number=rand();
if(char_array[random_number%3]==32 && counter_for_empty < numb_white_spaces)
{
array_completo[i][j]=char_array[random_number%3];
sarray.array_empty_coord[counter_for_empty].x=i;
sarray.array_empty_coord[counter_for_empty].y=j;
counter_for_empty++;
}
else if(char_array[random_number%3]!= 32)
array_completo[i][j]=char_array[random_number%3];
else
array_completo[i][j]=char_arr[random_number%2];
}
}
else
{
int seed_one=0;
int seed_two=0;
while (count_o < numb_o || count_x < numb_x)
{
seed_one++;
seed_two+=3;
srand(time(NULL)+seed_two);
int x_two = rand()%SIZE;
srand(time(NULL)+seed_two*seed_one);
int y_two=rand()%SIZE;
srand(time(NULL)+seed_one*2);
int x=rand()%SIZE;
srand(time(NULL)+seed_one);
int y=rand()%SIZE;
if(array_completo[x][y]!=32 && array_completo[x][y]!= 88 && array_completo[x][y] != 79 && count_o < numb_o)
{
array_completo[x][y]=79;
count_o++;
}
if(array_completo[x_two][y_two]!=32 && array_completo[x_two][y_two]!= 88 && array_completo[x_two][y_two] != 79 && count_x < numb_x)
{
array_completo[x_two][y_two]=88;
count_x++;
}
}
count_x=0;
count_o=0;
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
if(array_completo[i][j]!=88 && array_completo[i][j]!= 79)
{
array_completo[i][j]=32;
sarray.array_empty_coord[counter_for_empty].x=i;
sarray.array_empty_coord[counter_for_empty].y=j;
counter_for_empty++;
}
else if(array_completo[i][j]==79)
count_o++;
else if(array_completo[i][j]==88)
count_x++;
}
}
while(counter_for_empty < numb_white_spaces)
{
int x=rand()%SIZE;
srand(time(NULL));
int y=rand()%SIZE;
if(count_o>numb_o || count_x > numb_x)
{
if(array_completo[x][y]==79 && count_o>numb_o)
{
array_completo[x][y]=32;
sarray.array_empty_coord[counter_for_empty].x=x;
sarray.array_empty_coord[counter_for_empty].y=y;
counter_for_empty++;
count_o--;
}
else if(array_completo[x][y]==88 && count_x>numb_x)
{
array_completo[x][y]=32;
sarray.array_empty_coord[counter_for_empty].x=x;
sarray.array_empty_coord[counter_for_empty].y=y;
counter_for_empty++;
count_x--;
}
}
}
}
if(PRINT_MATRIX)
print_matrix_complete();
int modulo=SIZE%world_size;
int div=SIZE/world_size;
int check=0;
for(int i=0;i<world_size;i++)
{
if(modulo>0)
{
send_counts[i]=SIZE*(div+1);
if(i==0)
{
initial_row[0]=0;
displs[i]=0;
}
else
{
displs[i]=displs[i-1]+send_counts[i-1];
initial_row[i]=displs[i]/SIZE;
}
modulo--;
check=1;
}
else
{
send_counts[i]=SIZE*div;
if(i==0)
{
initial_row[0]=0;
displs[i]=0;
}
else
{
if(check==1)
{
displs[i]=displs[i-1]+send_counts[i-1];
check=0;
}
else
{
displs[i]=displs[i-1]+send_counts[i-1];
}
initial_row[i]=displs[i]/SIZE;
}
}
}
//All'interno dell'array all_int_info vengono salvate tutte le informazioni riguardanti gli elementi che possiede ciascun processore, la riga iniziale e le coordinate degli elementi vuoti
for(int i=0;i<world_size*2;i++)
all_int_info[i]= (i<world_size) ? send_counts[i] : initial_row[i-world_size];
for(int i=0,j=0;j<numb_white_spaces;i+=2,j++)
{
all_int_info[world_size*2+i]=sarray.array_empty_coord[j].x;
all_int_info[world_size*2+i+1]=sarray.array_empty_coord[j].y;
}
}
MPI_Barrier(MPI_COMM_WORLD);
double start, end;
start=MPI_Wtime();
if(world_size>1)
MPI_Bcast(all_int_info, world_size*2 + numb_white_spaces*2, MPI_INT, 0,MPI_COMM_WORLD);
sarray.dim_array_local_empty=0;
if(world_rank != 0)
{
int check_coord=0;
for(int i=0;i<world_size*2;i++)
{
if(i<world_size)
send_counts[i]=all_int_info[i];
else if(i >= world_size && i < world_size*2)
initial_row[i-world_size]=all_int_info[i];
}
for(int i=0,j=0;j<numb_white_spaces;i+=2,j++)
{
sarray.array_empty_coord[j].x=all_int_info[world_size*2+i];
sarray.array_empty_coord[j].y=all_int_info[world_size*2+i+1];
}
}
int rows=send_counts[world_rank]/SIZE;
sarray.rows_op=rows;
sarray.initial_row=initial_row[world_rank];
for(int i=0;i<numb_white_spaces;i++)
{
int empty_slot_x=sarray.array_empty_coord[i].x;
if( empty_slot_x < sarray.initial_row+rows && empty_slot_x >= sarray.initial_row)
{
sarray.dim_array_local_empty++;
sarray.array_local_empty_coord=realloc(sarray.array_local_empty_coord,sizeof(Coord)*sarray.dim_array_local_empty);
sarray.array_local_empty_coord[sarray.dim_array_local_empty-1].x=sarray.array_empty_coord[i].x;
sarray.array_local_empty_coord[sarray.dim_array_local_empty-1].y=sarray.array_empty_coord[i].y;
}
}
int col=SIZE;
int *ptr;
int len=sizeof(int *) * rows + sizeof(int) * col * rows;
int count=0;
sarray.array_op = (int **)malloc(len);
ptr = (int *)(sarray.array_op + rows);
for(int i = 0; i < rows; i++)
sarray.array_op[i] = (ptr + col * i);
if(world_size == 1)
{
for(int i=0;i<rows;i++)
for(int j=0;j<SIZE;j++)
sarray.array_op[i][j]=array_completo[i][j];
}
else
MPI_Scatterv(array_completo, send_counts, displs, MPI_INT,&sarray.array_op[0][0], send_counts[world_rank], MPI_INT, 0, MPI_COMM_WORLD);
def_array_border(world_rank,world_size);
int count_iterations=0;
count_stop=0;
int stop_iterations[world_size];
int round_check=0;
for(int a=0;a<ITERATIONS;a++){
if(world_size > 1)
MPI_Allgather(&stop,1,MPI_INT,stop_iterations,1,MPI_INT,MPI_COMM_WORLD);
else
stop_iterations[0]=stop;
for(int k=0;k<world_size;k++)
if(stop_iterations[k]==0)
count_stop++;
if(count_stop==world_size)
break;
count_stop=0;
count_iterations++;
round_check++;
stop=0;
find_places(world_rank,world_size,a);
if(world_size > 1)
{
int size_all_elements_array[world_size];
MPI_Allgather(&size_elements, 1, MPI_INT, size_all_elements_array, 1, MPI_INT,MPI_COMM_WORLD);
int size_all_elements=0;
int counts_swap[world_size];
int displacements_swap[world_size];
for(int i=0;i<world_size;i++)
{
size_all_elements+=size_all_elements_array[i];
counts_swap[i]=size_all_elements_array[i];
displacements_swap[i]= (i==0)? 0 : displacements_swap[i-1]+size_all_elements_array[i-1];
}
Element *all_elements=malloc(sizeof(Element)*size_all_elements);
MPI_Allgatherv(elements, size_elements, elementswap, all_elements, counts_swap, displacements_swap, elementswap, MPI_COMM_WORLD);
Element *elements_changed=malloc(sizeof(Element));
int size_elements_changed=0;
for(int i=0;i<size_all_elements;i++)
{
int coord_x=all_elements[i].x;
int coord_y=all_elements[i].y;
for(int j=0;j<sarray.dim_array_local_empty;j++)
{
if(coord_x== sarray.array_local_empty_coord[j].x && coord_y == sarray.array_local_empty_coord[j].y)
{
all_elements[i].changed=1;
sarray.array_local_empty_coord[j]=sarray.array_local_empty_coord[sarray.dim_array_local_empty-1];
sarray.dim_array_local_empty--;
sarray.array_local_empty_coord=realloc(sarray.array_local_empty_coord,sizeof(Coord)*sarray.dim_array_local_empty);
int x=all_elements[i].x;
sarray.array_op[x-sarray.initial_row][all_elements[i].y]=all_elements[i].val;
size_elements_changed++;
elements_changed=realloc(elements_changed,sizeof(Element)*size_elements_changed);
elements_changed[size_elements_changed-1]=all_elements[i];
}
}
}
int size_all_elements_changed_array[world_size];
MPI_Allgather(&size_elements_changed, 1, MPI_INT, size_all_elements_changed_array, 1, MPI_INT,MPI_COMM_WORLD);
int size_all_elements_changed=0;
int counts_swap_changed[world_size];
int displacements_swap_changed[world_size];
for(int i=0;i<world_size;i++)
{
size_all_elements_changed+=size_all_elements_changed_array[i];
counts_swap_changed[i]=size_all_elements_changed_array[i];
displacements_swap_changed[i] = (i==0) ? 0 : displacements_swap_changed[i-1]+size_all_elements_changed_array[i-1];
}
Element* all_elements_changed=malloc(sizeof(Element)*size_all_elements_changed);
if(size_all_elements_changed>0)
MPI_Allgatherv(elements_changed, size_elements_changed, elementswap, all_elements_changed, counts_swap_changed, displacements_swap_changed, elementswap, MPI_COMM_WORLD);
for(int i=0; i< size_all_elements_changed;i++)
{
if(all_elements_changed[i].rank==world_rank)
{
sarray.array_op[all_elements_changed[i].prev_x-sarray.initial_row][all_elements_changed[i].prev_y]=32;
sarray.dim_array_local_empty++;
sarray.array_local_empty_coord=realloc(sarray.array_local_empty_coord,sizeof(Coord)*sarray.dim_array_local_empty);
Coord coord;
coord.x=all_elements_changed[i].prev_x;
coord.y=all_elements_changed[i].prev_y;
sarray.array_local_empty_coord[sarray.dim_array_local_empty-1]=coord;
}
}
int size_all_elements_local_empty[world_size];
MPI_Allgather(&sarray.dim_array_local_empty, 1, MPI_INT, size_all_elements_local_empty, 1, MPI_INT,MPI_COMM_WORLD);
int counts_swap_coord[world_size];
int displacements_swap_coord[world_size];
for(int i=0;i<world_size;i++)
{
counts_swap_coord[i]=size_all_elements_local_empty[i];
displacements_swap_coord[i] = (i==0)? 0: displacements_swap_coord[i-1]+size_all_elements_local_empty[i-1];
}
MPI_Allgatherv(sarray.array_local_empty_coord,sarray.dim_array_local_empty,coordswap,sarray.array_empty_coord,counts_swap_coord,displacements_swap_coord,coordswap,MPI_COMM_WORLD);
def_array_border(world_rank,world_size);
free(elements_changed);
free(all_elements);
free(all_elements_changed);
elements=realloc(elements,0);
size_elements=0;
size_elements_changed=0;
}
}
if(world_size > 1)
{
int send_rows[world_size];
MPI_Allgather(&send_counts[world_rank],1,MPI_INT,send_rows,1,MPI_INT,MPI_COMM_WORLD);
int displs_rows[world_size];
for(int i=0;i<world_size;i++)
displs_rows[i]= (i==0) ? 0 : displs_rows[i-1]+send_rows[i-1];
MPI_Gatherv(&sarray.array_op[0][0],send_rows[world_rank],MPI_INT,&array_completo[0][0],send_rows,displs_rows,MPI_INT,0,MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
end = MPI_Wtime();
if(world_rank == 0)
{
if(count_iterations == ITERATIONS)
printf("sono il processo master e non sono riuscito a risolvere la matrice, tempo impiegato:%f\n",end-start);
else
printf("sono il processo master ed ho risolto la matrice ed il tempo impiegato è stato di:%f con un totale di %d iterazioni\n",end-start,count_iterations);
if(PRINT_MATRIX)
{
if(world_size== 1)
print_matrix_op();
else
print_matrix_complete();
}
}
MPI_Type_free(&elementswap);
MPI_Type_free(&coordswap);
MPI_Finalize();
}
int check(int array_border[],int size)
{
int count=0;
int count_to_remove=0;
if(array_border[0]==32)
{
return 0;
}
for(int i = 1 ; i< size;i++)
{
if(array_border[0]==array_border[i])
count++;
if(array_border[i]==32)
count_to_remove++;
}
size--;
int size_final=size-count_to_remove;
int final_result;
if(size_final==0)
final_result=100;
else
final_result=(count*100)/size_final;
if(final_result < TOLLERANCE )
{
return 1;
}
else
{
return 0;
}
}
void swap(int i,int j,int world_rank,int rows,int num_it)
{
stop=1;
srand(time(NULL) + num_it + i + j);
int r=(rand()+num_it*i+j)%numb_white_spaces;
int check=0;
int coord_empty_x=sarray.array_empty_coord[r].x;
int coord_empty_y=sarray.array_empty_coord[r].y;
int local_i;
for(int i=0;i<sarray.dim_array_local_empty;i++)
{
if(coord_empty_x==sarray.array_local_empty_coord[i].x && coord_empty_y == sarray.array_local_empty_coord[i].y)
{
check=1;
local_i=i;
}
}
if(check==1)
{
sarray.array_empty_coord[r].x=i+sarray.initial_row;
sarray.array_empty_coord[r].y=j;
sarray.array_local_empty_coord[local_i].x=i+sarray.initial_row;
sarray.array_local_empty_coord[local_i].y=j;
sarray.array_op[coord_empty_x-sarray.initial_row][coord_empty_y]=sarray.array_op[i][j];
sarray.array_op[i][j]=32;
}
else
{
int check_elements=0;
for(int i=0;i< size_elements;i++)
{
if(elements[i].x==coord_empty_x && elements[i].y == coord_empty_y)
check_elements=1;
}
if(check_elements==0)
{
Element elem;
elem.changed=0;
elem.rank=world_rank;
elem.val=sarray.array_op[i][j];
elem.prev_x=i+sarray.initial_row;
elem.prev_y=j;
elem.x=coord_empty_x;
elem.y=coord_empty_y;
size_elements++;
elements=realloc(elements,sizeof(Element)*size_elements);
elements[size_elements-1]=elem;
}
}
}
void print_matrix_complete()
{
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
if(array_completo[i][j]=='X')
printf("\033[1;33m%c ",array_completo[i][j]);
else
printf("\033[1;31m%c ",array_completo[i][j]);
}
printf("\n");
}
printf("\033[0m");
}
void print_matrix_op()
{
for(int i =0;i< sarray.rows_op;i++)
{
for(int j=0;j< SIZE;j++)
{
if(sarray.array_op[i][j]=='X')
printf("\033[1;33m%c ",sarray.array_op[i][j]);
else
printf("\033[1;31m%c ", sarray.array_op[i][j]);
}
printf("\n");
printf("\033[0m");
}
}
void print_local_empty()
{
for(int i=0;i<sarray.dim_array_local_empty;i++)
{
printf("%d%d ",sarray.array_local_empty_coord[i].x,sarray.array_local_empty_coord[i].y);
}
}
void def_array_border(int world_rank,int world_size)
{
if(world_size > 1)
{
if(world_rank == 0)
{
MPI_Status status;
MPI_Sendrecv(&sarray.array_op[sarray.rows_op-1][0], SIZE, MPI_INT, world_rank+1, 0,&sarray.array_border[0][0], SIZE, MPI_INT, world_rank+1,0, MPI_COMM_WORLD, &status);
}
else if(world_rank == world_size -1)
{
MPI_Status status;
MPI_Sendrecv(&sarray.array_op[0][0], SIZE, MPI_INT, world_rank-1, 0,&sarray.array_border[0][0], SIZE, MPI_INT, world_rank-1,0, MPI_COMM_WORLD, &status);
}
else
{
MPI_Status status;
MPI_Sendrecv(&sarray.array_op[0][0], SIZE, MPI_INT, world_rank-1, 0,&sarray.array_border[0][0], SIZE, MPI_INT, world_rank-1,0, MPI_COMM_WORLD, &status);
MPI_Sendrecv(&sarray.array_op[sarray.rows_op-1][0], SIZE, MPI_INT, world_rank+1, 0,&sarray.array_border[1][0], SIZE, MPI_INT, world_rank+1,0, MPI_COMM_WORLD, &status);
}
}
}
void find_places(int world_rank, int world_size,int num_it)
{
for(int i=0;i<sarray.rows_op;i++)
{
for(int j=0;j<SIZE;j++)
{
int *array_border_pointer;
int size_array_border;
if(j==0 && i == 0)
{
if(world_rank == 0)
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j+1],sarray.array_op[i][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i+1][j],sarray.array_op[i+1][j+1],sarray.array_op[i][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else if(world_rank == world_size-1)
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j+1],sarray.array_op[i][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j+1],sarray.array_op[i][j+1],sarray.array_op[i+1][j],sarray.array_op[i+1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j+1],sarray.array_op[i][j+1],sarray.array_border[1][j],sarray.array_border[1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j+1],sarray.array_op[i][j+1],sarray.array_op[i+1][j],sarray.array_op[i+1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
}
else if(i==0 && j==SIZE-1)
{
if(world_rank == 0)
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_op[i][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i+1][j],sarray.array_op[i+1][j-1],sarray.array_op[i][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else if(world_rank == world_size-1)
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_op[i][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_op[i][j-1],sarray.array_op[i+1][j],sarray.array_op[i+1][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_op[i][j-1],sarray.array_border[1][j],sarray.array_border[1][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_op[i][j-1],sarray.array_op[i+1][j],sarray.array_op[i+1][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
}
else if(i==0 && (j>0 && j<SIZE-1) )
{
if(world_rank == 0)
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_border[0][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i+1][j],sarray.array_op[i+1][j-1],sarray.array_op[i+1][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else if(world_rank == world_size-1)
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_border[0][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_border[0][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1],sarray.array_op[i+1][j],sarray.array_op[i+1][j-1],sarray.array_op[i+1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else
{
if(sarray.rows_op==1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_border[0][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1],sarray.array_border[1][j],sarray.array_border[1][j-1],sarray.array_border[1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[0][j],sarray.array_border[0][j-1],sarray.array_border[0][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1],sarray.array_op[i+1][j],sarray.array_op[i+1][j-1],sarray.array_op[i+1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
}
else if(j==0 && (i>0 && i<sarray.rows_op-1))
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j+1],sarray.array_op[i][j+1],sarray.array_op[i+1][j],sarray.array_op[i+1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else if(j==SIZE-1 && (i>0 && i<sarray.rows_op-1))
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j-1],sarray.array_op[i][j-1],sarray.array_op[i+1][j],sarray.array_op[i+1][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else if((i>0 && i<sarray.rows_op-1) && (j>0 && j<SIZE -1))
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j-1],sarray.array_op[i-1][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1],sarray.array_op[i+1][j-1],sarray.array_op[i+1][j],sarray.array_op[i+1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else if(j == 0 && i==sarray.rows_op-1)
{
if(world_rank == 0)
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j+1],sarray.array_op[i][j+1],sarray.array_border[0][j],sarray.array_border[0][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else if(world_rank == world_size-1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j+1],sarray.array_op[i][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[1][j],sarray.array_border[1][j+1],sarray.array_op[i][j+1],sarray.array_op[i-1][j],sarray.array_op[i-1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else if(j==SIZE-1 && i==sarray.rows_op-1)
{
if(world_rank == 0)
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j-1],sarray.array_op[i][j-1],sarray.array_border[0][j],sarray.array_border[0][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else if(world_rank == world_size-1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j-1],sarray.array_op[i][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_border[1][j],sarray.array_border[1][j-1],sarray.array_op[i][j-1],sarray.array_op[i-1][j],sarray.array_op[i-1][j-1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
else
{
if(world_rank == 0)
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j-1],sarray.array_op[i-1][j+1],sarray.array_op[i][j-1],sarray.array_op[i][j+1],sarray.array_border[0][j-1],sarray.array_border[0][j],sarray.array_border[0][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else if(world_rank == world_size - 1)
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i][j-1],sarray.array_op[i][j+1],sarray.array_op[i-1][j-1],sarray.array_op[i-1][j],sarray.array_op[i-1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
else
{
int array_border[]={sarray.array_op[i][j],sarray.array_op[i-1][j],sarray.array_op[i-1][j-1],sarray.array_op[i-1][j+1], sarray.array_op[i][j-1], sarray.array_op[i][j+1], sarray.array_border[1][j],sarray.array_border[1][j-1],sarray.array_border[1][j+1]};
size_array_border=sizeof(array_border)/sizeof(array_border[0]);
array_border_pointer=array_border;
}
}
if(check(array_border_pointer,size_array_border))
{
swap(i,j,world_rank,sarray.rows_op,num_it);
}
}
}
}