Skip to content

Commit

Permalink
fixed macos segfault (due to stack overflow?)
Browse files Browse the repository at this point in the history
  • Loading branch information
voutcn committed Jul 13, 2019
1 parent 02475be commit 533a602
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions src/main_iterate.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
* MEGAHIT
* Copyright (C) 2014 - 2015 The University of Hong Kong & L3 Bioinformatics
* Limited
* Copyright (C) 2014 - 2015 The University of Hong Kong & L3 Bioinformatics Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,15 +18,18 @@

/* contact: Dinghua Li <dhli@cs.hku.hk> */

#include <assert.h>
#include <omp.h>
#include <cassert>
#include <stdio.h>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <list>
#include <functional>

#include "definitions.h"
#include "iterate/contig_flank_index.h"
Expand Down Expand Up @@ -59,10 +61,9 @@ static void ParseIterOptions(int argc, char *argv[]) {
desc.AddOption("num_cpu_threads", "t", opt.num_cpu_threads, "number of cpu threads, at least 2. 0 for auto detect.");
desc.AddOption("kmer_k", "k", opt.kmer_k, "(*) current kmer size.");
desc.AddOption("step", "s", opt.step,
"(*) step for iteration (<= 29). i.e. this iteration is from "
"kmer_k to (kmer_k + step)");
"(*) step for iteration (<= 28). i.e. this iteration is from kmer_k to (kmer_k + step)");
desc.AddOption("output_prefix", "o", opt.output_prefix,
"(*) output_prefix.edges.0 and output_prefix.rr.pb will be created.");
"(*) output_prefix.edges.0 will be created.");

try {
desc.Parse(argc, argv);
Expand Down Expand Up @@ -110,19 +111,18 @@ static bool ReadReadsAndProcessKernel(const Option &opt, const IndexType &index)
return false;
}
xinfo("Selected kmer type size for next k: {}\n", sizeof(KmerType));
BinaryReader binary_reader(opt.read_file);
AsyncSequenceReader reader(&binary_reader);
AsyncReadReader reader(opt.read_file);
KmerCollector<KmerType> collector(opt.kmer_k + opt.step + 1, opt.output_prefix);
int64_t num_aligned_reads = 0;
int64_t num_total_reads = 0;

while (true) {
const auto &read_pkg = reader.Next();
if (read_pkg.seq_count() == 0) {
if (read_pkg.SeqCount() == 0) {
break;
}
num_aligned_reads += index.FindNextKmersFromReads(read_pkg, &collector);
num_total_reads += read_pkg.seq_count();
num_total_reads += read_pkg.SeqCount();
xinfo("Processed: {}, aligned: {}. Iterative edges: {}\n", num_total_reads, num_aligned_reads,
collector.collection().size());
}
Expand Down Expand Up @@ -152,39 +152,56 @@ static void ReadContigsAndBuildIndex(const Option &opt, const std::string &file_
auto &pkg = reader.Next();
auto &contig_pkg = pkg.first;
auto &mul = pkg.second;
if (contig_pkg.seq_count() == 0) {
if (contig_pkg.SeqCount() == 0) {
break;
}
xinfo("Read {} contigs\n", contig_pkg.seq_count());
xinfo("Read {} contigs\n", contig_pkg.SeqCount());
index->FeedBatchContigs(contig_pkg, mul);
xinfo("Number of flank kmers: {}\n", index->size());
}
}

struct BaseRunner {
virtual void Run(const Option &opt) = 0;
virtual uint32_t max_k() const = 0;
};

template <class KmerType>
bool KmerTypeSelectAndRun(const Option &opt) {
if (KmerType::max_size() >= static_cast<unsigned>(opt.kmer_k + 1)) {
struct Runner : public BaseRunner {
void Run(const Option &opt) override {
xinfo("Selected kmer type size for k: {}\n", sizeof(KmerType));
ContigFlankIndex<KmerType> index(opt.kmer_k, opt.step);
ReadContigsAndBuildIndex(opt, opt.contig_file, &index);
ReadContigsAndBuildIndex(opt, opt.bubble_file, &index);
ReadReadsAndProcess(opt, index);
return true;
}
return false;
}
uint32_t max_k() const override {
return KmerType::max_size();
}
};


int main_iterate(int argc, char *argv[]) {
AutoMaxRssRecorder recorder;
ParseIterOptions(argc, argv);

if (KmerTypeSelectAndRun<Kmer<1, uint64_t>>(opt)) return 0;
if (KmerTypeSelectAndRun<Kmer<3, uint32_t>>(opt)) return 0;
if (KmerTypeSelectAndRun<Kmer<2, uint64_t>>(opt)) return 0;
if (KmerTypeSelectAndRun<Kmer<5, uint32_t>>(opt)) return 0;
if (KmerTypeSelectAndRun<Kmer<3, uint64_t>>(opt)) return 0;
if (KmerTypeSelectAndRun<Kmer<7, uint32_t>>(opt)) return 0;
if (KmerTypeSelectAndRun<Kmer<4, uint64_t>>(opt)) return 0;
if (KmerTypeSelectAndRun<Kmer<kUint32PerKmerMaxK, uint32_t>>(opt)) return 0;
std::list<std::unique_ptr<BaseRunner>> runners;
runners.emplace_back(new Runner<Kmer<1, uint64_t>>());
runners.emplace_back(new Runner<Kmer<3, uint32_t>>());
runners.emplace_back(new Runner<Kmer<2, uint64_t>>());
runners.emplace_back(new Runner<Kmer<5, uint32_t>>());
runners.emplace_back(new Runner<Kmer<3, uint64_t>>());
runners.emplace_back(new Runner<Kmer<7, uint32_t>>());
runners.emplace_back(new Runner<Kmer<4, uint64_t>>());
runners.emplace_back(new Runner<Kmer<kUint32PerKmerMaxK, uint32_t>>());

for (auto &runner: runners) {
if (runner->max_k() >= static_cast<uint32_t>(opt.kmer_k + 1)) {
runner->Run(opt);
return 0;
}
}

xfatal("k is too large!\n");
return 1;
}

0 comments on commit 533a602

Please sign in to comment.