-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulate_gpu_load.py
More file actions
62 lines (49 loc) · 1.86 KB
/
Copy pathsimulate_gpu_load.py
File metadata and controls
62 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
GPU stress script for the Scenario 3 demo.
Fills the GPU with matrix multiplications to push utilization above the
85% threshold so the EMOS postprocessor triggers the model hot-swap live.
Usage:
python3 simulate_gpu_load.py # CUDA (NVIDIA)
python3 simulate_gpu_load.py --cpu # CPU fallback (raises load, not GPU%)
Press Ctrl+C to stop — the EMOS postprocessor will detect the drop below
60% and swap back to the full model automatically.
"""
import sys
import time
USE_CPU = "--cpu" in sys.argv
if USE_CPU:
print("Running CPU stress (GPU% will not rise — use only as fallback).")
import threading
def cpu_burn():
while True:
_ = sum(i * i for i in range(100_000))
for _ in range(4):
threading.Thread(target=cpu_burn, daemon=True).start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Stopped.")
else:
try:
import cupy as cp
except ImportError:
sys.exit("CuPy not found. Install with: pip install cupy-cuda12x (or use --cpu)")
# Test if GPU is actually accessible by CuPy
try:
cp.cuda.Device(0).compute_capability
except cp.cuda.runtime.CUDARuntimeError:
sys.exit("No CUDA GPU detected. Use --cpu flag or check your CUDA install.")
SIZE = 8192
print(f"Stressing GPU with {SIZE}×{SIZE} matrix multiplications. Press Ctrl+C to stop.")
# Create random matrices directly on the GPU
a = cp.random.randn(SIZE, SIZE, dtype=cp.float32)
b = cp.random.randn(SIZE, SIZE, dtype=cp.float32)
try:
while True:
# Matrix multiplication
_ = cp.matmul(a, b)
# Force synchronization so the GPU doesn't queue asynchronously and drop load
cp.cuda.Stream.null.synchronize()
except KeyboardInterrupt:
print("\nStopped — GPU load released.")