From 1ccd51d237128fd85c3d3146bee4e99e0e459d82 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 26 Jan 2024 12:41:06 -0500 Subject: [PATCH 1/4] Ensure pointer is 8-byte aligned in stack_alloc::alloc --- stan/math/memory/stack_alloc.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stan/math/memory/stack_alloc.hpp b/stan/math/memory/stack_alloc.hpp index 4706473c384..85f43016214 100644 --- a/stan/math/memory/stack_alloc.hpp +++ b/stan/math/memory/stack_alloc.hpp @@ -167,9 +167,11 @@ class stack_alloc { * @return A pointer to the allocated memory. */ inline void* alloc(size_t len) { + size_t pad = len % 8 == 0 ? 0 : 8 - len % 8; + // Typically, just return and increment the next location. char* result = next_loc_; - next_loc_ += len; + next_loc_ += len + pad; // Occasionally, we have to switch blocks. if (unlikely(next_loc_ >= cur_block_end_)) { result = move_to_next_block(len); From d743eeab505e9d13ed71040457b2a70ced7e18e0 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 26 Jan 2024 12:48:51 -0500 Subject: [PATCH 2/4] Update stack_alloc_test --- test/unit/math/memory/stack_alloc_test.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/unit/math/memory/stack_alloc_test.cpp b/test/unit/math/memory/stack_alloc_test.cpp index f4e244afcae..957a6d442c6 100644 --- a/test/unit/math/memory/stack_alloc_test.cpp +++ b/test/unit/math/memory/stack_alloc_test.cpp @@ -91,6 +91,15 @@ TEST(stack_alloc, alloc) { allocator.recover_all(); } +TEST(stack_alloc, alloc_aligned){ + stan::math::stack_alloc allocator; + int* x = allocator.alloc_array(3); + + double* y = allocator.alloc_array(4); + EXPECT_TRUE(stan::math::is_aligned(y, 8)); + allocator.recover_all(); +} + TEST(stack_alloc, in_stack) { stan::math::stack_alloc allocator; @@ -113,7 +122,11 @@ TEST(stack_alloc, in_stack_second_block) { char* y = allocator.alloc_array(1); EXPECT_TRUE(allocator.in_stack(x)); EXPECT_TRUE(allocator.in_stack(y)); - EXPECT_FALSE(allocator.in_stack(y + 1)); + // effect of padding + EXPECT_TRUE(allocator.in_stack(y + 1)); + EXPECT_TRUE(allocator.in_stack(y + 7)); + + EXPECT_FALSE(allocator.in_stack(y + 8)); allocator.recover_all(); EXPECT_FALSE(allocator.in_stack(x)); From 2099a7e41abc73ede25837e52fb18cdf7f85baa0 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 26 Jan 2024 12:52:17 -0500 Subject: [PATCH 3/4] Update doc --- stan/math/memory/stack_alloc.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stan/math/memory/stack_alloc.hpp b/stan/math/memory/stack_alloc.hpp index 85f43016214..8a8ef2cc7c1 100644 --- a/stan/math/memory/stack_alloc.hpp +++ b/stan/math/memory/stack_alloc.hpp @@ -158,7 +158,9 @@ class stack_alloc { * Return a newly allocated block of memory of the appropriate * size managed by the stack allocator. * - * The allocated pointer will be 8-byte aligned. + * The allocated pointer will be 8-byte aligned. If the number + * of bytes requested is not a multiple of 8, the reserved space + * will be padded up to the next multiple of 8. * * This function may call C++'s malloc() function, * with any exceptions percolated through this function. From e01f171493e501ade5a6156ff83551a8091962b6 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 26 Jan 2024 12:53:28 -0500 Subject: [PATCH 4/4] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- test/unit/math/memory/stack_alloc_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/math/memory/stack_alloc_test.cpp b/test/unit/math/memory/stack_alloc_test.cpp index 957a6d442c6..b395df387e9 100644 --- a/test/unit/math/memory/stack_alloc_test.cpp +++ b/test/unit/math/memory/stack_alloc_test.cpp @@ -91,7 +91,7 @@ TEST(stack_alloc, alloc) { allocator.recover_all(); } -TEST(stack_alloc, alloc_aligned){ +TEST(stack_alloc, alloc_aligned) { stan::math::stack_alloc allocator; int* x = allocator.alloc_array(3);