-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRandMatrixMultiplicator.cpp
More file actions
213 lines (165 loc) · 5.49 KB
/
Copy pathPRandMatrixMultiplicator.cpp
File metadata and controls
213 lines (165 loc) · 5.49 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "mpi.h"
#include "Logger.h"
#include "MatrixHandler.h"
#include "PRandMatrixMultiplicator.h"
#include <stdlib.h>
#include <map>
#include <string>
using namespace std;
PRandMatrixMultiplicator::PRandMatrixMultiplicator(void)
{
m_pLogger = Logger::getLogger();
}
PRandMatrixMultiplicator::~PRandMatrixMultiplicator(void)
{
}
bool
PRandMatrixMultiplicator::doTheWholeThing(int Seed,
int CommonSize,
int ResultWidth,
int ResultHeight)
{
bool Success = true;
//Get rank and world size
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
//Prepare Variables
int ValueCount = ResultWidth * ResultHeight;
int BufferSize = CommonSize * 2;
int *buffer;
MPI_Status status;
if (0 == rank)
{
//Main / Distribution Rank
//Set Seed for random numbers
srand(Seed);
m_pLogger->LogDebug("Set the seed to " + to_string(Seed));
m_pLogger->LogTrace("=================================================================");
m_pLogger->LogTrace("== Starting to create Matrices.. ==");
m_pLogger->LogTrace("=================================================================");
m_pLogger->LogTrace("");
//Create the Matrices
MatrixHandler* mCreator = new MatrixHandler();
//First Matrix
map<int, map<int, int>> firstMatrix;
Success &= mCreator->createMatrix(firstMatrix, CommonSize, ResultHeight);
m_pLogger->LogTrace("## First Matrix: ##");
m_pLogger->LogTrace("");
m_pLogger->printMatrix(&firstMatrix, true);
//Second Matrix
map<int, map<int, int>> secondMatrix;
Success &= mCreator->createMatrix(secondMatrix, ResultWidth, CommonSize);
m_pLogger->LogTrace("## Second Matrix: ##");
m_pLogger->LogTrace("");
m_pLogger->printMatrix(&secondMatrix, true);
delete mCreator;
m_pLogger->LogTrace("=================================================================");
m_pLogger->LogTrace("== Starting the computation.. ==");
m_pLogger->LogTrace("=================================================================");
m_pLogger->LogTrace("");
int currentRecv = 2;
int resultID = 0;
for (int currentRow = 0; currentRow < ResultHeight; currentRow++)
{
for (int currentCol = 0; currentCol < ResultWidth; currentCol++)
{
buffer = new int[BufferSize];
//Add Values of CurrentRow of first Matrix
for (int fCurrentCol = 0; fCurrentCol < CommonSize; fCurrentCol++)
{
buffer[fCurrentCol] = firstMatrix[currentRow][fCurrentCol];
}
//Add Values of CurrentCol of second Matrix
for (int sCurrentRow = 0; sCurrentRow < CommonSize; sCurrentRow++)
{
buffer[sCurrentRow + CommonSize] = secondMatrix[sCurrentRow][currentCol];
}
//Send to Process
MPI_Send(buffer, BufferSize, MPI_INT, currentRecv, resultID, MPI_COMM_WORLD);
m_pLogger->LogDebug("Sent Values for ResultID " + to_string(resultID) + " to process " + to_string(currentRecv) + ".");
//Update Receiver
currentRecv++;
if (world_size <= currentRecv)
{
currentRecv = 2;
}
//Increment ResultID
resultID++;
//Clear Buffer
delete[] buffer;
buffer = nullptr;
}
}
}
else if (1 == rank)
{
//Result Receiver Rank
map<int, map<int, int>> resultMatrix;
int currentRow = 0;
int currentCol = 0;
//Receive the resultvalues from every working process and put them on the right position
int currentSrc = 2;
for (int currentResID = 0; currentResID < ValueCount; currentResID++)
{
int resultValue;
MPI_Recv(&resultValue, 1, MPI_INT, currentSrc, currentResID, MPI_COMM_WORLD, &status);
m_pLogger->LogDebug("Received Result " + to_string(resultValue) + " for ResultID " + to_string(currentResID) + " from Process " + to_string(currentSrc) + ".");
resultMatrix[currentRow][currentCol] = resultValue;
//Update Column
currentCol++;
if (currentCol == (ResultWidth))
{
currentRow++;
currentCol = 0;
}
//Update source
currentSrc++;
if (world_size <= currentSrc)
{
currentSrc = 2;
}
}
m_pLogger->LogTrace("=================================================================");
m_pLogger->LogTrace("== Got the result Matrix! ==");
m_pLogger->LogTrace("=================================================================");
m_pLogger->LogTrace("");
m_pLogger->printMatrix(&resultMatrix, true);
}
else
{
//Work Ranks
//Compute how many values you have to compute
int NumberOfReceives = (ResultWidth * ResultHeight) / (world_size - 2);
int remainder = (ResultWidth * ResultHeight) % (world_size - 2);
if ((rank - 2) < remainder)
{
NumberOfReceives++;
}
int resultID = rank - 2;
for (int i = 0; i < NumberOfReceives; i++)
{
buffer = new int[BufferSize];
MPI_Recv(buffer, BufferSize, MPI_INT, 0, resultID, MPI_COMM_WORLD, &status);
m_pLogger->LogDebug("Received Values for ResultID " + to_string(resultID) + ".");
int resultValue = 0;
int secondIt = CommonSize;
//Add up all the values
for (int firstIt = 0; firstIt < CommonSize; firstIt++)
{
int currentValue = buffer[firstIt] * buffer[secondIt];
resultValue += currentValue;
secondIt++;
}
//Send result back to receiver
MPI_Send(&resultValue, 1, MPI_INT, 1, resultID, MPI_COMM_WORLD);
m_pLogger->LogDebug("Computed and sent ResultValue for ResultID " + to_string(resultID) + ".");
//Clear buffer
delete[] buffer;
buffer = nullptr;
resultID += (world_size - 2);
}
}
return Success;
}