okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221test(vmm): drain the device before unmapping chunks, and poll the free-VRAM readout after the release12d835dd6

main
3.5 KiB71 linesraw
1// The elastic K/V's VMM ranges (vmm.hpp): chunks map readable and writable, a chunk unmapped from one range and mapped
2// into another keeps its bytes (what the expert cache hands the K/V), and a range releases what it holds.
3#include "strata/core/vmm.hpp"
4
5#include <cuda_runtime.h>
6
7#include <chrono>
8#include <cstdio>
9#include <thread>
10#include <vector>
11
12using strata::core::VmmChunk;
13using strata::core::VmmRange;
14
15static int fails = 0;
16#define CHECK(c) do { if (!(c)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); ++fails; } } while (0)
17
18int main() {
19    if (cudaFree(nullptr) != cudaSuccess || !strata::core::vmm_available()) {
20        std::printf("vmm_test: no CUDA device with virtual memory - skipped\n");
21        return 0;
22    }
23    const uint64_t G = strata::core::vmm_granularity();
24    CHECK(G >= 4096);
25    VmmRange a, b;
26    CHECK(a.reserve(8 * G + 1));   // rounds up
27    CHECK(a.chunks() == 9);
28    CHECK(b.reserve(4 * G));
29    CHECK(a.mapped_count() == 0);
30    CHECK(a.map_range(0, 6, [] { return (VmmChunk) 0; }));
31    CHECK(a.mapped_count() == 6 && a.mapped(5) && !a.mapped(6));
32    std::vector<uint8_t> h(6 * G), back(6 * G);
33    for (size_t i = 0; i < h.size(); ++i) h[i] = (uint8_t) (i * 2654435761u >> 13);
34    CHECK(cudaMemcpy(a.base(), h.data(), h.size(), cudaMemcpyHostToDevice) == cudaSuccess);
35    // chunks 4 and 5 move to b's chunks 1 and 0. The host-to-device copy above may still be in flight (a pageable
36    // cudaMemcpy returns once the data is staged), and a chunk unmapped under a copy faults it (an illegal memory access
37    // that sticks to every later call), so the device is drained first, as generate.cpp does before it unmaps
38    CHECK(cudaDeviceSynchronize() == cudaSuccess);
39    const VmmChunk c4 = a.unmap(4), c5 = a.unmap(5);
40    CHECK(c4 != 0 && c5 != 0 && !a.mapped(4) && a.unmap(4) == 0);
41    std::vector<VmmChunk> give = {c4, c5};   // map_range takes them from the back: chunk 0 <- c5, chunk 1 <- c4
42    CHECK(b.map_range(0, 2, [&] { VmmChunk x = give.back(); give.pop_back(); return x; }));
43    CHECK(cudaMemcpy(back.data(), b.base(), G, cudaMemcpyDeviceToHost) == cudaSuccess);
44    bool same = true;
45    for (uint64_t i = 0; i < G; ++i) same = same && back[i] == h[5 * G + i];
46    CHECK(same);
47    CHECK(cudaMemcpy(back.data(), b.base() + G, G, cudaMemcpyDeviceToHost) == cudaSuccess);
48    same = true;
49    for (uint64_t i = 0; i < G; ++i) same = same && back[i] == h[4 * G + i];
50    CHECK(same);
51    // a's first chunks are untouched, and map_range fills a hole with new memory, skipping mapped chunks
52    CHECK(cudaMemcpy(back.data(), a.base(), 4 * G, cudaMemcpyDeviceToHost) == cudaSuccess);
53    same = true;
54    for (uint64_t i = 0; i < 4 * G; ++i) same = same && back[i] == h[i];
55    CHECK(same);
56    CHECK(a.map_range(3, 7, [] { return (VmmChunk) 0; }) && a.mapped_count() == 7);
57    CHECK(cudaMemset(a.base() + 4 * G, 7, 3 * G) == cudaSuccess && cudaDeviceSynchronize() == cudaSuccess);
58    size_t f0 = 0, f1 = 0, t = 0;
59    cudaMemGetInfo(&f0, &t);
60    a.release();
61    b.release();
62    for (int i = 0; i < 20; ++i) {   // the free-memory counter may lag a release by a moment on Windows (WDDM)
63        cudaMemGetInfo(&f1, &t);
64        if (f1 >= f0 + 8 * G) break;
65        std::this_thread::sleep_for(std::chrono::milliseconds(100));
66    }
67    CHECK(f1 >= f0 + 8 * G);   // 7 + 2 chunks went back (the driver may round)
68    CHECK(cudaGetLastError() == cudaSuccess);
69    std::printf("vmm_test: %s\n", fails ? "FAILED" : "ok");
70    return fails ? 1 : 0;
71}