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

Warn when running out of memory instead of segfaulting #251

Merged
merged 1 commit into from
Oct 3, 2016

Conversation

sambrightman
Copy link
Collaborator

  • continue with the currently allocated memory
  • warn continuously (maybe not ideal?)

@sambrightman
Copy link
Collaborator Author

In my testing this produces the same sort result, although in the low memory case it slightly differs for reads that compare equally (same chromosome, position and strand direction). Obviously this is due to them being split up and merged differently. Wonder if there is a way to make "equal" reads sort stably in this situation (fall-back to name?).

@sambrightman
Copy link
Collaborator Author

It's probably worth adding a test in the realloc slightly further down as well. I didn't know what was appropriate there - would it make sense to throw new OutOfMemoryError?

@lomereiter
Copy link
Contributor

Hi, thanks for looking into this.

Sorting is unstable, and that's fine in my opinion, although it could mean minor reproducibility concerns. Fallbacks raise the question where to stop. (E.g. Picard includes flags and even some tags into comparison.)

Continuous warnings can be avoided by introducing a flag, so that if it's set in case of failure, max_sz is decreased in clear() and read_storage is reallocated.
In the use of realloc further down it might help to do the same (since there no reads are yet copied). If nothing helps, I agree it makes sense to throw an exception.

@sambrightman
Copy link
Collaborator Author

I think for coordinate sort Picard code is here. Reproducibility is useful for regression testing but a separate concern from this PR, so maybe take it up elsewhere.

I think in the code further down there has to be an immediate exception because trying to grow read_storage already. I also notice that it doesn't grow max_sz, so the storage is underused from that point on. I'll edit and re-push under those assumptions.

* continue with the currently allocated memory
* warn continuously (maybe not ideal?)
}

void clear() {
_used = 0;
_n_reads = 0;
if (_low_memory) {
auto realloc_storage = cast(ubyte*)std.c.stdlib.realloc(read_storage, max_sz / 2);
if (realloc_storage !is null) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Technically, this realloc can also fail (could reallocate via allocate/copy/free). I choose not to throw an exception here, but to keep printing errors. This is a situation in which we can proceed, but sub-optimally, and cannot automatically do anything to improve things. Spamming the user probably highlights the problem but results should still be correct.

read_storage = cast(ubyte*)std.c.stdlib.realloc(read_storage, len);
auto realloc_storage = cast(ubyte*)std.c.stdlib.realloc(read_storage, len);
if (realloc_storage is null) {
throw new Exception("realloc failed: not enough memory for read");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we are already trying to grow the storage, think there's nothing we can do here if it fails, so throw.

throw new Exception("realloc failed: not enough memory for read");
} else {
read_storage = realloc_storage;
max_sz = len;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

max_sz was not being updated previously, so the storage would grow but not get used past max_sz in future fills.

Copy link
Collaborator Author

@sambrightman sambrightman left a comment

Choose a reason for hiding this comment

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

As discussed in comments.

@lomereiter
Copy link
Contributor

Thanks, looks good to me.

@lomereiter lomereiter merged commit 7d2fb88 into biod:master Oct 3, 2016
@sambrightman sambrightman deleted the sortsegfault branch November 18, 2016 15:25
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.

2 participants