Skip to content

Optimize chunk sizes - #44

Merged
sappelhoff merged 3 commits into
sappelhoff:masterfrom
a-hurst:optimized_chunk_size
Dec 4, 2020
Merged

sappelhoff merged 3 commits into
sappelhoff:masterfrom
a-hurst:optimized_chunk_size

Conversation

@a-hurst

@a-hurst a-hurst commented Dec 2, 2020

Copy link
Copy Markdown
Collaborator

PR Description

On my computer running PyPREP typically slows everything down to a crawl, even causing occasional brief screen blackouts as macOS tries to cope with all its RAM being consumed. Obviously this expected to some extent, but I noticed that it will take as much RAM as possible even when not necessary: e.g. the chunk size being set to 57 for a 64-channel dataset when 32 would have produced the same number of chunks with less RAM consumption.

To address this, I've written a bit of code that figures out the smallest possible chunk size for each possible number of chunks for a file. e.g. for 32 channels, we get:

chunk_sizes = [1, 2, 3, 4, 5, 6, 7, 8, 11, 16, 32]
chunk_counts = [32, 16, 11, 8, 7, 6, 5, 4, 3, 2, 1]

When the largest chunk size is too large, it falls back to the next-largest chunk size in the list, so 32 (1 chunk) -> 16 (2 chunks) -> 11 (3 chunks) -> 8 (4 chunks) etc.

In addition to freeing up some system RAM, this should also reduce the likelihood of an overzealous OS killing off the Python process mid-PREP because it's consumed too large a chunk of RAM (has happened to me twice on an 8GB MBP running Catalina, but never on the 24GB iMac running High Sierra or 16GB Hackintosh running Mojave that I've tested PyPREP extensively with)

Merge Checklist

To merge your PR we need to first take the following points into account:

  • the PR has been reviewed and all comments are resolved
  • all CI checks pass
  • (if applicable): the PR description includes the phrase closes #<issue-number> to automatically close an issue
  • (if applicable): bug fixes, new features, or API changes are documented in whats_new.rst

@sappelhoff
sappelhoff requested a review from yjmantilla December 2, 2020 18:21
@yjmantilla

yjmantilla commented Dec 2, 2020 •

Copy link
Copy Markdown
Collaborator

I think this is a better approach of the splitting than mine. Guess the only thing missing is to mention it in the changelog.

Originally I had

                chunk_size = chunk_size - 1  # // 2

When I did the PR I compared times and concluded that doing as much channels as possible is indeed considerably faster than splitting each time the chunk_size by 2. So I said "lets do it the fastest way" , and decided to decrease the chunk size by --1 instead of //2 and just commented that (though that comment wasn't really expressive).

but I noticed that it will take as much RAM as possible even when not necessary:

It was because I wanted the fastest one 😆 , guess it was too much of a greedy algorithm 😅 . I mean the necessary thing was to be fast.

Comparing your method and the naive --1 , yours gives a more stable ram consumption. I guess at some points it could make pyprep slower but since it tries to find the fastest method with chunks of roughly the same size I think this is a nice equilibrium point between being fast and using ram in an stable way.

So I guess we are establishing ( in the context of your example):

  • the faster [57,7] chunks is worst than the bit slower but ram-stable [32,32] chunks.

So if everyone agrees on that statement I say that we can merge this.

Now, just another remark regarding ram compsuntion. Should this line in run_ransac :

available_gb = virtual_memory().available * 1e-9

Stay like that or be instead a percentage of the ram available (like 70% for example). I guess that if the user is processing and leaving the computer alone is better for it to be 100% but if it is doing something at the same time it would be better a percentage...

What do you think?

@yjmantilla yjmantilla left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Only thing missing is to include it in the changelog. I mean I think @sappelhoff would ask me to do that xD.

@codecov-io

codecov-io commented Dec 3, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #44 (69e64d2) into master (afb5167) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #44      +/-   ##
==========================================
+ Coverage   97.02%   97.08%   +0.05%     
==========================================
  Files           5        5              
  Lines         505      514       +9     
==========================================
+ Hits          490      499       +9     
  Misses         15       15              
Impacted Files Coverage Δ
pyprep/find_noisy_channels.py 97.26% <100.00%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update afb5167...69e64d2. Read the comment docs.

@a-hurst

a-hurst commented Dec 3, 2020

Copy link
Copy Markdown
Collaborator Author

I've updated the whats_new and also added a weak implementation of your suggestion: I've made it so it adjusts the needed_gb amount, such that the available_gb is treated as being 5% smaller than it really is. I'm adjusting the needed_gb amount here instead of available_gb directly because otherwise the MemoryError exception text would be confusing.

I guess we can tune the exact proportion of free memory to use, but I think 95% is a good starting place from a stability vs performance perspective (i.e. we aren't losing much). Does that sound good to you?

Comment thread pyprep/find_noisy_channels.py Resolved

@sappelhoff sappelhoff left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM apart from one comment I made!

@sappelhoff
sappelhoff merged commit 89a0550 into sappelhoff:master Dec 4, 2020
@sappelhoff

Copy link
Copy Markdown
Owner

Thanks for your first PR to pyprep @a-hurst! may there be many more 🎉

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.

4 participants