Skip to content

Commit

Permalink
9 Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
At0mn1yIvan committed Nov 16, 2023
1 parent dce5489 commit 7eb6d27
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 50 deletions.
68 changes: 47 additions & 21 deletions tasks/task_1/khramov_i_vector_dot_product/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,48 @@
#include <numeric>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/vector.hpp>
#include "./vector_dot_product.h"
#include <boost/mpi.hpp>


TEST(Vector_Dot_Product_MPI, Test_Equal_Size_Vectors) {
boost::mpi::communicator world;
std::vector<int> global_vec1, global_vec2;
const int count_size_vector = 500;
const int count_size_vector = 10001;

if (world.rank() == 0) {
global_vec1 = getRandomVector(count_size_vector);
global_vec2 = getRandomVector(count_size_vector);
}

int global_product = getVectorDotProduct(world, global_vec1, global_vec2);
int global_product = getParallelVectorDotProduct(
world,
count_size_vector,
global_vec1,
global_vec2);

if (world.rank() == 0) {
int reference_product = std::inner_product(
std::begin(global_vec1),
std::end(global_vec1),
std::begin(global_vec2), 0);
int reference_product = getSequentialVectorDotProduct(
global_vec1,
global_vec2);
ASSERT_EQ(reference_product, global_product);
}
}

TEST(Vector_Dot_Product_MPI, Test_Not_Equal_Size_Vectors) {
boost::mpi::communicator world;
std::vector<int> global_vec1, global_vec2;
const int count_size_vector = 500;
const int count_size_vector = 10001;

if (world.rank() == 0) {
global_vec1 = getRandomVector(count_size_vector);
global_vec2 = getRandomVector(count_size_vector + 1);
}

int global_product = getVectorDotProduct(world, global_vec1, global_vec2);
int global_product = getParallelVectorDotProduct(
world,
count_size_vector,
global_vec1,
global_vec2);

if (world.rank() == 0) {
int reference_product = -1;
Expand All @@ -58,10 +63,16 @@ TEST(Vector_Dot_Product_MPI, Test_One_Size_Vectors) {
global_vec2 = getRandomVector(count_size_vector);
}

int global_product = getVectorDotProduct(world, global_vec1, global_vec2);
int global_product = getParallelVectorDotProduct(
world,
count_size_vector,
global_vec1,
global_vec2);

if (world.rank() == 0) {
int reference_product = global_vec1[0] * global_vec2[0];
int reference_product = getSequentialVectorDotProduct(
global_vec1,
global_vec2);
ASSERT_EQ(reference_product, global_product);
}
}
Expand All @@ -76,7 +87,11 @@ TEST(Vector_Dot_Product_MPI, Test_Both_Void_Vectors) {
global_vec2 = getRandomVector(count_size_vector);
}

int global_product = getVectorDotProduct(world, global_vec1, global_vec2);
int global_product = getParallelVectorDotProduct(
world,
count_size_vector,
global_vec1,
global_vec2);

if (world.rank() == 0) {
int reference_product = 0;
Expand All @@ -94,7 +109,11 @@ TEST(Vector_Dot_Product_MPI, Test_First_Void_Vector) {
global_vec2 = getRandomVector(count_size_vector + 1);
}

int global_product = getVectorDotProduct(world, global_vec1, global_vec2);
int global_product = getParallelVectorDotProduct(
world,
count_size_vector,
global_vec1,
global_vec2);

if (world.rank() == 0) {
int reference_product = -1;
Expand All @@ -112,7 +131,11 @@ TEST(Vector_Dot_Product_MPI, Test_Second_Void_Vector) {
global_vec2 = getRandomVector(count_size_vector);
}

int global_product = getVectorDotProduct(world, global_vec1, global_vec2);
int global_product = getParallelVectorDotProduct(
world,
count_size_vector,
global_vec1,
global_vec2);

if (world.rank() == 0) {
int reference_product = -1;
Expand All @@ -123,20 +146,23 @@ TEST(Vector_Dot_Product_MPI, Test_Second_Void_Vector) {
TEST(Vector_Dot_Product_MPI, Test_Order_Independent_Vectors) {
boost::mpi::communicator world;
std::vector<int> global_vec1, global_vec2;
const int count_size_vector = 500;
const int count_size_vector = 0;

if (world.rank() == 0) {
global_vec1 = getRandomVector(count_size_vector);
global_vec2 = getRandomVector(count_size_vector);
}

int global_product = getVectorDotProduct(world, global_vec1, global_vec2);
int global_product = getParallelVectorDotProduct(
world,
count_size_vector,
global_vec1,
global_vec2);

if (world.rank() == 0) {
int reference_product = std::inner_product(
std::begin(global_vec2),
std::end(global_vec2),
std::begin(global_vec1), 0);;
int reference_product = getSequentialVectorDotProduct(
global_vec2,
global_vec1);
ASSERT_EQ(reference_product, global_product);
}
}
Expand Down
93 changes: 69 additions & 24 deletions tasks/task_1/khramov_i_vector_dot_product/vector_dot_product.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,86 @@ std::vector<int> getRandomVector(int sz) {
return vec;
}

int getLocalVectorDotProduct(const std::vector<int>& vec1,
const std::vector<int>& vec2) {
int result = 0;
for (int i = 0; i < vec1.size(); i++) {
result += vec1[i] * vec2[i];
}
return result;
std::vector<int> getLocalVectorSizes(int comm_size, int global_vec_size) {
std::vector<int> vectors_sizes(comm_size, global_vec_size / comm_size);
vectors_sizes[0] += global_vec_size % comm_size;
return vectors_sizes;
}

int getVectorDotProduct(boost::mpi::communicator world,
const std::vector<int>& global_vec1, const std::vector<int>& global_vec2) {
if (global_vec1.size() != global_vec2.size())
return -1;
else if (global_vec1.size() == 0 || global_vec2.size() == 0)
return 0;
int getSequentialVectorDotProduct(
const std::vector<int>& vec1,
const std::vector<int>& vec2) {
return std::inner_product(
std::begin(vec1),
std::end(vec1),
std::begin(vec2),
0);
}

int getParallelVectorDotProduct(
boost::mpi::communicator world,
int count_size_vector,
const std::vector<int>& global_vec1,
const std::vector<int>& global_vec2) {
int isValid = 1;
if (world.rank() == 0 &&
(global_vec1.size() != global_vec2.size()))
isValid = -1;
else if (world.rank() == 0 &&
(global_vec1.size() == 0 || global_vec2.size() == 0))
isValid = 0;

boost::mpi::broadcast(world, isValid, 0);

std::vector<int> local_vec1(global_vec1.size() / world.size());
std::vector<int> local_vec2(global_vec2.size() / world.size());
if (isValid == -1 || isValid == 0)
return isValid;

boost::mpi::scatter(
std::vector<int> local_vec1, local_vec2;

std::vector<int> vectors_sizes = getLocalVectorSizes(
world.size(),
count_size_vector);

local_vec1.resize(vectors_sizes[world.rank()]);
local_vec2.resize(vectors_sizes[world.rank()]);

if (world.rank() == 0) {
boost::mpi::scatterv(
world,
&global_vec1,
local_vec1,
global_vec1,
vectors_sizes,
local_vec1.data(),
0);

boost::mpi::scatter(
boost::mpi::scatterv(
world,
global_vec2,
vectors_sizes,
local_vec2.data(),
0);
} else {
boost::mpi::scatterv(
world,
local_vec1.data(),
vectors_sizes[world.rank()],
0);
boost::mpi::scatterv(
world,
&global_vec2,
local_vec2,
local_vec2.data(),
vectors_sizes[world.rank()],
0);
}

int part_result = getLocalVectorDotProduct(local_vec1, local_vec2);
int part_product = getSequentialVectorDotProduct(
local_vec1,
local_vec2);

int global_product;
boost::mpi::reduce(world, part_result, global_product, std::plus<int>(), 0);
boost::mpi::reduce(
world,
part_product,
global_product,
std::plus<int>(),
0);

return global_product;
}
16 changes: 11 additions & 5 deletions tasks/task_1/khramov_i_vector_dot_product/vector_dot_product.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
#include <string>

std::vector<int> getRandomVector(int sz);
int getLocalVectorDotProduct(const std::vector<int>& vec1,
const std::vector<int>& vec2);
int getVectorDotProduct(boost::mpi::communicator world,
const std::vector<int>& global_vec1,
const std::vector<int>& global_vec2);
std::vector<int> getLocalVectorSizes(
int comm_size,
int global_vec_size);
int getSequentialVectorDotProduct(
const std::vector<int>& vec1,
const std::vector<int>& vec2);
int getParallelVectorDotProduct(
boost::mpi::communicator world,
int count_size_vector,
const std::vector<int>& global_vec1,
const std::vector<int>& global_vec2);

#endif // TASKS_TASK_1_KHRAMOV_I_VECTOR_DOT_PRODUCT_VECTOR_DOT_PRODUCT_H_

0 comments on commit 7eb6d27

Please sign in to comment.