-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.10.cpp
More file actions
94 lines (81 loc) · 2.12 KB
/
Copy path17.10.cpp
File metadata and controls
94 lines (81 loc) · 2.12 KB
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 <iostream>
#include <mpi.h>
#include <ctime>
#include <string>
using namespace std;
void printMatrix(int *data, int row, int col) {
if (row < 15 && col < 15) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << data[i * col + j] << " ";
}
cout << endl;
}
}
}
int* createMatrix(int row, int col) {
int *matrix;
matrix = new int[row*col];
return matrix;
}
void fullMatrix(int *matrix, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i*col + j] = rand() % 100;
}
}
}
int maxSearch(int a, int b) {
if (a >= b)
return a;
else return b;
}
int main(int argc, char **argv) {
int rank, size;
// int rows, cols;
int *matrix = nullptr;
int *sendCounts = nullptr, *offset = nullptr, *recBuf = nullptr, *localMax = nullptr, *totalMax;
int localBuf, tail;
// double time;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
const int rows = stoi(string(argv[1]));
const int cols = stoi(string(argv[2]));
if (rank == 0) {
matrix = new int[rows * cols];
fullMatrix(matrix, rows, cols);
printMatrix(matrix, rows, cols);
}
// time = MPI_Wtime();
sendCounts = new int[size];
offset = new int[size];
localBuf = rows*cols / size;
totalMax = new int[rows];
//элементы, оставшиеся после разделения между процессами
tail = (rows*cols) % size;
sendCounts[0] = localBuf + tail;
offset[0] = 0;
for (int i = 1; i < size; i++) {
sendCounts[i] = localBuf;
offset[i] = tail + i*localBuf;
}
recBuf = new int[sendCounts[rank]];
MPI_Scatterv(matrix, sendCounts, offset, MPI_INT, recBuf, sendCounts[rank], MPI_INT, 0, MPI_COMM_WORLD);
localMax = new int[sendCounts[rank]];
for (int i = 0; i < sendCounts[rank]; i++) {
localMax[i] = INT_MIN;
}
for (int i = 0; i < sendCounts[rank]; i++) {
if (localMax[i] < recBuf[i])
localMax[i] = recBuf[i];
}
MPI_Reduce(localMax, totalMax, rows, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (int i = 0; i < rows; i++) {
cout << totalMax[i] << endl;
}
}
MPI_Finalize();
return 0;
}