include mpi include ctime include cstdlib include stdio define MAX 100

 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
#include <mpi.h>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#define MAX 1000
#define NUM_OF_ACCOUNTENT 10
int main(int argc, char *argv[])
{
int rank, size;
MPI_Status stat;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0)
{
int report[NUM_OF_ACCOUNTENT];
int result = 0;
int source;
for (int i = 0; i < NUM_OF_ACCOUNTENT; i++)
report[i] = 0;
for (int i = 0; i < NUM_OF_ACCOUNTENT; i++)
{
MPI_Recv(&source, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
report[i] = source;
printf("Бухгалтер %d с результатом %d\n", i + 1, source);
}
for (int i = 0; i < NUM_OF_ACCOUNTENT; i++)
result += report[i];
printf("Результат %d. Еду в инспекцию.\n", result);
}
else
{
int r = 0;
srand(time(NULL));
for (int i = 0; i < 365; i++)
{
if (i % 2 == 0)
r += (rand() % (MAX+1-rank))+rank;
else
r -= (rand() % (MAX+1-rank))+rank;
}
MPI_Send(&r, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}