cuSBF
Loading...
Searching...
No Matches
fastx_chunk.cuh
Go to the documentation of this file.
1#pragma once
2
3#include <cuda_runtime.h>
4
5#include <cstddef>
6#include <cstdint>
8#include <filesystem>
9#include <format>
10#include <stdexcept>
11#include <string_view>
12
13#include <cusbf/config.cuh>
15#include <cusbf/error.hpp>
16
17#if defined(__linux__)
18 #include <sys/stat.h>
19#endif
20
21namespace cusbf::detail {
22
25
29 size_t free_bytes{};
30};
31
34 size_t free_bytes = 0;
35 size_t total_bytes = 0;
36 const cudaError_t error = cudaMemGetInfo(&free_bytes, &total_bytes);
37 if (error != cudaSuccess) {
38 return Err(Error::io(std::format("cudaMemGetInfo failed: {}", cudaGetErrorString(error))));
39 }
40 return cuda_free_memory{free_bytes};
41}
42
45 return 64u << 20;
46}
47
48[[nodiscard]] inline uint64_t fastx_file_bytes(const std::filesystem::path& path) {
49#if defined(__linux__)
50 const std::string path_string = path.string();
51 struct stat file_status{};
52 if (::stat(path_string.c_str(), &file_status) != 0 || file_status.st_size < 0) {
53 return 0;
54 }
55 return static_cast<uint64_t>(file_status.st_size);
56#else
57 (void)path;
58 return 0;
59#endif
60}
61
62template <typename Config>
66
67template <typename Config>
71
73template <typename Config>
74[[nodiscard]] constexpr uint64_t
78
80template <typename Config>
81[[nodiscard]] constexpr uint64_t
85
87template <typename Config>
88[[nodiscard]] constexpr uint64_t
93
95template <typename Config>
96[[nodiscard]] constexpr size_t
97fastx_staging_budget_bytes(double fill_fraction, size_t free_bytes) noexcept {
98 if (fill_fraction <= 0.0) {
99 return 0;
100 }
101
102 const size_t available =
103 free_bytes > fastx_chunk_slack_bytes() ? free_bytes - fastx_chunk_slack_bytes() : size_t{0};
104 const double budget = static_cast<double>(available) * fill_fraction;
105 return budget <= 0.0 ? size_t{0} : static_cast<size_t>(budget);
106}
107
109template <typename Config>
128
130[[nodiscard]] constexpr size_t
132 if (staging_budget_bytes == 0) {
133 return 0;
134 }
136 return staging_budget_bytes / 2;
137 }
138 return staging_budget_bytes / 3;
139}
140
142template <typename Config>
155
157template <typename Config>
159 const std::filesystem::path& path,
161 double fill_fraction
162) {
164 if (file_bytes == 0) {
165 return true;
166 }
167
168 const auto gpu_memory = query_cuda_free_memory();
169 if (!gpu_memory) {
170 return false;
171 }
172 const size_t staging_budget_bytes =
175}
176
178template <typename Config>
181 double fill_fraction,
184 std::string_view source_name
185) {
186 const auto gpu_memory = query_cuda_free_memory();
187 if (!gpu_memory) {
188 return Err(gpu_memory.error());
189 }
190 const size_t staging_budget_bytes =
194 )) {
195 return {};
196 }
197
198 return Err(
200 std::format(
201 "{}: FASTX input requires more GPU memory than available at fill_fraction={} "
202 "(free staging budget {} bytes)",
206 )
207 )
208 );
209}
210
211} // namespace cusbf::detail
constexpr uint64_t estimate_query_staging_bytes(uint64_t raw_bytes, uint64_t record_count) noexcept
Peak device bytes for query staging (d_sequence_ + d_resultBuffer_).
Result< cuda_free_memory > query_cuda_free_memory()
Queries current device free memory via cudaMemGetInfo.
uint64_t fastx_file_bytes(const std::filesystem::path &path)
constexpr bool fastx_chunk_reached_host_byte_limit(size_t host_chunk_max_bytes, uint64_t raw_chunk_bytes) noexcept
True when raw_chunk_bytes reaches the optional host assembly cap.
constexpr size_t fastx_pipelined_chunk_budget(fastx_chunk_mode mode, size_t staging_budget_bytes) noexcept
Per-chunk flush budget for dual-stream ping-pong (two device sequence buffers).
constexpr uint64_t estimate_insert_staging_bytes(uint64_t raw_bytes, uint64_t record_count) noexcept
Peak device bytes for insert staging (d_sequence_) for a host chunk.
constexpr size_t fastx_chunk_slack_bytes() noexcept
Reserved device memory left for allocator and kernel temporaries.
fastx_chunk_mode
FASTX mode used to size GPU staging buffers.
bool fastx_chunk_should_flush(fastx_chunk_mode mode, size_t gpu_staging_budget_bytes, size_t host_chunk_max_bytes, uint64_t raw_chunk_bytes, uint64_t record_count) noexcept
Whether a host chunk should flush based on GPU staging and host byte limits.
constexpr uint64_t fastx_record_kmer_count(uint64_t bases) noexcept
constexpr uint64_t estimate_normalized_sequence_bytes(uint64_t raw_bytes, uint64_t record_count) noexcept
Upper bound on normalized sequence bytes for a raw host chunk.
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
constexpr bool fastx_chunk_reached_staging_budget(fastx_chunk_mode mode, size_t staging_budget_bytes, uint64_t raw_bytes, uint64_t record_count) noexcept
Whether estimated device staging meets or exceeds staging_budget_bytes.
constexpr size_t fastx_staging_budget_bytes(double fill_fraction, size_t free_bytes) noexcept
Device staging byte budget derived from free VRAM and fill_fraction.
bool fastx_file_fits_gpu_staging(const std::filesystem::path &path, fastx_chunk_mode mode, double fill_fraction)
Whether the entire uncompressed file fits in one GPU staging pass.
Result< void > validate_fastx_staging_fits(fastx_chunk_mode mode, double fill_fraction, uint64_t raw_bytes, uint64_t record_count, std::string_view source_name)
constexpr uint64_t fastx_record_symbol_count(uint64_t bases) noexcept
cuda::std::unexpected< Error > Err(Error error)
Failure return; converts to any Result<T> via cuda::std::unexpected.
Definition error.hpp:219
static constexpr uint64_t symbolWidth
Input bytes per symbol.
Definition config.cuh:49
static Error io(std::string message)
Definition error.hpp:119
static Error resource(std::string message)
Definition error.hpp:135
Fallible API result: cuda::std::expected<T, Error> with cuSBF factories.
Definition error.hpp:152
Driver-reported free device memory (bytes available for new allocations).
size_t free_bytes
Bytes reported free by cudaMemGetInfo.