// Host DRAM streaming-read kernels and thread placement (AVX-512 with AVX2 fallback), no CUDA dependency. // Used by mb_dram.cpp and mb_dram_pcie.cu. The expert kernels of the CPU pool read ~2 MB blobs with 512-bit loads; // "stream" mode reads one contiguous slice per thread, "blob" mode reads random 2 MB blobs of a large pool. #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include __attribute__((target("avx512f"))) static uint64_t read_sum_avx512(const uint8_t* p, size_t n) { __m512i a0 = _mm512_setzero_si512(), a1 = a0, a2 = a0, a3 = a0; for (size_t i = 0; i + 256 <= n; i += 256) { a0 = _mm512_add_epi64(a0, _mm512_loadu_si512((const void*)(p + i))); a1 = _mm512_add_epi64(a1, _mm512_loadu_si512((const void*)(p + i + 64))); a2 = _mm512_add_epi64(a2, _mm512_loadu_si512((const void*)(p + i + 128))); a3 = _mm512_add_epi64(a3, _mm512_loadu_si512((const void*)(p + i + 192))); } a0 = _mm512_add_epi64(_mm512_add_epi64(a0, a1), _mm512_add_epi64(a2, a3)); return (uint64_t)_mm512_reduce_add_epi64(a0); } __attribute__((target("avx2"))) static uint64_t read_sum_avx2(const uint8_t* p, size_t n) { __m256i a0 = _mm256_setzero_si256(), a1 = a0, a2 = a0, a3 = a0; for (size_t i = 0; i + 128 <= n; i += 128) { a0 = _mm256_add_epi64(a0, _mm256_loadu_si256((const __m256i*)(p + i))); a1 = _mm256_add_epi64(a1, _mm256_loadu_si256((const __m256i*)(p + i + 32))); a2 = _mm256_add_epi64(a2, _mm256_loadu_si256((const __m256i*)(p + i + 64))); a3 = _mm256_add_epi64(a3, _mm256_loadu_si256((const __m256i*)(p + i + 96))); } a0 = _mm256_add_epi64(_mm256_add_epi64(a0, a1), _mm256_add_epi64(a2, a3)); alignas(32) uint64_t t[4]; _mm256_store_si256((__m256i*)t, a0); return t[0] + t[1] + t[2] + t[3]; } static uint64_t read_sum(const uint8_t* p, size_t n) { static const bool avx512 = __builtin_cpu_supports("avx512f"); return avx512 ? read_sum_avx512(p, n) : read_sum_avx2(p, n); } // ---- topology: logical CPUs grouped by L3 (CCD on Zen), physical cores before SMT siblings struct Topo { std::vector> phys; // per L3 group: first hardware thread of each core std::vector> smt; // per L3 group: the sibling threads std::vector spread; // alternate groups, physical first, then SMT int groups() const { return (int)phys.size(); } }; static std::string slurp(const std::string& path) { std::ifstream f(path); std::stringstream ss; ss << f.rdbuf(); return ss.str(); } static std::vector parse_list(const std::string& s) { std::vector v; std::stringstream ss(s); std::string tok; while (std::getline(ss, tok, ',')) { int a, b; if (std::sscanf(tok.c_str(), "%d-%d", &a, &b) == 2) { for (int i = a; i <= b; ++i) v.push_back(i); } else if (std::sscanf(tok.c_str(), "%d", &a) == 1) { v.push_back(a); } } return v; } static Topo read_topo() { Topo t; const int n = (int)std::thread::hardware_concurrency(); std::vector group_of(n, -1); std::vector> members; for (int c = 0; c < n; ++c) { if (group_of[c] >= 0) continue; std::string l3 = slurp("/sys/devices/system/cpu/cpu" + std::to_string(c) + "/cache/index3/shared_cpu_list"); std::vector m = parse_list(l3); if (m.empty()) for (int i = 0; i < n; ++i) m.push_back(i); for (int x : m) if (x < n) group_of[x] = (int)members.size(); members.push_back(m); } t.phys.resize(members.size()); t.smt.resize(members.size()); for (size_t g = 0; g < members.size(); ++g) for (int c : members[g]) { std::vector sib = parse_list(slurp("/sys/devices/system/cpu/cpu" + std::to_string(c) + "/topology/thread_siblings_list")); if (sib.empty() || sib[0] == c) t.phys[g].push_back(c); else t.smt[g].push_back(c); } size_t mx = 0; for (auto& v : t.phys) mx = std::max(mx, v.size()); for (size_t i = 0; i < mx; ++i) for (size_t g = 0; g < t.phys.size(); ++g) if (i < t.phys[g].size()) t.spread.push_back(t.phys[g][i]); mx = 0; for (auto& v : t.smt) mx = std::max(mx, v.size()); for (size_t i = 0; i < mx; ++i) for (size_t g = 0; g < t.smt.size(); ++g) if (i < t.smt[g].size()) t.spread.push_back(t.smt[g][i]); return t; } static std::vector placement(const Topo& t, const std::string& kind, int n) { std::vector order; if (kind == "spread") { order = t.spread; } else { int g = kind == "ccd1" ? 1 : 0; if (g >= t.groups()) g = 0; order = t.phys[g]; order.insert(order.end(), t.smt[g].begin(), t.smt[g].end()); } if ((int)order.size() > n) order.resize(n); return order; } static void pin_self(int cpu) { cpu_set_t set; CPU_ZERO(&set); CPU_SET(cpu, &set); pthread_setaffinity_np(pthread_self(), sizeof set, &set); } // Reader threads pinned to `cpus` reading `region` until finish(); mode 0 = contiguous slice per thread, // mode 1 = random blobs of `blob` bytes. finish() returns the summed GB/s over the window start()..finish(). struct ReaderPool { std::vector th; std::atomic stop{false}; std::atomic ready{0}; std::vector bytes; std::vector sink; double t0 = 0, t1 = 0; static double clock_s() { return std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); } void start(const uint8_t* region, size_t region_bytes, const std::vector& cpus, int mode, size_t blob) { stop = false; ready = 0; bytes.assign(cpus.size(), 0); sink.assign(cpus.size(), 0); const int n = (int)cpus.size(); for (int i = 0; i < n; ++i) th.emplace_back([=] { pin_self(cpus[i]); const size_t slice = (region_bytes / n) & ~(size_t)4095; const uint8_t* mine = region + (size_t)i * slice; uint64_t rng = 0x9E3779B97F4A7C15ull * (uint64_t)(i + 1), acc = 0, total = 0; ready.fetch_add(1); while (ready.load() < n + 1) std::this_thread::yield(); while (!stop.load(std::memory_order_relaxed)) { if (mode == 0) { const size_t chunk = 1 << 20; // re-check stop every MiB for (size_t off = 0; off + chunk <= slice && !stop.load(std::memory_order_relaxed); off += chunk) { acc += read_sum(mine + off, chunk); total += chunk; } } else { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; const size_t nb = region_bytes / blob; const size_t b = (size_t)((rng >> 11) % nb); acc += read_sum(region + b * blob, blob); total += blob; } } bytes[i] = total; sink[i] = acc; }); while (ready.load() < n) std::this_thread::yield(); t0 = clock_s(); ready.fetch_add(1); } double finish() { // GB/s stop = true; t1 = clock_s(); for (auto& t : th) t.join(); th.clear(); uint64_t tot = 0; for (uint64_t b : bytes) tot += b; return (t1 - t0) > 0 ? (double)tot / (t1 - t0) / 1e9 : 0.0; } };