Skip to content

Instantly share code, notes, and snippets.

@tfarago
Created April 11, 2023 10:47
Show Gist options
  • Save tfarago/933a0eec9f5f24229bc4a9c9b1528d2d to your computer and use it in GitHub Desktop.
Save tfarago/933a0eec9f5f24229bc4a9c9b1528d2d to your computer and use it in GitHub Desktop.
GPU memory leak with UFO when resources are not re-used
import argparse
import gi
import numpy as np
gi.require_version("Ufo", "0.0")
from gi.repository import Ufo
N = 8192
RESOURCES = Ufo.Resources()
PLUGIN_MANAGER = Ufo.PluginManager()
def get_memory_in(array):
in_task = PLUGIN_MANAGER.get_task("memory-in")
in_task.props.pointer = array.__array_interface__["data"][0]
in_task.props.width = array.shape[1]
in_task.props.height = array.shape[0]
in_task.props.number = 1
in_task.props.bitdepth = 32
return in_task
def get_memory_out(width, height):
array = np.empty((height, width), dtype=np.float32)
out_task = PLUGIN_MANAGER.get_task("memory-out")
out_task.props.pointer = array.__array_interface__["data"][0]
out_task.props.max_size = array.nbytes
out_task.np_array = array
return (out_task, array)
def cartesian_to_polar(image=None, num_angles=None, resources=None):
"""Read one full flat-corrected image."""
if image is None:
image = np.arange(N ** 2).reshape(N, N).astype(np.float32)
if image.shape[0] != image.shape[1]:
raise ValueError("Only square images are supported")
n = image.shape[1]
width = n // 2
height = num_angles if num_angles else int(np.ceil(2 * np.pi / (2 / n)))
graph = Ufo.TaskGraph()
sched = Ufo.Scheduler()
if resources is not None:
sched.set_resources(resources)
in_task = get_memory_in(image)
out_task, np_out_array = get_memory_out(width, height)
polar_task = PLUGIN_MANAGER.get_task("polar-coordinates")
polar_task.props.width = width
polar_task.props.height = height
graph.connect_nodes(in_task, polar_task)
graph.connect_nodes(polar_task, out_task)
sched.run(graph)
return np_out_array
def main(reuse_resources=False):
parser = argparse.ArgumentParser()
parser.add_argument("--reuse-resources", action="store_true", help="Re-use Ufo resources")
parser.add_argument("--number", type=int, default=10, help="Number of iterations")
args = parser.parse_args()
rng = range(args.number)
try:
import tqdm
# Make progress bar
rng = tqdm.tqdm(rng)
except ImportError:
pass
for i in rng:
cartesian_to_polar(resources=RESOURCES if args.reuse_resources else None)
if __name__ == '__main__':
main()
input("Press enter...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment