File tree Expand file tree Collapse file tree 3 files changed +22
-14
lines changed
unified-runtime/source/adapters/native_cpu Expand file tree Collapse file tree 3 files changed +22
-14
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,20 @@ inline void *aligned_malloc(size_t alignment, size_t size) {
81
81
return ptr;
82
82
}
83
83
84
+ // In many cases we require aligned memory without being told what the alignment
85
+ // requirement is. This helper function returns maximally aligned memory based
86
+ // on the size.
87
+ inline void *aligned_malloc (size_t size) {
88
+ constexpr size_t max_alignment = 16 * sizeof (double );
89
+ size_t alignment = max_alignment;
90
+ while (alignment > size) {
91
+ alignment >>= 1 ;
92
+ }
93
+ // aligned_malloc requires size to be a multiple of alignment; round up.
94
+ size = (size + alignment - 1 ) & ~(alignment - 1 );
95
+ return aligned_malloc (alignment, size);
96
+ }
97
+
84
98
inline void aligned_free (void *ptr) {
85
99
#ifdef _MSC_VER
86
100
_aligned_free (ptr);
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ struct ur_kernel_handle_t_ : RefCounted {
42
42
43
43
~ur_kernel_handle_t_ () {
44
44
removeArgReferences ();
45
- free (_localMemPool);
45
+ native_cpu::aligned_free (_localMemPool);
46
46
}
47
47
48
48
ur_kernel_handle_t_ (ur_program_handle_t hProgram, const char *name,
@@ -59,7 +59,6 @@ struct ur_kernel_handle_t_ : RefCounted {
59
59
args_index_t Indices;
60
60
std::vector<size_t > ParamSizes;
61
61
std::vector<bool > OwnsMem;
62
- static constexpr size_t MaxAlign = 16 * sizeof (double );
63
62
64
63
arguments () = default ;
65
64
@@ -109,11 +108,7 @@ struct ur_kernel_handle_t_ : RefCounted {
109
108
}
110
109
}
111
110
if (NeedAlloc) {
112
- size_t Align = MaxAlign;
113
- while (Align > Size) {
114
- Align >>= 1 ;
115
- }
116
- Indices[Index] = native_cpu::aligned_malloc (Align, Size);
111
+ Indices[Index] = native_cpu::aligned_malloc (Size);
117
112
ParamSizes[Index] = Size;
118
113
OwnsMem[Index] = true ;
119
114
}
@@ -158,8 +153,8 @@ struct ur_kernel_handle_t_ : RefCounted {
158
153
if (reqSize == 0 || reqSize == _localMemPoolSize) {
159
154
return ;
160
155
}
161
- // realloc handles nullptr case
162
- _localMemPool = ( char *) realloc (_localMemPool, reqSize);
156
+ native_cpu::aligned_free (_localMemPool);
157
+ _localMemPool = static_cast < char *>( native_cpu::aligned_malloc ( reqSize) );
163
158
_localMemPoolSize = reqSize;
164
159
}
165
160
Original file line number Diff line number Diff line change 19
19
20
20
struct ur_mem_handle_t_ : ur_object {
21
21
ur_mem_handle_t_ (size_t Size, bool _IsImage)
22
- : _mem{static_cast <char *>(malloc (Size))}, _ownsMem{ true },
23
- IsImage{_IsImage} {}
22
+ : _mem{static_cast <char *>(native_cpu::aligned_malloc (Size))},
23
+ _ownsMem{ true }, IsImage{_IsImage} {}
24
24
25
25
ur_mem_handle_t_ (void *HostPtr, size_t Size, bool _IsImage)
26
- : _mem{static_cast <char *>(malloc (Size))}, _ownsMem{true },
27
- IsImage{_IsImage} {
26
+ : ur_mem_handle_t_(Size, _IsImage) {
28
27
memcpy (_mem, HostPtr, Size);
29
28
}
30
29
@@ -34,7 +33,7 @@ struct ur_mem_handle_t_ : ur_object {
34
33
35
34
~ur_mem_handle_t_ () {
36
35
if (_ownsMem) {
37
- free (_mem);
36
+ native_cpu::aligned_free (_mem);
38
37
}
39
38
}
40
39
You can’t perform that action at this time.
0 commit comments