Skip to content

Commit

Permalink
fixed bug with aligned alloc for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
GagaLP committed Feb 6, 2024
1 parent 28a74bb commit 30444a7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
9 changes: 8 additions & 1 deletion include/simsycl/detail/allocation.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ namespace simsycl::detail {
// NOTE: returned pointers must be freed with aligned_free
inline void *aligned_alloc(size_t alignment, size_t size) {
#if defined(_MSC_VER)
// MSVC does not have std::aligned_alloc because the pointers it returns cannot be freed with std::free
return _aligned_malloc(size, alignment);
#else
return std::aligned_alloc(alignment, size);
// POSIX and notably macOS requires a minimum alignment of sizeof(void*) for aligned_alloc, so we only use that for
// over-aligned allocations
if(alignment <= alignof(std::max_align_t)) {
return std::malloc(size);
} else {
return std::aligned_alloc(alignment, size);
}
#endif
}

Expand Down
14 changes: 1 addition & 13 deletions src/simsycl/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,7 @@ void *usm_alloc(const sycl::context &context, sycl::usm::alloc kind, std::option
if(*bytes_free < size_bytes) return nullptr;
}

void *ptr;
#if defined(_MSC_VER)
// MSVC does not have std::aligned_alloc because the pointers it returns cannot be freed with std::free
ptr = _aligned_malloc(size_bytes, alignment_bytes);
#else
// POSIX and notably macOS requires a minimum alignment of sizeof(void*) for aligned_alloc, so we only use that for
// over-aligned allocations
if(alignment_bytes <= alignof(std::max_align_t)) {
ptr = std::malloc(size_bytes);
} else {
ptr = std::aligned_alloc(alignment_bytes, size_bytes);
}
#endif
void *ptr = aligned_alloc(alignment_bytes, size_bytes);

if(ptr == nullptr) return nullptr;
std::memset(ptr, static_cast<int>(uninitialized_memory_pattern), size_bytes);
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ add_executable(tests
math_tests.cc
reduction_tests.cc
simulation_tests.cc
usm_tests.cc
alloc_tests.cc
vec_tests.cc
)

Expand Down
13 changes: 13 additions & 0 deletions test/usm_tests.cc → test/alloc_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@

#include <catch2/catch_test_macros.hpp>

TEST_CASE("allocates memory of any alignment", "[aligned_alloc]") {
const size_t largest_sycl_align_bytes = alignof(sycl::long16);
const size_t size_bytes = 4096;
CAPTURE(size_bytes);
for(size_t align_bytes = 1; align_bytes <= largest_sycl_align_bytes; align_bytes *= 2) {
CAPTURE(align_bytes);
errno = 0;
const auto p = simsycl::detail::aligned_alloc(align_bytes, size_bytes);
CHECK(p != nullptr);
CHECK(reinterpret_cast<std::uintptr_t>(p) % align_bytes == 0);
simsycl::detail::aligned_free(p);
}
}

TEST_CASE("usm_alloc allocates memory of any alignment", "[usm]") {
const size_t largest_sycl_align_bytes = alignof(sycl::long16);
Expand Down

0 comments on commit 30444a7

Please sign in to comment.