Skip to content

Instantly share code, notes, and snippets.

@attentionmech
Created June 4, 2025 16:30
Show Gist options
  • Save attentionmech/316cefec4dbda20ea341cc10af2bde21 to your computer and use it in GitHub Desktop.
Save attentionmech/316cefec4dbda20ea341cc10af2bde21 to your computer and use it in GitHub Desktop.
CUDA animation from Nsight Data
# this needs a sqlite dump from nsight nsys utility
# for example on windows i ran this:
# "C:\Program Files\NVIDIA Corporation\Nsight Systems 2025.1.3\target-windows-x64\nsys.exe" profile --trace=cuda,nvtx --export sqlite --output profile_matmul you_cuda_program.exe
# once that dump is there run the follow python code on it basically it takes few key tables and animate a timeline
import sqlite3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
# --- CONFIG ---
db_path = "profile_matmul.sqlite" # Adjust if needed
bin_width = 0.5
time_start = 480
time_end = 580
time_bins = np.arange(time_start, time_end, bin_width)
# 💡 Neon palette (adjusted for dark background)
yticks = {
"kernel": 0,
"memcpy": 1,
"sync": 2,
"runtime": 3,
"driver": 4,
"overhead": 5,
}
colors = {
"kernel": "#00ffff", # neon cyan
"memcpy": "#39ff14", # neon green
"sync": "#ff007f", # hot pink
"runtime": "#fffb00", # neon yellow
"driver": "#9400ff", # neon purple
"overhead": "#ff4c00", # neon orange
}
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# --- Collect Events ---
events = []
def collect_tiles(label, sql, scale_fn):
try:
rows = cursor.execute(sql).fetchall()
except sqlite3.OperationalError:
return
for row in rows:
start, end = row[:2]
z = scale_fn(row)
for t in time_bins:
if start / 1e6 <= t <= end / 1e6:
events.append((t, yticks[label], z, colors[label]))
collect_tiles("kernel", "SELECT start, end FROM CUPTI_ACTIVITY_KIND_KERNEL", lambda r: 1.0)
collect_tiles("memcpy", "SELECT start, end, bytes FROM CUPTI_ACTIVITY_KIND_MEMCPY", lambda r: max(0.3, np.log2(r[2]) / 10))
collect_tiles("sync", "SELECT start, end FROM CUPTI_ACTIVITY_KIND_SYNCHRONIZATION", lambda r: 0.6)
collect_tiles("runtime", "SELECT start, end FROM CUPTI_ACTIVITY_KIND_RUNTIME", lambda r: 0.6)
collect_tiles("driver", "SELECT start, end FROM CUPTI_ACTIVITY_KIND_DRIVER", lambda r: 0.6)
collect_tiles("overhead", "SELECT start, end FROM PROFILER_OVERHEAD", lambda r: 0.5)
events.sort(key=lambda e: e[0]) # Sort by time
# --- Setup Matplotlib 3D ---
fig = plt.figure(figsize=(14, 7))
ax = fig.add_subplot(111, projection='3d')
fig.patch.set_facecolor("black")
ax.set_facecolor("black")
# Clean background
ax.xaxis.pane.set_visible(False)
ax.yaxis.pane.set_visible(False)
ax.zaxis.pane.set_visible(False)
for axis in [ax.xaxis, ax.yaxis, ax.zaxis]:
axis._axinfo["grid"]["linewidth"] = 0
axis._axinfo["tick"]["in"] = False
axis._axinfo["tick"]["out"] = False
axis._axinfo["tick"]["color"] = (1, 1, 1, 0)
axis._axinfo["axisline"]["color"] = (1, 1, 1, 0)
ax.set_xlim(time_start, time_end)
ax.set_ylim(-1, len(yticks))
ax.set_zlim(0, 5)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
ax.grid(False)
# Title (neon style)
ax.set_title("🔮 GPU Timeline — Neon Voxel Animation", fontsize=14, color="white")
# --- Legend ---
legend_elements = [
Patch(facecolor=colors[name], edgecolor='white', label=name)
for name in yticks.keys()
]
ax.legend(
handles=legend_elements,
loc='upper left',
bbox_to_anchor=(1.05, 1),
fontsize=9,
title="Activity Type",
title_fontsize=10,
frameon=False
)
# --- Animation Logic ---
bars = []
def animate(frame):
if frame >= len(events):
return
x, y, z, color = events[frame]
bar = ax.bar3d(
x, y, 0,
bin_width * 0.95, 0.6, z,
color=color,
edgecolor="white",
linewidth=0.3,
alpha=0.9
)
bars.append(bar)
# --- Animate ---
anim = FuncAnimation(fig, animate, frames=len(events), interval=10, repeat=False)
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment