Skip to content

Fix double allocation in sid::allocator - #1832

Open
egparedes wants to merge 2 commits into
GridTools:masterfrom
egparedes:fix-sid-allocator-double-allocation
Open

egparedes wants to merge 2 commits into
GridTools:masterfrom
egparedes:fix-sid-allocator-double-allocation

Conversation

@egparedes

@egparedes egparedes commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

allocate in sid::allocator (include/gridtools/sid/allocator.hpp) calls the allocation functor twice and only keeps the second result:

auto ptr = self.m_impl(sizeof(type) * size);
self.m_buffers.push_back(self.m_impl(sizeof(type) * size));

ptr is never used. Every allocate call therefore requests two buffers from the functor and releases the unused one when it returns. This PR removes the first line.

The effect depends on the allocator:

  • CPU fn backend (plain sid::allocator over std::make_unique<char[]>): the change removes one allocation and one zero-fill per temporary per call.
  • cached_allocator (GPU fn backend, cpu_kfirst and cpu_ifirst stencil backends): the discarded buffer was returned to the cache, so the change saves an extra cached buffer per temporary size rather than time.

How it was observed

Observed on a GT4Py gtfn_cpu program: strace showed two mmap/munmap pairs of the temporary's size per call.

Testing

  • New unit test test_sid_allocator, which counts the calls of the allocation functor. Without the fix it fails (2 calls for one allocate), with the fix it passes.
  • CPU only, GCC 13.3, Release build. 26 tests built and passed: all test_sid_* unit tests, test_tmp_storage_sid_cpu_ifirst, test_fn_{backend_naive,cartesian,executor,run,unstructured}, the fn naive regressions cartesian_horizontal_diffusion, cartesian_vertical_advection, tridiagonal_solve and unstructured_nabla, and horizontal_diffusion with naive, cpu_kfirst and cpu_ifirst.
  • No GPU builds or tests, and not the full test suite.

Possible follow-up (not part of this PR)

The CPU fn backend allocates with std::make_unique<char[]>, which value-initializes the buffer, so the remaining allocation is still zero-filled. Using an allocation that leaves the buffer uninitialized would avoid that, but it changes behavior and is better discussed separately.

🤖 Written by an agent on my behalf.

allocate() called the allocation functor twice and used only the second buffer; the first one was freed right away.
@gridtoolsjenkins

Copy link
Copy Markdown
Collaborator

Hi there, this is jenkins continuous integration...
Do you want me to verify this patch?

@egparedes
egparedes marked this pull request as ready for review November 27, 2026 07:46
@egparedes

Copy link
Copy Markdown
Contributor Author

@havogt can you take a look at this?

@havogt

havogt commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

cscs-ci run

1 similar comment
@edopao

edopao commented Dec 3, 2026

Copy link
Copy Markdown

cscs-ci run

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants