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

Хованский Дмитрий. Задача 2. Вариант 16. Ленточная вертикальная схема. #395

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
131 changes: 131 additions & 0 deletions tasks/mpi/khovansky_d_ribbon_vertical_scheme/func_tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2024 Khovansky Dmitry
#include <gtest/gtest.h>

#include <boost/mpi/communicator.hpp>
#include <boost/mpi/environment.hpp>
#include <cstdlib>
#include <ctime>
#include <memory>
#include <random>
#include <vector>

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

void khovansky_d_fragmentation(int rows_count, int columns_count, int proc_count, std::vector<int>& rows_per_process,
std::vector<int>& rows_offsets) {
if (proc_count > rows_count) {
for (int i = 0; i < rows_count; ++i) {
rows_offsets[i] = i * columns_count;
rows_per_process[i] = columns_count;
}
for (int i = rows_count; i < proc_count; ++i) {
rows_offsets[i] = -1;
rows_per_process[i] = 0;
}
} else {
int rows_count_per_proc = rows_count / proc_count;
int remainder = rows_count % proc_count;
int offset = 0;
for (int i = 0; i < proc_count; ++i) {
if (remainder > 0) {
rows_per_process[i] = (rows_count_per_proc + 1) * columns_count;
--remainder;
} else {
rows_per_process[i] = rows_count_per_proc * columns_count;
}
rows_offsets[i] = offset;
offset += rows_per_process[i];
}
}
}

TEST(khovansky_d_ribbon_vertical_scheme_mpi, procs_more_than_rows) {
int rows_count = 3;
int columns_count = 5;
Comment on lines +43 to +44
Copy link
Member

Choose a reason for hiding this comment

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

please check case when matrix and vector sizes are different (e.g. 10, 11, 12)

int proc_count = 6;

std::vector<int> rows_per_process(proc_count, 0);
std::vector<int> rows_offsets(proc_count, 0);

khovansky_d_fragmentation(rows_count, columns_count, proc_count, rows_per_process, rows_offsets);

std::vector<int> expected_rows_per_process = {5, 5, 5, 0, 0, 0};
std::vector<int> expected_rows_offsets = {0, 5, 10, -1, -1, -1};

EXPECT_EQ(rows_per_process, expected_rows_per_process);
EXPECT_EQ(rows_offsets, expected_rows_offsets);
}

TEST(khovansky_d_ribbon_vertical_scheme_mpi, procs_less_than_rows) {
int rows_count = 10;
int columns_count = 3;
int proc_count = 4;

std::vector<int> rows_per_process(proc_count, 0);
std::vector<int> rows_offsets(proc_count, 0);

khovansky_d_fragmentation(rows_count, columns_count, proc_count, rows_per_process, rows_offsets);

std::vector<int> expected_rows_per_process = {9, 9, 6, 6};
std::vector<int> expected_rows_offsets = {0, 9, 18, 24};

EXPECT_EQ(rows_per_process, expected_rows_per_process);
EXPECT_EQ(rows_offsets, expected_rows_offsets);
}

TEST(khovansky_d_ribbon_vertical_scheme_mpi, procs_equal_rows) {
int rows_count = 5;
int columns_count = 3;
int proc_count = 5;

std::vector<int> rows_per_process(proc_count, 0);
std::vector<int> rows_offsets(proc_count, 0);

khovansky_d_fragmentation(rows_count, columns_count, proc_count, rows_per_process, rows_offsets);

std::vector<int> expected_rows_per_process = {3, 3, 3, 3, 3};
std::vector<int> expected_rows_offsets = {0, 3, 6, 9, 12};

EXPECT_EQ(rows_per_process, expected_rows_per_process);
EXPECT_EQ(rows_offsets, expected_rows_offsets);
}

TEST(khovansky_d_ribbon_vertical_scheme_mpi, standart_matrix) {
boost::mpi::communicator world;

int rows_count = 3;
int columns_count = 3;
std::vector<int> input_matrix;
std::vector<int> input_vector;
std::vector<int> output_vector;

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

if (world.rank() == 0) {
input_matrix.resize(rows_count * columns_count);
input_vector.resize(rows_count);
output_vector.resize(columns_count, 0);

for (int i = 0; i < rows_count * columns_count; ++i) {
input_matrix[i] = (rand() % 1000) - 500;
}

for (int i = 0; i < rows_count; ++i) {
input_vector[i] = (rand() % 1000) - 500;
}

taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_matrix.data()));
taskDataPar->inputs_count.emplace_back(input_matrix.size());
taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_vector.data()));
taskDataPar->inputs_count.emplace_back(input_vector.size());
taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t*>(output_vector.data()));
taskDataPar->outputs_count.emplace_back(output_vector.size());
}

auto taskParallel = std::make_shared<khovansky_d_ribbon_vertical_scheme_mpi::RibbonVerticalSchemeMPI>(taskDataPar);

ASSERT_TRUE(taskParallel->validation());
ASSERT_TRUE(taskParallel->pre_processing());
ASSERT_TRUE(taskParallel->run());
ASSERT_TRUE(taskParallel->post_processing());
}
53 changes: 53 additions & 0 deletions tasks/mpi/khovansky_d_ribbon_vertical_scheme/include/ops_mpi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Khovansky Dmitry
#pragma once

#include <gtest/gtest.h>

#include <boost/mpi/collectives.hpp>
#include <boost/mpi/communicator.hpp>
#include <memory>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

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

namespace khovansky_d_ribbon_vertical_scheme_mpi {

class RibbonVerticalSchemeSeq : public ppc::core::Task {
public:
explicit RibbonVerticalSchemeSeq(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:
int* hello_matrix;
int* hello_vector;
int rows_count{};
int columns_count{};
std::vector<int> goodbye_vector;
};

class RibbonVerticalSchemeMPI : public ppc::core::Task {
public:
explicit RibbonVerticalSchemeMPI(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<int> hello_matrix;
std::vector<int> hello_vector;
int rows_count{};
int columns_count{};
std::vector<int> rows_per_process;
std::vector<int> rows_offsets;
std::vector<int> goodbye_vector;
boost::mpi::communicator world;
};

} // namespace khovansky_d_ribbon_vertical_scheme_mpi
179 changes: 179 additions & 0 deletions tasks/mpi/khovansky_d_ribbon_vertical_scheme/perf_tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright 2024 Khovansky Dmitry
#include <gtest/gtest.h>

#include <boost/mpi.hpp>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/timer.hpp>
#include <memory>
#include <random>
#include <vector>

#include "core/perf/include/perf.hpp"
#include "mpi/khovansky_d_ribbon_vertical_scheme/include/ops_mpi.hpp"

TEST(khovansky_d_ribbon_vertical_scheme_mpi, Performance_Pipeline_Run) {
boost::mpi::environment env;
boost::mpi::communicator world;

std::vector<int> input_matrix;
std::vector<int> input_vector;
std::vector<int> output_vector;

std::shared_ptr<ppc::core::TaskData> taskDataPar = std::make_shared<ppc::core::TaskData>();
int rows_count;
int columns_count;

if (world.rank() == 0) {
rows_count = 8192;
columns_count = 8192;

input_vector.resize(columns_count);
input_matrix.resize(rows_count * columns_count);

for (int j = 0; j < rows_count; ++j) {
for (int i = 0; i < columns_count; ++i) {
input_matrix[j * columns_count + i] = (rand() % 1001) - 500;
}
}

for (int i = 0; i < rows_count; ++i) {
input_vector[i] = (rand() % 1000) - 500;
}

output_vector.resize(columns_count, 0);

taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_matrix.data()));
taskDataPar->inputs_count.emplace_back(input_matrix.size());

taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_vector.data()));
taskDataPar->inputs_count.emplace_back(input_vector.size());

taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t*>(output_vector.data()));
taskDataPar->outputs_count.emplace_back(output_vector.size());
}

auto taskParallel = std::make_shared<khovansky_d_ribbon_vertical_scheme_mpi::RibbonVerticalSchemeMPI>(taskDataPar);
ASSERT_TRUE(taskParallel->validation());
taskParallel->pre_processing();
taskParallel->run();
taskParallel->post_processing();

auto perfAttr = std::make_shared<ppc::core::PerfAttr>();
perfAttr->num_running = 10;
const boost::mpi::timer current_timer;
perfAttr->current_timer = [&] { return current_timer.elapsed(); };

auto perfResults = std::make_shared<ppc::core::PerfResults>();

auto perfAnalyzer = std::make_shared<ppc::core::Perf>(taskParallel);
perfAnalyzer->pipeline_run(perfAttr, perfResults);

if (world.rank() == 0) {
ppc::core::Perf::print_perf_statistic(perfResults);

std::vector<int> seq_result(output_vector.size(), 0);

auto taskDataSeq = std::make_shared<ppc::core::TaskData>();

taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_matrix.data()));
taskDataSeq->inputs_count.emplace_back(input_matrix.size());
taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_vector.data()));
taskDataSeq->inputs_count.emplace_back(input_vector.size());
taskDataSeq->outputs.emplace_back(reinterpret_cast<uint8_t*>(seq_result.data()));
taskDataSeq->outputs_count.emplace_back(seq_result.size());

auto taskSequential =
std::make_shared<khovansky_d_ribbon_vertical_scheme_mpi::RibbonVerticalSchemeSeq>(taskDataSeq);
ASSERT_TRUE(taskSequential->validation());
taskSequential->pre_processing();
taskSequential->run();
taskSequential->post_processing();

ASSERT_EQ(output_vector.size(), seq_result.size());
for (size_t i = 0; i < output_vector.size(); ++i) {
ASSERT_EQ(output_vector[i], seq_result[i]);
}
}
}

TEST(khovansky_d_ribbon_vertical_scheme_mpi, Performance_Task_Run) {
boost::mpi::environment env;
boost::mpi::communicator world;

std::vector<int> input_matrix;
std::vector<int> input_vector;
std::vector<int> output_vector;

std::shared_ptr<ppc::core::TaskData> taskDataPar = std::make_shared<ppc::core::TaskData>();
int rows_count;
int columns_count;

if (world.rank() == 0) {
rows_count = 8000;
columns_count = 8000;

input_matrix.resize(rows_count * columns_count);
input_vector.resize(columns_count);

for (int j = 0; j < rows_count; ++j) {
for (int i = 0; i < columns_count; ++i) {
input_matrix[j * columns_count + i] = (rand() % 1001) - 500;
}
}

for (int i = 0; i < rows_count; ++i) {
input_vector[i] = (rand() % 1000) - 500;
}

output_vector.resize(columns_count, 0);

taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_matrix.data()));
taskDataPar->inputs_count.emplace_back(input_matrix.size());
taskDataPar->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_vector.data()));
taskDataPar->inputs_count.emplace_back(input_vector.size());
taskDataPar->outputs.emplace_back(reinterpret_cast<uint8_t*>(output_vector.data()));
taskDataPar->outputs_count.emplace_back(output_vector.size());
}

auto taskParallel = std::make_shared<khovansky_d_ribbon_vertical_scheme_mpi::RibbonVerticalSchemeMPI>(taskDataPar);
ASSERT_TRUE(taskParallel->validation());
taskParallel->pre_processing();
taskParallel->run();
taskParallel->post_processing();

auto perfAttr = std::make_shared<ppc::core::PerfAttr>();
perfAttr->num_running = 10;
const boost::mpi::timer current_timer;
perfAttr->current_timer = [&] { return current_timer.elapsed(); };

auto perfResults = std::make_shared<ppc::core::PerfResults>();

auto perfAnalyzer = std::make_shared<ppc::core::Perf>(taskParallel);
perfAnalyzer->task_run(perfAttr, perfResults);

if (world.rank() == 0) {
ppc::core::Perf::print_perf_statistic(perfResults);

std::vector<int> seq_result(output_vector.size(), 0);

auto taskDataSeq = std::make_shared<ppc::core::TaskData>();
taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_matrix.data()));
taskDataSeq->inputs_count.emplace_back(input_matrix.size());
taskDataSeq->inputs.emplace_back(reinterpret_cast<uint8_t*>(input_vector.data()));
taskDataSeq->inputs_count.emplace_back(input_vector.size());
taskDataSeq->outputs.emplace_back(reinterpret_cast<uint8_t*>(seq_result.data()));
taskDataSeq->outputs_count.emplace_back(seq_result.size());

auto taskSequential =
std::make_shared<khovansky_d_ribbon_vertical_scheme_mpi::RibbonVerticalSchemeSeq>(taskDataSeq);
ASSERT_TRUE(taskSequential->validation());
taskSequential->pre_processing();
taskSequential->run();
taskSequential->post_processing();

ASSERT_EQ(output_vector.size(), seq_result.size());
for (size_t i = 0; i < output_vector.size(); ++i) {
ASSERT_EQ(output_vector[i], seq_result[i]);
}
}
}
Loading
Loading