Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dim constructor explicit #1474

Merged
merged 3 commits into from
Nov 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion benchmark/matrix_generator/matrix_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ gko::matrix_data<etype, itype> generate_block_diagonal(
auto block = gko::matrix_data<etype, itype>(
gko::dim<2>(block_size),
std::uniform_real_distribution<rc_etype>(-1.0, 1.0), engine);
return gko::matrix_data<etype, itype>::diag(num_blocks, block);
return gko::matrix_data<etype, itype>::diag(gko::dim<2>{num_blocks}, block);
}


Expand Down
7 changes: 4 additions & 3 deletions benchmark/preconditioner/preconditioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ struct PreconditionerBenchmark : Benchmark<preconditioner_benchmark_state> {

state.system_matrix =
formats::matrix_factory(FLAGS_formats, exec, data);
state.b = Generator::create_multi_vector_random(exec, data.size[0]);
state.x = Generator::create_multi_vector(exec, data.size[0],
gko::zero<etype>());
state.b = Generator::create_multi_vector_random(
exec, gko::dim<2>{data.size[0]});
state.x = Generator::create_multi_vector(
exec, gko::dim<2>{data.size[0]}, gko::zero<etype>());

std::clog << "Matrix is of size (" << data.size[0] << ", "
<< data.size[1] << "), " << data.nonzeros.size() << std::endl;
Expand Down
5 changes: 3 additions & 2 deletions core/matrix/permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,15 @@ Permutation<IndexType>::create_const(
template <typename IndexType>
Permutation<IndexType>::Permutation(std::shared_ptr<const Executor> exec,
size_type size)
: EnableLinOp<Permutation>(exec, size), permutation_{exec, size}
: EnableLinOp<Permutation>(exec, dim<2>{size}), permutation_{exec, size}
{}


template <typename IndexType>
Permutation<IndexType>::Permutation(std::shared_ptr<const Executor> exec,
array<index_type> permutation_indices)
: EnableLinOp<Permutation>(exec, permutation_indices.get_num_elems()),
: EnableLinOp<Permutation>(exec,
dim<2>{permutation_indices.get_num_elems()}),
permutation_{exec, std::move(permutation_indices)}
{}

Expand Down
48 changes: 26 additions & 22 deletions include/ginkgo/core/base/dim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ struct dim {

using dimension_type = DimensionType;

/**
* Creates a dimension object with all dimensions set to zero.
*/
constexpr GKO_ATTRIBUTES dim() : dim{dimension_type{}} {}

/**
* Creates a dimension object with all dimensions set to the same value.
*
* @param size the size of each dimension
*/
constexpr GKO_ATTRIBUTES dim(const dimension_type& size = dimension_type{})
explicit constexpr GKO_ATTRIBUTES dim(const dimension_type& size)
: first_{size}, rest_{size}
{}

Expand All @@ -52,7 +57,8 @@ struct dim {
* @param first first dimension
* @param rest other dimensions
*/
template <typename... Rest>
template <typename... Rest, std::enable_if_t<sizeof...(Rest) ==
Dimensionality - 1>* = nullptr>
constexpr GKO_ATTRIBUTES dim(const dimension_type& first,
const Rest&... rest)
: first_{first}, rest_{static_cast<dimension_type>(rest)...}
Expand Down Expand Up @@ -115,6 +121,19 @@ struct dim {
return x.first_ == y.first_ && x.rest_ == y.rest_;
}

/**
* Checks if two dim objects are not equal.
*
* @param x first object
* @param y second object
*
* @return false if and only if all dimensions of both objects are equal.
*/
friend constexpr GKO_ATTRIBUTES bool operator!=(const dim& x, const dim& y)
{
return !(x == y);
}

/**
* Multiplies two dim objects.
*
Expand Down Expand Up @@ -195,6 +214,11 @@ struct dim<1u, DimensionType> {
return x.first_ == y.first_;
}

friend constexpr GKO_ATTRIBUTES bool operator!=(const dim& x, const dim& y)
{
return !(x == y);
}

friend constexpr GKO_ATTRIBUTES dim operator*(const dim& x, const dim& y)
{
return dim(x.first_ * y.first_);
Expand All @@ -215,26 +239,6 @@ struct dim<1u, DimensionType> {
};


/**
* Checks if two dim objects are different.
*
* @tparam Dimensionality number of dimensions of the dim objects
* @tparam DimensionType datatype used to represent each dimension
*
* @param x first object
* @param y second object
*
* @return `!(x == y)`
*/
template <size_type Dimensionality, typename DimensionType>
constexpr GKO_ATTRIBUTES GKO_INLINE bool operator!=(
const dim<Dimensionality, DimensionType>& x,
const dim<Dimensionality, DimensionType>& y)
{
return !(x == y);
}


/**
* Returns a dim<2> object with its dimensions swapped.
*
Expand Down
2 changes: 1 addition & 1 deletion test/matrix/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ TYPED_TEST(Matrix, DeviceReadMoveIsEquivalentToHostRef)
dev_result->read(std::move(ref_device_data));

ASSERT_EQ(ref_device_data.get_size(), gko::dim<2>{});
ASSERT_EQ(ref_device_data.get_num_elems(), gko::dim<2>{});
ASSERT_EQ(ref_device_data.get_num_elems(), 0);
GKO_ASSERT_MTX_NEAR(ref_result, dev_result, 0.0);
GKO_ASSERT_MTX_EQ_SPARSITY(ref_result, dev_result);
});
Expand Down
Loading