From 67379f7c039ef86dae79bf2f0826f90a5aa6b006 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Thu, 19 Dec 2024 23:07:43 +0300 Subject: [PATCH 01/12] Sequential solution --- .../func_tests/main.cpp | 139 ++++++++++++++++++ .../include/ops_seq.hpp | 37 +++++ .../perf_tests/main.cpp | 98 ++++++++++++ .../src/ops_seq.cpp | 99 +++++++++++++ 4 files changed, 373 insertions(+) create mode 100644 tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp create mode 100644 tasks/seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp create mode 100644 tasks/seq/kalyakina_a_trapezoidal_integration_seq/perf_tests/main.cpp create mode 100644 tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp new file mode 100644 index 00000000000..fee250308ac --- /dev/null +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -0,0 +1,139 @@ +// Copyright 2023 Nesterov Alexander +#include + +#include +#include + +#include "seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp" + +double function1(std::vector input) { return pow(input[0], 3) + pow(input[1], 3); }; +double function2(std::vector input) { return sin(input[0]) + sin(input[1]) + sin(input[2]); }; +double function3(std::vector input) { return 8 * input[0] * input[1] * input[2]; }; +double function4(std::vector input) { return -1.0 / sqrt(1 - pow(input[0], 2)); }; +double function5(std::vector input) { return -(sin(input[0]) * cos(input[1])); }; +double function6(std::vector input) { return (-3 * pow(input[1], 2) * sin(5 * input[0])) / 2; }; + +void TestOfValidation(double (*function)(std::vector), std::vector& count, + std::vector>& limits, std::vector& intervals, + double answer) { + std::vector out(1, 0.0); + + // Create TaskData + std::shared_ptr taskDataSequential = std::make_shared(); + + taskDataSequential->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataSequential->inputs_count.emplace_back(count.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataSequential->inputs_count.emplace_back(limits.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataSequential->inputs_count.emplace_back(intervals.size()); + taskDataSequential->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataSequential->outputs_count.emplace_back(out.size()); + + kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask TaskSequential(taskDataSequential, function); + + ASSERT_EQ(TaskSequential.validation(), false); +} + +void TestOfFunction(double (*function)(std::vector), std::vector& count, + std::vector>& limits, std::vector& intervals, + double answer) { + std::vector out(1, 0.0); + + // Create TaskData + std::shared_ptr taskDataSequential = std::make_shared(); + + taskDataSequential->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataSequential->inputs_count.emplace_back(count.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataSequential->inputs_count.emplace_back(limits.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataSequential->inputs_count.emplace_back(intervals.size()); + taskDataSequential->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataSequential->outputs_count.emplace_back(out.size()); + + kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask TaskSequential(taskDataSequential, function); + + ASSERT_EQ(TaskSequential.validation(), true); + TaskSequential.pre_processing(); + TaskSequential.run(); + TaskSequential.post_processing(); + + EXPECT_NEAR(answer, out[0], 0.001); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_count_of_variables) { + std::vector count(1, 3); + std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; + std::vector intervals = {1000, 1000}; + + TestOfValidation(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_numbers_of_intervals) { + std::vector count(1, 2); + std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; + std::vector intervals = {1000}; + + TestOfValidation(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_limits) { + std::vector count(1, 2); + std::vector> limits = {{2.5, 4.5}}; + std::vector intervals = {1000, 1000}; + + TestOfValidation(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_1) { + std::vector count(1, 2); + std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; + std::vector intervals = {1000, 1000}; + + TestOfFunction(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_2) { + std::vector count(1, 3); + std::vector> limits = {{0.0, 1.0}, {0.0, 1.0}, {0.0, 1.0}}; + std::vector intervals = {100, 100, 100}; + + TestOfFunction(function2, count, limits, intervals, -3 * std::cos(1) + 3 * std::cos(0)); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_3) { + std::vector count(1, 3); + std::vector> limits = {{0.0, 3.0}, {0.0, 4.0}, {0.0, 5.0}}; + std::vector intervals = {100, 100, 100}; + + TestOfFunction(function3, count, limits, intervals, pow(3 - 0, 2) * pow(4 - 0, 2) * pow(5 - 0, 2)); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_4) { + std::vector count(1, 1); + std::vector> limits = {{0.0, 0.5}}; + std::vector intervals = {10000}; + + TestOfFunction(function4, count, limits, intervals, acos(0.5) - acos(0)); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_5) { + std::vector count(1, 2); + std::vector> limits = {{0.0, 1.0}, {0.0, 1.0}}; + std::vector intervals = {1000, 1000}; + + TestOfFunction(function5, count, limits, intervals, (sin(1) - sin(0)) * (cos(1) - cos(0))); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_6) { + std::vector count(1, 2); + std::vector> limits = {{0.0, 1.0}, {4.0, 6.0}}; + std::vector intervals = {1000, 1000}; + + TestOfFunction(function6, count, limits, intervals, (pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10); +} \ No newline at end of file diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp new file mode 100644 index 00000000000..2e21e07b673 --- /dev/null +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp @@ -0,0 +1,37 @@ +// Copyright 2023 Nesterov Alexander +#pragma once + +#include + +#include +#include +#include +#include +#include + +#include "core/task/include/task.hpp" + +namespace kalyakina_a_trapezoidal_integration_seq { + +class TrapezoidalIntegrationTask : public ppc::core::Task { + unsigned int CalculationOfCoefficient(const std::vector& point); + void Recursive(std::vector& _point, unsigned int& definition, unsigned int divider, unsigned int variable); + std::vector GetPointFromNumber(unsigned int number); + + public: + explicit TrapezoidalIntegrationTask(std::shared_ptr taskData_, + double (*_function)(std::vector)) + : Task(std::move(taskData_)), function(_function) {} + bool pre_processing() override; + bool validation() override; + bool run() override; + bool post_processing() override; + + private: + double (*function)(std::vector); + std::vector> limits; + std::vector number_of_intervals; + double result; +}; + +} // namespace kalyakina_a_trapezoidal_integration_seq \ No newline at end of file diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/perf_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/perf_tests/main.cpp new file mode 100644 index 00000000000..77688b31649 --- /dev/null +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/perf_tests/main.cpp @@ -0,0 +1,98 @@ +// Copyright 2023 Nesterov Alexander +#include + +#include +#include + +#include "core/perf/include/perf.hpp" +#include "seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp" + +double function(std::vector input) { return (-3 * pow(input[1], 2) * sin(5 * input[0])) / 2; }; + +TEST(kalyakina_a_trapezoidal_integration_seq, TrapezoidalIntegrationSequential) { + // Create data + std::vector> limits = {{0.0, 1.0}, {4.0, 6.0}}; + std::vector intervals = {500, 500}; + std::vector count(1, 2); + std::vector out(1, 0.0); + + // Create TaskData + std::shared_ptr taskDataSequential = std::make_shared(); + + taskDataSequential->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataSequential->inputs_count.emplace_back(count.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataSequential->inputs_count.emplace_back(limits.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataSequential->inputs_count.emplace_back(intervals.size()); + taskDataSequential->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataSequential->outputs_count.emplace_back(out.size()); + + // Create Task + auto testTaskSequential = std::make_shared( + taskDataSequential, function); + + // Create Perf attributes + auto perfAttr = std::make_shared(); + perfAttr->num_running = 10; + const auto t0 = std::chrono::high_resolution_clock::now(); + perfAttr->current_timer = [&] { + auto current_time_point = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(current_time_point - t0).count(); + return static_cast(duration) * 1e-9; + }; + + // Create and init perf results + auto perfResults = std::make_shared(); + + // Create Perf analyzer + auto perfAnalyzer = std::make_shared(testTaskSequential); + perfAnalyzer->pipeline_run(perfAttr, perfResults); + + ppc::core::Perf::print_perf_statistic(perfResults); + ASSERT_NEAR((pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10, out[0], 0.001); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, TrapezoidalIntegrationSequential_task_run) { + // Create data + std::vector> limits = {{0.0, 1.0}, {4.0, 6.0}}; + std::vector intervals = {500, 500}; + std::vector count(1, 2); + std::vector out(1, 0.0); + + // Create TaskData + std::shared_ptr taskDataSequential = std::make_shared(); + + taskDataSequential->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataSequential->inputs_count.emplace_back(count.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataSequential->inputs_count.emplace_back(limits.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataSequential->inputs_count.emplace_back(intervals.size()); + taskDataSequential->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataSequential->outputs_count.emplace_back(out.size()); + + // Create Task + auto testTaskSequential = std::make_shared( + taskDataSequential, function); + + // Create Perf attributes + auto perfAttr = std::make_shared(); + perfAttr->num_running = 10; + const auto t0 = std::chrono::high_resolution_clock::now(); + perfAttr->current_timer = [&] { + auto current_time_point = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(current_time_point - t0).count(); + return static_cast(duration) * 1e-9; + }; + + // Create and init perf results + auto perfResults = std::make_shared(); + + // Create Perf analyzer + auto perfAnalyzer = std::make_shared(testTaskSequential); + perfAnalyzer->task_run(perfAttr, perfResults); + + ppc::core::Perf::print_perf_statistic(perfResults); + ASSERT_NEAR((pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10, out[0], 0.001); +} diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp new file mode 100644 index 00000000000..90997905496 --- /dev/null +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp @@ -0,0 +1,99 @@ +// Copyright 2023 Nesterov Alexander +#include "seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp" + +#include +#include +#include +#include + +unsigned int kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::CalculationOfCoefficient( + const std::vector& point) { + unsigned int degree = limits.size(); + for (unsigned int i = 0; i < limits.size(); i++) { + if ((limits[i].first == point[i]) || (limits[i].second == point[i])) { + degree--; + } + } + + return pow(2, degree); +} + +void kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::Recursive(std::vector& _point, + unsigned int& definition, + unsigned int divider, + unsigned int variable) { + if (variable > 0) { + Recursive(_point, definition, divider * (number_of_intervals[variable] + 1), variable - 1); + } + _point[variable] = limits[variable].first + definition / divider * + (limits[variable].second - limits[variable].first) / + number_of_intervals[variable]; + definition = definition % divider; +} + +std::vector kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::GetPointFromNumber( + unsigned int number) { + std::vector point(limits.size()); + unsigned int definition = number; + Recursive(point, definition, 1, limits.size() - 1); + + return point; +} + +bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::pre_processing() { + internal_order_test(); + + limits = std::vector>(taskData->inputs_count[1]); + std::pair* it1 = reinterpret_cast*>(taskData->inputs[1]); + std::copy(it1, it1 + taskData->inputs_count[1], limits.begin()); + + number_of_intervals = std::vector(taskData->inputs_count[2]); + unsigned int* it2 = reinterpret_cast(taskData->inputs[2]); + std::copy(it2, it2 + taskData->inputs_count[2], number_of_intervals.begin()); + + result = 0.0; + + return true; +} + +bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::validation() { + internal_order_test(); + + if ((taskData->inputs_count[0] != 1) || + (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || + (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)) { + return false; + } + + return true; +} + +bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::run() { + internal_order_test(); + + unsigned int count = 1; + for (unsigned int i = 0; i < number_of_intervals.size(); i++) { + count *= (number_of_intervals[i] + 1); + } + + for (unsigned int i = 0; i < count; i++) { + std::vector point = GetPointFromNumber(i); + result += CalculationOfCoefficient(point) * function(point); + } + + for (unsigned int i = 0; i < limits.size(); i++) { + result *= (limits[i].second - limits[i].first) / number_of_intervals[i]; + } + + result /= pow(2, limits.size()); + + return true; +} + +bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::post_processing() { + internal_order_test(); + + reinterpret_cast(taskData->outputs[0])[0] = result; + + return true; +} From dc0fb94a966505b66b985c09830bcbd14ce997a1 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Thu, 19 Dec 2024 23:08:49 +0300 Subject: [PATCH 02/12] Parallel solution --- .../func_tests/main.cpp | 225 ++++++++++++++++++ .../include/ops_mpi.hpp | 43 ++++ .../perf_tests/main.cpp | 112 +++++++++ .../src/ops_mpi.cpp | 140 +++++++++++ 4 files changed, 520 insertions(+) create mode 100644 tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp create mode 100644 tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp create mode 100644 tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/perf_tests/main.cpp create mode 100644 tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp new file mode 100644 index 00000000000..ba538c0035c --- /dev/null +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp @@ -0,0 +1,225 @@ +// Copyright 2023 Nesterov Alexander +#include + +#include +#include +#include +#include + +#include "mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp" + +double function1(std::vector input) { return pow(input[0], 3) + pow(input[1], 3); }; +double function2(std::vector input) { return sin(input[0]) + sin(input[1]) + sin(input[2]); }; +double function3(std::vector input) { return 8 * input[0] * input[1] * input[2]; }; +double function4(std::vector input) { return -1.0 / sqrt(1 - pow(input[0], 2)); }; +double function5(std::vector input) { return -(sin(input[0]) * cos(input[1])); }; +double function6(std::vector input) { return (-3 * pow(input[1], 2) * sin(5 * input[0])) / 2; }; + +void TestOfValidation(double (*function)(std::vector), std::vector& count, + std::vector>& limits, std::vector& intervals, + double answer) { + boost::mpi::communicator world; + + std::vector out(1, 0.0); + + std::shared_ptr taskDataParallel = std::make_shared(); + + // Create TaskData + + if (world.rank() == 0) { + taskDataParallel->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataParallel->inputs_count.emplace_back(count.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataParallel->inputs_count.emplace_back(limits.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataParallel->inputs_count.emplace_back(intervals.size()); + taskDataParallel->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataParallel->outputs_count.emplace_back(out.size()); + } + + kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel TaskParallel(taskDataParallel, function); + if (world.rank() == 0) { + ASSERT_EQ(TaskParallel.validation(), false); + } +} + +void TestOfFunction(double (*function)(std::vector), std::vector& count, + std::vector>& limits, std::vector& intervals, + double answer) { + boost::mpi::communicator world; + + std::vector out(1, 0.0); + + std::shared_ptr taskDataParallel = std::make_shared(); + + // Create TaskData + + if (world.rank() == 0) { + taskDataParallel->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataParallel->inputs_count.emplace_back(count.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataParallel->inputs_count.emplace_back(limits.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataParallel->inputs_count.emplace_back(intervals.size()); + taskDataParallel->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataParallel->outputs_count.emplace_back(out.size()); + } + + kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel TaskParallel(taskDataParallel, function); + ASSERT_EQ(TaskParallel.validation(), true); + TaskParallel.pre_processing(); + TaskParallel.run(); + TaskParallel.post_processing(); + + if (world.rank() == 0) { + EXPECT_NEAR(answer, out[0], 0.001); + } +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_count_of_variables) { + boost::mpi::communicator world; + + std::vector> limits; + std::vector intervals; + std::vector count; + + if (world.rank() == 0) { + limits = {{2.5, 4.5}, {1.0, 3.2}}; + intervals = {1000, 1000}; + count = std::vector{3}; + } + + TestOfValidation(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_size_numbers_of_intervals) { + boost::mpi::communicator world; + + std::vector> limits; + std::vector intervals; + std::vector count; + + if (world.rank() == 0) { + limits = {{2.5, 4.5}, {1.0, 3.2}}; + intervals = {1000}; + count = std::vector{2}; + } + + TestOfValidation(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_size_limits) { + boost::mpi::communicator world; + + std::vector> limits; + std::vector intervals; + std::vector count; + + if (world.rank() == 0) { + limits = {{2.5, 4.5}}; + intervals = {1000, 1000}; + count = std::vector{2}; + } + + TestOfValidation(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_1) { + boost::mpi::communicator world; + + std::vector> limits; + std::vector intervals; + std::vector count; + + if (world.rank() == 0) { + limits = {{2.5, 4.5}, {1.0, 3.2}}; + intervals = {1000, 1000}; + count = std::vector{2}; + } + + TestOfFunction(function1, count, limits, intervals, + (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_2) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{3}; + limits = {{0.0, 1.0}, {0.0, 1.0}, {0.0, 1.0}}; + intervals = {100, 100, 100}; + } + + TestOfFunction(function2, count, limits, intervals, -3 * std::cos(1) + 3 * std::cos(0)); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_3) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{3}; + limits = {{0.0, 3.0}, {0.0, 4.0}, {0.0, 5.0}}; + intervals = {100, 100, 100}; + } + + TestOfFunction(function3, count, limits, intervals, pow(3 - 0, 2) * pow(4 - 0, 2) * pow(5 - 0, 2)); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_4) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{1}; + limits = {{0.0, 0.5}}; + intervals = {10000}; + } + + TestOfFunction(function4, count, limits, intervals, acos(0.5) - acos(0)); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_5) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{2}; + limits = {{0.0, 1.0}, {0.0, 1.0}}; + intervals = {1000, 1000}; + } + + TestOfFunction(function5, count, limits, intervals, (sin(1) - sin(0)) * (cos(1) - cos(0))); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_6) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{2}; + limits = {{0.0, 1.0}, {4.0, 6.0}}; + intervals = {1000, 1000}; + } + + TestOfFunction(function6, count, limits, intervals, (pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10); +} \ No newline at end of file diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp new file mode 100644 index 00000000000..d460124f093 --- /dev/null +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp @@ -0,0 +1,43 @@ +// Copyright 2023 Nesterov Alexander +#pragma once + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "core/task/include/task.hpp" + +namespace kalyakina_a_trapezoidal_integration_mpi { + +class TrapezoidalIntegrationTaskParallel : public ppc::core::Task { + unsigned int CalculationOfCoefficient(const std::vector& point); + void Recursive(std::vector& _point, unsigned int& definition, unsigned int divider, unsigned int variable); + std::vector GetPointFromNumber(unsigned int number); + + public: + explicit TrapezoidalIntegrationTaskParallel(std::shared_ptr taskData_, + double (*_function)(std::vector)) + : Task(std::move(taskData_)), function(_function) {} + bool pre_processing() override; + bool validation() override; + bool run() override; + bool post_processing() override; + + private: + double (*function)(std::vector); + std::vector> limits; + std::vector number_of_intervals; + double result; + + boost::mpi::communicator world; +}; + +} // namespace kalyakina_a_trapezoidal_integration_mpi \ No newline at end of file diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/perf_tests/main.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/perf_tests/main.cpp new file mode 100644 index 00000000000..f4efec37f3f --- /dev/null +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/perf_tests/main.cpp @@ -0,0 +1,112 @@ +// Copyright 2023 Nesterov Alexander +#include + +#include +#include +#include + +#include "core/perf/include/perf.hpp" +#include "mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp" + +double function(std::vector input) { return (-3 * pow(input[1], 2) * sin(5 * input[0])) / 2; }; + +TEST(kalyakina_a_trapezoidal_integration_mpi, TrapezoidalIntegrationParallel_pipeline_run) { + boost::mpi::communicator world; + + std::vector> limits; + std::vector intervals; + std::vector count; + std::vector out(1, 0.0); + + // Create TaskData + std::shared_ptr taskDataParallel = std::make_shared(); + + if (world.rank() == 0) { + limits = {{0.0, 1.0}, {4.0, 6.0}}; + intervals = {500, 500}; + count = std::vector{2}; + taskDataParallel->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataParallel->inputs_count.emplace_back(count.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataParallel->inputs_count.emplace_back(limits.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataParallel->inputs_count.emplace_back(intervals.size()); + taskDataParallel->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataParallel->outputs_count.emplace_back(out.size()); + } + + auto testTaskParallel = std::make_shared( + taskDataParallel, function); + ASSERT_EQ(testTaskParallel->validation(), true); + testTaskParallel->pre_processing(); + testTaskParallel->run(); + testTaskParallel->post_processing(); + + // Create Perf attributes + auto perfAttr = std::make_shared(); + perfAttr->num_running = 10; + const boost::mpi::timer current_timer; + perfAttr->current_timer = [&] { return current_timer.elapsed(); }; + + // Create and init perf results + auto perfResults = std::make_shared(); + + // Create Perf analyzer + auto perfAnalyzer = std::make_shared(testTaskParallel); + perfAnalyzer->pipeline_run(perfAttr, perfResults); + + if (world.rank() == 0) { + ppc::core::Perf::print_perf_statistic(perfResults); + ASSERT_NEAR((pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10, out[0], 0.001); + } +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, TrapezoidalIntegrationParallel_task_run) { + boost::mpi::communicator world; + + std::vector> limits; + std::vector intervals; + std::vector count; + std::vector out(1, 0.0); + + // Create TaskData + std::shared_ptr taskDataParallel = std::make_shared(); + + if (world.rank() == 0) { + limits = {{0.0, 1.0}, {4.0, 6.0}}; + intervals = {500, 500}; + count = std::vector{2}; + taskDataParallel->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataParallel->inputs_count.emplace_back(count.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataParallel->inputs_count.emplace_back(limits.size()); + taskDataParallel->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataParallel->inputs_count.emplace_back(intervals.size()); + taskDataParallel->outputs.emplace_back(reinterpret_cast(out.data())); + taskDataParallel->outputs_count.emplace_back(out.size()); + } + + auto testTaskParallel = std::make_shared( + taskDataParallel, function); + ASSERT_EQ(testTaskParallel->validation(), true); + testTaskParallel->pre_processing(); + testTaskParallel->run(); + testTaskParallel->post_processing(); + + // Create Perf attributes + auto perfAttr = std::make_shared(); + perfAttr->num_running = 10; + const boost::mpi::timer current_timer; + perfAttr->current_timer = [&] { return current_timer.elapsed(); }; + + // Create and init perf results + auto perfResults = std::make_shared(); + + // Create Perf analyzer + auto perfAnalyzer = std::make_shared(testTaskParallel); + perfAnalyzer->task_run(perfAttr, perfResults); + if (world.rank() == 0) { + ppc::core::Perf::print_perf_statistic(perfResults); + ASSERT_NEAR((pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10, out[0], 0.001); + } +} \ No newline at end of file diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp new file mode 100644 index 00000000000..147fb9c9856 --- /dev/null +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp @@ -0,0 +1,140 @@ +// Copyright 2023 Nesterov Alexander +#include "mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp" + +#include +#include +#include +#include + +unsigned int kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::CalculationOfCoefficient( + const std::vector& point) { + unsigned int degree = limits.size(); + for (unsigned int i = 0; i < limits.size(); i++) { + if ((limits[i].first == point[i]) || (limits[i].second == point[i])) { + degree--; + } + } + + return pow(2, degree); +} + +void kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::Recursive(std::vector& _point, + unsigned int& definition, + unsigned int divider, + unsigned int variable) { + if (variable > 0) { + Recursive(_point, definition, divider * (number_of_intervals[variable] + 1), variable - 1); + } + _point[variable] = limits[variable].first + definition / divider * + (limits[variable].second - limits[variable].first) / + number_of_intervals[variable]; + definition = definition % divider; +} + +std::vector kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::GetPointFromNumber( + unsigned int number) { + std::vector point(limits.size()); + unsigned int definition = number; + Recursive(point, definition, 1, limits.size() - 1); + + return point; +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::pre_processing() { + internal_order_test(); + + if (world.rank() == 0) { + limits = std::vector>(taskData->inputs_count[1]); + std::pair* it1 = reinterpret_cast*>(taskData->inputs[1]); + std::copy(it1, it1 + taskData->inputs_count[1], limits.begin()); + + number_of_intervals = std::vector(taskData->inputs_count[2]); + unsigned int* it2 = reinterpret_cast(taskData->inputs[2]); + std::copy(it2, it2 + taskData->inputs_count[2], number_of_intervals.begin()); + + result = 0.0; + } + + return true; +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::validation() { + internal_order_test(); + + if (world.rank() == 0) { + // Check count elements of input and output + if ((taskData->inputs_count[0] != 1) || + (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || + (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)) { + return false; + } + } + + return true; +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::run() { + internal_order_test(); + + boost::mpi::broadcast(world, limits, 0); + boost::mpi::broadcast(world, number_of_intervals, 0); + + std::vector count_of_points; + std::vector first_point_numbers; + unsigned int local_count_of_points; + unsigned int local_first_point_numbers; + double local_result = 0.0; + + if (world.rank() == 0) { + count_of_points.resize(world.size()); + first_point_numbers.resize(world.size()); + unsigned int delta = 1; + unsigned int current_number = 0; + for (unsigned int i = 0; i < number_of_intervals.size(); i++) { + delta *= (number_of_intervals[i] + 1); + } + unsigned int remainder = delta % world.size(); + delta /= world.size(); + for (unsigned int i = 0; i < world.size() - remainder; i++) { + count_of_points[i] = delta; + first_point_numbers[i] = current_number; + current_number += delta; + } + delta++; + for (int i = world.size() - remainder; i < world.size(); i++) { + count_of_points[i] = delta; + first_point_numbers[i] = current_number; + current_number += delta; + } + } + + boost::mpi::scatter(world, &count_of_points[0], &local_count_of_points, 1, 0); + boost::mpi::scatter(world, &first_point_numbers[0], &local_first_point_numbers, 1, 0); + + for (unsigned int i = 0; i < local_count_of_points; i++) { + std::vector point = GetPointFromNumber(local_first_point_numbers + i); + local_result += CalculationOfCoefficient(point) * function(point); + } + + boost::mpi::reduce(world, local_result, result, std::plus(), 0); + + if (world.rank() == 0) { + for (unsigned int i = 0; i < limits.size(); i++) { + result *= (limits[i].second - limits[i].first) / number_of_intervals[i]; + } + result /= pow(2, limits.size()); + } + + return true; +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::post_processing() { + internal_order_test(); + + if (world.rank() == 0) { + reinterpret_cast(taskData->outputs[0])[0] = result; + } + + world.barrier(); + return true; +} From 9b4aafbe0a997e586bf0c3e834d1cdcfd9f37f89 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Thu, 19 Dec 2024 23:32:27 +0300 Subject: [PATCH 03/12] Code changes --- .../func_tests/main.cpp | 12 ++++-------- .../func_tests/main.cpp | 12 ++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp index ba538c0035c..e6c6a2c5be5 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp @@ -16,8 +16,7 @@ double function5(std::vector input) { return -(sin(input[0]) * cos(input double function6(std::vector input) { return (-3 * pow(input[1], 2) * sin(5 * input[0])) / 2; }; void TestOfValidation(double (*function)(std::vector), std::vector& count, - std::vector>& limits, std::vector& intervals, - double answer) { + std::vector>& limits, std::vector& intervals) { boost::mpi::communicator world; std::vector out(1, 0.0); @@ -89,8 +88,7 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_count_of_variab count = std::vector{3}; } - TestOfValidation(function1, count, limits, intervals, - (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); + TestOfValidation(function1, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_size_numbers_of_intervals) { @@ -106,8 +104,7 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_size_numbers_of count = std::vector{2}; } - TestOfValidation(function1, count, limits, intervals, - (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); + TestOfValidation(function1, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_size_limits) { @@ -123,8 +120,7 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_size_limits) { count = std::vector{2}; } - TestOfValidation(function1, count, limits, intervals, - (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); + TestOfValidation(function1, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_1) { diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp index fee250308ac..8866db37677 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -14,8 +14,7 @@ double function5(std::vector input) { return -(sin(input[0]) * cos(input double function6(std::vector input) { return (-3 * pow(input[1], 2) * sin(5 * input[0])) / 2; }; void TestOfValidation(double (*function)(std::vector), std::vector& count, - std::vector>& limits, std::vector& intervals, - double answer) { + std::vector>& limits, std::vector& intervals) { std::vector out(1, 0.0); // Create TaskData @@ -67,8 +66,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_count_of_variab std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; std::vector intervals = {1000, 1000}; - TestOfValidation(function1, count, limits, intervals, - (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); + TestOfValidation(function1, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_numbers_of_intervals) { @@ -76,8 +74,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_numbers_of std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; std::vector intervals = {1000}; - TestOfValidation(function1, count, limits, intervals, - (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); + TestOfValidation(function1, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_limits) { @@ -85,8 +82,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_limits) { std::vector> limits = {{2.5, 4.5}}; std::vector intervals = {1000, 1000}; - TestOfValidation(function1, count, limits, intervals, - (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); + TestOfValidation(function1, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_1) { From 349c73783c1aea85d22e54b51157bdb7adfa23a8 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Fri, 20 Dec 2024 01:07:30 +0300 Subject: [PATCH 04/12] Code changes 0.1 --- .../src/ops_mpi.cpp | 16 +++++++-------- .../func_tests/main.cpp | 20 +++++++++---------- .../src/ops_seq.cpp | 14 +++++-------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp index 147fb9c9856..aaa96cf5942 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp @@ -45,11 +45,11 @@ bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel if (world.rank() == 0) { limits = std::vector>(taskData->inputs_count[1]); - std::pair* it1 = reinterpret_cast*>(taskData->inputs[1]); + auto* it1 = reinterpret_cast*>(taskData->inputs[1]); std::copy(it1, it1 + taskData->inputs_count[1], limits.begin()); number_of_intervals = std::vector(taskData->inputs_count[2]); - unsigned int* it2 = reinterpret_cast(taskData->inputs[2]); + auto* it2 = reinterpret_cast(taskData->inputs[2]); std::copy(it2, it2 + taskData->inputs_count[2], number_of_intervals.begin()); result = 0.0; @@ -63,11 +63,9 @@ bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel if (world.rank() == 0) { // Check count elements of input and output - if ((taskData->inputs_count[0] != 1) || - (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || - (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)) { - return false; - } + return !((taskData->inputs_count[0] != 1) || + (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || + (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)); } return true; @@ -108,8 +106,8 @@ bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel } } - boost::mpi::scatter(world, &count_of_points[0], &local_count_of_points, 1, 0); - boost::mpi::scatter(world, &first_point_numbers[0], &local_first_point_numbers, 1, 0); + boost::mpi::scatter(world, count_of_points.data(), &local_count_of_points, 1, 0); + boost::mpi::scatter(world, first_point_numbers.data(), &local_first_point_numbers, 1, 0); for (unsigned int i = 0; i < local_count_of_points; i++) { std::vector point = GetPointFromNumber(local_first_point_numbers + i); diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp index 8866db37677..baa22feefbf 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -58,13 +58,13 @@ void TestOfFunction(double (*function)(std::vector), std::vector count(1, 3); std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; - std::vector intervals = {1000, 1000}; + std::vector intervals = {100, 100}; TestOfValidation(function1, count, limits, intervals); } @@ -72,7 +72,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_count_of_variab TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_numbers_of_intervals) { std::vector count(1, 2); std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; - std::vector intervals = {1000}; + std::vector intervals = {100}; TestOfValidation(function1, count, limits, intervals); } @@ -80,7 +80,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_numbers_of TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_limits) { std::vector count(1, 2); std::vector> limits = {{2.5, 4.5}}; - std::vector intervals = {1000, 1000}; + std::vector intervals = {100, 100}; TestOfValidation(function1, count, limits, intervals); } @@ -88,7 +88,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_validation_size_limits) { TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_1) { std::vector count(1, 2); std::vector> limits = {{2.5, 4.5}, {1.0, 3.2}}; - std::vector intervals = {1000, 1000}; + std::vector intervals = {100, 100}; TestOfFunction(function1, count, limits, intervals, (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); @@ -97,7 +97,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_1) { TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_2) { std::vector count(1, 3); std::vector> limits = {{0.0, 1.0}, {0.0, 1.0}, {0.0, 1.0}}; - std::vector intervals = {100, 100, 100}; + std::vector intervals = {50, 50, 50}; TestOfFunction(function2, count, limits, intervals, -3 * std::cos(1) + 3 * std::cos(0)); } @@ -105,7 +105,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_2) { TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_3) { std::vector count(1, 3); std::vector> limits = {{0.0, 3.0}, {0.0, 4.0}, {0.0, 5.0}}; - std::vector intervals = {100, 100, 100}; + std::vector intervals = {50, 50, 50}; TestOfFunction(function3, count, limits, intervals, pow(3 - 0, 2) * pow(4 - 0, 2) * pow(5 - 0, 2)); } @@ -113,7 +113,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_3) { TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_4) { std::vector count(1, 1); std::vector> limits = {{0.0, 0.5}}; - std::vector intervals = {10000}; + std::vector intervals = {1000}; TestOfFunction(function4, count, limits, intervals, acos(0.5) - acos(0)); } @@ -121,7 +121,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_4) { TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_5) { std::vector count(1, 2); std::vector> limits = {{0.0, 1.0}, {0.0, 1.0}}; - std::vector intervals = {1000, 1000}; + std::vector intervals = {100, 100}; TestOfFunction(function5, count, limits, intervals, (sin(1) - sin(0)) * (cos(1) - cos(0))); } @@ -129,7 +129,7 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_5) { TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_6) { std::vector count(1, 2); std::vector> limits = {{0.0, 1.0}, {4.0, 6.0}}; - std::vector intervals = {1000, 1000}; + std::vector intervals = {100, 100}; TestOfFunction(function6, count, limits, intervals, (pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10); } \ No newline at end of file diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp index 90997905496..cb97252b2e8 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp @@ -44,11 +44,11 @@ bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::pre_pr internal_order_test(); limits = std::vector>(taskData->inputs_count[1]); - std::pair* it1 = reinterpret_cast*>(taskData->inputs[1]); + auto* it1 = reinterpret_cast*>(taskData->inputs[1]); std::copy(it1, it1 + taskData->inputs_count[1], limits.begin()); number_of_intervals = std::vector(taskData->inputs_count[2]); - unsigned int* it2 = reinterpret_cast(taskData->inputs[2]); + auto* it2 = reinterpret_cast(taskData->inputs[2]); std::copy(it2, it2 + taskData->inputs_count[2], number_of_intervals.begin()); result = 0.0; @@ -59,13 +59,9 @@ bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::pre_pr bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::validation() { internal_order_test(); - if ((taskData->inputs_count[0] != 1) || - (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || - (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)) { - return false; - } - - return true; + return !((taskData->inputs_count[0] != 1) || + (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || + (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)); } bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::run() { From 82997c0c351fc3f2029508edb8bff86e66ce9513 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Fri, 20 Dec 2024 01:20:53 +0300 Subject: [PATCH 05/12] Format changes --- .../kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp | 6 +++--- .../kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp index aaa96cf5942..78a103baaa0 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp @@ -63,9 +63,9 @@ bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel if (world.rank() == 0) { // Check count elements of input and output - return !((taskData->inputs_count[0] != 1) || - (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || - (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)); + return ((taskData->inputs_count[0] == 1) && + (reinterpret_cast(taskData->inputs[0])[0] == taskData->inputs_count[1]) && + (taskData->inputs_count[1] == taskData->inputs_count[2]) && (taskData->outputs_count[0] == 1)); } return true; diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp index cb97252b2e8..d9e0b2182ee 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp @@ -59,9 +59,9 @@ bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::pre_pr bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::validation() { internal_order_test(); - return !((taskData->inputs_count[0] != 1) || - (reinterpret_cast(taskData->inputs[0])[0] != taskData->inputs_count[1]) || - (taskData->inputs_count[1] != taskData->inputs_count[2]) || (taskData->outputs_count[0] != 1)); + return ((taskData->inputs_count[0] == 1) && + (reinterpret_cast(taskData->inputs[0])[0] == taskData->inputs_count[1]) && + (taskData->inputs_count[1] == taskData->inputs_count[2]) && (taskData->outputs_count[0] == 1)); } bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::run() { From cf2ffa36fde29526ce8015309d1fee408adda811 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Sat, 21 Dec 2024 13:38:22 +0300 Subject: [PATCH 06/12] Add sequential solution to parallel --- .../func_tests/main.cpp | 64 +++++++++----- .../include/ops_mpi.hpp | 21 +++++ .../src/ops_mpi.cpp | 86 +++++++++++++++++++ 3 files changed, 150 insertions(+), 21 deletions(-) diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp index e6c6a2c5be5..5c7788e48b3 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp @@ -43,11 +43,10 @@ void TestOfValidation(double (*function)(std::vector), std::vector), std::vector& count, - std::vector>& limits, std::vector& intervals, - double answer) { + std::vector>& limits, std::vector& intervals) { boost::mpi::communicator world; - std::vector out(1, 0.0); + std::vector out_mpi(1, 0.0); std::shared_ptr taskDataParallel = std::make_shared(); @@ -60,8 +59,8 @@ void TestOfFunction(double (*function)(std::vector), std::vectorinputs_count.emplace_back(limits.size()); taskDataParallel->inputs.emplace_back(reinterpret_cast(intervals.data())); taskDataParallel->inputs_count.emplace_back(intervals.size()); - taskDataParallel->outputs.emplace_back(reinterpret_cast(out.data())); - taskDataParallel->outputs_count.emplace_back(out.size()); + taskDataParallel->outputs.emplace_back(reinterpret_cast(out_mpi.data())); + taskDataParallel->outputs_count.emplace_back(out_mpi.size()); } kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel TaskParallel(taskDataParallel, function); @@ -71,7 +70,31 @@ void TestOfFunction(double (*function)(std::vector), std::vector out_seq(1, 0.0); + + // Create TaskData + std::shared_ptr taskDataSequential = std::make_shared(); + + taskDataSequential->inputs.emplace_back(reinterpret_cast(count.data())); + taskDataSequential->inputs_count.emplace_back(count.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(limits.data())); + taskDataSequential->inputs_count.emplace_back(limits.size()); + taskDataSequential->inputs.emplace_back(reinterpret_cast(intervals.data())); + taskDataSequential->inputs_count.emplace_back(intervals.size()); + taskDataSequential->outputs.emplace_back(reinterpret_cast(out_seq.data())); + taskDataSequential->outputs_count.emplace_back(out_seq.size()); + + // Create Task + kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential TaskSequential(taskDataSequential, + function); + + ASSERT_EQ(TaskSequential.validation(), true); + TaskSequential.pre_processing(); + TaskSequential.run(); + TaskSequential.post_processing(); + + EXPECT_NEAR(out_seq[0], out_mpi[0], 0.00001); } } @@ -84,7 +107,7 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_count_of_variab if (world.rank() == 0) { limits = {{2.5, 4.5}, {1.0, 3.2}}; - intervals = {1000, 1000}; + intervals = {100, 100}; count = std::vector{3}; } @@ -116,7 +139,7 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_validation_size_limits) { if (world.rank() == 0) { limits = {{2.5, 4.5}}; - intervals = {1000, 1000}; + intervals = {100, 100}; count = std::vector{2}; } @@ -132,12 +155,11 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_1) { if (world.rank() == 0) { limits = {{2.5, 4.5}, {1.0, 3.2}}; - intervals = {1000, 1000}; + intervals = {100, 100}; count = std::vector{2}; } - TestOfFunction(function1, count, limits, intervals, - (3.2 - 1) * (pow(4.5, 4) - pow(2.5, 4)) / 4 + (4.5 - 2.5) * (pow(3.2, 4) - pow(1, 4)) / 4); + TestOfFunction(function1, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_2) { @@ -150,10 +172,10 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_2) { if (world.rank() == 0) { count = std::vector{3}; limits = {{0.0, 1.0}, {0.0, 1.0}, {0.0, 1.0}}; - intervals = {100, 100, 100}; + intervals = {80, 80, 80}; } - TestOfFunction(function2, count, limits, intervals, -3 * std::cos(1) + 3 * std::cos(0)); + TestOfFunction(function2, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_3) { @@ -166,10 +188,10 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_3) { if (world.rank() == 0) { count = std::vector{3}; limits = {{0.0, 3.0}, {0.0, 4.0}, {0.0, 5.0}}; - intervals = {100, 100, 100}; + intervals = {80, 80, 80}; } - TestOfFunction(function3, count, limits, intervals, pow(3 - 0, 2) * pow(4 - 0, 2) * pow(5 - 0, 2)); + TestOfFunction(function3, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_4) { @@ -182,10 +204,10 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_4) { if (world.rank() == 0) { count = std::vector{1}; limits = {{0.0, 0.5}}; - intervals = {10000}; + intervals = {1000}; } - TestOfFunction(function4, count, limits, intervals, acos(0.5) - acos(0)); + TestOfFunction(function4, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_5) { @@ -198,10 +220,10 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_5) { if (world.rank() == 0) { count = std::vector{2}; limits = {{0.0, 1.0}, {0.0, 1.0}}; - intervals = {1000, 1000}; + intervals = {100, 100}; } - TestOfFunction(function5, count, limits, intervals, (sin(1) - sin(0)) * (cos(1) - cos(0))); + TestOfFunction(function5, count, limits, intervals); } TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_6) { @@ -214,8 +236,8 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_6) { if (world.rank() == 0) { count = std::vector{2}; limits = {{0.0, 1.0}, {4.0, 6.0}}; - intervals = {1000, 1000}; + intervals = {100, 100}; } - TestOfFunction(function6, count, limits, intervals, (pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10); + TestOfFunction(function6, count, limits, intervals); } \ No newline at end of file diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp index d460124f093..9d537d8020b 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp @@ -17,6 +17,27 @@ namespace kalyakina_a_trapezoidal_integration_mpi { +class TrapezoidalIntegrationTaskSequential : public ppc::core::Task { + unsigned int CalculationOfCoefficient(const std::vector& point); + void Recursive(std::vector& _point, unsigned int& definition, unsigned int divider, unsigned int variable); + std::vector GetPointFromNumber(unsigned int number); + + public: + explicit TrapezoidalIntegrationTaskSequential(std::shared_ptr taskData_, + double (*_function)(std::vector)) + : Task(std::move(taskData_)), function(_function) {} + bool pre_processing() override; + bool validation() override; + bool run() override; + bool post_processing() override; + + private: + double (*function)(std::vector); + std::vector> limits; + std::vector number_of_intervals; + double result; +}; + class TrapezoidalIntegrationTaskParallel : public ppc::core::Task { unsigned int CalculationOfCoefficient(const std::vector& point); void Recursive(std::vector& _point, unsigned int& definition, unsigned int divider, unsigned int variable); diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp index 78a103baaa0..830456edfca 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp @@ -6,6 +6,92 @@ #include #include +unsigned int kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::CalculationOfCoefficient( + const std::vector& point) { + unsigned int degree = limits.size(); + for (unsigned int i = 0; i < limits.size(); i++) { + if ((limits[i].first == point[i]) || (limits[i].second == point[i])) { + degree--; + } + } + + return pow(2, degree); +} + +void kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::Recursive( + std::vector& _point, unsigned int& definition, unsigned int divider, unsigned int variable) { + if (variable > 0) { + Recursive(_point, definition, divider * (number_of_intervals[variable] + 1), variable - 1); + } + _point[variable] = limits[variable].first + definition / divider * + (limits[variable].second - limits[variable].first) / + number_of_intervals[variable]; + definition = definition % divider; +} + +std::vector kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::GetPointFromNumber( + unsigned int number) { + std::vector point(limits.size()); + unsigned int definition = number; + Recursive(point, definition, 1, limits.size() - 1); + + return point; +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::pre_processing() { + internal_order_test(); + + limits = std::vector>(taskData->inputs_count[1]); + auto* it1 = reinterpret_cast*>(taskData->inputs[1]); + std::copy(it1, it1 + taskData->inputs_count[1], limits.begin()); + + number_of_intervals = std::vector(taskData->inputs_count[2]); + auto* it2 = reinterpret_cast(taskData->inputs[2]); + std::copy(it2, it2 + taskData->inputs_count[2], number_of_intervals.begin()); + + result = 0.0; + + return true; +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::validation() { + internal_order_test(); + + return ((taskData->inputs_count[0] == 1) && + (reinterpret_cast(taskData->inputs[0])[0] == taskData->inputs_count[1]) && + (taskData->inputs_count[1] == taskData->inputs_count[2]) && (taskData->outputs_count[0] == 1)); +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::run() { + internal_order_test(); + + unsigned int count = 1; + for (unsigned int i = 0; i < number_of_intervals.size(); i++) { + count *= (number_of_intervals[i] + 1); + } + + for (unsigned int i = 0; i < count; i++) { + std::vector point = GetPointFromNumber(i); + result += CalculationOfCoefficient(point) * function(point); + } + + for (unsigned int i = 0; i < limits.size(); i++) { + result *= (limits[i].second - limits[i].first) / number_of_intervals[i]; + } + + result /= pow(2, limits.size()); + + return true; +} + +bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::post_processing() { + internal_order_test(); + + reinterpret_cast(taskData->outputs[0])[0] = result; + + return true; +} + unsigned int kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::CalculationOfCoefficient( const std::vector& point) { unsigned int degree = limits.size(); From 5d32cb885e3f9c522d9bba27dd879aa13b7fe3c3 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Sat, 21 Dec 2024 21:22:15 +0300 Subject: [PATCH 07/12] Changes in validation --- .../src/ops_mpi.cpp | 14 +++++--------- .../src/ops_seq.cpp | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp index 830456edfca..bcb45776385 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/src/ops_mpi.cpp @@ -57,7 +57,7 @@ bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequenti bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskSequential::validation() { internal_order_test(); - return ((taskData->inputs_count[0] == 1) && + return ((taskData->inputs.size() == 3) && (taskData->inputs_count[0] == 1) && (reinterpret_cast(taskData->inputs[0])[0] == taskData->inputs_count[1]) && (taskData->inputs_count[1] == taskData->inputs_count[2]) && (taskData->outputs_count[0] == 1)); } @@ -147,14 +147,10 @@ bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::validation() { internal_order_test(); - if (world.rank() == 0) { - // Check count elements of input and output - return ((taskData->inputs_count[0] == 1) && - (reinterpret_cast(taskData->inputs[0])[0] == taskData->inputs_count[1]) && - (taskData->inputs_count[1] == taskData->inputs_count[2]) && (taskData->outputs_count[0] == 1)); - } - - return true; + return (world.rank() != 0) || + ((taskData->inputs.size() == 3) && (taskData->inputs_count[0] == 1) && + (reinterpret_cast(taskData->inputs[0])[0] == taskData->inputs_count[1]) && + (taskData->inputs_count[1] == taskData->inputs_count[2]) && (taskData->outputs_count[0] == 1)); } bool kalyakina_a_trapezoidal_integration_mpi::TrapezoidalIntegrationTaskParallel::run() { diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp index d9e0b2182ee..59adda8ac08 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/src/ops_seq.cpp @@ -59,7 +59,7 @@ bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::pre_pr bool kalyakina_a_trapezoidal_integration_seq::TrapezoidalIntegrationTask::validation() { internal_order_test(); - return ((taskData->inputs_count[0] == 1) && + return ((taskData->inputs.size() == 3) && (taskData->inputs_count[0] == 1) && (reinterpret_cast(taskData->inputs[0])[0] == taskData->inputs_count[1]) && (taskData->inputs_count[1] == taskData->inputs_count[2]) && (taskData->outputs_count[0] == 1)); } From 10634d4d2a80f050cdbc508c3679f0294fa5fabd Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Sat, 21 Dec 2024 22:20:58 +0300 Subject: [PATCH 08/12] Add tests with random datas --- .../func_tests/main.cpp | 68 +++++++++++++++++++ .../func_tests/main.cpp | 54 +++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp index 5c7788e48b3..defbcd35034 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp @@ -4,10 +4,30 @@ #include #include #include +#include #include #include "mpi/kalyakina_a_trapezoidal_integration_mpi/include/ops_mpi.hpp" +std::pair GetRandomLimit(double min_value, double max_value) { + std::random_device dev; + std::mt19937 gen(dev()); + std::pair result; + max_value = max_value * 1000 - 5; + min_value *= 1000; + result.first = (double)(gen() % (int)(max_value - min_value) + min_value) / 1000; + max_value += 5; + min_value = result.first * 1000; + result.second = (double)(gen() % (int)(max_value - min_value) + min_value) / 1000; + return result; +} + +unsigned int GetRandomIntegerData(unsigned int min_value, unsigned int max_value) { + std::random_device dev; + std::mt19937 gen(dev()); + return gen() % (max_value - min_value) + min_value; +} + double function1(std::vector input) { return pow(input[0], 3) + pow(input[1], 3); }; double function2(std::vector input) { return sin(input[0]) + sin(input[1]) + sin(input[2]); }; double function3(std::vector input) { return 8 * input[0] * input[1] * input[2]; }; @@ -240,4 +260,52 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_6) { } TestOfFunction(function6, count, limits, intervals); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_random_limits) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{3}; + limits = {GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0)}; + intervals = {80, 80, 80}; + } + + TestOfFunction(function3, count, limits, intervals); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_random_intervals) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{2}; + limits = {{0.0, 1.0}, {4.0, 6.0}}; + intervals = {GetRandomIntegerData(100, 200), GetRandomIntegerData(100, 200)}; + } + + TestOfFunction(function6, count, limits, intervals); +} + +TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_random_limits_and_intervals) { + boost::mpi::communicator world; + + std::vector count; + std::vector> limits; + std::vector intervals; + + if (world.rank() == 0) { + count = std::vector{3}; + limits = {GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0)}; + intervals = {GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80)}; + } + + TestOfFunction(function3, count, limits, intervals); } \ No newline at end of file diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp index baa22feefbf..14ad8df86d5 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -3,9 +3,29 @@ #include #include +#include #include "seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp" +std::pair GetRandomLimit(double min_value, double max_value) { + std::random_device dev; + std::mt19937 gen(dev()); + std::pair result; + max_value = max_value * 1000 - 5; + min_value *= 1000; + result.first = (double)(gen() % (int)(max_value - min_value) + min_value) / 1000; + max_value += 5; + min_value = result.first * 1000; + result.second = (double)(gen() % (int)(max_value - min_value) + min_value) / 1000; + return result; +} + +unsigned int GetRandomIntegerData(unsigned int min_value, unsigned int max_value) { + std::random_device dev; + std::mt19937 gen(dev()); + return gen() % (max_value - min_value) + min_value; +} + double function1(std::vector input) { return pow(input[0], 3) + pow(input[1], 3); }; double function2(std::vector input) { return sin(input[0]) + sin(input[1]) + sin(input[2]); }; double function3(std::vector input) { return 8 * input[0] * input[1] * input[2]; }; @@ -132,4 +152,38 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_6) { std::vector intervals = {100, 100}; TestOfFunction(function6, count, limits, intervals, (pow(6.0, 3) - pow(4.0, 3)) * (cos(5 * 1.0) - cos(5 * 0.0)) / 10); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_random_limits) { + std::vector count(1, 3); + std::vector> limits = {GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0), + GetRandomLimit(0.0, 10.0)}; + std::vector intervals = {50, 50, 50}; + + TestOfFunction(function3, count, limits, intervals, + (pow(limits[0].second, 2) - pow(limits[0].first, 2)) * + (pow(limits[1].second, 2) - pow(limits[1].first, 2)) * + (pow(limits[2].second, 2) - pow(limits[2].first, 2))); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_intervals) { + std::vector count(1, 3); + std::vector> limits = {{0.0, 3.0}, {0.0, 4.0}, {0.0, 5.0}}; + std::vector intervals = {GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80), + GetRandomIntegerData(50, 80)}; + + TestOfFunction(function3, count, limits, intervals, pow(3 - 0, 2) * pow(4 - 0, 2) * pow(5 - 0, 2)); +} + +TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_limits_and_intervals) { + std::vector count(1, 3); + std::vector> limits = {GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0), + GetRandomLimit(0.0, 10.0)}; + std::vector intervals = {GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80), + GetRandomIntegerData(50, 80)}; + + TestOfFunction(function3, count, limits, intervals, + (pow(limits[0].second, 2) - pow(limits[0].first, 2)) * + (pow(limits[1].second, 2) - pow(limits[1].first, 2)) * + (pow(limits[2].second, 2) - pow(limits[2].first, 2))); } \ No newline at end of file From 83dd47bf82d92a68259a1095ded40014f95f90a6 Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Sat, 21 Dec 2024 22:24:46 +0300 Subject: [PATCH 09/12] Format changes --- .../kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp index 14ad8df86d5..673d7f9d24d 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -2,8 +2,8 @@ #include #include -#include #include +#include #include "seq/kalyakina_a_trapezoidal_integration_seq/include/ops_seq.hpp" From 1df369750e0d2c38a741f6bf8d044aea33ae296c Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Sat, 21 Dec 2024 23:03:01 +0300 Subject: [PATCH 10/12] Test values changes --- .../func_tests/main.cpp | 4 ++-- .../func_tests/main.cpp | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp index defbcd35034..e742285e54c 100644 --- a/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp +++ b/tasks/mpi/kalyakina_a_trapezoidal_integration_mpi/func_tests/main.cpp @@ -288,7 +288,7 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_random_inter if (world.rank() == 0) { count = std::vector{2}; limits = {{0.0, 1.0}, {4.0, 6.0}}; - intervals = {GetRandomIntegerData(100, 200), GetRandomIntegerData(100, 200)}; + intervals = {GetRandomIntegerData(100, 150), GetRandomIntegerData(100, 150)}; } TestOfFunction(function6, count, limits, intervals); @@ -304,7 +304,7 @@ TEST(kalyakina_a_trapezoidal_integration_mpi, Test_of_functionality_random_limit if (world.rank() == 0) { count = std::vector{3}; limits = {GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0)}; - intervals = {GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80)}; + intervals = {GetRandomIntegerData(40, 60), GetRandomIntegerData(40, 60), GetRandomIntegerData(40, 60)}; } TestOfFunction(function3, count, limits, intervals); diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp index 673d7f9d24d..027fe1b356f 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -156,8 +156,8 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_6) { TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_random_limits) { std::vector count(1, 3); - std::vector> limits = {GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0), - GetRandomLimit(0.0, 10.0)}; + std::vector> limits = {GetRandomLimit(0.0, 1.0), GetRandomLimit(0.0, 1.0), + GetRandomLimit(0.0, 1.0)}; std::vector intervals = {50, 50, 50}; TestOfFunction(function3, count, limits, intervals, @@ -169,18 +169,18 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_random_limit TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_intervals) { std::vector count(1, 3); std::vector> limits = {{0.0, 3.0}, {0.0, 4.0}, {0.0, 5.0}}; - std::vector intervals = {GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80), - GetRandomIntegerData(50, 80)}; + std::vector intervals = {GetRandomIntegerData(40, 60), GetRandomIntegerData(40, 60), + GetRandomIntegerData(40, 60)}; TestOfFunction(function3, count, limits, intervals, pow(3 - 0, 2) * pow(4 - 0, 2) * pow(5 - 0, 2)); } TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_limits_and_intervals) { std::vector count(1, 3); - std::vector> limits = {GetRandomLimit(0.0, 10.0), GetRandomLimit(0.0, 10.0), - GetRandomLimit(0.0, 10.0)}; - std::vector intervals = {GetRandomIntegerData(50, 80), GetRandomIntegerData(50, 80), - GetRandomIntegerData(50, 80)}; + std::vector> limits = {GetRandomLimit(0.0, 1.0), GetRandomLimit(0.0, 1.0), + GetRandomLimit(0.0, 1.0)}; + std::vector intervals = {GetRandomIntegerData(40, 60), GetRandomIntegerData(40, 60), + GetRandomIntegerData(40, 60)}; TestOfFunction(function3, count, limits, intervals, (pow(limits[0].second, 2) - pow(limits[0].first, 2)) * From c5110b4176802f612e8ae5456075b6d2e5897d2b Mon Sep 17 00:00:00 2001 From: kalyakinaa Date: Sun, 22 Dec 2024 00:07:00 +0300 Subject: [PATCH 11/12] Test values changes 0.1 --- .../kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp index 027fe1b356f..47f7dfaf73b 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -78,7 +78,7 @@ void TestOfFunction(double (*function)(std::vector), std::vector Date: Sun, 29 Dec 2024 20:03:10 +0300 Subject: [PATCH 12/12] Fix parameters in seq_func_tests --- .../func_tests/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp index 47f7dfaf73b..721f2afdb4e 100644 --- a/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp +++ b/tasks/seq/kalyakina_a_trapezoidal_integration_seq/func_tests/main.cpp @@ -169,8 +169,8 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_random_limit TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_intervals) { std::vector count(1, 3); std::vector> limits = {{0.0, 3.0}, {0.0, 4.0}, {0.0, 5.0}}; - std::vector intervals = {GetRandomIntegerData(40, 60), GetRandomIntegerData(40, 60), - GetRandomIntegerData(40, 60)}; + std::vector intervals = {GetRandomIntegerData(40, 50), GetRandomIntegerData(40, 50), + GetRandomIntegerData(40, 50)}; TestOfFunction(function3, count, limits, intervals, pow(3 - 0, 2) * pow(4 - 0, 2) * pow(5 - 0, 2)); } @@ -179,8 +179,8 @@ TEST(kalyakina_a_trapezoidal_integration_seq, Test_of_functionality_limits_and_i std::vector count(1, 3); std::vector> limits = {GetRandomLimit(0.0, 1.0), GetRandomLimit(0.0, 1.0), GetRandomLimit(0.0, 1.0)}; - std::vector intervals = {GetRandomIntegerData(40, 60), GetRandomIntegerData(40, 60), - GetRandomIntegerData(40, 60)}; + std::vector intervals = {GetRandomIntegerData(40, 50), GetRandomIntegerData(40, 50), + GetRandomIntegerData(40, 50)}; TestOfFunction(function3, count, limits, intervals, (pow(limits[0].second, 2) - pow(limits[0].first, 2)) *