Files
Strata/include/strata/platform/direct_file.hpp
T
Niko1221 dfe61b1ca0 C-4: the prompt's embedding in one gather, and the n-gram table read by several threads
- One batched gather per chunk instead of a launch per token (same arithmetic: identical bits).
- The prompt path's 'embed+steps' time was mostly the host reading the n-gram (PLE) rows from the SSD
  with the GPU idle: one thread issued every 90-byte read (~12 us of kernel time each, ~80K reads/s).
  DirectFile now only queues a read; a pool issues them (4 threads on Windows, overlapped; 16 blocking
  O_DIRECT preads on Linux, which read them one at a time before - perf-review F-2). STRATA_IO_THREADS.
  Q2_0 4K prompt: PLE 591 -> 291 ms, 941 -> 1010 tok/s.
- STRATA_PREFILL_TIMING also prints the host's share (chunk setup, the per-chunk callback).
Exactness: the prompt path's final residuals (STRATA_PREFILL_DUMP_R) and the continuation are
byte-identical to 0.1.21 at 4K and 20K tokens, IQ and S2 embedding tables.
2026-09-29 07:23:09 +02:00

61 lines
2.3 KiB
C++

// include/strata/platform/direct_file.hpp - plan v0.3 P1/P2: unbuffered asynchronous file reads.
//
// The n-gram table (26.8 GiB) stays on the SSD and must never occupy RAM, including the OS file cache. A memory
// map cannot promise that; an unbuffered read can. On Windows this is FILE_FLAG_NO_BUFFERING | OVERLAPPED with
// an I/O completion port; on Linux, O_DIRECT preads. Both issue the reads from a small thread pool (submit only
// queues), so many reads are in flight: STRATA_IO_THREADS sets its size (default 4 on Windows, 16 on Linux).
//
// Contract of every read: offset, length and buffer address are multiples of `alignment()` (4096 here).
// Reads past end of file return the bytes that exist; `Completion::bytes` says how many.
#pragma once
#include <cstdint>
#include <string>
namespace strata::platform {
struct Completion {
uint64_t tag = 0; ///< the caller's tag from `submit`
uint32_t bytes = 0; ///< bytes transferred (short only at end of file)
bool ok = false;
};
class DirectFile {
public:
DirectFile();
~DirectFile();
DirectFile(const DirectFile&) = delete;
DirectFile& operator=(const DirectFile&) = delete;
bool open(const std::string& path, std::string& err);
void close();
bool is_open() const;
uint64_t size() const;
static constexpr uint32_t alignment() { return 4096; }
/// Queue one read. Returns false (and sets `err`) if the request could not be queued; a queued request
/// always produces exactly one completion.
bool submit(uint64_t offset, void* buffer, uint32_t length, uint64_t tag, std::string& err);
/// Wait for up to `max` completions; returns how many were written to `out`. `timeout_ms` < 0 waits
/// forever; 0 polls. A `wake()` shows up as one completion with tag `WAKE_TAG`.
int wait(Completion* out, int max, int timeout_ms);
/// Make a thread blocked in `wait` return (thread-safe). Used to hand it newly queued work.
static constexpr uint64_t WAKE_TAG = ~0ull;
void wake();
/// Allocation helpers for aligned read buffers (page-aligned, never touched by the cache manager).
static void* alloc_aligned(size_t bytes);
static void free_aligned(void* p);
private:
struct Impl;
Impl* impl_ = nullptr;
};
/// Monotonic time in microseconds, for latency accounting.
double now_us();
} // namespace strata::platform