cuSBF
Loading...
Searching...
No Matches
fastx_pipeline_state.cuh
Go to the documentation of this file.
1#pragma once
2
3#include <cuda_runtime.h>
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8#include <span>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
14#include <thrust/device_vector.h>
15
18#include <cusbf/device_span.cuh>
19#include <cusbf/error.hpp>
21namespace cusbf::detail {
22
24 public:
25 FastxPipelineState() = default;
31
33 return d_sequence_;
34 }
35
36 [[nodiscard]] const thrust::device_vector<char>& sequence_device() const noexcept {
37 return d_sequence_;
38 }
39
40 [[nodiscard]] thrust::device_vector<uint64_t>& dense_packed_words_device() noexcept {
41 return d_dense_packed_words_;
42 }
43
44 [[nodiscard]] const thrust::device_vector<uint64_t>&
46 return d_dense_packed_words_;
47 }
48
49 [[nodiscard]] thrust::device_vector<uint8_t>& result_buffer_device() noexcept {
50 return d_resultBuffer_;
51 }
52
53 [[nodiscard]] const thrust::device_vector<uint8_t>& result_buffer_device() const noexcept {
54 return d_resultBuffer_;
55 }
56
57 [[nodiscard]] thrust::device_vector<QueryLayoutRecord>& query_layout_records_device() noexcept {
58 return d_query_layout_records_;
59 }
60
61 [[nodiscard]] const thrust::device_vector<QueryLayoutRecord>&
63 return d_query_layout_records_;
64 }
65
66 [[nodiscard]] thrust::device_vector<uint64_t>& record_positive_kmers_device() noexcept {
67 return d_record_positive_kmers_;
68 }
69
70 [[nodiscard]] const thrust::device_vector<uint64_t>&
72 return d_record_positive_kmers_;
73 }
74
76 return normalized_sequence_pings_[ping & 1U];
77 }
78
80 size_t ping
81 ) const noexcept {
82 return normalized_sequence_pings_[ping & 1U];
83 }
84
85 [[nodiscard]] std::vector<NormalizedRecord>& normalized_records_ping(size_t ping) noexcept {
86 return normalized_records_pings_[ping & 1U];
87 }
88
89 [[nodiscard]] const std::vector<NormalizedRecord>& normalized_records_ping(
90 size_t ping
91 ) const noexcept {
92 return normalized_records_pings_[ping & 1U];
93 }
94
96 return normalized_sequence_scratch_;
97 }
98
100 return normalized_sequence_scratch_;
101 }
102
103 [[nodiscard]] std::vector<NormalizedRecord>& normalized_records_scratch() noexcept {
104 return normalized_records_scratch_;
105 }
106
107 [[nodiscard]] const std::vector<NormalizedRecord>& normalized_records_scratch() const noexcept {
108 return normalized_records_scratch_;
109 }
110
111 [[nodiscard]] std::vector<uint64_t>& record_positive_kmers_scratch() noexcept {
112 return record_positive_kmers_scratch_;
113 }
114
115 [[nodiscard]] const std::vector<uint64_t>& record_positive_kmers_scratch() const noexcept {
116 return record_positive_kmers_scratch_;
117 }
118
119 [[nodiscard]] std::vector<uint8_t>& result_hits_scratch() noexcept {
120 return result_hits_scratch_;
121 }
122
123 [[nodiscard]] const std::vector<uint8_t>& result_hits_scratch() const noexcept {
124 return result_hits_scratch_;
125 }
126
128 if (bases > d_sequence_.size()) {
129 d_sequence_.resize(bases);
130 }
131 }
132
134 if (words > d_dense_packed_words_.size()) {
135 d_dense_packed_words_.resize(words);
136 }
137 }
138
140 if (kmers > d_resultBuffer_.size()) {
141 d_resultBuffer_.resize(kmers);
142 }
143 }
144
146 stage_sequence(std::span<const char> sequence, cuda::stream_ref stream) {
147 ensure_sequence_capacity(sequence.size());
149 thrust::raw_pointer_cast(d_sequence_.data()),
150 sequence.data(),
151 sequence.size_bytes(),
153 stream.get()
154 ));
155 return {};
156 }
157
159 staged_sequence_view(std::span<const char> sequence, cuda::stream_ref stream) {
160 CUSBF_TRY(stage_sequence(sequence, stream));
162 thrust::raw_pointer_cast(d_sequence_.data()), sequence.size()
163 };
164 }
165
167 stage_dense_packed(std::span<const uint64_t> words, cuda::stream_ref stream) {
168 ensure_dense_packed_capacity(words.size());
170 thrust::raw_pointer_cast(d_dense_packed_words_.data()),
171 words.data(),
172 words.size_bytes(),
174 stream.get()
175 ));
176 return {};
177 }
178
180 staged_dense_packed_view(std::span<const uint64_t> words, cuda::stream_ref stream) {
183 thrust::raw_pointer_cast(d_dense_packed_words_.data()), words.size()
184 };
185 }
186
188 stage_sequence_ping(size_t ping, std::string_view sequence, cuda::stream_ref stream) {
189 auto& buffer = d_sequence_pings_[ping & 1U];
190 if (sequence.size() > buffer.size()) {
191 buffer.resize(sequence.size());
192 }
194 thrust::raw_pointer_cast(buffer.data()),
195 sequence.data(),
196 sequence.size(),
198 stream.get()
199 ));
200 return device_span<const char>{thrust::raw_pointer_cast(buffer.data()), sequence.size()};
201 }
202
204 for (FastxPinnedSequenceBuffer& sequence : normalized_sequence_pings_) {
205 sequence.release();
206 }
207 for (std::vector<NormalizedRecord>& records : normalized_records_pings_) {
208 std::vector<NormalizedRecord>{}.swap(records);
209 }
210 }
211
213 std::string{}.swap(normalized_sequence_scratch_);
214 std::vector<NormalizedRecord>{}.swap(normalized_records_scratch_);
215 std::vector<uint64_t>{}.swap(record_positive_kmers_scratch_);
216 std::vector<uint8_t>{}.swap(result_hits_scratch_);
217 }
218
220 thrust::device_vector<char>{}.swap(d_sequence_);
221 thrust::device_vector<uint64_t>{}.swap(d_dense_packed_words_);
222 for (thrust::device_vector<char>& buffer : d_sequence_pings_) {
223 thrust::device_vector<char>{}.swap(buffer);
224 }
225 thrust::device_vector<uint8_t>{}.swap(d_resultBuffer_);
226 thrust::device_vector<QueryLayoutRecord>{}.swap(d_query_layout_records_);
227 thrust::device_vector<uint64_t>{}.swap(d_record_positive_kmers_);
228 }
229
235
239
243
244 void shrink_all() {
245 release_all();
246 }
247
248 private:
249 thrust::device_vector<char> d_sequence_;
250 thrust::device_vector<uint64_t> d_dense_packed_words_;
251 std::array<thrust::device_vector<char>, 2> d_sequence_pings_;
252 thrust::device_vector<uint8_t> d_resultBuffer_;
253 thrust::device_vector<QueryLayoutRecord> d_query_layout_records_;
254 thrust::device_vector<uint64_t> d_record_positive_kmers_;
255 std::array<FastxPinnedSequenceBuffer, 2> normalized_sequence_pings_;
256 std::array<std::vector<NormalizedRecord>, 2> normalized_records_pings_;
257 std::string normalized_sequence_scratch_;
258 std::vector<NormalizedRecord> normalized_records_scratch_;
259 std::vector<uint64_t> record_positive_kmers_scratch_;
260 std::vector<uint8_t> result_hits_scratch_;
261};
262
263} // namespace cusbf::detail
Page-locked host buffer for fused FASTX normalize + H2D staging.
std::vector< NormalizedRecord > & normalized_records_ping(size_t ping) noexcept
std::vector< uint64_t > & record_positive_kmers_scratch() noexcept
std::vector< uint8_t > & result_hits_scratch() noexcept
Result< device_span< const uint64_t > > staged_dense_packed_view(std::span< const uint64_t > words, cuda::stream_ref stream)
FastxPipelineState & operator=(const FastxPipelineState &)=delete
thrust::device_vector< QueryLayoutRecord > & query_layout_records_device() noexcept
const FastxPinnedSequenceBuffer & normalized_sequence_ping(size_t ping) const noexcept
Result< device_span< const char > > staged_sequence_view(std::span< const char > sequence, cuda::stream_ref stream)
const std::vector< uint8_t > & result_hits_scratch() const noexcept
const std::vector< NormalizedRecord > & normalized_records_scratch() const noexcept
const thrust::device_vector< uint64_t > & dense_packed_words_device() const noexcept
thrust::device_vector< char > & sequence_device() noexcept
const std::vector< NormalizedRecord > & normalized_records_ping(size_t ping) const noexcept
const thrust::device_vector< uint64_t > & record_positive_kmers_device() const noexcept
FastxPipelineState(const FastxPipelineState &)=delete
Result< void > stage_sequence(std::span< const char > sequence, cuda::stream_ref stream)
const std::string & normalized_sequence_scratch() const noexcept
thrust::device_vector< uint64_t > & dense_packed_words_device() noexcept
const thrust::device_vector< char > & sequence_device() const noexcept
const thrust::device_vector< QueryLayoutRecord > & query_layout_records_device() const noexcept
const std::vector< uint64_t > & record_positive_kmers_scratch() const noexcept
FastxPinnedSequenceBuffer & normalized_sequence_ping(size_t ping) noexcept
std::vector< NormalizedRecord > & normalized_records_scratch() noexcept
FastxPipelineState(FastxPipelineState &&) noexcept=default
thrust::device_vector< uint8_t > & result_buffer_device() noexcept
std::string & normalized_sequence_scratch() noexcept
Result< void > stage_dense_packed(std::span< const uint64_t > words, cuda::stream_ref stream)
thrust::device_vector< uint64_t > & record_positive_kmers_device() noexcept
const thrust::device_vector< uint8_t > & result_buffer_device() const noexcept
Result< device_span< const char > > stage_sequence_ping(size_t ping, std::string_view sequence, cuda::stream_ref stream)
#define CUSBF_TRY(expr)
Propagates a cusbf::Result failure from the enclosing function (GNU statement expression).
Definition error.hpp:246
#define CUSBF_CUDA_TRY(expr)
Propagates a CUDA error wrapped in cusbf::Result<void>.
Definition error.hpp:269
consteval bool separatorPositionAlwaysEncodesInvalid(char *input, uint64_t separatorPosition, uint64_t index)
Recursively tests whether placing the separator byte at any position in an input of valid bytes alway...
Definition Alphabet.cuh:37
Fallible API result: cuda::std::expected<T, Error> with cuSBF factories.
Definition error.hpp:152
A span that is assumed to point to device-accessible memory.