Skip to content

Conversation

@ax3l
Copy link
Member

@ax3l ax3l commented Sep 17, 2025

For AoS and SoA .to_numpy() and dependent logic
like .to_df(), the lifetime of the temporary copied variable is only handled by NumPy if we first create a named variable.

The to_host() returned object is a temporary, and np.array using the __array_interface__ protocol does not keep it alive automatically unless it is stored in an actual variable (eg., tmp).

X-ref:

@ax3l ax3l added bug Something isn't working bug: affects latest release Bug also exists in latest release version labels Sep 17, 2025
ax3l added a commit to ax3l/pyamrex-feedstock that referenced this pull request Sep 17, 2025
For AoS and SoA `.to_numpy()` and dependent logic
like `.to_df()`, the lifetime of the temporary copied
variable is only handled by NumPy if we first create
a named variable.

The `to_host()` returned object is a temporary, and
`np.array` using the `__array_interface__` protocol does
not keep it alive automatically unless it is stored
in an actual variable (eg., `tmp`).
@ax3l ax3l force-pushed the fix-to-host-temporary branch from a7dfdc0 to da9d3a4 Compare September 17, 2025 23:09
ax3l added a commit to ax3l/pyamrex-feedstock that referenced this pull request Sep 17, 2025
import numpy as np

if self.size() > 0:
if copy:
Copy link
Member Author

@ax3l ax3l Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can generally make this a bit more explicit by checking if self is on GPU and otherwise just copying from it directly, with another if/else branch. But the logic here should have the same effect and amount of copies involved...

The only difference is that here we will always end up with pinned memory, while we might be sometimes interested using regular host memory. But seems niche.

Copy link
Contributor

@roelof-groenewald roelof-groenewald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
Just one question: Do we need the asserts to "trick" Python into keeping the object alive?

@ax3l
Copy link
Member Author

ax3l commented Sep 18, 2025

The assert is not needed, but if it is ever violated I want a python error instead of a segfault in code that uses the returned variable. I was too lasy to raise, but maybe that would be cleaner?

@ax3l
Copy link
Member Author

ax3l commented Sep 18, 2025

Ah, assert just raises AssertionError in Python, so its good enough for here.

@ax3l ax3l merged commit 0e615a3 into AMReX-Codes:development Sep 18, 2025
18 checks passed
@ax3l ax3l deleted the fix-to-host-temporary branch September 18, 2025 15:50
# np.array using the __array_interface__ protocol does
# not keep it alive automatically unless it is stored
# in an actual variable (tmp).
tmp = self.to_host()
Copy link
Member Author

@ax3l ax3l Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, looks like there is more / this is not a sufficient fix (at least for some SP tests on conda-forge):
conda-forge/impactx-feedstock#56 (comment)

It already has an issue here, so the problem could be in the implementation of to_host()...
https://github.com/AMReX-Codes/pyamrex/blob/25.09/src/Base/PODVector.cpp#L79-L87

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very hard to reproduce locally... also cannot spot anything in valgrind yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be fixed by BLAST-ImpactX/impactx#1156 o.0

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

Labels

bug: affects latest release Bug also exists in latest release version bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants