Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Велиев Эльвин. Задача 1. Вариант 11. Сумма значений по строкам матрицы #791

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 278 additions & 0 deletions tasks/mpi/veliev_e_sum_rows_matrix/func_tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
#include <gtest/gtest.h>

#include <boost/mpi/communicator.hpp>
#include <climits>
#include <random>
#include <vector>

#include "mpi/veliev_e_sum_rows_matrix/include/ops_mpi.hpp"

namespace veliev_e_sum_rows_matrix_test_function {

std::vector<std::vector<int32_t>> getRandomData(uint32_t rows, uint32_t cols) {
std::random_device dev;
std::mt19937 gen(dev());
std::vector<std::vector<int32_t>> data(rows, std::vector<int32_t>(cols));

for (auto &row : data) {
for (auto &el : row) {
el = -200 + gen() % (300 + 200 + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please paramterize it in tests

}
}

return data;
}

} // namespace veliev_e_sum_rows_matrix_test_function

TEST(veliev_e_sum_rows_matrix_mpi, rectangular_matrix_stretched_horizontally_7x17) {
uint32_t rows = 7;
uint32_t cols = 17;

boost::mpi::communicator world;
std::vector<std::vector<int32_t>> randomData;
std::vector<int32_t> mpiSum;

std::shared_ptr<ppc::core::TaskData> taskDataPar = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskParallel taskMPI(taskDataPar);

if (world.rank() == 0) {
randomData = veliev_e_sum_rows_matrix_test_function::getRandomData(rows, cols);
mpiSum.resize(rows);

for (auto &row : randomData) {
taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataPar->inputs_count.push_back(rows);
taskDataPar->inputs_count.push_back(cols);
taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t *>(mpiSum.data()));
taskDataPar->outputs_count.push_back(rows);
}

ASSERT_TRUE(taskMPI.validation());
ASSERT_TRUE(taskMPI.pre_processing());
ASSERT_TRUE(taskMPI.run());
ASSERT_TRUE(taskMPI.post_processing());

if (world.rank() == 0) {
std::vector<int32_t> seqSum(rows);

std::shared_ptr<ppc::core::TaskData> taskDataSeq = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskSequential taskSeq(taskDataSeq);

for (auto &row : randomData) {
taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataSeq->inputs_count.push_back(rows);
taskDataSeq->inputs_count.push_back(cols);
taskDataSeq->outputs.emplace_back(reinterpret_cast<uint8_t *>(seqSum.data()));
taskDataSeq->outputs_count.push_back(seqSum.size());

ASSERT_TRUE(taskSeq.validation());
ASSERT_TRUE(taskSeq.pre_processing());
ASSERT_TRUE(taskSeq.run());
ASSERT_TRUE(taskSeq.post_processing());

for (uint32_t i = 0; i < mpiSum.size(); i++) {
ASSERT_EQ(seqSum[i], mpiSum[i]);
}
}
}

TEST(veliev_e_sum_rows_matrix_mpi, rectangular_matrix_stretched_verticaly_100x75) {
uint32_t rows = 100;
uint32_t cols = 75;

boost::mpi::communicator world;
std::vector<std::vector<int32_t>> randomData;
std::vector<int32_t> mpiSum;

std::shared_ptr<ppc::core::TaskData> taskDataPar = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskParallel taskMPI(taskDataPar);

if (world.rank() == 0) {
randomData = veliev_e_sum_rows_matrix_test_function::getRandomData(rows, cols);
mpiSum.resize(rows);

for (auto &row : randomData) {
taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataPar->inputs_count.push_back(rows);
taskDataPar->inputs_count.push_back(cols);
taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t *>(mpiSum.data()));
taskDataPar->outputs_count.push_back(rows);
}

ASSERT_TRUE(taskMPI.validation());
ASSERT_TRUE(taskMPI.pre_processing());
ASSERT_TRUE(taskMPI.run());
ASSERT_TRUE(taskMPI.post_processing());

if (world.rank() == 0) {
std::vector<int32_t> seqSum(rows);

std::shared_ptr<ppc::core::TaskData> taskDataSeq = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskSequential taskSeq(taskDataSeq);

for (auto &row : randomData) {
taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataSeq->inputs_count.push_back(rows);
taskDataSeq->inputs_count.push_back(cols);
taskDataSeq->outputs.emplace_back(reinterpret_cast<uint8_t *>(seqSum.data()));
taskDataSeq->outputs_count.push_back(seqSum.size());

ASSERT_TRUE(taskSeq.validation());
ASSERT_TRUE(taskSeq.pre_processing());
ASSERT_TRUE(taskSeq.run());
ASSERT_TRUE(taskSeq.post_processing());

for (uint32_t i = 0; i < mpiSum.size(); i++) {
ASSERT_EQ(seqSum[i], mpiSum[i]);
}
}
}

TEST(veliev_e_sum_rows_matrix_mpi, squere_matrix_100x100) {
uint32_t rows = 100;
uint32_t cols = 100;

boost::mpi::communicator world;
std::vector<std::vector<int32_t>> randomData;
std::vector<int32_t> mpiSum;

std::shared_ptr<ppc::core::TaskData> taskDataPar = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskParallel taskMPI(taskDataPar);

if (world.rank() == 0) {
randomData = veliev_e_sum_rows_matrix_test_function::getRandomData(rows, cols);
mpiSum.resize(rows);

for (auto &row : randomData) {
taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataPar->inputs_count.push_back(rows);
taskDataPar->inputs_count.push_back(cols);
taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t *>(mpiSum.data()));
taskDataPar->outputs_count.push_back(rows);
}

ASSERT_TRUE(taskMPI.validation());
ASSERT_TRUE(taskMPI.pre_processing());
ASSERT_TRUE(taskMPI.run());
ASSERT_TRUE(taskMPI.post_processing());

if (world.rank() == 0) {
std::vector<int32_t> seqSum(rows);

std::shared_ptr<ppc::core::TaskData> taskDataSeq = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskSequential taskSeq(taskDataSeq);

for (auto &row : randomData) {
taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataSeq->inputs_count.push_back(rows);
taskDataSeq->inputs_count.push_back(cols);
taskDataSeq->outputs.emplace_back(reinterpret_cast<uint8_t *>(seqSum.data()));
taskDataSeq->outputs_count.push_back(seqSum.size());

ASSERT_TRUE(taskSeq.validation());
ASSERT_TRUE(taskSeq.pre_processing());
ASSERT_TRUE(taskSeq.run());
ASSERT_TRUE(taskSeq.post_processing());

for (uint32_t i = 0; i < mpiSum.size(); i++) {
ASSERT_EQ(seqSum[i], mpiSum[i]);
}
}
}

TEST(veliev_e_sum_rows_matrix_mpi, matrix_1x1) {
uint32_t rows = 1;
uint32_t cols = 1;

boost::mpi::communicator world;
std::vector<std::vector<int32_t>> randomData;
std::vector<int32_t> mpiSum;

std::shared_ptr<ppc::core::TaskData> taskDataPar = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskParallel taskMPI(taskDataPar);

if (world.rank() == 0) {
randomData = veliev_e_sum_rows_matrix_test_function::getRandomData(rows, cols);
mpiSum.resize(rows);

for (auto &row : randomData) {
taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataPar->inputs_count.push_back(rows);
taskDataPar->inputs_count.push_back(cols);
taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t *>(mpiSum.data()));
taskDataPar->outputs_count.push_back(rows);
}

ASSERT_TRUE(taskMPI.validation());
ASSERT_TRUE(taskMPI.pre_processing());
ASSERT_TRUE(taskMPI.run());
ASSERT_TRUE(taskMPI.post_processing());

if (world.rank() == 0) {
std::vector<int32_t> seqSum(rows);

std::shared_ptr<ppc::core::TaskData> taskDataSeq = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskSequential taskSeq(taskDataSeq);

for (auto &row : randomData) {
taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataSeq->inputs_count.push_back(rows);
taskDataSeq->inputs_count.push_back(cols);
taskDataSeq->outputs.emplace_back(reinterpret_cast<uint8_t *>(seqSum.data()));
taskDataSeq->outputs_count.push_back(seqSum.size());

ASSERT_TRUE(taskSeq.validation());
ASSERT_TRUE(taskSeq.pre_processing());
ASSERT_TRUE(taskSeq.run());
ASSERT_TRUE(taskSeq.post_processing());

for (uint32_t i = 0; i < mpiSum.size(); i++) {
ASSERT_EQ(seqSum[i], mpiSum[i]);
}
}
}

TEST(veliev_e_sum_rows_matrix_mpi, test_validation) {
uint32_t rows = 7;
uint32_t cols = 17;

boost::mpi::communicator world;
std::vector<std::vector<int32_t>> randomData;
std::vector<int32_t> mpiSum;

std::shared_ptr<ppc::core::TaskData> taskDataPar = std::make_shared<ppc::core::TaskData>();
veliev_e_sum_rows_matrix_mpi::TestTaskParallel taskMPI(taskDataPar);

if (world.rank() == 0) {
randomData = veliev_e_sum_rows_matrix_test_function::getRandomData(rows, cols);
mpiSum.resize(rows);

for (auto &row : randomData) {
taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t *>(row.data()));
}

taskDataPar->inputs_count.push_back(rows);
taskDataPar->inputs_count.push_back(cols);
taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t *>(mpiSum.data()));
taskDataPar->outputs_count.push_back(0);

ASSERT_FALSE(taskMPI.validation());
}
}
42 changes: 42 additions & 0 deletions tasks/mpi/veliev_e_sum_rows_matrix/include/ops_mpi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <gtest/gtest.h>

#include <boost/mpi/collectives.hpp>
#include <boost/mpi/communicator.hpp>
#include <vector>

#include "core/task/include/task.hpp"

namespace veliev_e_sum_rows_matrix_mpi {

class TestTaskSequential : public ppc::core::Task {
public:
explicit TestTaskSequential(std::shared_ptr<ppc::core::TaskData> taskData_) : Task(std::move(taskData_)) {}
bool pre_processing() override;
bool validation() override;
bool run() override;
bool post_processing() override;

private:
std::vector<std::vector<int32_t>> input_;
std::vector<int32_t> res_;
};

class TestTaskParallel : public ppc::core::Task {
public:
explicit TestTaskParallel(std::shared_ptr<ppc::core::TaskData> taskData_) : Task(std::move(taskData_)) {}
bool pre_processing() override;
bool validation() override;
bool run() override;
bool post_processing() override;

private:
std::vector<std::vector<int32_t>> input_, local_input_;
std::vector<int32_t> res_, local_res_;
uint32_t delta_, ext_;

boost::mpi::communicator world;
};

} // namespace veliev_e_sum_rows_matrix_mpi
Loading
Loading