include stdio include mpi define SIZE 40 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
 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
#include <stdio.h>
#include <mpi.h>
#define SIZE 40
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);
if (size > 5 || size < 2) {
if (rank == 0)
printf("Невозможно выполнить рассчёт с такой конфигурацией.\n");
MPI_Finalize();
return 0;
}
int arr[16][4] = {
{0,0,0,0}, {0,0,0,1}, {0,0,1,0}, {0,0,1,1},
{0,1,0,0}, {0,1,0,1}, {0,1,1,0}, {0,1,1,1},
{1,0,0,0}, {1,0,0,1}, {1,0,1,0}, {1,0,1,1},
{1,1,0,0}, {1,1,0,1}, {1,1,1,0}, {1,1,1,1}
};
int range[size][2];
int wts[] = {29, 10, 20, 15};
int calories[] = {64, 20, 31, 24};
if (rank == 0) {
int n_size = size - 1;
int mod = (SIZE + 1) % n_size;
int step;
if (n_size == 1)
step = SIZE / n_size;
else
step = SIZE / n_size - 1;
range[0][0] = 0;
range[0][1] = 0;
range[1][0] = 0;
if (mod > 0) {
range[1][1] = step + 1;
mod--;
}
else
range[1][1] = step;
for (int i = 1; i < size - 1; i++)
{
range[i + 1][0] = range[i][1] + 1;
if (mod > 0) {
range[i + 1][1] = range[i + 1][0] + step + 1;
mod--;
}
else
range[i + 1][1] = range[i + 1][0] + step;
}
}
MPI_Bcast(&range, size * 2, MPI_INT, 0, MPI_COMM_WORLD);
printf("rank: %d b: %d e: %d\n", rank, range[rank][0], range[rank][1]);
MPI_Barrier(MPI_COMM_WORLD);
int r_size = range[rank][1] - range[rank][0];
unsigned short chosen[r_size];
unsigned int result[r_size];
int max;
if (rank != 0) {
int m = 0;
for (int i = range[rank][0]; i < range[rank][1]; i++) {
int sum = 0;
for (int j = 0; j < 4; j++) {
if (arr[i][j] == 1)
sum += wts[j];
}
if (sum > SIZE || sum == 0)
chosen[m++] = 0;
else
chosen[m++] = 1;
}
int k = 0;
m = 0;
for (int i = range[rank][0]; i < range[rank][1]; i++) {
int sum = 0;
if (chosen[m++] == 1) {
for (int j = 0; j < 4; j++) {
if (arr[i][j] == 1)
sum += calories[j];
}
result[k++] = sum;
}
else {
result[k++] = 0;
}
}
max = result[0];
for (int i = 0; i < r_size; i++) {
if (max < result[i])
max = result[i];
}
printf("Rank: %d size: %d\n", rank, max);
}
if (rank != 0) {
MPI_Send(&max, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
else {
int get_result[size - 1];
for (int i = 1; i < size; i++) {
int temp;
MPI_Recv(&temp, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
get_result[i - 1] = temp;
}
int maximum = get_result[0];
for (int i = 0; i < size - 1; i++) {
if (maximum < get_result[i])
maximum = get_result[i];
}
printf("Result %d\n", maximum);
}
MPI_Finalize();
return 0;
}