Skip to content

Fix native CUDA cache retention in fill_holes early returns#32

Open
luca-888 wants to merge 1 commit into
JeffreyXiang:mainfrom
luca-888:fix-fill-holes-cache-cleanup
Open

Fix native CUDA cache retention in fill_holes early returns#32
luca-888 wants to merge 1 commit into
JeffreyXiang:mainfrom
luca-888:fix-fill-holes-cache-cleanup

Conversation

@luca-888
Copy link
Copy Markdown

Summary

This PR fixes native CUDA cache retention in CuMesh::fill_holes() early-return paths.

fill_holes() already calls clear_cache() on the successful fill path. This PR makes the no-op / no-fill paths consistent with that behavior.

Changes

Call clear_cache() before returning when:

  • there are no boundary loops
  • boundary loops exist, but none are selected by max_hole_perimeter

This clears connectivity/boundary cache created during fill_holes() before the function exits.

Verification

Used the following repro script:

import gc
import torch
from cumesh import CuMesh


def mib(x):
    return x / 1024 / 1024


def used_mib():
    torch.cuda.synchronize()
    free, total = torch.cuda.mem_get_info()
    return mib(total - free)


def cleanup():
    gc.collect()
    torch.cuda.empty_cache()
    torch.cuda.synchronize()


def make_grid(n=768):
    y, x = torch.meshgrid(
        torch.linspace(0, 1, n + 1, device="cuda"),
        torch.linspace(0, 1, n + 1, device="cuda"),
        indexing="ij",
    )
    vertices = torch.stack([x, y, torch.zeros_like(x)], -1).reshape(-1, 3).contiguous()

    cy, cx = torch.meshgrid(
        torch.arange(n, device="cuda", dtype=torch.int64),
        torch.arange(n, device="cuda", dtype=torch.int64),
        indexing="ij",
    )
    base = cy * (n + 1) + cx
    f1 = torch.stack([base, base + 1, base + n + 2], -1)
    f2 = torch.stack([base, base + n + 2, base + n + 1], -1)
    faces = torch.stack([f1, f2], 2).reshape(-1, 3).to(torch.int32).contiguous()
    return vertices, faces


v, f = make_grid()
cleanup()
base = used_mib()

mesh = CuMesh()
mesh.init(v, f)
cleanup()
print(f"after init:        delta={used_mib() - base:+.2f} MiB E={mesh.num_edges} B={mesh.num_boundaries}")

mesh.fill_holes(9999.0)
cleanup()
print(f"after first fill:  delta={used_mib() - base:+.2f} MiB E={mesh.num_edges} B={mesh.num_boundaries}")

mesh.fill_holes(9999.0)
cleanup()
print(f"after second fill: delta={used_mib() - base:+.2f} MiB E={mesh.num_edges} B={mesh.num_boundaries}")

mesh.clear_cache()
cleanup()
print(f"after clear_cache: delta={used_mib() - base:+.2f} MiB E={mesh.num_edges} B={mesh.num_boundaries}")

Before:

after init:        delta=+22.00 MiB E=0 B=0
after first fill:  delta=+24.00 MiB E=0 B=0
after second fill: delta=+162.00 MiB E=1774080 B=0
after clear_cache: delta=+24.00 MiB E=0 B=0

After:

after init:        delta=+22.00 MiB E=0 B=0
after first fill:  delta=+24.00 MiB E=0 B=0
after second fill: delta=+24.00 MiB E=0 B=0
after clear_cache: delta=+24.00 MiB E=0 B=0

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.

1 participant