-
Notifications
You must be signed in to change notification settings - Fork 280
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
Trial tracemalloc as memory tracker (replacement v2 PR) #5946
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
94494fb
Trial tracemalloc as alternative memory tracker.
pp-mo d039ad0
Use more practical nworkers params.
pp-mo 84ff4ad
Fix task scaling; test WorkersAndBlocks with process-based tasks.
pp-mo 8f7166e
Investigate repeated ops with simulated memory 'leak'.
pp-mo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Copyright Iris contributors | ||
# | ||
# This file is part of Iris and is released under the BSD license. | ||
# See LICENSE in the root of the repository for full licensing details. | ||
"""Benchmarks to evaluate tracemalloc/rss methods of memory measurement.""" | ||
|
||
from .. import TrackAddedMemoryAllocation | ||
from .memory_exercising_task import SampleParallelTask | ||
|
||
|
||
class MemcheckCommon: | ||
# Basic controls over the test calculation | ||
default_params = { | ||
"measure": "tracemalloc", # alternate: "rss" | ||
"runtype": "threads", # alternate: "processes" | ||
"ysize": 10000, | ||
"nx": 2000, | ||
"nblocks": 6, | ||
"nworkers": 3, | ||
} | ||
|
||
def _setup(self, **kwargs): | ||
params = self.default_params.copy() | ||
params.update(kwargs) | ||
measure = params["measure"] | ||
runtype = params["runtype"] | ||
ysize = params["ysize"] | ||
nx = params["nx"] | ||
nblocks = params["nblocks"] | ||
nworkers = params["nworkers"] | ||
|
||
nyfull = ysize // nblocks | ||
use_processes = {"threads": False, "processes": True}[runtype] | ||
self.task = SampleParallelTask( | ||
n_blocks=nblocks, | ||
outerdim=nyfull // nblocks, | ||
innerdim=nx, | ||
n_workers=nworkers, | ||
use_process_workers=use_processes, | ||
) | ||
self.use_tracemalloc = {"tracemalloc": True, "rss": False}[measure] | ||
|
||
def run_time_calc(self): | ||
# This usage is a bit crap, as we don't really care about the runtype. | ||
self.task.perform() | ||
|
||
def run_addedmem_calc(self): | ||
with TrackAddedMemoryAllocation( | ||
use_tracemalloc=self.use_tracemalloc, | ||
result_min_mb=0.0, | ||
) as tracer: | ||
self.task.perform() | ||
return tracer.addedmem_mb() | ||
|
||
|
||
def memory_units_mib(func): | ||
func.unit = "Mib" | ||
return func | ||
|
||
|
||
class MemcheckRunstyles(MemcheckCommon): | ||
# only some are parametrised, or it's just too complicated! | ||
param_names = [ | ||
"measure", | ||
"runtype", | ||
"ysize", | ||
] | ||
params = [ | ||
# measure | ||
["tracemalloc", "rss"], | ||
# runtype | ||
["threads", "processes"], | ||
# ysize | ||
[10000, 40000], | ||
] | ||
|
||
def setup(self, measure, runtype, ysize): | ||
self._setup(measure=measure, runtype=runtype, ysize=ysize) | ||
|
||
def time_calc(self, measure, runtype, ysize): | ||
self.run_time_calc() | ||
|
||
@memory_units_mib | ||
def track_addmem_calc(self, measure, runtype, ysize): | ||
return self.run_addedmem_calc() | ||
|
||
|
||
class MemcheckBlocksAndWorkers(MemcheckCommon): | ||
# only some are parametrised, or it's just too complicated! | ||
param_names = [ | ||
"nblocks", | ||
"nworkers", | ||
] | ||
params = [ | ||
# nblocks | ||
[1, 4, 9], | ||
# nworkers | ||
[1, 2, 3, 4], | ||
] | ||
|
||
def setup(self, nblocks, nworkers): | ||
self.default_params["ysize"] = 20000 | ||
self._setup( | ||
nblocks=nblocks, | ||
nworkers=nworkers, | ||
) | ||
|
||
def time_calc(self, nblocks, nworkers): | ||
self.run_time_calc() | ||
|
||
@memory_units_mib | ||
def track_addmem_calc(self, nblocks, nworkers): | ||
return self.run_addedmem_calc() | ||
|
||
|
||
class MemcheckBlocksAndWorkers_Rss(MemcheckBlocksAndWorkers): | ||
def setup(self, nblocks, nworkers): | ||
self.default_params["measure"] = "rss" | ||
super().setup( | ||
nblocks=nblocks, | ||
nworkers=nworkers, | ||
) |
94 changes: 94 additions & 0 deletions
94
benchmarks/benchmarks/memtrace_evaluation/memory_exercising_task.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright Iris contributors | ||
# | ||
# This file is part of Iris and is released under the BSD license. | ||
# See LICENSE in the root of the repository for full licensing details. | ||
"""Provide a standard parallel calculation for testing the memory tracing.""" | ||
|
||
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor | ||
|
||
import numpy as np | ||
|
||
""" | ||
the basic operation is to for each worker to construct a (NY, NX) numpy | ||
random array, of which it calculates and returns the mean(axis=0) | ||
--> (NX,) result | ||
The results are then collected --> (N_BLOCKS, NX), | ||
and a mean over all calculated --> (NX,) | ||
The final (single-value) result is the *minimum* of that. | ||
""" | ||
|
||
# _SHOW_DEBUG = True | ||
_SHOW_DEBUG = False | ||
|
||
|
||
def debug(msg): | ||
if _SHOW_DEBUG: | ||
print(msg) | ||
|
||
|
||
def subtask_operation(arg): | ||
i_task, ny, nx = arg | ||
debug(f"\nRunning #{i_task}({ny}, {nx}) ..") | ||
data = np.random.uniform(0.0, 1.0, size=(ny, nx)) # noqa: NPY002 | ||
sub_result = data.mean(axis=0) | ||
debug(f"\n.. completed #{i_task}") | ||
return sub_result | ||
|
||
|
||
class SampleParallelTask: | ||
def __init__( | ||
self, | ||
n_blocks=5, | ||
outerdim=1000, | ||
innerdim=250, | ||
n_workers=4, | ||
use_process_workers=False, | ||
): | ||
self.n_blocks = n_blocks | ||
self.outerdim = outerdim | ||
self.innerdim = innerdim | ||
self.n_workers = n_workers | ||
if use_process_workers: | ||
self.pool_type = ProcessPoolExecutor | ||
else: | ||
self.pool_type = ThreadPoolExecutor | ||
self._setup_calc() | ||
|
||
def _setup_calc(self): | ||
self._pool = self.pool_type(self.n_workers) | ||
|
||
def perform(self): | ||
partial_results = self._pool.map( | ||
subtask_operation, | ||
[ | ||
(i_task + 1, self.outerdim, self.innerdim) | ||
for i_task in range(self.n_blocks) | ||
], | ||
) | ||
combined = np.stack(list(partial_results)) | ||
stephenworsley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = np.mean(combined, axis=0) | ||
result = result.min() | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
nb = 12 | ||
nw = 3 | ||
ny, nx = 1000, 200 | ||
dims = (ny, nx) | ||
use_processes = False | ||
typ = "process" if use_processes else "thread" | ||
msg = f"Starting: blocks={nb} workers={nw} size={dims} type={typ}" | ||
print(msg) | ||
calc = SampleParallelTask( | ||
n_blocks=nb, | ||
outerdim=ny, | ||
innerdim=nx, | ||
n_workers=nw, | ||
use_process_workers=use_processes, | ||
) | ||
debug("Created.") | ||
debug("Run..") | ||
result = calc.perform() | ||
debug("\n.. Run DONE.") | ||
debug(f"result = {result}") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you're applying this division twice, I think that's why adding blocks has such an exagerated effect on performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes I see !
Hopefully that may explain the peculiar behaviour of the timings too.
I will fix this and re-investigate ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
84ff4ad fixes, I think