-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
816 lines (668 loc) · 18.6 KB
/
Source.cpp
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <io.h>
#include "mpi.h"
#include "iostream"
using namespace std;
void matrixVectorMultiply(int argc, char* argv[]);
void hello_world(int argc, char* argv[]);
void vectorComponentsSum(int argc, char* argv[]);
void vectorMax(int argc, char* argv[]);
void maxMatrix(int argc, char* argv[]);
void dotProduct(int argc, char* argv[]);
void monteCarloPi(int argc, char* argv[]);
void averagePositiveCompon(int argc, char* argv[]);
void vectorInvert(int argc, char* argv[]);
void matrixVectorMultiplyColumn(int argc, char* argv[]);
int MatrixNormal(int argc, char **argv);
int MatrixInverse(int argc, char **argv);
int multMatrixFinal(int argc, char* argv[]);
int timeRecv(int argc, char *argv[]);
int poKrugu(int argc, char *argv[]);
int InverFinal(int argc, char *argv[]);
void main(int argc, char* argv[]){
//matrixVectorMultiply(argc, argv);
//hello_world(argc, argv);
//vectorComponentsSum(argc, argv);
//vectorMax(argc, argv);
//maxMatrix(argc, argv);
//dotProduct(argc, argv);
//monteCarloPi(argc, argv);
//averagePositiveCompon(argc, argv);
//matrixVectorMultiply(argc, argv);
//MatrixNormal(argc, argv);
//multMatrixFinal(argc, argv);
//timeRecv(argc, argv);
//poKrugu(argc, argv);
InverFinal(argc, argv);
}
/*Hello World 2áàëëà*/
void hello_world(int argc, char* argv[]){
int ProcNum, ProcRank, RecvRank;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0){
// Äåéñòâèÿ, âûïîëíÿåìûå òîëüêî ïðîöåññîì ñ ðàíãîì 0
printf("\n Hello from process %3d", ProcRank);
for (int i = 1; i < ProcNum; i++) {
MPI_Recv(&RecvRank, 1, MPI_INT, MPI_ANY_SOURCE,
MPI_ANY_TAG, MPI_COMM_WORLD, &Status);
printf("\n Hello from process %3d", RecvRank);
}
}
else
// Ñîîáùåíèå, îòïðàâëÿåìîå âñåìè ïðîöåññàìè,
// êðîìå ïðîöåññà ñ ðàíãîì 0
MPI_Send(&ProcRank, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
MPI_Finalize();
}
/*ñóììà êîìïîí. âåêòîðà*/
void vectorComponentsSum(int argc, char* argv[]){
double x[100], TotalSum, ProcSum = 0.0;
int ProcRank, ProcNum, N = 100;
MPI_Status Status;
// èíèöèàëèçàöèÿ
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
// ïîäãîòîâêà äàííûõ
if (ProcRank == 0) {
for (int i = 0; i < N; i++){
x[i] = 1;
}
}
// ðàññûëêà äàííûõ íà âñå ïðîöåññû
MPI_Bcast(x, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
// âû÷èñëåíèå ÷àñòè÷íîé ñóììû íà êàæäîì èç ïðîöåññîâ
// íà êàæäîì ïðîöåññå ñóììèðóþòñÿ ýëåìåíòû âåêòîðà x îò i1 äî i2
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++)
ProcSum = ProcSum + x[i];
// ñáîðêà ÷àñòè÷íûõ ñóìì íà ïðîöåññå ñ ðàíãîì 0
if (ProcRank == 0) {
TotalSum = ProcSum;
for (int i = 1; i < ProcNum; i++) {
MPI_Recv(&ProcSum, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD,
&Status);
TotalSum = TotalSum + ProcSum;
}
}
else // âñå ïðîöåññû îòñûëàþò ñâîè ÷àñòè÷íûå ñóììû
MPI_Send(&ProcSum, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
// âûâîä ðåçóëüòàòà
if (ProcRank == 0)
printf("\nTotal Sum = %10.2f", TotalSum);
MPI_Finalize();
system("pause");
}
/*2. Max âåêòîðà(3 áàëëà)*/
void vectorMax(int argc, char* argv[]){
double x[100], TotalMax, ProcMax = 0.0;
int ProcRank, ProcNum, N = 100;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
for (int i = 0; i < N; i++){
int max = 1000;
int min = 0;
x[i] = min + (rand() % (int)(max - min + 1));
}
}
MPI_Bcast(x, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++)
if (x[i]>ProcMax){
ProcMax = x[i];
}
MPI_Reduce(&ProcMax, &TotalMax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (ProcRank == 0)
printf("\nTotal Max = %10.2f", TotalMax);
MPI_Finalize();
system("pause");
}
/*2. Max ìàòðèöû(5 áàëëà)*/
void maxMatrix(int argc, char* argv[]){
double x[100][100], TotalMax, max = 0, ProcMax;
int ProcRank, ProcNum, N = 100;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
int max = 1000;
int min = 0;
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
x[i][j] = min + (rand() % (int)(max - min + 1));
}
}
}
MPI_Bcast(x, N*N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++){
ProcMax = x[i][0];
for (int j = 0; j < N; j++){
if (x[i][j] < ProcMax){
ProcMax = x[i][j];
}
}
if (max < ProcMax){
max = ProcMax;
}
}
MPI_Reduce(&max, &TotalMax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (ProcRank == 0)
printf("\nTotal Max = %10.2f", TotalMax);
MPI_Finalize();
system("pause");
}
/*4. Ñêàëÿðíîå ïðîèçâåäåíèå(3 áàëëà)*/
void dotProduct(int argc, char* argv[]){
double x[100],y[100], TotalSum, ProcSum = 0.0;
int ProcRank, ProcNum, N = 100;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
int max = 1000;
int min = 0;
for (int i = 0; i < N; i++){
x[i] = 1;
y[i] = 1;
}
}
MPI_Bcast(x, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Bcast(y, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++){
ProcSum = ProcSum + x[i] * y[i];
}
MPI_Reduce(&ProcSum, &TotalSum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (ProcRank == 0)
printf("\nTotal Sum = %10.2f", TotalSum);
MPI_Finalize();
system("pause");
}
/*16. Âû÷èñëåíèå ÷èñëà Ïè ìåòîäîì Ìîíòå-Êàðëî ( 5 áàëëîâ) */
void monteCarloPi(int argc, char* argv[]){
double x, y, pi;
int ProcRank, ProcNum, N = 1000000, in = 0,allIn;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
int k = N / ProcNum;
srand(time(NULL)); // change seed value for random number generation
for (int i = 1; i <= k; i++){
x = (double) rand() / RAND_MAX;;
y = (double)rand() / RAND_MAX;;
if (x*x + y*y <= 1)
in++;
}
MPI_Reduce(&in, &allIn, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (ProcRank == 0){
pi =(double) 4*allIn/N;
printf("\nPI = %10.2f", pi);
}
MPI_Finalize();
system("pause");
}
/*14. Ñðåäíåå àðèôìåòè÷åñêîå ñðåäè ïîëîæèòåëüíûõ ÷èñåë ìàññèâà */
void averagePositiveCompon(int argc, char* argv[]){
double x[100], TotalAver, ProcAver = 0.0;
int ProcRank, ProcNum, N = 100, TotalCount, ProcCount = 0;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
for (int i = 0; i < N; i++){
int max = 1000;
int min = -1000;
x[i] = min + (rand() % (int)(max - min + 1));
}
}
MPI_Bcast(x, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++)
if (x[i]>0){
ProcAver=+x[i];
ProcCount++;
}
MPI_Reduce(&ProcAver, &TotalAver, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&ProcCount, &TotalCount, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Finalize();
double average = (double)TotalAver / TotalCount;
printf("\nTotal Average = %10.2f", average);
system("pause");
}
/* Óìíîæåíèå ìàòðèöû íà âåêòîð ïî ñòðîêàì */
void matrixVectorMultiply(int argc, char* argv[]){
double x[100][100],y[100],z[100], TotalProd[100];
int ProcRank, ProcNum, N = 100;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
int max = 1000;
int min = 0;
for (int i = 0; i < N; i++){
z[i] = 0;
y[i] = 1;
for (int j = 0; j < N; j++){
x[i][j] = 1;
}
}
}
MPI_Bcast(x, N*N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Bcast(y, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++)
for (int j = 0; j<N;j++){
z[i] =+ y[j] * x[i][j];
}
MPI_Reduce(&z, &TotalProd, N, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Finalize();
for (int i = 0; i < N; i++){
printf("\nProds[%i] = %10.2f", i, TotalProd[i]);
}
system("pause");
}
/*15. Èíâåðòèðîâàòü ìàññèâ(7 áàëëîâ) */
void vectorInvert(int argc, char* argv[]){
double x[100], z[100], y[100];
int ProcRank, ProcNum, N = 100;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
for (int i = 0; i < N; i++){
int max = 1000;
int min = 0;
y[i] = 0;
x[i] = i;
}
}
MPI_Bcast(x, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Bcast(y, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++)
y[N - 1 - i] = x[i];
MPI_Reduce(&y, &z, N, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Finalize();
for (int i = 0; i < N; i++){
printf("\nProds[%i] = %10.2f", i, z[i]);
}
system("pause");
}
/* óìíîæåíèå ìàòðèöû íà âåêòîð ïî ñòîëáöàì*/
void matrixVectorMultiplyColumn(int argc, char* argv[]){
double x[100][100], y[100], z[100],d[100], TotalRes[100], res[100];
int ProcRank, ProcNum, N = 100;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
int max = 1000;
int min = 0;
for (int i = 0; i < N; i++){
z[i] = 0;
y[i] = 1;
for (int j = 0; j < N; j++){
x[i][j]=1;
}
}
}
MPI_Bcast(x, N*N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Bcast(y, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Bcast(z, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int k = N / ProcNum;
int i1 = k * ProcRank;
int i2 = k * (ProcRank + 1);
if (ProcRank == ProcNum - 1) i2 = N;
for (int i = i1; i < i2; i++)
for (int j = 0; j<N; j++){
z[j] = +y[j] * x[j][i];
}
MPI_Reduce(&z, &TotalRes, N, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Finalize();
for (int i = 0; i < N; i++){
printf("\nProds[%i] = %10.2f", i, TotalRes[i]);
}
system("pause");
}
int MatrixNormal(int argc, char **argv) {
int myrank, total, N_PER_PROC=2;
double *A, *B, *C; // Èñïîëüçóþòñÿ òîëüêî â root
double *a, *b, *c; // Ëåíòà ìàòðèöû [mxn], âåêòîð [n], ðåç-ò [m]
int n, m;
int i, j;
int intBuf[2];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &total);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
printf("Total=%d, rank=%d\n", total, myrank);
if (!myrank) { // Ïîäãîòîâêà èñõ. äàííûõ (òîëüêî root)
n = N_PER_PROC * total;
A = (double *)malloc(sizeof(double)*n*n);
B = (double *)malloc(sizeof(double)*n);
C = (double *)malloc(sizeof(double)*n);
for (i = 0; i<n; i++) {
B[i] = (double)i;
for (j = 0; j<n; j++)
A[i*n + j] = (double)(i + j);
};
};
if (!myrank) {
intBuf[0] = n;
intBuf[1] = N_PER_PROC;
};
MPI_Bcast((void *)intBuf, 2, MPI_INT, 0, MPI_COMM_WORLD);
n = intBuf[0];
m = intBuf[1];
a = (double *)malloc(sizeof(double)*n*m);
b = (double *)malloc(sizeof(double)*n);
c = (double *)malloc(sizeof(double)*m);
if (!myrank) { // Ëèøíåå äåéñòâèå, B íå íóæåí
for (int i = 0; i < n; i++){
b[i] = B[i];
}
};
MPI_Bcast((void *)b, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Scatter((void *)A, n*m, MPI_DOUBLE,
(void *)a, n*m, MPI_DOUBLE, 0, MPI_COMM_WORLD);
for (i = 0; i<m; i++) {
c[i] = 0;
for (j = 0; j<n; j++)
c[i] += a[n*i + j] * b[j];
};
MPI_Gather((void *)c, m, MPI_DOUBLE,
(void *)C, m, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if (!myrank)
for (i = 0; i<n; i++)
printf("%g\n", C[i]);
MPI_Finalize();
system("pause");
exit(0);
}
int MatrixInverse(int argc, char **argv) {
int myrank, total, N_PER_PROC = 2;
double *A, *B, *C;
double *a, *b, *c; // [mxn], [n], [m]
int n, m;
int i, j;
int intBuf[2];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &total);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
printf("Total=%d, rank=%d\n", total, myrank);
if (!myrank) {
n = N_PER_PROC * total;
A = (double *)malloc(sizeof(double)*n*n);
B = (double *)malloc(sizeof(double)*n);
C = (double *)malloc(sizeof(double)*n);
for (i = 0; i<n; i++) {
B[i] = (double)i;
for (j = 0; j<n; j++)
A[i*n + j] = (double)(i + j);
};
for (i = 0; i<n; i++) {
B[i] = (double)i;
for (j = 0; j<n; j++)
A[i*n + j] = (double)(i + j);
};
for ( i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
{
double tmp = A[i*n + j];
A[i*n + j] = A[j*n + i];
A[j*n + i] = tmp;
}
}
};
if (!myrank) {
intBuf[0] = n;
intBuf[1] = N_PER_PROC;
};
MPI_Bcast((void *)intBuf, 2, MPI_INT, 0, MPI_COMM_WORLD);
n = intBuf[0];
m = intBuf[1];
a = (double *)malloc(sizeof(double)*n*m);
b = (double *)malloc(sizeof(double)*n);
c = (double *)malloc(sizeof(double)*m);
if (!myrank) {
for (int i = 0; i < n; i++){
b[i] = B[i];
}
};
MPI_Bcast((void *)b, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Scatter((void *)A, n*m, MPI_DOUBLE,
(void *)a, n*m, MPI_DOUBLE, 0, MPI_COMM_WORLD);
for (i = 0; i<m; i++) {
for (j = 0; j<n; j++)
c[j] = a[n*i + j] * b[j];
};
MPI_Gather((void *)c, m, MPI_DOUBLE,
(void *)C, m, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if (!myrank)
for (i = 0; i<n; i++)
printf("%g\n", C[i]);
MPI_Finalize();
system("pause");
exit(0);
}
int multMatrixFinal(int argc, char* argv[]){
int x[10][10];
int y[10];
int ProcRank, ProcNum, N = 10;
int resultMin;
int resultMax;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
int elements_per_proc = N * N / ProcNum;
int *subarr1 = new int[elements_per_proc];
srand(time(NULL));
if (ProcRank == 0){
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++) {
x[i][j] = rand() % 100;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf(" %d ", x[j][i]);
}
printf("\n");
}
printf("\n");
for (int i = 0; i < N; i++)
{
y[i] = rand() % 100;
printf(" %d ", y[i]);
}
printf("\n\n");
}
// Distribute the arrays
MPI_Scatter(x, elements_per_proc, MPI_INT,
subarr1, elements_per_proc, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(y, N, MPI_INT, 0, MPI_COMM_WORLD);
for (int i = 0; i < elements_per_proc; i++)
subarr1[i] *= y[i % N];
// Perform global max reduction
MPI_Gather(subarr1, elements_per_proc, MPI_INT, x, elements_per_proc, MPI_INT, 0, MPI_COMM_WORLD);
if (ProcRank == 0)
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf(" %d ", x[j][i]);
}
printf("\n");
}
MPI_Finalize();
system("pause");
return 0;
}
int timeRecv(int argc, char *argv[]) {
MPI_Status status;
int rank, size;
int height = 10;
int n = 100;
int a[100];
for (int i = 0; i < n; i++)
a[i] = i;
int min = a[20];
int bufsize;
int *buf;
bufsize = 3 * MPI_BSEND_OVERHEAD + min;
buf = (int *)malloc(bufsize);
//Èíèöèëèçèðóåì mpi
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
//Îòïðàâëÿåì äðóãèì ïðîöåññàì
MPI_Bcast(a, n, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Buffer_attach(buf, bufsize);
double startSend, endSend, startSsend, endSsend, startBsend, endBsend, startRsend, endRsend;
if (rank == 0){
//SEND
startSend = MPI_Wtime();
MPI_Send(&min, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&min, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, &status);
endSend = MPI_Wtime();
//SSEND
startSsend = MPI_Wtime();
MPI_Ssend(&min, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
MPI_Recv(&min, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &status);
endSsend = MPI_Wtime();
//BSEND
startBsend = MPI_Wtime();
MPI_Bsend(&min, 1, MPI_INT, 1, 2, MPI_COMM_WORLD);
MPI_Recv(&min, 1, MPI_INT, 1, 2, MPI_COMM_WORLD, &status);
endBsend = MPI_Wtime();
//RSEND-
startRsend = MPI_Wtime();
MPI_Rsend(&min, 1, MPI_INT, 1, 3, MPI_COMM_WORLD);
MPI_Recv(&min, 1, MPI_INT, 1, 3, MPI_COMM_WORLD, &status);
endRsend = MPI_Wtime();
//Âûâîä âðåìåíè
cout << "Send = " << endSend - startSend << " Ssend = " << endSsend - startSsend << " Bsend = " << endBsend - startBsend << " Rsend = " << endRsend - startRsend << endl;
}
if (rank == 1){
//SEND
MPI_Recv(&min, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(&min, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
//SSEND
MPI_Recv(&min, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
MPI_Ssend(&min, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
//BSEND
MPI_Recv(&min, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, &status);
MPI_Bsend(&min, 1, MPI_INT, 0, 2, MPI_COMM_WORLD);
//RSEND
MPI_Recv(&min, 1, MPI_INT, 0, 3, MPI_COMM_WORLD, &status);
MPI_Rsend(&min, 1, MPI_INT, 0, 3, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
system("pause");
}
int poKrugu(int argc, char *argv[]){
int rank, size;
int digit = 2;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
MPI_Ssend(&digit, 1, MPI_INTEGER, 1, 3, MPI_COMM_WORLD);
}
else {
MPI_Recv(&digit, 1, MPI_INTEGER, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
digit++;
if (rank != size - 1) {
MPI_Ssend(&digit, 1, MPI_INTEGER, rank + 1, 4, MPI_COMM_WORLD);
}
else {
MPI_Ssend(&digit, 1, MPI_INTEGER, 0, 5, MPI_COMM_WORLD);
}
}
if (rank == 0) {
MPI_Recv(&digit, 1, MPI_INTEGER, size - 1, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
cout << "Digit is " << digit;
}
MPI_Finalize();
return 0;
}
int InverFinal(int argc, char *argv[]) {
int n = 100;
int *a = new int[100];
for (int i = 0; i < n; i++) {
a[i] = i;
}
int myRank, size;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (myRank != size - 1) {
for (int i = myRank; i < n / 2; i += (size - 1)) {
swap(a[i], a[n - 1 - i]);
}
MPI_Send(a, n, MPI_INT, size - 1, 0, MPI_COMM_WORLD);
}
else {
cout << "Array:" << endl;
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
for (int i = 0; i < size - 1; i++) {
int *tempArr = new int[n];
MPI_Recv(tempArr, n, MPI_INT, i, 0, MPI_COMM_WORLD, &status);
for (int j = i; j < n / 2; j += (size - 1)) {
a[j] = tempArr[j];
a[n - 1 - j] = tempArr[n - 1 - j];
}
}
cout << "Return array :" << endl;
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
MPI_Finalize();
system("pause");
return 0;
}