Skip to content

Instantly share code, notes, and snippets.

@alifarazz
Last active October 6, 2024 23:02
Show Gist options
  • Save alifarazz/c067d0f2095da40252e33ff0a94d369d to your computer and use it in GitHub Desktop.
Save alifarazz/c067d0f2095da40252e33ff0a94d369d to your computer and use it in GitHub Desktop.
Stress test CPU with int ops + ld + st
// g++ -std=c++20 cpu_burn.cc -o cpu_burn
#include <bits/stdc++.h>
constexpr size_t maxn = 192;
constexpr int iter = 1024 * 1024;
std::array<std::jthread, maxn> threads;
std::array<unsigned int, (1024ull * 1024 * 512)> data;
// NOTE(Ali): Ripped from GLSL shader code
constexpr unsigned int pcg_hash(unsigned int input) {
unsigned int state = input * 747796405u + 2891336453u;
unsigned int word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
int main(int argc, char *argv[]) {
assert(argc == 2);
const size_t n = std::min(maxn, static_cast<size_t>(atol(argv[1])));
std::generate(std::begin(data), std::end(data),
[i = 0ull]() mutable { return i++; });
std::generate_n(std::begin(threads), n, [n, i = 0ull]() mutable {
return std::jthread(
[n](size_t tid) {
size_t offset = tid * data.size() / n;
for (int j = 0; j < iter; j++) {
for (size_t i = 0; i < data.size() / n; i++) {
// NOTE(Ali): Who needs rand amirite?
auto &d = data[offset + i];
d = pcg_hash(d);
}
}
},
i++);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment