okami.mom/strata

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

waveringanaserve: restore the DeepSeek V4.1 server paths, tools and headers removed upstream, keeping the native picture tower firstba8745e13

main
7.3 KiB143 linesraw
1// hwbench: host RAM bandwidth, PCIe host->device, and unbuffered overlapped NVMe reads (Windows).
2//   hwbench ram [threads] [MiB]
3//   hwbench pcie
4//   hwbench nvme <file> <block_KiB> <qd> <seconds> [seq|rand]
5#include <cuda_runtime.h>
6#include <windows.h>
7#include <algorithm>
8#include <chrono>
9#include <cstdint>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <random>
14#include <string>
15#include <thread>
16#include <vector>
17
18static double now() { return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
19
20static void bench_ram(int threads, size_t mib) {
21    size_t n = mib << 20;
22    std::vector<char*> src(threads), dst(threads);
23    for (int t = 0; t < threads; t++) {
24        src[t] = (char*)_aligned_malloc(n, 4096); dst[t] = (char*)_aligned_malloc(n, 4096);
25        memset(src[t], t + 1, n); memset(dst[t], 0, n);
26    }
27    for (int mode = 0; mode < 2; mode++) {
28        double best = 0;
29        for (int rep = 0; rep < 4; rep++) {
30            std::vector<std::thread> th; volatile uint64_t sink = 0;
31            double t0 = now();
32            for (int t = 0; t < threads; t++)
33                th.emplace_back([&, t] {
34                    if (mode == 0) memcpy(dst[t], src[t], n);
35                    else { uint64_t s = 0; const uint64_t* p = (const uint64_t*)src[t]; for (size_t i = 0; i < n / 8; i += 1) s += p[i]; sink = sink + s; }
36                });
37            for (auto& x : th) x.join();
38            double dt = now() - t0; double gb = (mode == 0 ? 2.0 : 1.0) * n * threads / 1e9 / dt;  // copy counts read+write
39            if (gb > best) best = gb;
40        }
41        printf("ram %s threads=%d size/thread=%zu MiB best=%.1f GB/s (%s)\n", mode == 0 ? "copy" : "read", threads, mib, best, mode == 0 ? "read+write counted" : "read only");
42    }
43}
44
45static void bench_pcie() {
46    for (size_t mib : {1, 4, 16, 64, 256}) {
47        size_t n = mib << 20; void *h, *d; cudaStream_t s; cudaStreamCreate(&s);
48        cudaMallocHost(&h, n); cudaMalloc(&d, n); memset(h, 1, n);
49        void* hp = _aligned_malloc(n, 4096); memset(hp, 1, n);
50        for (int pinned = 1; pinned >= 0; pinned--) {
51            void* src = pinned ? h : hp;
52            for (int w = 0; w < 3; w++) cudaMemcpyAsync(d, src, n, cudaMemcpyHostToDevice, s);
53            cudaStreamSynchronize(s);
54            int reps = (int)std::max<size_t>(8, (1024u / mib)); double t0 = now();
55            for (int i = 0; i < reps; i++) cudaMemcpyAsync(d, src, n, cudaMemcpyHostToDevice, s);
56            cudaStreamSynchronize(s); double dt = now() - t0;
57            printf("pcie H2D %s %4zu MiB: %.2f GB/s\n", pinned ? "pinned  " : "pageable", mib, (double)n * reps / 1e9 / dt);
58        }
59        cudaFreeHost(h); cudaFree(d); _aligned_free(hp); cudaStreamDestroy(s);
60    }
61}
62
63static void bench_nvme(const char* path, size_t block, int qd, double secs, bool seq) {
64    HANDLE f = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, nullptr);
65    if (f == INVALID_HANDLE_VALUE) { printf("open failed %lu\n", GetLastError()); return; }
66    LARGE_INTEGER sz; GetFileSizeEx(f, &sz);
67    HANDLE port = CreateIoCompletionPort(f, nullptr, 0, 1);
68    uint64_t nblk = (uint64_t)sz.QuadPart / block;
69    struct Slot { OVERLAPPED ov; char* buf; };
70    std::vector<Slot> slots(qd);
71    std::mt19937_64 rng(12345);
72    uint64_t seqpos = 0, bytes = 0, ops = 0;
73    std::vector<double> lat; std::vector<double> t_issue(qd);
74    auto issue = [&](int i) {
75        uint64_t b = seq ? (seqpos++ % nblk) : (rng() % nblk);
76        uint64_t off = b * block;
77        memset(&slots[i].ov, 0, sizeof(OVERLAPPED));
78        slots[i].ov.Offset = (DWORD)(off & 0xffffffff); slots[i].ov.OffsetHigh = (DWORD)(off >> 32);
79        t_issue[i] = now();
80        if (!ReadFile(f, slots[i].buf, (DWORD)block, nullptr, &slots[i].ov) && GetLastError() != ERROR_IO_PENDING) { printf("ReadFile err %lu\n", GetLastError()); exit(1); }
81    };
82    for (int i = 0; i < qd; i++) { slots[i].buf = (char*)VirtualAlloc(nullptr, block, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); issue(i); }
83    double t0 = now(), tend = t0 + secs;
84    while (true) {
85        DWORD nb; ULONG_PTR key; OVERLAPPED* ov;
86        if (!GetQueuedCompletionStatus(port, &nb, &key, &ov, 10000) && !ov) { printf("wait err\n"); break; }
87        int i = (int)(((Slot*)((char*)ov - offsetof(Slot, ov))) - slots.data());
88        bytes += nb; ops++; lat.push_back(now() - t_issue[i]);
89        if (now() < tend) issue(i); else if (--qd == 0) break;
90    }
91    double dt = now() - t0;
92    std::sort(lat.begin(), lat.end());
93    printf("nvme %s block=%zuKiB qd=%zu: %.2f GB/s, %.0f IOPS, lat p50=%.2f ms p99=%.2f ms\n", seq ? "seq " : "rand", block >> 10, slots.size(),
94           bytes / 1e9 / dt, ops / dt, lat[lat.size() / 2] * 1e3, lat[lat.size() * 99 / 100] * 1e3);
95    CloseHandle(f);
96}
97
98// GPU kernels reading host memory over PCIe in place (zero-copy): the pinned + mapped (or registered) arena as a weights source
99__global__ void zc_sum(const int4* __restrict__ p, size_t n, unsigned long long* out) {
100    unsigned long long acc = 0;
101    for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t) gridDim.x * blockDim.x) {
102        const int4 v = p[i];
103        acc += (unsigned) v.x ^ (unsigned) v.y ^ (unsigned) v.z ^ (unsigned) v.w;
104    }
105    if (acc == 0x123456789ull) atomicAdd(out, acc);
106}
107static void bench_zc(size_t mib) {
108    const size_t n = mib << 20;
109    for (int mode = 0; mode < 2; ++mode) {
110        void *h = nullptr, *dp = nullptr;
111        if (mode == 0) {
112            if (cudaHostAlloc(&h, n, cudaHostAllocMapped | cudaHostAllocPortable) != cudaSuccess) { printf("hostalloc failed\n"); continue; }
113        } else {
114            h = VirtualAlloc(nullptr, n, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
115            memset(h, 1, n);
116            if (cudaHostRegister(h, n, cudaHostRegisterMapped | cudaHostRegisterPortable) != cudaSuccess) { printf("hostregister failed\n"); continue; }
117        }
118        memset(h, 1, n);
119        cudaHostGetDevicePointer(&dp, h, 0);
120        unsigned long long* out; cudaMalloc(&out, 8);
121        for (int blocks : {64, 256, 1024}) {
122            zc_sum<<<blocks, 256>>>((const int4*) dp, n / 16, out);
123            cudaDeviceSynchronize();
124            cudaEvent_t a, b; cudaEventCreate(&a); cudaEventCreate(&b);
125            cudaEventRecord(a);
126            for (int r = 0; r < 4; ++r) zc_sum<<<blocks, 256>>>((const int4*) dp, n / 16, out);
127            cudaEventRecord(b); cudaEventSynchronize(b);
128            float ms; cudaEventElapsedTime(&ms, a, b);
129            printf("zero-copy %s %zu MiB, %d blocks: %.2f GB/s\n", mode == 0 ? "cudaHostAlloc(mapped)" : "VirtualAlloc+cudaHostRegister(mapped)", mib, blocks, 4.0 * n / 1e9 / (ms / 1e3));
130        }
131        if (mode == 0) cudaFreeHost(h); else { cudaHostUnregister(h); VirtualFree(h, 0, MEM_RELEASE); }
132        cudaFree(out);
133    }
134}
135
136int main(int argc, char** argv) {
137    std::string m = argc > 1 ? argv[1] : "";
138    if (m == "ram") bench_ram(argc > 2 ? atoi(argv[2]) : 6, argc > 3 ? atoi(argv[3]) : 512);
139    else if (m == "pcie") bench_pcie();
140    else if (m == "zc") bench_zc(argc > 2 ? atoi(argv[2]) : 1024);
141    else if (m == "nvme" && argc > 5) bench_nvme(argv[2], (size_t)atoi(argv[3]) << 10, atoi(argv[4]), atof(argv[5]), argc > 6 && std::string(argv[6]) == "seq");
142    else printf("usage: hwbench ram|pcie|nvme ...\n");
143}