Skip to content

Commit 33ce6df

Browse files
committed
create matrixMultiplication
1 parent 8a538f5 commit 33ce6df

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/rabbitCore/worker/RabbitWorker.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ int RabbitWorker::determinant(std::vector<std::vector<int>> matrix, int n) {
158158
return det;
159159
}
160160

161+
void RabbitWorker::matrixMultiplication(const std::vector<std::vector<int>> &matrixA, const std::vector<std::vector<int>> &matrixB,
162+
std::vector<std::vector<int>> &resultMatrix, int row, int col) {
163+
int colsA = matrixA[0].size();
164+
for (int k = 0; k < colsA; ++k) {
165+
resultMatrix[row][col] += matrixA[row][k] * matrixB[k][col];
166+
}
167+
}
168+
161169
void RabbitWorker::simpleMathHandler(std::string request_id, json data, int taskCores) {
162170
std::cout << "RabbitWorker::simpleMathHandler - Handling simpleMath for request_id: " << request_id << std::endl;
163171
int a, b, result;
@@ -222,3 +230,44 @@ void RabbitWorker::determinantHandler(std::string request_id, json data, int tas
222230
connection->sendMessage(json(message).dump());
223231
std::cout << "RabbitWorker::determinantHandler - Results sent for request_id: " << request_id << std::endl;
224232
}
233+
234+
void RabbitWorker::matrixMultiplicationHandler(std::string id, json data, int taskCores) {
235+
std::cout << "RabbitWorker::matrixMultiplicationHandler - Handling matrix multiplication for request_id: " << id << std::endl;
236+
std::vector<std::thread> threads;
237+
threads.reserve(taskCores);
238+
239+
std::vector<std::vector<std::vector<int>>> matrices = data.get<std::vector<std::vector<std::vector<int>>>>();
240+
std::vector<std::vector<int>> matrixA = matrices[0];
241+
std::vector<std::vector<int>> matrixB = matrices[1];
242+
243+
if (matrixA[0].size() != matrixB.size()) {
244+
std::cerr << "RabbitWorker::matrixMultiplicationHandler - Matrix dimensions do not match for multiplication" << std::endl;
245+
return;
246+
}
247+
248+
int rowsA = matrixA.size();
249+
int colsB = matrixB[0].size();
250+
std::vector<std::vector<int>> resultMatrix(rowsA, std::vector<int>(colsB, 0));
251+
252+
for (int row = 0; row < rowsA; ++row) {
253+
for (int col = 0; col < colsB; ++col) {
254+
threads.emplace_back(&RabbitWorker::matrixMultiplication, this, std::ref(matrixA), std::ref(matrixB), std::ref(resultMatrix), row, col);
255+
256+
if (threads.size() == taskCores || (row == rowsA - 1 && col == colsB - 1)) {
257+
for (auto &t : threads) {
258+
t.join();
259+
}
260+
threads.clear();
261+
}
262+
}
263+
}
264+
265+
json jResultMatrix = resultMatrix;
266+
struct TaskResult taskResult{id, jResultMatrix.dump(), 1};
267+
268+
json response = taskResult;
269+
connection->sendMessage(response.dump());
270+
std::cout << "RabbitWorker::matrixMultiplicationHandler - Results sent for request_id: " << id << std::endl;
271+
}
272+
273+

src/rabbitCore/worker/RabbitWorker.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ class RabbitWorker {
5757

5858
int determinant(std::vector<std::vector<int>> matrix, int n);
5959

60+
void matrixMultiplication(const std::vector<std::vector<int>> &matrixA, const std::vector<std::vector<int>> &matrixB,
61+
std::vector<std::vector<int>> &resultMatrix, int row, int col);
62+
6063
static void doWait(int seconds);
6164

6265
void simpleMathHandler(std::string request_id, json data, int taskCores);
6366

6467
void determinantHandler(std::string id, json data, int taskCores);
6568

69+
void matrixMultiplicationHandler(std::string id, json data, int taskCores);
70+
6671
func_map_type mapping;
6772
};
6873

0 commit comments

Comments
 (0)