Skip to content

Commit

Permalink
Add threading option to client input
Browse files Browse the repository at this point in the history
  • Loading branch information
potat-dev committed May 24, 2024
1 parent 77df178 commit 8dc934e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
21 changes: 15 additions & 6 deletions src/cmd/clientMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,29 @@ std::string promptMatrixDeterminantTask() {
std::cout << "> ";
std::cin >> matrixCount;

// Optimized random matrix generation using pre-allocated memory
std::vector<int> randomNumbers(matrixSize * matrixSize * matrixCount);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(0, 99);
std::generate(randomNumbers.begin(), randomNumbers.end(), [&]() { return distrib(gen); });

json data = json::array();
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int index = 0;
std::cout << "Start randoming " << std::endl;
for (int i = 0; i < matrixCount; ++i) {
json matrixJson = json::array();
for (int j = 0; j < matrixSize; ++j) {
json rowJson = json::array();
for (int k = 0; k < matrixSize; ++k) {
rowJson.emplace_back(rand() % 100);
rowJson.emplace_back(randomNumbers[index++]);
}
matrixJson.emplace_back(rowJson);
}
data.emplace_back(matrixJson);
}

std::cout << "End randoming" << std::endl;
// measure end time
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::seconds>(end - begin).count() << std::endl;
return data.dump();
}

Expand Down Expand Up @@ -125,7 +130,11 @@ void *senderThread(void *arg) {

std::string requestFunc;
std::string requestParams;
int cores = 1;
// enter cores
int cores;
std::cout << "Enter cores count:" << std::endl;
std::cout << "> ";
std::cin >> cores;

try {
switch (taskNum) {
Expand Down
11 changes: 10 additions & 1 deletion src/rabbitCore/client/RabbitClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "protocol/Connection.h"
#include "DataModel/Client.h"
#include "Message.h"
#include <algorithm>

using namespace STIP;

Expand Down Expand Up @@ -90,7 +91,15 @@ void RabbitClient::receiveResutls() {
}

json data = json::parse(result.data);
std::cout << data.dump(2) << std::endl;
std::string pretty = data.dump(2);
int count = std::count(pretty.begin(), pretty.end(), '\n');

#define PRETTY_MAX_LINES_THR 35
if (count > PRETTY_MAX_LINES_THR) {
pretty = data.dump();
}

std::cout << pretty << std::endl;

// if (data.is_array()) {
// for (auto &row: data) {
Expand Down
2 changes: 2 additions & 0 deletions src/rabbitCore/server/RabbitServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ void RabbitServer::processWorker(Worker &worker) {
auto receiveMessage = worker.connection->receiveMessage();
// std::cout << "Received message: " << receiveMessage->getDataAsString() << std::endl;
json request = json::parse(receiveMessage->getDataAsString()); // TODO change parse
#ifdef SERVER_ARCH_DEBUG
std::cout << "Received message: " << request.dump() << std::endl;
#endif
Message message;
json data;
try {
Expand Down
41 changes: 30 additions & 11 deletions src/rabbitCore/worker/RabbitWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ int RabbitWorker::determinant(std::vector<std::vector<int>> matrix, int n) {
return det;
}

void RabbitWorker::matrixMultiplication(const std::vector<std::vector<int>> &matrixA, const std::vector<std::vector<int>> &matrixB,
void RabbitWorker::matrixMultiplication(const std::vector<std::vector<int>> &matrixA,
const std::vector<std::vector<int>> &matrixB,
std::vector<std::vector<int>> &resultMatrix, int row, int col) {
int colsA = matrixA[0].size();
for (int k = 0; k < colsA; ++k) {
Expand Down Expand Up @@ -232,17 +233,22 @@ void RabbitWorker::determinantHandler(std::string request_id, json data, int tas
std::cout << "RabbitWorker::determinantHandler - Results sent for request_id: " << request_id << std::endl;
}

void RabbitWorker::matrixMultiplicationHandler(std::string id, json data, int taskCores) {
std::cout << "RabbitWorker::matrixMultiplicationHandler - Handling matrix multiplication for request_id: " << id << std::endl;
void RabbitWorker::matrixMultiplicationHandler(std::string request_id, json data, int taskCores) {
std::cout << "RabbitWorker::matrixMultiplicationHandler - Handling matrix multiplication for request_id: " << id
<< std::endl;
std::vector<std::thread> threads;
threads.reserve(taskCores);

// засекаем время
auto start = std::chrono::high_resolution_clock::now();

std::vector<std::vector<std::vector<int>>> matrices = data.get<std::vector<std::vector<std::vector<int>>>>();
std::vector<std::vector<int>> matrixA = matrices[0];
std::vector<std::vector<int>> matrixB = matrices[1];

if (matrixA[0].size() != matrixB.size()) {
std::cerr << "RabbitWorker::matrixMultiplicationHandler - Matrix dimensions do not match for multiplication" << std::endl;
std::cerr << "RabbitWorker::matrixMultiplicationHandler - Matrix dimensions do not match for multiplication"
<< std::endl;
return;
}

Expand All @@ -252,23 +258,36 @@ void RabbitWorker::matrixMultiplicationHandler(std::string id, json data, int ta

for (int row = 0; row < rowsA; ++row) {
for (int col = 0; col < colsB; ++col) {
threads.emplace_back(&RabbitWorker::matrixMultiplication, this, std::ref(matrixA), std::ref(matrixB), std::ref(resultMatrix), row, col);
threads.emplace_back(&RabbitWorker::matrixMultiplication, this, std::ref(matrixA), std::ref(matrixB),
std::ref(resultMatrix), row, col);

if (threads.size() == taskCores || (row == rowsA - 1 && col == colsB - 1)) {
for (auto &t : threads) {
for (auto &t: threads) {
t.join();
}
threads.clear();
}
}
}

json jResultMatrix = resultMatrix;
struct TaskResult taskResult{id, jResultMatrix.dump(), 1};
struct TaskResult taskResult = {
request_id,
json(resultMatrix).dump(),
1
};

Message message = {
MessageType::TaskResult,
json(taskResult).dump()
};

connection->sendMessage(json(message).dump());
std::cout << "RabbitWorker::matrixMultiplicationHandler - Results sent for request_id: " << request_id << std::endl;

json response = taskResult;
connection->sendMessage(response.dump());
std::cout << "RabbitWorker::matrixMultiplicationHandler - Results sent for request_id: " << id << std::endl;
// считаем время выполнения
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "RabbitWorker::matrixMultiplicationHandler - Execution time: " << elapsed.count() << "s" << std::endl;
}


0 comments on commit 8dc934e

Please sign in to comment.