forked from DamirV/2018_par_prog_381606-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask13.cpp
84 lines (64 loc) · 1.92 KB
/
task13.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
#include <iostream>
#include <ctime>
#include "MPI.h"
int main(int argc, char* argv[])
{
long long int* matrix = 0;
int width, height;
int max = 0, GlobalMax = 0;
srand(6);
int ProcSize, procID;
int *send_counts, *displacements, *RecieveBuffer;
int localBuff, reminder;
double time;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcSize);
MPI_Comm_rank(MPI_COMM_WORLD, &procID);
width = atoi(argv[1]);
height = atoi(argv[2]);
if (procID == 0)
{
matrix = new long long int[width * height];
for (int i = 0; i < width * height; ++i)
matrix[i] = rand();
if ((width < 10) && (height < 10))
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
std::cout << matrix[i * height + j] << " ";
std::cout << "\n";
}
}
time = MPI_Wtime();
send_counts = new int[ProcSize];
displacements = new int[ProcSize];
localBuff = width * height / ProcSize;
reminder = (width * height) % ProcSize; /// elements remaining after division among processes
send_counts[0] = localBuff + reminder;
displacements[0] = 0;
for (int i = 1; i < ProcSize; ++i)
{
send_counts[i] = localBuff;
displacements[i] = reminder + i * localBuff;
}
RecieveBuffer = new int[send_counts[procID]];
///Divide the data among processes
MPI_Scatterv(matrix, send_counts, displacements, MPI_INT, RecieveBuffer, send_counts[procID], MPI_INT, 0, MPI_COMM_WORLD);
max = RecieveBuffer[0];
for (int i = 0; i < send_counts[procID]; ++i) ///Maximum Search
if (max < RecieveBuffer[i])
max = RecieveBuffer[i];
MPI_Reduce(&max, &GlobalMax, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD); ///Transfer of maximum from local buffers to ROOT proc
if (procID == 0)
{
time = MPI_Wtime() - time;
std::cout << "Max is: " << GlobalMax << std::endl;
std::cout << "Time spent: " << time << std::endl;
delete matrix;
}
MPI_Finalize();
delete[] send_counts;
delete[] displacements;
delete[] RecieveBuffer;
return 0;
}