@@ -158,6 +158,14 @@ int RabbitWorker::determinant(std::vector<std::vector<int>> matrix, int n) {
158
158
return det;
159
159
}
160
160
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
+
161
169
void RabbitWorker::simpleMathHandler (std::string request_id, json data, int taskCores) {
162
170
std::cout << " RabbitWorker::simpleMathHandler - Handling simpleMath for request_id: " << request_id << std::endl;
163
171
int a, b, result;
@@ -222,3 +230,44 @@ void RabbitWorker::determinantHandler(std::string request_id, json data, int tas
222
230
connection->sendMessage (json (message).dump ());
223
231
std::cout << " RabbitWorker::determinantHandler - Results sent for request_id: " << request_id << std::endl;
224
232
}
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
+
0 commit comments