14
14
15
15
#include < sycl.hpp>
16
16
17
+ #include < iostream>
18
+
17
19
using namespace sycl ;
18
20
19
21
// Allocator has a minimum internal alignment of 64, so use higher values for
@@ -25,17 +27,20 @@ struct alignas(128) Aligned128 {
25
27
int x;
26
28
};
27
29
28
- template <class T , class Allocator >
29
- void test_align (Allocator alloc, size_t Align) {
30
- std::vector<T *> Ptrs;
31
- for (int i = 0 ; i < 10 ; ++i )
32
- Ptrs. push_back ( alloc.allocate (1 ) );
30
+ template <class Allocator >
31
+ void test_align (Allocator alloc, size_t Align, int Line = __builtin_LINE() ) {
32
+ decltype (alloc. allocate ( 1 )) Ptrs[ 10 ] ;
33
+ for (auto *&Elem : Ptrs )
34
+ Elem = alloc.allocate (1 );
33
35
34
36
int NumExactlyAligned = 0 ;
35
37
36
- for (T *Ptr : Ptrs) {
38
+ for (auto *Ptr : Ptrs) {
37
39
auto Val = reinterpret_cast <uintptr_t >(Ptr);
38
- assert ((Val & (Align - 1 )) == 0 && " Not properly aligned!" );
40
+ if ((Val & (Align - 1 )) != 0 ) {
41
+ std::cout << " Failed at line " << Line << std::endl;
42
+ assert (false && " Not properly aligned!" );
43
+ }
39
44
if ((Val & (Align * 2 - 1 )) != 0 )
40
45
++NumExactlyAligned;
41
46
}
@@ -50,9 +55,12 @@ void test_align(Allocator alloc, size_t Align) {
50
55
// needs to be updated to find configuration where we still test our runtime
51
56
// by ensuring that underlying allocations' alignment differs between
52
57
// Aligned128/Aligned256.
53
- assert (NumExactlyAligned != 0 && " All allocations are over-aligned!" );
58
+ if (NumExactlyAligned == 0 ) {
59
+ std::cout << " Failed at line " << Line << std::endl;
60
+ assert (false && " All allocations are over-aligned!" );
61
+ }
54
62
55
- for (T *Ptr : Ptrs)
63
+ for (auto *Ptr : Ptrs)
56
64
alloc.deallocate (Ptr, 1 );
57
65
}
58
66
@@ -67,24 +75,24 @@ int main() {
67
75
{
68
76
// Test default value of Alignment template parameter.
69
77
usm_allocator<Aligned256, usm::alloc::host> alloc (ctx, dev);
70
- test_align<Aligned256> (alloc, 256 );
78
+ test_align (alloc, 256 );
71
79
72
80
using traits_t = std::allocator_traits<decltype (alloc)>;
73
81
using rebind_t = typename traits_t ::template rebind_alloc<Aligned128>;
74
82
rebind_t alloc_rebound = alloc;
75
- test_align<Aligned128> (alloc_rebound, 128 );
83
+ test_align (alloc_rebound, 128 );
76
84
}
77
85
78
86
{
79
87
// Test explicit value of Alignment template parameter.
80
88
usm_allocator<Aligned256, usm::alloc::host, 256 > alloc (ctx, dev);
81
- test_align<Aligned256> (alloc, 256 );
89
+ test_align (alloc, 256 );
82
90
using traits_t = std::allocator_traits<decltype (alloc)>;
83
91
using rebind_t = typename traits_t ::template rebind_alloc<Aligned128>;
84
92
rebind_t alloc_rebound = alloc;
85
93
// Rebound allocator must use the higher alignment from the explicit
86
94
// template parameter, not the type's alignment.
87
- test_align<Aligned128> (alloc_rebound, 256 );
95
+ test_align (alloc_rebound, 256 );
88
96
}
89
97
90
98
return 0 ;
0 commit comments