Skip to content

Commit 8bf5778

Browse files
committed
Refactoring - offset
1 parent 05a8420 commit 8bf5778

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

src/includes/LayoutProvider.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ struct AbstractLayoutProvider {
3131
}
3232

3333
template <typename Pos, typename Layout>
34-
static constexpr auto offset(Pos &&pos, Layout &&layout) {
34+
static constexpr auto offset(Layout &&layout, Pos &&pos) {
3535
static_assert(is_dims_type<std::remove_reference_t<Pos> >);
3636
static_assert(is_layout_type<std::remove_reference_t<Layout> >);
3737
auto dimensions = view_to_dimensions(std::forward<Pos>(pos));
3838
auto strides = layout.strides();
3939
// Takes RowMajorLayout for example:
4040
// view: [rows: 2, cols: 4]
41-
// layout.shape [dim0: 4, dim0: 2]
41+
// layout.dimens [dim0: 4, dim0: 2]
4242
// layout.strides[dim0: 1, dim1: 4]
4343
// pos: [row: 1, col: 2]
4444
// dimensions: [dim0: 2, dim1: 1]

src/includes/Tensor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct Tensor : TensorHandle {
113113
template <typename Pos>
114114
constexpr ElemType *elem_addr(Pos const &pos) const {
115115
using Layout = typename format_traits<Format>::layout_provider_type;
116-
auto offset = Layout::offset(pos, layout());
116+
auto offset = Layout::offset(layout(), pos);
117117
return (ElemType *)addr() + (long)offset;
118118
}
119119

test/TestTensorFormat.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ TEST(TestTensorFormat, TestRowMajor) {
4848

4949
auto layout = format.layout();
5050
auto off1 =
51-
AbstractLayoutProvider<RowMajorLayout>::offset(Dim2(1_c, 2_c), layout);
52-
auto off2 = RowMajorLayout::offset(Dim2(1_c, 2_c), layout);
51+
AbstractLayoutProvider<RowMajorLayout>::offset(layout, Dim2(1_c, 2_c));
52+
auto off2 = RowMajorLayout::offset(layout, Dim2(1_c, 2_c));
5353
static_assert(off1 == off2);
5454
static_assert(off1 == 6_c);
5555
}
@@ -64,8 +64,8 @@ TEST(TestTensorFormat, TestColMajor) {
6464

6565
auto layout = format.layout();
6666
auto off1 =
67-
AbstractLayoutProvider<ColMajorLayout>::offset(Dim2(1_c, 2_c), layout);
68-
auto off2 = ColMajorLayout::offset(Dim2(1_c, 2_c), layout);
67+
AbstractLayoutProvider<ColMajorLayout>::offset(layout, Dim2(1_c, 2_c));
68+
auto off2 = ColMajorLayout::offset(layout, Dim2(1_c, 2_c));
6969
static_assert(off1 == off2);
7070
static_assert(off1 == 5_c);
7171
}
@@ -99,7 +99,7 @@ TEST(TestTensorFormat, TestOffset1) {
9999
auto pos = Dim2(row, col);
100100
using LayoutProvider =
101101
typename format_traits<decltype(format1)>::layout_provider_type;
102-
auto offset = LayoutProvider::offset(pos, layout1);
102+
auto offset = LayoutProvider::offset(layout1, pos);
103103
static_assert(offset == row * 4_c + col);
104104
// std::cout << "offset: " << offset << std::endl;
105105
});
@@ -122,7 +122,7 @@ TEST(TestTensorFormat, TestOffset2) {
122122
auto pos = Dim2(row, col);
123123
using LayoutProvider =
124124
typename format_traits<decltype(format1)>::layout_provider_type;
125-
auto offset = LayoutProvider::offset(pos, layout1);
125+
auto offset = LayoutProvider::offset(layout1, pos);
126126
static_assert(offset == col * 2_c + row);
127127
// std::cout << "offset: " << offset << std::endl;
128128
});

0 commit comments

Comments
 (0)