Skip to content

Commit

Permalink
Make stride&dim-array value-type non-const
Browse files Browse the repository at this point in the history
Changes `std::array<const size_type, dim>` to
`std::array<size_type, dim>` in order to have an easier user-interface
(and const-ness is preserved because all length_types and stride_types
are const themselves).

Additionally, add a dim_type in {scaled_,}reduced_row_major accessors.
  • Loading branch information
Thomas Grützmacher committed Mar 10, 2021
1 parent 1d5c08f commit 92af985
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 119 deletions.
4 changes: 2 additions & 2 deletions accessor/block_col_major.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class block_col_major {
using data_type = value_type *;

using const_accessor = block_col_major<const ValueType, Dimensionality>;
using stride_type = std::array<const size_type, dimensionality - 1>;
using length_type = std::array<const size_type, dimensionality>;
using stride_type = std::array<size_type, dimensionality - 1>;
using length_type = std::array<size_type, dimensionality>;

protected:
/**
Expand Down
27 changes: 12 additions & 15 deletions accessor/reduced_row_major.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class reduced_row_major {
friend class range<reduced_row_major>;

protected:
using dim_type = std::array<size_type, dimensionality>;
using storage_stride_type = std::array<size_type, dimensionality - 1>;
using reference_type =
reference_class::reduced_storage<arithmetic_type, storage_type>;
Expand All @@ -103,9 +104,9 @@ class reduced_row_major {
* @param storage pointer to the block of memory containing the storage
* @param stride stride array used for memory accesses
*/
constexpr GKO_ACC_ATTRIBUTES reduced_row_major(
std::array<size_type, dimensionality> size, storage_type *storage,
storage_stride_type stride)
constexpr GKO_ACC_ATTRIBUTES reduced_row_major(dim_type size,
storage_type *storage,
storage_stride_type stride)
: size_(size), storage_{storage}, stride_(stride)
{}

Expand All @@ -119,9 +120,9 @@ class reduced_row_major {
* @param strides strides used for memory accesses
*/
template <typename... Strides>
constexpr GKO_ACC_ATTRIBUTES reduced_row_major(
std::array<size_type, dimensionality> size, storage_type *storage,
Strides &&... strides)
constexpr GKO_ACC_ATTRIBUTES reduced_row_major(dim_type size,
storage_type *storage,
Strides &&... strides)
: reduced_row_major{
size, storage,
storage_stride_type{{std::forward<Strides>(strides)...}}}
Expand All @@ -137,8 +138,8 @@ class reduced_row_major {
* @param storage pointer to the block of memory containing the storage
* @param size multidimensional size of the memory
*/
constexpr GKO_ACC_ATTRIBUTES reduced_row_major(
std::array<size_type, dimensionality> size, storage_type *storage)
constexpr GKO_ACC_ATTRIBUTES reduced_row_major(dim_type size,
storage_type *storage)
: reduced_row_major{
size, storage,
helper::compute_default_row_major_stride_array<size_type>(size)}
Expand Down Expand Up @@ -229,7 +230,7 @@ class reduced_row_major {
{
return helper::validate_index_spans(size_, spans...),
range<reduced_row_major>{
std::array<size_type, dimensionality>{
dim_type{
(index_span{spans}.end - index_span{spans}.begin)...},
storage_ + compute_index((index_span{spans}.begin)...),
stride_};
Expand All @@ -240,11 +241,7 @@ class reduced_row_major {
*
* @returns the size of the accessor
*/
constexpr GKO_ACC_ATTRIBUTES std::array<size_type, dimensionality>
get_size() const
{
return size_;
}
constexpr GKO_ACC_ATTRIBUTES dim_type get_size() const { return size_; }

/**
* Returns a pointer to a stride array of size dimensionality - 1
Expand Down Expand Up @@ -286,7 +283,7 @@ class reduced_row_major {
}

private:
const std::array<size_type, dimensionality> size_;
const dim_type size_;
storage_type *storage_;
const storage_stride_type stride_;
};
Expand Down
11 changes: 5 additions & 6 deletions accessor/row_major.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class row_major {
*/

using const_accessor = row_major<const ValueType, Dimensionality>;
using stride_type = std::array<const size_type, dimensionality - 1>;
using length_type = std::array<const size_type, dimensionality>;
using stride_type = std::array<size_type, dimensionality - 1>;
using length_type = std::array<size_type, dimensionality>;

protected:
/**
Expand Down Expand Up @@ -112,10 +112,9 @@ class row_major {
*/
constexpr GKO_ACC_ATTRIBUTES explicit row_major(data_type data,
length_type size)
: row_major{
data, size,
helper::compute_default_row_major_stride_array<const size_type>(
size)}
: row_major{data, size,
helper::compute_default_row_major_stride_array<
typename stride_type::value_type>(size)}
{}

public:
Expand Down
30 changes: 14 additions & 16 deletions accessor/scaled_reduced_row_major.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class scaled_reduced_row_major
static constexpr size_type scalar_stride_dim{
scalar_dim == 0 ? 0 : (scalar_dim - 1)};

using dim_type = std::array<size_type, dimensionality>;
using storage_stride_type = std::array<size_type, dimensionality - 1>;
using scalar_stride_type = std::array<size_type, scalar_stride_dim>;
using reference_type =
Expand All @@ -211,7 +212,7 @@ class scaled_reduced_row_major
* @param scalar_stride stride array used for memory accesses to scalar
*/
constexpr GKO_ACC_ATTRIBUTES scaled_reduced_row_major(
std::array<size_type, dimensionality> size, storage_type *storage,
dim_type size, storage_type *storage,
storage_stride_type storage_stride, scalar_type *scalar,
scalar_stride_type scalar_stride)
: size_(size),
Expand All @@ -233,13 +234,13 @@ class scaled_reduced_row_major
* values.
*/
constexpr GKO_ACC_ATTRIBUTES scaled_reduced_row_major(
std::array<size_type, dimensionality> size, storage_type *storage,
storage_stride_type stride, scalar_type *scalar)
dim_type size, storage_type *storage, storage_stride_type stride,
scalar_type *scalar)
: scaled_reduced_row_major{
size, storage, stride, scalar,
helper::compute_default_masked_row_major_stride_array<
size_type, scalar_mask, scalar_stride_dim, dimensionality>(
size)}
typename scalar_stride_type::value_type, scalar_mask,
scalar_stride_dim, dimensionality>(size)}
{}

/**
Expand All @@ -251,12 +252,13 @@ class scaled_reduced_row_major
* @param scalar pointer to the block of memory containing the scalar
* values.
*/
constexpr GKO_ACC_ATTRIBUTES scaled_reduced_row_major(
std::array<size_type, dimensionality> size, storage_type *storage,
scalar_type *scalar)
constexpr GKO_ACC_ATTRIBUTES scaled_reduced_row_major(dim_type size,
storage_type *storage,
scalar_type *scalar)
: scaled_reduced_row_major{
size, storage,
helper::compute_default_row_major_stride_array<size_type>(size),
helper::compute_default_row_major_stride_array<
typename storage_stride_type::value_type>(size),
scalar}
{}

Expand Down Expand Up @@ -384,7 +386,7 @@ class scaled_reduced_row_major
{
return helper::validate_index_spans(size_, spans...),
range<scaled_reduced_row_major>{
std::array<size_type, dimensionality>{
dim_type{
(index_span{spans}.end - index_span{spans}.begin)...},
storage_ + compute_index((index_span{spans}.begin)...),
storage_stride_,
Expand All @@ -398,11 +400,7 @@ class scaled_reduced_row_major
*
* @returns the size of the accessor
*/
constexpr GKO_ACC_ATTRIBUTES std::array<size_type, dimensionality>
get_size() const
{
return size_;
}
constexpr GKO_ACC_ATTRIBUTES dim_type get_size() const { return size_; }

/**
* Returns a const reference to the storage stride array of size
Expand Down Expand Up @@ -504,7 +502,7 @@ class scaled_reduced_row_major


private:
const std::array<size_type, dimensionality> size_;
const dim_type size_;
storage_type *storage_;
const storage_stride_type storage_stride_;
scalar_type *scalar_;
Expand Down
38 changes: 4 additions & 34 deletions core/test/accessor/block_col_major.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@ class BlockColMajorAccessor3d : public ::testing::Test {
*/
};
// clang-format on
const std::array<const gko::acc::size_type, dimensionality> dim1{2, 3, 4};
const std::array<const gko::acc::size_type, dimensionality> dim2{2, 2, 3};
const std::array<gko::acc::size_type, dimensionality> dim1{2, 3, 4};
const std::array<gko::acc::size_type, dimensionality> dim2{2, 2, 3};
blk_col_major_range default_r{data, dim1};
blk_col_major_range custom_r{
data, dim2,
std::array<const gko::acc::size_type, dimensionality - 1>{12, 3}};
data, dim2, std::array<gko::acc::size_type, dimensionality - 1>{12, 3}};
};


TEST_F(BlockColMajorAccessor3d, ComputesCorrectStride)
{
auto range_stride = default_r.get_accessor().stride;
auto check_stride = std::array<const gko::acc::size_type, 2>{12, 3};
auto check_stride = std::array<gko::acc::size_type, 2>{12, 3};

ASSERT_EQ(range_stride, check_stride);
}
Expand Down Expand Up @@ -154,34 +153,5 @@ TEST_F(BlockColMajorAccessor3d, CanCreateColumnVector)
EXPECT_EQ(subr(1, 0, 0), 28);
}

/*
TEST_F(BlockColMajorAccessor3d, CanAssignValues)
{
default_r(1, 1, 1) = default_r(0, 0, 0);
EXPECT_EQ(data[16], 1);
}
TEST_F(BlockColMajorAccessor3d, CanAssignSubranges)
{
default_r(1u, span{0, 2}, span{0, 3}) =
custom_r(0u, span{0, 2}, span{0, 3});
EXPECT_EQ(data[12], 1);
EXPECT_EQ(data[15], 2);
EXPECT_EQ(data[18], -1);
EXPECT_EQ(data[21], 24);
EXPECT_EQ(data[13], 3);
EXPECT_EQ(data[16], 4);
EXPECT_EQ(data[19], -2);
EXPECT_EQ(data[22], 28);
EXPECT_EQ(data[14], 29);
EXPECT_EQ(data[17], 30);
EXPECT_EQ(data[20], 31);
EXPECT_EQ(data[23], 32);
}
*/


} // namespace
51 changes: 5 additions & 46 deletions core/test/accessor/row_major.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ namespace {
class RowMajorAccessor : public ::testing::Test {
protected:
using span = gko::acc::index_span;
using dim_type = std::array<const gko::acc::size_type, 2>;
using stride_type = std::array<const gko::acc::size_type, 1>;
using dim_type = std::array<gko::acc::size_type, 2>;
using stride_type = std::array<gko::acc::size_type, 1>;

using row_major_int_range = gko::acc::range<gko::acc::row_major<int, 2>>;

Expand Down Expand Up @@ -156,24 +156,6 @@ TEST_F(RowMajorAccessor, CanAssignValues)
}


/*
TEST_F(RowMajorAccessor, CanAssignSubranges)
{
r(0, span{0, 2}) = r(1, span{0, 2});
EXPECT_EQ(data[0], 3);
EXPECT_EQ(data[1], 4);
EXPECT_EQ(data[2], -1);
EXPECT_EQ(data[3], 3);
EXPECT_EQ(data[4], 4);
EXPECT_EQ(data[5], -2);
EXPECT_EQ(data[6], 5);
EXPECT_EQ(data[7], 6);
EXPECT_EQ(data[8], -3);
}
*/


class RowMajorAccessor3d : public ::testing::Test {
protected:
using span = gko::acc::index_span;
Expand All @@ -193,12 +175,11 @@ class RowMajorAccessor3d : public ::testing::Test {
29, 30, 31, 32
};
// clang-format on
const std::array<const gko::acc::size_type, dimensionality> dim1{2, 3, 4};
const std::array<const gko::acc::size_type, dimensionality> dim2{2, 2, 3};
const std::array<gko::acc::size_type, dimensionality> dim1{2, 3, 4};
const std::array<gko::acc::size_type, dimensionality> dim2{2, 2, 3};
row_major_int_range default_r{data, dim1};
row_major_int_range custom_r{
data, dim2,
std::array<const gko::size_type, dimensionality - 1>{12, 4}};
data, dim2, std::array<gko::size_type, dimensionality - 1>{12, 4}};
};


Expand Down Expand Up @@ -265,26 +246,4 @@ TEST_F(RowMajorAccessor3d, CanAssignValues)
}


/*
TEST_F(RowMajorAccessor3d, CanAssignSubranges)
{
default_r(1u, span{0, 2}, span{0, 3}) =
custom_r(0u, span{0, 2}, span{0, 3});
EXPECT_EQ(data[12], 1);
EXPECT_EQ(data[13], 2);
EXPECT_EQ(data[14], -1);
EXPECT_EQ(data[15], 24);
EXPECT_EQ(data[16], 3);
EXPECT_EQ(data[17], 4);
EXPECT_EQ(data[18], -2);
EXPECT_EQ(data[19], 28);
EXPECT_EQ(data[20], 29);
EXPECT_EQ(data[21], 30);
EXPECT_EQ(data[22], 31);
EXPECT_EQ(data[23], 32);
}
*/


} // namespace

0 comments on commit 92af985

Please sign in to comment.