Skip to content
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
14 changes: 13 additions & 1 deletion tasks/ermakov_a_spar_mat_mult/tests/functional/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdint>
#include <random>
#include <string>
#include <tuple>
Expand Down Expand Up @@ -101,6 +102,17 @@ void FillRandom(DenseMatrix &a, DenseMatrix &b, int n, double density, std::mt19
}
}

std::uint32_t MakeSeed(int n, const std::string &desc) {
std::uint32_t seed = 2166136261U;
for (unsigned char ch : desc) {
seed ^= ch;
seed *= 16777619U;
}
seed ^= static_cast<std::uint32_t>(n);
seed *= 16777619U;
return seed;
}

MatrixCRS DenseToCRS(const DenseMatrix &m, double eps = 1e-12) {
MatrixCRS r;

Expand Down Expand Up @@ -179,7 +191,7 @@ class ErmakovARunFuncTestSparMatMult : public ppc::util::BaseRunFuncTests<InType
}
FillFixed(a, b);
} else {
std::mt19937 gen(std::random_device{}());
std::mt19937 gen(MakeSeed(n, desc));
const double density = ResolveDensity(desc);
FillRandom(a, b, n, density, gen);
}
Expand Down
54 changes: 15 additions & 39 deletions tasks/ermakov_a_spar_mat_mult/tests/performance/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <gtest/gtest.h>

#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdint>
#include <random>
#include <vector>

Expand All @@ -17,48 +16,28 @@ namespace ermakov_a_spar_mat_mult {

namespace {

using DenseMatrix = std::vector<std::vector<std::complex<double>>>;
constexpr std::uint32_t kPerfSeedA = 0x13579BDFU;
constexpr std::uint32_t kPerfSeedB = 0x2468ACE0U;

DenseMatrix MakeRandomDense(int n, double density) {
DenseMatrix m(n, std::vector<std::complex<double>>(n, {0.0, 0.0}));

std::mt19937 gen(std::random_device{}());
MatrixCRS MakeRandomCRS(int n, double density, std::uint32_t seed) {
MatrixCRS matrix;
matrix.rows = n;
matrix.cols = n;
matrix.row_ptr.resize(static_cast<std::size_t>(n) + 1ULL, 0);
std::mt19937 gen(seed);
std::uniform_real_distribution<double> dis_val(-5.0, 5.0);
std::uniform_real_distribution<double> dis_prob(0.0, 1.0);

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (dis_prob(gen) < density) {
m[i][j] = {dis_val(gen), dis_val(gen)};
matrix.values.emplace_back(dis_val(gen), dis_val(gen));
matrix.col_index.push_back(j);
}
}
matrix.row_ptr[static_cast<std::size_t>(i) + 1ULL] = static_cast<int>(matrix.values.size());
}
return m;
}

MatrixCRS DenseToCRS(const DenseMatrix &m, double eps = 1e-12) {
MatrixCRS r;

const int rows = static_cast<int>(m.size());
const int cols = (rows != 0) ? static_cast<int>(m[0].size()) : 0;

r.rows = rows;
r.cols = cols;
r.row_ptr.resize(rows + 1);
r.row_ptr[0] = 0;

for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
const auto v = m[i][j];
if (std::abs(v.real()) > eps || std::abs(v.imag()) > eps) {
r.values.push_back(v);
r.col_index.push_back(j);
}
}
r.row_ptr[i + 1] = static_cast<int>(r.values.size());
}

return r;
return matrix;
}

} // namespace
Expand All @@ -71,11 +50,8 @@ class ErmakovARunPerfTestSparMatMult : public ppc::util::BaseRunPerfTests<InType
void SetUp() override {
const double density = 0.001;

auto a_dense = MakeRandomDense(k_count, density);
auto b_dense = MakeRandomDense(k_count, density);

input_data.A = DenseToCRS(a_dense);
input_data.B = DenseToCRS(b_dense);
input_data.A = MakeRandomCRS(k_count, density, kPerfSeedA);
input_data.B = MakeRandomCRS(k_count, density, kPerfSeedB);
}

bool CheckTestOutputData(OutType &output_data) final {
Expand Down
Loading