Skip to content

Commit 69bfa02

Browse files
authored
Merge dense device_view type
This is a simpler representation of Dense that provides a simple ABI-stable representation to be passed between libraries.
2 parents 827139d + ee44d4b commit 69bfa02

File tree

8 files changed

+193
-8
lines changed

8 files changed

+193
-8
lines changed

core/matrix/dense.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -1956,6 +1956,22 @@ void Dense<ValueType>::get_imag(ptr_param<real_type> result) const
19561956
}
19571957

19581958

1959+
template <typename ValueType>
1960+
auto Dense<ValueType>::get_device_view() -> device_view
1961+
{
1962+
return device_view{this->get_size(), this->get_stride(),
1963+
this->get_values()};
1964+
};
1965+
1966+
1967+
template <typename ValueType>
1968+
auto Dense<ValueType>::get_const_device_view() const -> const_device_view
1969+
{
1970+
return const_device_view{this->get_size(), this->get_stride(),
1971+
this->get_const_values()};
1972+
};
1973+
1974+
19591975
template <typename ValueType>
19601976
void Dense<ValueType>::add_scaled_identity_impl(const LinOp* a, const LinOp* b)
19611977
{

core/test/matrix/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ginkgo_create_test(coo_builder)
77
ginkgo_create_test(csr)
88
ginkgo_create_test(csr_builder)
99
ginkgo_create_test(dense)
10+
ginkgo_create_test(device_views)
1011
ginkgo_create_test(diagonal)
1112
ginkgo_create_test(ell)
1213
ginkgo_create_test(fbcsr)

core/test/matrix/dense.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -306,6 +306,26 @@ TYPED_TEST(Dense, CanBeReadFromMatrixAssemblyData)
306306
}
307307

308308

309+
TYPED_TEST(Dense, CanCreateDeviceView)
310+
{
311+
auto view = this->mtx->get_device_view();
312+
313+
EXPECT_EQ(view.size, this->mtx->get_size());
314+
EXPECT_EQ(view.stride, this->mtx->get_stride());
315+
EXPECT_EQ(view.data, this->mtx->get_values());
316+
}
317+
318+
319+
TYPED_TEST(Dense, CanCreateConstDeviceView)
320+
{
321+
auto view = this->mtx->get_const_device_view();
322+
323+
EXPECT_EQ(view.size, this->mtx->get_size());
324+
EXPECT_EQ(view.stride, this->mtx->get_stride());
325+
EXPECT_EQ(view.data, this->mtx->get_values());
326+
}
327+
328+
309329
TYPED_TEST(Dense, CanCreateSubmatrix)
310330
{
311331
using value_type = typename TestFixture::value_type;

core/test/matrix/device_views.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-FileCopyrightText: 2026 The Ginkgo authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
#include <gtest/gtest.h>
6+
7+
#include <ginkgo/core/matrix/device_views.hpp>
8+
9+
#include "core/test/utils.hpp"
10+
11+
12+
template <typename T>
13+
class DenseView : public ::testing::Test {};
14+
15+
TYPED_TEST_SUITE(DenseView, gko::test::ValueTypes, TypenameNameGenerator);
16+
17+
18+
TYPED_TEST(DenseView, AccessWorks)
19+
{
20+
std::vector<TypeParam> values(10);
21+
gko::matrix::view::dense<TypeParam> view{gko::dim<2>{1, 2}, 3,
22+
values.data()};
23+
auto const_view = view.as_const();
24+
25+
ASSERT_EQ(view.size, gko::dim<2>(1, 2));
26+
ASSERT_EQ(view.stride, 3);
27+
ASSERT_EQ(view.data, values.data());
28+
ASSERT_EQ(&view(0, 0), &values[0]);
29+
ASSERT_EQ(&view(1, 0), &values[3]);
30+
ASSERT_EQ(&view(1, 1), &values[4]);
31+
ASSERT_EQ(const_view.size, view.size);
32+
ASSERT_EQ(const_view.stride, view.stride);
33+
ASSERT_EQ(const_view.data, view.data);
34+
}

include/ginkgo/core/matrix/dense.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -16,6 +16,7 @@
1616
#include <ginkgo/core/base/range_accessors.hpp>
1717
#include <ginkgo/core/base/types.hpp>
1818
#include <ginkgo/core/base/utils.hpp>
19+
#include <ginkgo/core/matrix/device_views.hpp>
1920
#include <ginkgo/core/matrix/permutation.hpp>
2021
#include <ginkgo/core/matrix/scaled_permutation.hpp>
2122

@@ -177,14 +178,16 @@ class Dense
177178

178179
using value_type = ValueType;
179180
using index_type = int64;
180-
using transposed_type = Dense<ValueType>;
181-
using mat_data = matrix_data<ValueType, int64>;
182-
using mat_data32 = matrix_data<ValueType, int32>;
183-
using device_mat_data = device_matrix_data<ValueType, int64>;
184-
using device_mat_data32 = device_matrix_data<ValueType, int32>;
181+
using transposed_type = Dense<value_type>;
182+
using mat_data = matrix_data<value_type, int64>;
183+
using mat_data32 = matrix_data<value_type, int32>;
184+
using device_mat_data = device_matrix_data<value_type, int64>;
185+
using device_mat_data32 = device_matrix_data<value_type, int32>;
185186
using absolute_type = remove_complex<Dense>;
186187
using real_type = absolute_type;
187188
using complex_type = to_complex<Dense>;
189+
using device_view = matrix::view::dense<value_type>;
190+
using const_device_view = matrix::view::dense<const value_type>;
188191

189192
using row_major_range = gko::range<gko::accessor::row_major<ValueType, 2>>;
190193

@@ -889,6 +892,10 @@ class Dense
889892
return values_.get_size();
890893
}
891894

895+
device_view get_device_view();
896+
897+
const_device_view get_const_device_view() const;
898+
892899
/**
893900
* Returns a single element of the matrix.
894901
*
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: 2026 The Ginkgo authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
#ifndef GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
6+
#define GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
7+
8+
#include <ginkgo/core/base/dim.hpp>
9+
10+
11+
namespace gko {
12+
namespace matrix {
13+
namespace view {
14+
15+
16+
/**
17+
* Non-owning view of a matrix::Dense to be used inside device kernels.
18+
* This type is used to provide a simple and stable ABI for passing data between
19+
* libraries.
20+
*
21+
* @tparam ValueType the value type used to store matrix entries.
22+
*/
23+
template <typename ValueType>
24+
struct dense {
25+
dim<2> size;
26+
size_type stride;
27+
ValueType* data;
28+
29+
/** Constructs a dense view from size, stride and data. */
30+
constexpr dense(dim<2> size, size_type stride, ValueType* data)
31+
: size{size}, stride{stride}, data{data}
32+
{}
33+
34+
/** Returns a const view of the same data */
35+
constexpr dense<const ValueType> as_const() const
36+
{
37+
return dense<const ValueType>{size, stride, data};
38+
}
39+
40+
/** Subscript operator accessing the given row and column */
41+
constexpr ValueType& operator()(size_type row, size_type col) const
42+
{
43+
return data[row * stride + col];
44+
}
45+
};
46+
47+
48+
} // namespace view
49+
} // namespace matrix
50+
} // namespace gko
51+
52+
53+
#endif // GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_

test/matrix/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ginkgo_create_common_device_test(csr_kernels)
55
ginkgo_create_common_test(csr_kernels2)
66
ginkgo_create_common_test(coo_kernels)
77
ginkgo_create_common_test(dense_kernels)
8+
ginkgo_create_common_device_test(device_views)
89
ginkgo_create_common_test(diagonal_kernels)
910
ginkgo_create_common_test(ell_kernels)
1011
ginkgo_create_common_test(fbcsr_kernels DISABLE_EXECUTORS dpcpp)

test/matrix/device_views.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
/*@GKO_PREPROCESSOR_FILENAME_HELPER@*/
6+
7+
#include <type_traits>
8+
9+
#include <gtest/gtest.h>
10+
11+
#include "common/unified/base/kernel_launch.hpp"
12+
#include "core/base/array_access.hpp"
13+
#include "core/test/utils.hpp"
14+
#include "test/utils/common_fixture.hpp"
15+
16+
17+
template <typename ValueType>
18+
class DenseView : public CommonTestFixture {
19+
public:
20+
using value_type = ValueType;
21+
using view_type = gko::matrix::view::dense<value_type>;
22+
};
23+
24+
TYPED_TEST_SUITE(DenseView, gko::test::ValueTypes, TypenameNameGenerator);
25+
26+
27+
template <typename ValueType>
28+
void assert_dense_view(std::shared_ptr<const gko::EXEC_TYPE> exec)
29+
{
30+
gko::array<bool> correct{exec, {false}};
31+
gko::array<ValueType> values{exec, 5};
32+
values.fill(gko::one<ValueType>());
33+
gko::kernels::GKO_DEVICE_NAMESPACE::run_kernel(
34+
exec,
35+
[] GKO_KERNEL(auto i, auto values, auto correct) {
36+
using device_type = std::decay_t<decltype(values[0])>;
37+
gko::matrix::view::dense<device_type> view{gko::dim<2>{1, 2}, 3,
38+
values};
39+
if (view.size == gko::dim<2>(1, 2) && view.stride == 3 &&
40+
view.data == values && &view(0, 0) == &values[0] &&
41+
&view(1, 0) == &values[3] && &view(1, 1) == &values[4] &&
42+
view(1, 1) == gko::one(view(1, 1))) {
43+
*correct = true;
44+
}
45+
},
46+
1, values, correct);
47+
ASSERT_TRUE(get_element(correct, 0));
48+
}
49+
50+
TYPED_TEST(DenseView, WorksOnDevice)
51+
{
52+
assert_dense_view<TypeParam>(this->exec);
53+
}

0 commit comments

Comments
 (0)