cuDDL
Loading...
Searching...
No Matches
query_sketch.cuh
Go to the documentation of this file.
1#pragma once
2
3#include <cuda/buffer>
4#include <cuda/std/cstdint>
5#include <cuda/stream>
6
7#include <filesystem>
8#include <span>
9#include <utility>
10#include <vector>
11
12#include <cuddl/batch.cuh>
15#include <cuddl/device_span.cuh>
16#include <cuddl/error.hpp>
19
20namespace cuddl {
21
35template <uint32_t K, size_t BucketCount, typename Layout = default_register_layout>
37 public:
38 using score_type = uint16_t;
39
42
43 // Explicit host bodies prevent NVCC from inferring device-side buffer operations.
44 __host__ ~query_sketch_batch() {} // NOLINT(modernize-use-equals-default)
45
47 : scores_(std::move(other.scores_)),
48 query_count_(std::exchange(other.query_count_, 0U)),
49 compatibility_(other.compatibility_) {}
50
53 if (this != &other) {
54 scores_ = std::move(other.scores_);
55 query_count_ = std::exchange(other.query_count_, 0U);
56 compatibility_ = other.compatibility_;
57 }
58 return *this;
59 }
60
68 [[nodiscard]] static Result<query_sketch_batch> sketch(
69 std::span<std::filesystem::path const> paths,
70 cuda::stream_ref stream,
71 path_build_options options = {}
72 ) try {
73 return [&]() -> Result<query_sketch_batch> {
74 query_sketch_batch batch(stream, static_cast<uint32_t>(paths.size()));
75 CUDDL_TRY(batch.set_blacklist(options.blacklist, stream));
76 CUDDL_TRY((detail::stage_paths<K, BucketCount, Layout>(
77 paths,
78 stream,
79 options.staging_bytes,
80 options.parser_workers,
81 options.transfer,
82 options.decompression,
83 options.direct_reads,
84 options.statistics,
85 batch.reduce_rows(stream),
86 options.blacklist ? options.blacklist->get().view() : cuda::std::nullopt
87 )));
88 return batch;
89 }();
90 } catch (cuda::cuda_error const& error) {
91 return Err(Error::cuda(static_cast<cudaError_t>(error.status())));
92 } catch (std::system_error const& error) {
93 return Err(Error::resource(error.what()));
94 } catch (std::bad_alloc const& error) {
95 return Err(Error::resource(error.what()));
96 }
97
104 std::span<sequence_genome const> genomes,
105 cuda::stream_ref stream,
106 sequence_build_options options = {}
107 ) try {
108 return [&]() -> Result<query_sketch_batch> {
109 query_sketch_batch batch(stream, static_cast<uint32_t>(genomes.size()));
110 CUDDL_TRY(batch.set_blacklist(options.blacklist, stream));
111 CUDDL_TRY((detail::stage_sequences<K, BucketCount, Layout>(
112 genomes,
113 stream,
114 options.staging_bytes,
115 options.statistics,
116 batch.reduce_rows(stream),
117 options.blacklist ? options.blacklist->get().view() : cuda::std::nullopt
118 )));
119 return batch;
120 }();
121 } catch (cuda::cuda_error const& error) {
122 return Err(Error::cuda(static_cast<cudaError_t>(error.status())));
123 } catch (std::system_error const& error) {
124 return Err(Error::resource(error.what()));
125 } catch (std::bad_alloc const& error) {
126 return Err(Error::resource(error.what()));
127 }
128
130 [[nodiscard]] device_span<score_type const> scores() const noexcept {
131 return {scores_.data(), static_cast<size_t>(query_count_) * BucketCount};
132 }
133
135 [[nodiscard]] uint32_t query_count() const noexcept {
136 return query_count_;
137 }
138
140 [[nodiscard]] score_compatibility compatibility() const noexcept {
141 return compatibility_;
142 }
143
144 private:
145 query_sketch_batch(cuda::stream_ref stream, uint32_t queries)
146 : scores_(
147 cuda::make_device_buffer<score_type>(
148 stream,
149 stream.device(),
150 static_cast<size_t>(queries) * BucketCount,
151 cuda::no_init
152 )
153 ),
154 query_count_(queries) {}
155
158 [[nodiscard]] auto reduce_rows(cuda::stream_ref stream) {
159 return [this, stream](
160 device_span<uint32_t const> group, size_t base, size_t count
161 ) -> Result<void> {
162 return extract_scores_batch_async<BucketCount>(
163 group,
164 device_span<score_type>{scores_.data() + base * BucketCount, count * BucketCount},
165 stream
166 );
167 };
168 }
169
170 cuda::device_buffer<score_type> scores_;
171 uint32_t query_count_ = 0;
172 score_compatibility compatibility_ = score_compatibility::current<K, BucketCount, Layout>();
173
174 Result<void> set_blacklist(
175 std::optional<std::reference_wrapper<device_blacklist const>> filter,
176 cuda::stream_ref stream
177 ) {
178 if (!filter) return Ok();
179 CUDDL_TRY(filter->get().validate(K, BucketCount, stream));
180 compatibility_.blacklist_identity = filter->get().source().identity();
181 compatibility_.blacklist_version = filter->get().source().version();
182 return Ok();
183 }
184};
185
186} // namespace cuddl
Query sketches held on the device, in the shape a search consumes them.
static Result< query_sketch_batch > sketch(std::span< std::filesystem::path const > paths, cuda::stream_ref stream, path_build_options options={})
Sketches one query per plain or gzip/BGZF FASTA/FASTQ file on the GPU.
device_span< score_type const > scores() const noexcept
Query rows in query-ID order, BucketCount scores each.
query_sketch_batch & operator=(query_sketch_batch &&other) noexcept
Move-assigns the batch, leaving the source empty.
score_compatibility compatibility() const noexcept
Compatibility a search must be given alongside scores.
query_sketch_batch & operator=(query_sketch_batch const &)=delete
uint32_t query_count() const noexcept
Query count, in the order the paths or genomes were supplied.
static Result< query_sketch_batch > sketch_from_sequences(std::span< sequence_genome const > genomes, cuda::stream_ref stream, sequence_build_options options={})
Sketches one query per genome of bases the caller already holds.
query_sketch_batch(query_sketch_batch &&other) noexcept
query_sketch_batch(query_sketch_batch const &)=delete
#define CUDDL_TRY(expr)
Propagates a cuddl::Result failure from the enclosing function (GNU statement expression).
Definition error.hpp:225
Batch operations over contiguous device rows.
Definition a48.hpp:14
Result< void > Ok() noexcept
Success return for Result<void>; same as return {}.
Definition error.hpp:181
cuda::std::unexpected< Error > Err(Error error)
Failure return; converts to any Result<T> via cuda::std::unexpected.
Definition error.hpp:176
static Error cuda(cudaError_t code, std::source_location location=std::source_location::current())
Definition error.hpp:90
static Error resource(std::string message)
Definition error.hpp:103
Fallible API result: cuda::std::expected<T, Error> with cuDDL factories.
Definition error.hpp:118
A span that is assumed to point to device-accessible memory.
Knobs for a build the caller feeds with FASTA/FASTQ paths.
Construction parameters of compatible score rows.
Knobs for a build the caller feeds with bases it already holds.