Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Octree converter #3

Merged
merged 13 commits into from
Jul 22, 2024
Merged

Octree converter #3

merged 13 commits into from
Jul 22, 2024

Conversation

cphyc
Copy link
Member

@cphyc cphyc commented Dec 5, 2023

This adds the ability to convert a list of positions (x, y, z) with levels into an octree that can be loaded by yt.load_octree.

import numpy as np
import yt
from yt_experiments.octree.converter import OctTree
xyz = np.array([
    [0.375, 0.125, 0.125],
    [0.125, 0.125, 0.125],
    [0.375, 0.375, 0.375],
    [0.75, 0.75, 0.75],
])
levels = np.array([2, 2, 2, 1], dtype=np.int32)
data = {
    ("gas", "density"): np.random.rand(4)
}

oct = OctTree.from_list(xyz, levels, check=True)
ref_mask, leaf_order = oct.get_refmask()

for k, v in data.items():
    # Make it 2D so that yt doesn't think those are particles
    data[k] = np.where(leaf_order >= 0, v[leaf_order], np.nan)[:, None]

ds = yt.load_octree(ref_mask, data)

@cphyc cphyc marked this pull request as draft December 6, 2023 07:01
@cphyc cphyc marked this pull request as ready for review July 19, 2024 13:28
@chrishavlin chrishavlin self-requested a review July 19, 2024 14:16
Copy link
Contributor

@chrishavlin chrishavlin left a comment

Choose a reason for hiding this comment

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

Generally looks good to me, but I think there's a memory leak? I haven't been able to pin it down, but here's a little test script:

# mem_leak_test.py
import sys
import numpy as np
from yt_experiments.octree.converter import OctTree

if __name__ == '__main__':
    xyz = np.array(
        [
            [0.375, 0.125, 0.125],
            [0.125, 0.125, 0.125],
            [0.375, 0.375, 0.375],
            [0.75, 0.75, 0.75],
        ],
    )
    levels = np.array([2, 2, 2, 1], dtype=np.int32)

    for _ in range(int(sys.argv[1])):
        octree = OctTree.from_list(xyz, levels, check=True)        
        del octree

running with python mem_leak_test 1000000 (need a large number of iterations since the number of nodes is small), memory usage grows continuously. Ends up using close to 400 Mb before finishing the iterations.

yt_experiments/octree/converter.pyx Outdated Show resolved Hide resolved
yt_experiments/octree/converter.pyx Outdated Show resolved Hide resolved
yt_experiments/octree/converter.pyx Show resolved Hide resolved
@chrishavlin
Copy link
Contributor

Screenshot 2024-07-19 at 3 41 55 PM

e.g., here's a flamegraph from memray for the above example -- the blue and orange lines are resident and heap size over time, y scale on the plot is from 0 to ~500 Mb

@chrishavlin
Copy link
Contributor

oh! one more suggestion -- would be good to add *converter.cpp to .gitignore

@cphyc
Copy link
Member Author

cphyc commented Jul 22, 2024

There was indeed a memory leak (I didn't implement a deallocate step). I've just checked with your code snippet, and the memory use is now constant. Thanks for the thorough review!

@chrishavlin
Copy link
Contributor

Great! Ya, I thought you needed a __deallocate__ but i wasn't sure about whether it needed to recursively walk through the children (which based on your implementation now I see that it does!).

@chrishavlin chrishavlin merged commit 23eb280 into yt-project:main Jul 22, 2024
5 checks passed
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.

None yet

2 participants