Skip to content

Commit 2ef7c49

Browse files
committed
add dense device_view type
1 parent 591cd13 commit 2ef7c49

File tree

6 files changed

+123
-8
lines changed

6 files changed

+123
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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::device_view::dense<TypeParam> view{gko::dim<2>{1, 2}, 3,
22+
values.data()};
23+
24+
ASSERT_EQ(view.size, gko::dim<2>(1, 2));
25+
ASSERT_EQ(view.stride, 3);
26+
ASSERT_EQ(view.data, values.data());
27+
ASSERT_EQ(&view(0, 0), &values[0]);
28+
ASSERT_EQ(&view(1, 0), &values[3]);
29+
ASSERT_EQ(&view(1, 1), &values[4]);
30+
}

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::device_view::dense<value_type>;
190+
using const_device_view = matrix::device_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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 device_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+
ValueType& operator()(size_type row, size_type col) const
30+
{
31+
return data[row * stride + col];
32+
}
33+
};
34+
35+
36+
} // namespace device_view
37+
} // namespace matrix
38+
} // namespace gko
39+
40+
41+
#endif // GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_

0 commit comments

Comments
 (0)