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

Taxonomy segfault fix #952

Merged
merged 2 commits into from
Feb 21, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move random number generation w/in get_best_genus to C++11 <random> l…
…ibrary

The new formulation should be threadsafe, and address the
assignTaxonomy segfault issue #916

The downside is that the new C++ random number generation is not seeded
from R, so the tie-breaking between multiple best matches will not be
completely reproducible by fixing the random number seed in R.

However, this will have a tiny-to-zero effect on most output, and is
far preferable to segafulting.
  • Loading branch information
benjjneb committed Feb 19, 2020
commit 997389b78f7f55116b4ba43ed0bfd3b569149aa0
8 changes: 7 additions & 1 deletion src/taxonomy.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "dada.h"
#include <Rcpp.h>
#include <RcppParallel.h>
#include <random>

using namespace Rcpp;

// Gets kmer index
Expand Down Expand Up @@ -70,6 +72,9 @@ int get_best_genus(int *karray, double *out_logp, unsigned int arraylen, unsigne
double p, logp, max_logp = 1.0; // Init value to be replaced on first iteration
double rv; // Dummy random variable
unsigned int nmax=0; // Number of times the current max logp has been seen
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> cunif(0.0, 1.0);

for(g=0;g<ngenus;g++) {
genus_kv = &genus_kmers[g*n_kmers];
Expand Down Expand Up @@ -97,7 +102,8 @@ int get_best_genus(int *karray, double *out_logp, unsigned int arraylen, unsigne
nmax=1;
} else if (max_logp == logp) { // With uniform prob, store if equal to current max
nmax++;
rv = (double) Rcpp::runif(1)[0];
rv = (double) cunif(gen);
///! rv = (double) Rcpp::runif(1)[0];
if(rv < 1.0/nmax) {
max_g = g;
}
Expand Down