Docs · Memory expansion
An array larger than host memory.
cpu_vector gives ordinary C++ code an array that can be larger than the machine's RAM. Pages live in the Context Transfer Engine, and a cache in host memory holds the ones you are working on. It is plain C++: any compiler, any thread, no GPU.
- Header
- clio_cte/cpu_vector/cpu_vector.h
- Namespace
- clio::cte::cpu_vector
- Compiler
- any C++20 compiler
- GPU version
- gpu_vector
The idea in four steps
cpu_vector works like gpu_vector, but the cache is in host memory and the calls are ordinary blocking functions instead of coroutines. For each page you touch:
Fetch
v.Fetch(0, off, count)makes elements[off, off + count)resident and pins their pages.Hold
v.HoldPage(off, count)returns a window onto the cached page that you index like an array.Flush
v.Flush(0, off, count)writes changed elements back to the CTE. Nothing is written back automatically.Unpin
v.UnpinRange(off, count)lets the cache reuse the page's slot.
Example: double every element
This program creates a 256 MiB vector of floats with a 32 MiB cache, fills it, doubles every element page by page, and reads the result back.
#include <clio_runtime/clio_runtime.h>
#include <clio_cte/core/core_client.h>
#include <clio_cte/cpu_vector/cpu_vector.h>
#include <algorithm>
#include <vector>
namespace cv = clio::cte::cpu_vector;
using clio::run::u64;
// Double pages first, first + stride, first + 2*stride, ...
void DoublePages(cv::Vector<float> &v, u64 first, u64 stride) {
const u64 epp = v.ElemsPerPage(); // elements per page
for (u64 pg = first; pg < v.NumPages(); pg += stride) {
const u64 off = pg * epp; // first element of the page
const u64 count = std::min(epp, v.size() - off);
v.Fetch(0, off, count); // 1. bring the page in, pin it
auto h = v.HoldPage(off, count, /*write=*/true); // 2. window onto the page
for (u64 i = off; i < off + h.run(); ++i) {
h[i] *= 2.0f; // h is indexed by absolute offset
}
v.Flush(0, off, count); // 3. write the page back
v.UnpinRange(off, count); // 4. let the cache reuse the slot
}
}
int main() {
// Start a runtime in this process (or attach to one) and the CTE client.
if (!clio::run::CLIO_INIT(clio::run::RuntimeMode::kClient, true)) return 1;
if (!clio::cte::core::CLIO_CTE_CLIENT_INIT()) return 1;
// 64M floats (256 MiB) in 1 MiB pages; cache of 8 sets x 4 pages = 32 MiB.
const u64 n = 64ull << 20;
cv::Vector<float> v("doubled_cpu", /*page_bytes=*/1ull << 20, n,
/*nsets=*/8, /*set_size=*/4);
std::vector<float> data(n, 1.5f);
v.Preload(data.data(), n); // fill: writes straight to the CTE
DoublePages(v, /*first=*/0, /*stride=*/1);
v.Download(data.data(), n); // read back: straight from the CTE
bool ok = (data[0] == 3.0f && data[n - 1] == 3.0f);
clio::run::CLIO_RUNTIME_FINALIZE();
return ok ? 0 : 2;
}Vector(tag, page_bytes, n, nsets, set_size)- The tag names the vector in the CTE, and opening the same tag again gives the same data, even from a
gpu_vectorwith the same page size and element type.page_bytesmust be a multiple ofsizeof(T). The cache holdsnsets * set_sizepages. - Holds use absolute offsets
h[i]is valid forifromh.begin_off()toh.begin_off() + h.run().h.run()is how many of the requested elements lie on this page.- Errors throw
- A caller error, such as holding elements that were never fetched, throws
std::runtime_error. So does a fetch that waits 30 seconds for a free slot because every page of a set is still pinned. - Unwritten pages read as zeros
- You can skip
Preloadand start withFetch; a page with nothing stored yet comes back filled with zeros.
From many threads
Every call is thread-safe, so threads can share one vector and one cache. Give each thread its own pages and reuse DoublePages from above:
#include <thread>
const u64 nthreads = 8;
std::vector<std::thread> workers;
for (u64 t = 0; t < nthreads; ++t) {
workers.emplace_back([&v, t, nthreads] { DoublePages(v, /*first=*/t, /*stride=*/nthreads); });
}
for (auto &w : workers) w.join();A page read by several threads is fetched and cached once. Size the cache so each set has at least as many slots as threads that may hold one of its pages at the same time.
Shortcuts
UpdateRange(off, count, write)fetches the whole pages under a range and returns the hold in one call. Release it withUnpinRange(v.PageLo(off), v.PageSpan(off, count)).- One
FetchorFlushtakes up to eight(off, count)pairs:v.Fetch(0, a, 100, b, 100). PreloadPagesandDownloadPagestake a callback per page, for data that does not fit in host memory either.Prefetch(pg_lo, pg_hi)warms the cache without pinning.ClearCache()empties it and discards unflushed writes.
Build and run
cpu_vector is header-only and is installed with every CLIO Core build that includes C++ headers, so no special flags are needed. In the CLIO Core build tree, link the interface target and the runtime pieces the in-process runtime needs, as the adapter's test does:
add_executable(double_example double_example.cc)
target_link_libraries(double_example
clio::cte::cpu_vector
clio_cte_core_runtime clio_cte_core_client
clio::run::admin_runtime clio::run::admin_client
clio::run::bdev_runtime clio::run::bdev_client
ctp::cxx ${CMAKE_THREAD_LIBS_INIT})Against an installed tree, include the header and link the exported client targets; the install page shows how. The runtime needs a CTE pool with a storage tier large enough for the vector. The clio.yaml on the gpu_vector page works here too (the gpu section is ignored):
$ CLIO_SERVER_CONF=$PWD/clio.yaml ./double_example; echo "exit $?"
exit 0Where to go next
- gpu_vector: the same model inside a CUDA kernel.
- Usage: lazy copies, checkpoints and tier placement hints.
- API reference: every constructor argument and method.