Skip to content

Commit

Permalink
review updates:
Browse files Browse the repository at this point in the history
- add documentation on empty dim
- fix documentation of return type
- rename `is_in_box` -> `is_in_range`
- rename manufactured rhs

Co-authored-by: Fritz Goebel <[email protected]>
Co-authored-by: Thomas Grützmacher <[email protected]>
Co-authored-by: Tobias Ribizel <[email protected]>
  • Loading branch information
4 people committed Feb 17, 2025
1 parent 5ea378d commit 13a489a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 56 deletions.
5 changes: 2 additions & 3 deletions benchmark/solver/distributed/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ struct Generator : public DistributedDefaultSystemGenerator<SolverGenerator> {
gko::dim<2> local_vec_size{
gko::detail::get_local(system_matrix)->get_size()[1],
FLAGS_nrhs};
return create_manufactured_rhs(
return create_normalized_manufactured_rhs(
exec, system_matrix,
Vec::create(exec, comm, vec_size,
create_matrix_sin<etype>(exec, local_vec_size))
.get(),
true);
.get());
}
return Vec::create(
exec, comm, gko::dim<2>{system_matrix->get_size()[0], FLAGS_nrhs},
Expand Down
4 changes: 2 additions & 2 deletions benchmark/solver/solver_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ struct SolverGenerator : DefaultSystemGenerator<> {
return create_multi_vector_random(exec, vec_size,
local_vec_size);
} else if (FLAGS_rhs_generation == "sinus") {
return create_manufactured_rhs(
return create_normalized_manufactured_rhs(
exec, system_matrix,
create_matrix_sin<etype>(exec, vec_size).get(), true);
create_matrix_sin<etype>(exec, vec_size).get());
}
throw std::invalid_argument(std::string("\"rhs_generation\" = ") +
FLAGS_rhs_generation +
Expand Down
35 changes: 16 additions & 19 deletions benchmark/utils/general.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,31 +529,28 @@ gko::remove_complex<ValueType> compute_max_relative_norm2(


template <typename VectorType>
std::unique_ptr<VectorType> create_manufactured_rhs(
std::unique_ptr<VectorType> create_normalized_manufactured_rhs(
std::shared_ptr<const gko::Executor> exec, const gko::LinOp* system_matrix,
const VectorType* solution, bool normalize)
const VectorType* solution)
{
auto rhs = VectorType::create_with_config_of(solution);

if (!normalize) {
system_matrix->apply(solution, rhs);
auto vec_size = solution->get_size();
auto scaled_solution = gko::clone(solution);
auto scalar = gko::matrix::Dense<rc_etype>::create(
exec->get_master(), gko::dim<2>{1, vec_size[1]});
solution->compute_norm2(scalar);
for (gko::size_type i = 0; i < vec_size[1]; ++i) {
scalar->at(0, i) = gko::one<rc_etype>() / scalar->at(0, i);
}
// normalize solution
if (gko::is_complex_s<etype>::value) {
scaled_solution->scale(scalar->make_complex());
} else {
auto vec_size = solution->get_size();
auto scaled_solution = gko::clone(solution);
auto scalar = gko::matrix::Dense<rc_etype>::create(
exec->get_master(), gko::dim<2>{1, vec_size[1]});
solution->compute_norm2(scalar);
for (gko::size_type i = 0; i < vec_size[1]; ++i) {
scalar->at(0, i) = gko::one<rc_etype>() / scalar->at(0, i);
}
// normalize sin-vector
if (gko::is_complex_s<etype>::value) {
scaled_solution->scale(scalar->make_complex());
} else {
scaled_solution->scale(scalar);
}
system_matrix->apply(scaled_solution, rhs);
scaled_solution->scale(scalar);
}
system_matrix->apply(scaled_solution, rhs);

return rhs;
}

Expand Down
6 changes: 4 additions & 2 deletions benchmark/utils/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct DefaultSystemGenerator {
auto [data, size] = [&] {
if (config.contains("filename")) {
std::ifstream in(config["filename"].get<std::string>());
// Returning an empty dim means that there is no specified local
// size, which is relevant in the distributed case
return std::make_pair(
gko::read_generic_raw<ValueType, IndexType>(in),
gko::dim<2>());
Expand Down Expand Up @@ -179,6 +181,8 @@ struct DistributedDefaultSystemGenerator {
auto [data, local_size] = [&] {
if (config.contains("filename")) {
std::ifstream in(config["filename"].get<std::string>());
// Returning an empty dim means that no local size is specified,
// and thus the partition has to be deduced from the global size
return std::make_pair(
gko::read_generic_raw<value_type, index_type>(in),
gko::dim<2>());
Expand Down Expand Up @@ -262,8 +266,6 @@ struct DistributedDefaultSystemGenerator {

auto dist_mat = dist_mtx<etype, itype, global_itype>::create(
exec, comm, local_mat, non_local_mat);
gko::matrix_data<value_type, index_type> global_data(
{part->get_size(), part->get_size()});
dist_mat->read_distributed(data, part);

if (spmv_case) {
Expand Down
53 changes: 23 additions & 30 deletions benchmark/utils/stencil_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ double closest_nth_root(T v, int n)
* @return True if i in [0, bound).
*/
template <typename IndexType>
bool is_in_box(const IndexType i, const IndexType bound)
bool is_in_range(const IndexType i, const IndexType bound)
{
return 0 <= i && i < bound;
}
Expand All @@ -57,10 +57,11 @@ bool is_in_box(const IndexType i, const IndexType bound)
* dimension.
* @param target_local_size The desired size of the subdomains. The actual size
* can deviate from this to accommodate the square
* size of the subdomains.
* size of the global domain.
* @param restricted If true, a 5-pt stencil is used, else a 9-pt stencil.
*
* @return matrix data of a subdomain using either 5-pt or 9-pt stencil.
* @return pair of (matrix data, local size) of a subdomain using either 5-pt
* or 9-pt stencil.
*/
template <typename ValueType, typename IndexType>
std::pair<gko::matrix_data<ValueType, IndexType>, gko::dim<2>>
Expand Down Expand Up @@ -101,8 +102,8 @@ generate_2d_stencil_subdomain(std::array<int, 2> dims,
/**
* The offset of a subdomain in a single dimension. Since the first R
* processes have a subdomain size of discretization_points_min[dim]+1, the
* offset adds min(subdomain-id, R) to
* discretization_points_min[dim]*subdomain-id
* offset adds min(subdomain_id, R) to
* discretization_points_min[dim]*subdomain_id
*/
auto subdomain_offset_1d = [&](const IndexType dim, const IndexType i) {
assert(0 <= i && i < dims[dim]);
Expand Down Expand Up @@ -142,7 +143,7 @@ generate_2d_stencil_subdomain(std::array<int, 2> dims,
*/
auto target_position = [&](const IndexType dim, const IndexType i,
const int position) {
return is_in_box(i, discretization_points[dim])
return is_in_range(i, discretization_points[dim])
? position
: (i < 0 ? position - 1 : position + 1);
};
Expand All @@ -156,7 +157,7 @@ generate_2d_stencil_subdomain(std::array<int, 2> dims,
*/
auto target_local_idx = [&](const IndexType dim, const IndexType pos,
const IndexType i) {
return is_in_box(i, subdomain_size_1d(dim, pos))
return is_in_range(i, subdomain_size_1d(dim, pos))
? i
: (i < 0 ? i + subdomain_size_1d(dim, pos)
: i - subdomain_size_1d(dim, positions[dim]));
Expand All @@ -171,7 +172,7 @@ generate_2d_stencil_subdomain(std::array<int, 2> dims,
auto flat_idx = [&](const IndexType iy, const IndexType ix) {
auto tpx = target_position(0, ix, positions[0]);
auto tpy = target_position(1, iy, positions[1]);
if (is_in_box(tpx, dims[0]) && is_in_box(tpy, dims[1])) {
if (is_in_range(tpx, dims[0]) && is_in_range(tpy, dims[1])) {
return subdomain_offset(tpy, tpx) + target_local_idx(0, tpx, ix) +
target_local_idx(1, tpy, iy) * subdomain_size_1d(0, tpx);
} else {
Expand Down Expand Up @@ -213,8 +214,8 @@ generate_2d_stencil_subdomain(std::array<int, 2> dims,
for (IndexType dx : {-1, 0, 1}) {
if (is_valid_neighbor(dy, dx)) {
auto col = flat_idx(iy + dy, ix + dx);
if (is_in_box(col,
static_cast<IndexType>(global_size))) {
if (is_in_range(col,
static_cast<IndexType>(global_size))) {
if (col != row) {
A_data.nonzeros.emplace_back(
row, col, -gko::one<ValueType>());
Expand All @@ -237,15 +238,6 @@ generate_2d_stencil_subdomain(std::array<int, 2> dims,
* Generates matrix data for a 3D stencil matrix. If restricted is set to true,
* creates a 7-pt stencil, if it is false creates a 27-pt stencil.
*
*
* If `dim != [1 1]` then the matrix data is a subset of a larger matrix.
* The total matrix is a discretization of `[0, 1]^2`, and each subdomain has
* (roughly) the shape `[global_size_1d / dims[0]; global_size_1d / dims[1]]`.
* The position of the subdomain defines the subset of the matrix.
* The degrees of freedom are ordered subdomain-wise and the subdomains
* themselves are ordered lexicographical. This means that the indices are with
* respect to the larger matrix, i.e. they might not start with 0.
*
* If `dim != [1 1 1]` then the matrix data is a subset of a larger matrix.
* The total matrix is a discretization of `[0, 1]^3`, and each subdomain has
* (roughly) the shape
Expand All @@ -261,10 +253,11 @@ generate_2d_stencil_subdomain(std::array<int, 2> dims,
* dimension.
* @param target_local_size The desired size of the subdomains. The actual size
* can deviate from this to accommodate the uniform
* size of the subdomains.
* size of the global domain.
* @param restricted If true, a 7-pt stencil is used, else a 27-pt stencil.
*
* @return matrix data of a subdomain using either 7-pt or 27-pt stencil.
* @return pair of (matrix data, local size) of a subdomain using either 7-pt
* or 27-pt stencil.
*/
template <typename ValueType, typename IndexType>
std::pair<gko::matrix_data<ValueType, IndexType>, gko::dim<2>>
Expand Down Expand Up @@ -307,8 +300,8 @@ generate_3d_stencil_subdomain(std::array<int, 3> dims,
/**
* The offset of a subdomain in a single dimension. Since the first R
* processes have a subdomain size of discretization_points_min[dim]+1, the
* offset adds min(subdomain-id, R) to
* discretization_points_min[dim]*subdomain-id
* offset adds min(subdomain_id, R) to
* discretization_points_min[dim]*subdomain_id
*/
auto subdomain_offset_1d = [&](const IndexType dim, const IndexType i) {
assert(0 <= i && i < dims[dim]);
Expand Down Expand Up @@ -356,7 +349,7 @@ generate_3d_stencil_subdomain(std::array<int, 3> dims,
*/
auto target_position = [&](const IndexType dim, const IndexType i,
const int position) {
return is_in_box(i, discretization_points[dim])
return is_in_range(i, discretization_points[dim])
? position
: (i < 0 ? position - 1 : position + 1);
};
Expand All @@ -370,7 +363,7 @@ generate_3d_stencil_subdomain(std::array<int, 3> dims,
*/
auto target_local_idx = [&](const IndexType dim, const IndexType pos,
const IndexType i) {
return is_in_box(i, subdomain_size_1d(dim, pos))
return is_in_range(i, subdomain_size_1d(dim, pos))
? i
: (i < 0 ? i + subdomain_size_1d(dim, pos)
: i - subdomain_size_1d(dim, positions[dim]));
Expand All @@ -387,8 +380,8 @@ generate_3d_stencil_subdomain(std::array<int, 3> dims,
auto tpx = target_position(0, ix, positions[0]);
auto tpy = target_position(1, iy, positions[1]);
auto tpz = target_position(2, iz, positions[2]);
if (is_in_box(tpx, dims[0]) && is_in_box(tpy, dims[1]) &&
is_in_box(tpz, dims[2])) {
if (is_in_range(tpx, dims[0]) && is_in_range(tpy, dims[1]) &&
is_in_range(tpz, dims[2])) {
return subdomain_offset(tpz, tpy, tpx) +
target_local_idx(0, tpx, ix) +
target_local_idx(1, tpy, iy) * subdomain_size_1d(0, tpx) +
Expand Down Expand Up @@ -438,8 +431,8 @@ generate_3d_stencil_subdomain(std::array<int, 3> dims,
for (IndexType dx : {-1, 0, 1}) {
if (is_valid_neighbor(dz, dy, dx)) {
auto col = flat_idx(iz + dz, iy + dy, ix + dx);
if (is_in_box(col, static_cast<IndexType>(
global_size))) {
if (is_in_range(col, static_cast<IndexType>(
global_size))) {
if (col != row) {
A_data.nonzeros.emplace_back(
row, col, -gko::one<ValueType>());
Expand Down Expand Up @@ -469,7 +462,7 @@ generate_3d_stencil_subdomain(std::array<int, 3> dims,
* @param target_local_size The desired size of the matrix. The actual size can
* deviate from this to accommodate the uniform size
* of the discretization.
* @return matrix data using the requested stencil.
* @return pair of (matrix data, local size) using the requested stencil.
*/
template <typename ValueType, typename IndexType>
std::pair<gko::matrix_data<ValueType, IndexType>, gko::dim<2>> generate_stencil(
Expand Down

0 comments on commit 13a489a

Please sign in to comment.