okami.mom/strata

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

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
4.4 KiB111 linesraw
1// pinned_alias_test (#3431, Windows HIP): the GPU alias of every range the expert arena registers is reserved in the process's
2// own address space while the arena lives, and a range whose alias covers memory already in use is not kept registered - a
3// copy through that memory must still reach the device and come back.  Needs an AMD GPU on Windows, else it skips (77).
4#include "strata/core/pinned.hpp"
5
6#include <cuda_runtime.h>
7
8#include <cstdint>
9#include <cstdio>
10#include <cstring>
11#include <vector>
12
13#if defined(_WIN32)
14#include <windows.h>
15#endif
16
17#if defined(_WIN32) && defined(STRATA_USE_HIP)
18static int g_fail = 0;
19#define CHECK(c)                                                              \
20    do {                                                                      \
21        if (!(c)) { std::fprintf(stderr, "FAIL %s:%d  %s\n", __FILE__, __LINE__, #c); ++g_fail; } \
22    } while (0)
23
24static bool address_free(const void* p) {
25    MEMORY_BASIC_INFORMATION m{};
26    return VirtualQuery(p, &m, sizeof m) == sizeof m && m.State == MEM_FREE;
27}
28
29static void* alias_of(void* host) {
30    void* d = nullptr;
31    if (cudaHostGetDevicePointer(&d, host, 0) != cudaSuccess) {
32        (void) cudaGetLastError();
33        return nullptr;
34    }
35    return d;
36}
37
38// 16 bytes host -> device -> host through `buf`
39static bool round_trip(uint8_t* buf) {
40    const uint32_t pat[4] = {0x11223344u, 0x55667788u, 0x99aabbccu, 0xddeeff00u};
41    void* d = nullptr;
42    if (cudaMalloc(&d, 64) != cudaSuccess) return false;
43    std::memcpy(buf, pat, sizeof pat);
44    std::memset(buf + 64, 0, sizeof pat);
45    const bool ok = cudaMemcpy(d, buf, sizeof pat, cudaMemcpyHostToDevice) == cudaSuccess &&
46                    cudaMemcpy(buf + 64, d, sizeof pat, cudaMemcpyDeviceToHost) == cudaSuccess &&
47                    std::memcmp(buf + 64, pat, sizeof pat) == 0;
48    cudaFree(d);
49    return ok;
50}
51#endif
52
53int main() {
54#if !(defined(_WIN32) && defined(STRATA_USE_HIP))
55    std::printf("pinned_alias_test: SKIP (Windows HIP only)\n");
56    return 77;
57#else
58    using namespace strata::core;
59    if (cudaFree(0) != cudaSuccess) {
60        std::printf("pinned_alias_test: SKIP (no GPU)\n");
61        return 77;
62    }
63    (void) cudaGetLastError();
64    const uint64_t slice = 64ull << 20, bytes = 4 * slice;
65    const std::vector<uint64_t> bounds{0, slice, 2 * slice, 3 * slice, 4 * slice};
66
67    // 1. while the arena lives, nothing can be placed under the alias of what it registered; afterwards the range is free again
68    void* first_alias = nullptr;
69    {
70        PinnedArena a(bytes, bounds);
71        CHECK(a.valid());
72        CHECK(a.registered_bytes == bytes);
73        first_alias = alias_of(a.base);
74        CHECK(first_alias != nullptr);
75        if (first_alias != nullptr && first_alias != a.base) {
76            CHECK(!a.alias_claims.empty());
77            CHECK(!address_free(first_alias));
78            CHECK(!address_free((uint8_t*) first_alias + bytes - 4096));
79        }
80    }
81    if (first_alias == nullptr) {
82        std::printf("pinned_alias_test: %d FAILED\n", g_fail);
83        return 1;
84    }
85    CHECK(address_free(first_alias));
86
87    // 2. memory already in use under the alias (in the engine: the stack of the thread that pins the arena).  The runtime hands
88    //    the same alias out again, so the next arena's first range would cover it: that range must stay unregistered, and a
89    //    copy through the memory must work.  Without the guard the copy reads and writes the arena instead (round trip zeros).
90    uint8_t* victim = (uint8_t*) VirtualAlloc(first_alias, 1u << 20, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
91    CHECK(victim != nullptr);
92    if (victim != nullptr) {
93        PinnedArena b(bytes, bounds);
94        CHECK(b.valid());
95        bool covered = false;
96        for (uint64_t off = 0; off < b.registered_bytes; off += slice) {
97            uint8_t* al = (uint8_t*) alias_of((uint8_t*) b.base + off);
98            if (al != nullptr && al <= victim && victim < al + slice) covered = true;
99        }
100        CHECK(!covered);
101        if (b.registered_bytes == bytes)
102            std::printf("pinned_alias_test: note: the runtime chose another alias this time; the refusal was not exercised\n");
103        else
104            CHECK(b.note.find("#3431") != std::string::npos);
105        CHECK(round_trip(victim));
106        VirtualFree(victim, 0, MEM_RELEASE);
107    }
108    std::printf(g_fail == 0 ? "pinned_alias_test: OK\n" : "pinned_alias_test: %d FAILED\n", g_fail);
109    return g_fail == 0 ? 0 : 1;
110#endif
111}