include stdio include stdlib include mpi int main int argc char argv i

 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
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char** argv)
{
int size, rank;
MPI_Status stat;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int root;
root = 0;
int r = 0;
srand(time(NULL));
if (rank == 0)
r = rand();
printf("Rank: %d; val: %d\n", rank, r);
MPI_Barrier(MPI_COMM_WORLD);
double t1 =MPI_Wtime();
if (rank == root)
{
int i;
for (i = 0; i < size; i++)
if (i != root)
MPI_Send(&r, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
else
{
MPI_Recv(&r, 1, MPI_INT, root, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
}
printf("\n");
MPI_Barrier(MPI_COMM_WORLD);
double t2 = MPI_Wtime();
printf("Rank: %d; val: %d\n", rank, r);
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0)
printf("Потрачено: %f\n", t2 - t1);
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char** argv)
{
int size, rank;
MPI_Status stat;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int root;
root = 0;
int r = 0;
srand(time(NULL));
if (rank == 0)
r = rand();
printf("Rank: %d; val: %d\n", rank, r);
MPI_Barrier(MPI_COMM_WORLD);
double t1 = MPI_Wtime();
MPI_Bcast(&r, 1, MPI_INT, 0, MPI_COMM_WORLD);
double t2 = MPI_Wtime();
printf("\n");
MPI_Barrier(MPI_COMM_WORLD);
printf("Rank: %d; val: %d\n", rank, r);
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0)
printf("Потрачено: %f\n", t2 - t1);
MPI_Finalize();
return 0;
}