Skip to content

Add index type template parameter to memory accessor - #2050

Open
taaae wants to merge 2 commits into
developfrom
memory_accessor_add_index_type_as_template_parameter
Open

Add index type template parameter to memory accessor#2050
taaae wants to merge 2 commits into
developfrom
memory_accessor_add_index_type_as_template_parameter

Conversation

@taaae

@taaae taaae commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Things to note/discuss:

  • I use IndexType for the dense-matrix accessors as well:

    using b_accessor = acc::reduced_row_major<2, arithmetic_type,
    const InputValueType, IndexType>;

    const auto b_vals = acc::range<b_accessor>(
    typename b_accessor::dim_type{{static_cast<IndexType>(b.size[0]),
    static_cast<IndexType>(b.size[1])}},
    b.values,
    typename b_accessor::storage_stride_type{
    {static_cast<IndexType>(b.stride)}});

    It is useful in this line:

    temp += val(ind) * b(col_idx, column_id);

    But I guess it can be weird from the user's perspective. Like, they define an ELL matrix with IndexType, but suddenly dense matrices passed in the kernel also are getting accessed with this IndexType.

  • I also added bunch of asserts, like this one:

    GKO_ASSERT(
    fits_index_type<IndexType>(num_stored_elements_per_row * stride));
    GKO_ASSERT(fits_index_type<IndexType>(b.size[0] * b.stride));

    To prevent such overflows:

    const auto a_vals = gko::acc::range<a_accessor>(
    typename a_accessor::dim_type{
    {static_cast<IndexType>(num_stored_elements_per_row * stride)}},
    a.values);

  • Also, the default matrix IndexType is int32:

    template <typename ValueType = default_precision, typename IndexType = int32>
    class Ell : public EnableLinOp<Ell<ValueType, IndexType>>,

    while the accessor's internal index type was hardcoded to int64 before this change:

    using index_type = std::int64_t;

    So after this change, all users of default-constructed matrices silently switch from int64 to
    int32 indexing in these kernels. Do we have cases where people use matrices/operands big enough that int32 would overflow?

  • Small note: I use IndexType for strides and lengths as well:

    using dim_type = std::array<index_type, dimensionality>;
    using storage_stride_type = std::array<index_type, dimensionality - 1>;

    constexpr GKO_ACC_ATTRIBUTES index_type length(size_type dimension) const
    {
    return dimension < dimensionality ? size_[dimension] : 1;
    }

    It is useful because it turns the 'stored stride type' -> 'kernel index type' cast into a no-op:

    const dim_type size_;
    storage_type* const storage_;
    const storage_stride_type stride_;

    operator()(Indices&&... indices) const
    {
    return reference_type{storage_ +
    compute_index(std::forward<Indices>(indices)...)};
    }

    return GKO_ACC_ASSERT(first < static_cast<IndexType>(size[dim_idx])),
    first * static_cast<IndexType>(stride[dim_idx]) +
    row_major_helper_s<IndexType, total_dim, current_iter + 1>::
    compute(size, stride, std::forward<Indices>(idxs)...);

    If the members stayed size_type, that static_cast<IndexType>(stride[dim_idx]) would be a real
    64 -> 32 bit conversion on every single access.

    No similar improvements for dim_type and length(), but I think IndexType fits better there.

@taaae
taaae requested a review from yhmtsai July 16, 2026 14:25
@taaae taaae self-assigned this Jul 16, 2026
@ginkgo-bot ginkgo-bot added reg:testing This is related to testing. type:matrix-format This is related to the Matrix formats mod:all This touches all Ginkgo modules. labels Jul 16, 2026
@taaae
taaae force-pushed the memory_accessor_add_index_type_as_template_parameter branch from 741c786 to 1e076e8 Compare July 16, 2026 15:03
@taaae
taaae force-pushed the memory_accessor_add_index_type_as_template_parameter branch from 1e076e8 to 6601dc0 Compare July 16, 2026 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:all This touches all Ginkgo modules. reg:testing This is related to testing. type:matrix-format This is related to the Matrix formats

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants