Skip to content

Commit c76e293

Browse files
authored
Adding macro XTENSOR_SELECT_ALIGN (#2152)
Adding macro XTENSOR_SELECT_ALIGN
1 parent d509a41 commit c76e293

7 files changed

Lines changed: 111 additions & 5 deletions

File tree

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ for details.
7070
histogram
7171
random
7272
sfinae
73+
xsimd
7374
file_loading
7475
build-options
7576
pitfall

docs/source/sfinae.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
The full license is in the file LICENSE, distributed with this software.
66
7-
.. _histogram:
7+
.. _sfinae:
88

99
SFINAE
1010
======

docs/source/xsimd.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht
2+
3+
Distributed under the terms of the BSD 3-Clause License.
4+
5+
The full license is in the file LICENSE, distributed with this software.
6+
7+
.. _xsimd:
8+
9+
xsimd
10+
=====
11+
12+
Alignment of fixed-size members
13+
-------------------------------
14+
15+
.. note::
16+
17+
If you are using ``C++ >= 17`` you should not have to worry about this.
18+
19+
If you define a structure having members of fixed-size xtensor types,
20+
you must ensure that the buffers properly aligned.
21+
For this you can use the macro ``XTENSOR_SELECT_ALIGN`` available in
22+
``xtensor/xtensor_config.hpp``.
23+
Consider the following example:
24+
25+
.. code-block:: cpp
26+
27+
template <typename T>
28+
class alignas(XTENSOR_SELECT_ALIGN(T)) Foo
29+
{
30+
public:
31+
32+
using allocator_type = std::conditional_t<XTENSOR_SELECT_ALIGN(T) != 0,
33+
xt_simd::aligned_allocator<T, XTENSOR_SELECT_ALIGN(T)>,
34+
std::allocator<T>>;
35+
36+
Foo(T fac) : m_fac(fac)
37+
{
38+
m_bar.fill(fac);
39+
}
40+
41+
auto get() const
42+
{
43+
return m_bar;
44+
}
45+
46+
private:
47+
48+
xt::xtensor_fixed<T, xt::xshape<10, 10>> m_bar;
49+
T m_fac;
50+
};
51+
52+
Whereby it is important to store the fixed-sized xtensor type (in this case ``xt::xtensor_fixed<T, xt::xshape<10, 10>>``) as first member.

include/xtensor/xstorage.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,8 +1368,6 @@ namespace xt
13681368
lhs.swap(rhs);
13691369
}
13701370

1371-
#define XTENSOR_SELECT_ALIGN (XTENSOR_DEFAULT_ALIGNMENT != 0 ? XTENSOR_DEFAULT_ALIGNMENT : alignof(T))
1372-
13731371
template <class X, class T, std::size_t N, class A, bool B>
13741372
struct rebind_container<X, svector<T, N, A, B>>
13751373
{
@@ -1383,7 +1381,7 @@ namespace xt
13831381
*
13841382
* To be moved to xtl, along with the rest of xstorage.hpp
13851383
*/
1386-
template <class T, std::size_t N, std::size_t Align = XTENSOR_SELECT_ALIGN>
1384+
template <class T, std::size_t N, std::size_t Align = XTENSOR_SELECT_ALIGN(T)>
13871385
class alignas(Align) aligned_array : public std::array<T, N>
13881386
{
13891387
public:
@@ -1936,6 +1934,5 @@ namespace std
19361934
#endif
19371935

19381936
#undef XTENSOR_CONST
1939-
#undef XTENSOR_SELECT_ALIGN
19401937

19411938
#endif

include/xtensor/xtensor_config.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
#define XTENSOR_OPENMP_TRESHOLD 0
111111
#endif
112112

113+
#ifndef XTENSOR_SELECT_ALIGN
114+
#define XTENSOR_SELECT_ALIGN(T) (XTENSOR_DEFAULT_ALIGNMENT != 0 ? XTENSOR_DEFAULT_ALIGNMENT : alignof(T))
115+
#endif
116+
113117
#ifdef IN_DOXYGEN
114118
namespace xtl
115119
{

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ set(XTENSOR_TESTS
219219
test_xrandom.cpp
220220
test_xrepeat.cpp
221221
test_xsort.cpp
222+
test_xsimd.cpp
222223
test_xvectorize.cpp
223224
test_extended_xmath_interp.cpp
224225
test_extended_broadcast_view.cpp

test/test_xsimd.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/***************************************************************************
2+
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3+
* Copyright (c) QuantStack *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#include <complex>
11+
#include <limits>
12+
13+
#include "gtest/gtest.h"
14+
#include "xtensor/xfixed.hpp"
15+
#include "xtensor/xtensor_config.hpp"
16+
17+
template <typename T>
18+
class alignas(XTENSOR_SELECT_ALIGN(T)) Foo
19+
{
20+
public:
21+
22+
using allocator_type = std::conditional_t<XTENSOR_SELECT_ALIGN(T) != 0,
23+
xt_simd::aligned_allocator<T, XTENSOR_SELECT_ALIGN(T)>,
24+
std::allocator<T>>;
25+
26+
Foo(T fac) : m_fac(fac)
27+
{
28+
m_bar.fill(fac);
29+
}
30+
31+
auto get() const
32+
{
33+
return m_bar;
34+
}
35+
36+
private:
37+
38+
xt::xtensor_fixed<T, xt::xshape<10, 10>> m_bar;
39+
T m_fac;
40+
};
41+
42+
namespace xt
43+
{
44+
45+
TEST(xsimd, alignas)
46+
{
47+
int fac = 10;
48+
Foo<int> foo(10);
49+
EXPECT_TRUE(xt::sum(foo.get())() == fac * 10 * 10);
50+
}
51+
}

0 commit comments

Comments
 (0)