cuSBF
Loading...
Searching...
No Matches
fastx_dispatch.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstdint>
5#include <filesystem>
6#include <functional>
7#include <istream>
8#include <memory>
9#include <string>
10#include <string_view>
11#include <type_traits>
12
18#include <cusbf/error.hpp>
19#include <cusbf/Fastx.hpp>
20#include <cusbf/gzstreambuf.hpp>
21
22#if defined(__linux__)
23 #include <sys/stat.h>
24#endif
25
26namespace cusbf::detail {
27
39
46
52
54template <typename Config>
55[[nodiscard]] inline bool
57 if (file_bytes == 0) {
58 return true;
59 }
60
62 if (!gpu_memory) {
63 return false;
64 }
65 const size_t staging_budget_bytes =
67 if (staging_budget_bytes == 0) {
68 return true;
69 }
70
72}
73
78 constexpr uint64_t kDefaultBytes = 32u << 20;
79 const uint64_t mebibytes =
80 parse_env_mebibytes(getenv_value("CUSBF_FASTX_SINGLE_CHUNK_STREAM_MAX_MB"));
81 if (mebibytes == 0) {
82 return kDefaultBytes;
83 }
84 return mebibytes << 20;
85}
86
88template <typename Config>
111
113template <typename Config>
127
128template <typename T>
129concept cusbf_result_like = requires {
130 typename T::value_type;
131 typename T::error_type;
132} && std::same_as<typename T::error_type, cusbf::Error>;
133
135template <typename Handler>
137 std::invoke_result_t<Handler&, FastxReader&, fastx_dispatch_path>;
138
142template <typename Handler>
143concept fastx_dispatch_handler = requires(
145 FastxReader& reader,
146 FastxBufferReader& buffer_reader,
148) {
150 { handler(buffer_reader, path) } -> std::same_as<fastx_dispatch_handler_result_t<Handler>>;
151};
152
158template <typename Config, fastx_dispatch_handler Handler>
160 const std::filesystem::path& path,
162 double fill_fraction,
164) {
165 const std::string path_string = path.string();
166 const std::string_view path_view{path_string};
169
171 const auto buffer = FastxFileBuffer::load(path);
172 if (!buffer) {
173 return Err(buffer.error());
174 }
175 FastxBufferReader reader((*buffer)->data(), path_view);
177 }
178
179 const auto input = openFastxFile(path);
180 if (!input) {
181 return Err(input.error());
182 }
185}
186
187} // namespace cusbf::detail
FASTA/FASTQ parser over a contiguous in-memory buffer.
static Result< std::unique_ptr< FastxFileBuffer > > load(const std::filesystem::path &path)
Loads an uncompressed file via mmap (Linux) or read into memory.
Streaming FASTA/FASTQ parser.
Definition Fastx.hpp:297
Handler invoked by dispatch_fastx_file with either reader type and a dispatch path.
Result< cuda_free_memory > query_cuda_free_memory()
Queries current device free memory via cudaMemGetInfo.
bool isGzipFile(const std::filesystem::path &path)
True when path begins with the gzip magic bytes (0x1F, 0x8B).
Result< std::unique_ptr< std::istream > > openFastxFile(const std::filesystem::path &path)
Opens a FASTA/FASTQ file for reading.
Definition Fastx.hpp:470
uint64_t fastx_file_bytes(const std::filesystem::path &path)
bool fastx_fits_single_gpu_chunk(fastx_chunk_mode mode, double fill_fraction, uint64_t file_bytes)
Whether the entire file fits in a single GPU staging chunk at fill_fraction.
fastx_dispatch_path
How a FASTX file is read and chunked for GPU processing.
@ chunked_mmap
Multiple GPU chunks, file mmap'd when it fits in host RAM.
@ single_chunk_mmap
Whole file in one GPU chunk, mmap'd when it fits in host RAM.
@ chunked_stream
Multiple GPU chunks, stream via istream (gzip or larger than RAM).
@ single_chunk_stream
Whole file in one GPU chunk, stream via istream (no mmap).
constexpr bool fastx_is_single_chunk_path(fastx_dispatch_path path) noexcept
True for fastx_dispatch_path::single_chunk_stream or fastx_dispatch_path::single_chunk_mmap.
fastx_dispatch_handler_result_t< Handler > dispatch_fastx_file(const std::filesystem::path &path, fastx_chunk_mode mode, double fill_fraction, Handler &&handler)
Opens a FASTX path and invokes handler with a reader and dispatch path.
fastx_dispatch_path select_fastx_dispatch_path_for_file_bytes(uint64_t file_bytes, fastx_chunk_mode mode, double fill_fraction, bool file_fits_in_memory)
Selects mmap vs stream and single- vs multi-chunk processing from file size.
fastx_dispatch_path select_fastx_dispatch_path(const std::filesystem::path &path, fastx_chunk_mode mode, double fill_fraction)
Selects mmap vs stream and single- vs multi-chunk processing for a path.
fastx_chunk_mode
FASTX mode used to size GPU staging buffers.
bool fastx_file_fits_in_memory(const std::filesystem::path &path)
True when uncompressed path size is within fastx_memory_map_max_bytes.
constexpr bool fastx_uses_mmap_reader(fastx_dispatch_path path) noexcept
True when dispatch uses FastxBufferReader over an mmap'd file.
uint64_t parse_env_mebibytes(std::string_view value)
Parses a decimal mebibyte count from value.
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
std::invoke_result_t< Handler &, FastxReader &, fastx_dispatch_path > fastx_dispatch_handler_result_t
Return type of a fastx_dispatch_handler when invoked with a stream reader.
std::string_view getenv_value(const char *env_name)
Reads env_name via getenv, or an empty view when unset.
uint64_t fastx_single_chunk_stream_max_bytes()
Max raw file size for fastx_dispatch_path::single_chunk_stream (istream, no mmap).
cuda::std::unexpected< Error > Err(Error error)
Failure return; converts to any Result<T> via cuda::std::unexpected.
Definition error.hpp:219