1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------
#include <benchmark/benchmark.h>
#include <seqan3/alphabet/nucleotide/dna4.hpp>
#include <seqan3/range/views/minimiser_hash.hpp>
#include <seqan3/test/performance/naive_minimiser_hash.hpp>
#include <seqan3/test/performance/sequence_generator.hpp>
#include <seqan3/test/performance/units.hpp>
#ifdef SEQAN3_HAS_SEQAN2
#include <seqan/index.h>
#include <seqan3/test/performance/seqan2_minimiser.h>
#endif // SEQAN3_HAS_SEQAN2
inline benchmark::Counter bp_per_second(size_t const basepairs)
{
return benchmark::Counter(basepairs,
benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
}
inline seqan3::shape make_gapped_shape(size_t const k)
{
seqan3::shape shape{};
for (size_t i{0}; i < k - 1; ++i)
shape.push_back((i + 1) % 2);
shape.push_back(1u);
shape.push_back(0u);
return shape;
}
static void arguments(benchmark::internal::Benchmark* b)
{
for (int32_t sequence_length : {50'000, /*1'000'000*/})
{
for (int32_t k : {8, /*16, 24,*/ 30})
{
for (int32_t w : {k + 5, k + 20})
{
b->Args({sequence_length, k, w});
}
}
}
}
enum class method_tag
{
seqan3_ungapped,
seqan3_gapped,
naive,
seqan2_ungapped,
seqan2_gapped
};
#ifdef SEQAN3_HAS_SEQAN2
inline auto make_gapped_shape_seqan2(size_t const k)
{
seqan::String<char> bitmap;
for (size_t i{0}; i < k - 1; ++i)
seqan::append(bitmap, seqan::CharString(std::to_string((i + 1) % 2)));
seqan::append(bitmap, seqan::CharString("1"));
return seqan::Shape<seqan::Dna, seqan::GenericShape>(bitmap);
}
#endif // SEQAN3_HAS_SEQAN2
template <method_tag tag>
void compute_minimisers(benchmark::State & state)
{
auto sequence_length = state.range(0);
size_t k = static_cast<size_t>(state.range(1));
uint32_t w = static_cast<size_t>(state.range(2));
assert(sequence_length > 0);
assert(k > 0);
assert(w > k);
auto seq = seqan3::test::generate_sequence<seqan3::dna4>(sequence_length, 0, 0);
size_t sum{0};
for (auto _ : state)
{
if constexpr (tag == method_tag::naive)
{
for (auto h : seq | seqan3::views::naive_minimiser_hash(seqan3::ungapped{static_cast<uint8_t>(k)}, w))
benchmark::DoNotOptimize(sum += h);
}
else if constexpr (tag == method_tag::seqan3_ungapped)
{
for (auto h : seq | seqan3::views::minimiser_hash(seqan3::ungapped{static_cast<uint8_t>(k)}, seqan3::window_size{w}))
benchmark::DoNotOptimize(sum += h);
}
else if constexpr (tag == method_tag::seqan3_gapped)
{
for (auto h : seq | seqan3::views::minimiser_hash(make_gapped_shape(k), seqan3::window_size{w}))
benchmark::DoNotOptimize(sum += h);
}
#ifdef SEQAN3_HAS_SEQAN2
else
{
auto seqan2_seq = seqan3::test::generate_sequence_seqan2<seqan::Dna>(sequence_length, 0, 0);
using shape_t = std::conditional_t<tag == method_tag::seqan2_ungapped,
seqan::Shape<seqan::Dna, seqan::SimpleShape>,
seqan::Shape<seqan::Dna, seqan::GenericShape>>;
shape_t shape;
if constexpr (tag == method_tag::seqan2_ungapped)
seqan::resize(shape, k);
else
shape = make_gapped_shape_seqan2(k);
minimiser<decltype(shape)> seqan_minimiser(window{w}, kmer{k}, shape);
seqan_minimiser.compute(seqan2_seq);
for (auto h : seqan_minimiser.minimiser_hash)
benchmark::DoNotOptimize(sum += h);
}
#endif // SEQAN3_HAS_SEQAN2
}
state.counters["Throughput[bp/s]"] = bp_per_second(sequence_length - k + 1);
}
template <method_tag tag>
void compute_minimisers_on_poly_A_sequence(benchmark::State & state)
{
auto sequence_length = state.range(0);
size_t k = static_cast<size_t>(state.range(1));
uint32_t w = static_cast<size_t>(state.range(2));
assert(sequence_length > 0);
assert(k > 0);
assert(w > k);
auto seq = std::vector<seqan3::dna4>(sequence_length);
size_t sum{0};
for (auto _ : state)
{
if constexpr (tag == method_tag::seqan3_ungapped)
{
for (auto h : seq | seqan3::views::minimiser_hash(seqan3::ungapped{static_cast<uint8_t>(k)}, seqan3::window_size{w}))
benchmark::DoNotOptimize(sum += h);
}
else if constexpr (tag == method_tag::seqan3_gapped)
{
for (auto h : seq | seqan3::views::minimiser_hash(make_gapped_shape(k), seqan3::window_size{w}))
benchmark::DoNotOptimize(sum += h);
}
}
state.counters["Throughput[bp/s]"] = bp_per_second(sequence_length - k + 1);
}
#ifdef SEQAN3_HAS_SEQAN2
BENCHMARK_TEMPLATE(compute_minimisers, method_tag::seqan2_ungapped)->Apply(arguments);
BENCHMARK_TEMPLATE(compute_minimisers, method_tag::seqan2_gapped)->Apply(arguments);
#endif // SEQAN3_HAS_SEQAN2
BENCHMARK_TEMPLATE(compute_minimisers, method_tag::naive)->Apply(arguments);
BENCHMARK_TEMPLATE(compute_minimisers, method_tag::seqan3_ungapped)->Apply(arguments);
BENCHMARK_TEMPLATE(compute_minimisers, method_tag::seqan3_gapped)->Apply(arguments);
BENCHMARK_TEMPLATE(compute_minimisers_on_poly_A_sequence, method_tag::seqan3_ungapped)->Apply(arguments);
BENCHMARK_TEMPLATE(compute_minimisers_on_poly_A_sequence, method_tag::seqan3_gapped)->Apply(arguments);
BENCHMARK_MAIN();
|