prefetchprobe

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package prefetchprobe measures L2 and global memory prefetch latency and SM rank behavior.

This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.

Index

Examples

Constants

View Source
const (
	NoHint = iota
	EvictFirst
	EvictLast
	EvictNormal
)

Hints on a load, as the kernel numbers them.

View Source
const (
	NoPrefetch = iota
	BulkPrefetch
	LinePrefetch
	TouchPrefetch
	MarkEvictLast
	MarkEvictNormal
	CopyPrefetch // bulk copies into a shared-memory slot nobody reads
)

What the spare warp does, as the kernel numbers it.

View Source
const (
	SMRankNodes   = 1024 // nodes of the global chain
	SMRankLine    = 256  // bytes between them
	SMRankSMNodes = 1024 // nodes of the shared-memory chain
	SMRankOps     = 4096 // steps of the compute chain
	SMRankSlots   = 16   // output words per block
)

Constants of SMRankSource. The global chain has SMRankNodes nodes SMRankLine bytes apart unless the source is compiled with SR_N and SR_LINE defined otherwise.

View Source
const (
	SMRankSliceSlot   = 13 // the chain slice the block used
	SMRankComputeSlot = 14 // the compute chain's result
	SMRankCtlSlot     = 12 // the control section
)

Output words of sr_map that are not sections.

View Source
const CodeKernel = "pf_code"

CodeKernel is the name of the kernel in CodeSource.

View Source
const SpellKernel = "pf_spell"

SpellKernel is the name of the kernel in SpellSource.

View Source
const Tile = 32768

Tile is the size in bytes of one bulk copy, and of one ring slot.

Variables

View Source
var CodeSizes = []int{512, 4096}

CodeSizes is the statement counts of the code kernels in the plan.

View Source
var Features = []Feature{
	{0, "globaltimer", false, "mov.u64 %globaltimer"},
	{1, "bulk-copy", false, "cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes"},
	{2, "bulk-copy-evict-first", true, "createpolicy.fractional.L2::evict_first.b64; cp.async.bulk...bytes.L2::cache_hint"},
	{3, "bulk-copy-evict-last", true, "createpolicy.fractional.L2::evict_last.b64; cp.async.bulk...bytes.L2::cache_hint"},
	{4, "ld-evict-first", true, "createpolicy.fractional.L2::evict_first.b64; ld.global.L2::cache_hint.b32"},
	{5, "ld-evict-last", true, "createpolicy.fractional.L2::evict_last.b64; ld.global.L2::cache_hint.b32"},
	{6, "bulk-prefetch", false, "cp.async.bulk.prefetch.L2.global"},
	{7, "bulk-prefetch-evict-last", true, "cp.async.bulk.prefetch.L2.global.L2::cache_hint"},
	{8, "line-prefetch", false, "prefetch.global.L2"},
	{9, "line-prefetch-evict-last", false, "prefetch.global.L2::evict_last"},
	{10, "line-prefetch-evict-normal", false, "prefetch.global.L2::evict_normal"},
	{11, "ld-l1-evict-first", false, "ld.global.L1::evict_first.b32"},
	{12, "bulk-copy-evict-normal", true, "createpolicy.fractional.L2::evict_normal.b64; cp.async.bulk...bytes.L2::cache_hint"},
}

Features lists what the device test tries before it measures anything. Feature 0 is not an instruction: it measures the timer's step.

View Source
var SMRankSource string

SMRankSource is the CUDA source of the per-SM ranking kernels: sr_map times a table of small sections on one thread per SM, keyed by %smid.

View Source
var SMSections = []SMSection{
	{Metric: "compute_ns", Slot: 4, Ops: true},
	{Metric: "smem_load_ns", Slot: 5, Shared: true},
	{Metric: "l2_miss_ns", Slot: 6},
	{Metric: "l2_hit_ns", Slot: 7},
	{Metric: "l2_hit_3rd_ns", Slot: 8},
	{Metric: "l2_hit_4th_ns", Slot: 9},
	{Metric: "plain_ns", Slot: 10},
	{Metric: "plain_again_ns", Slot: 11},
	{Metric: "l2_hit_ctl_ns", Slot: 12},
}

SMSections lists the sections. The global chain's loads do not allocate in L1 except in the two plain sections. l2_hit_ctl_ns is the positive control: SMs whose number is a multiple of 4 take twice the steps it is divided by, so they must read twice what the others do.

View Source
var Source string

Source is the CUDA source of the probe's kernels.

View Source
var Spellings = []Spelling{
	{Name: "plain", Load: "v = *p;"},
	{Name: "plain-restrict", Restrict: true, Load: "v = *p;"},
	{Name: "ldcs", Restrict: true, Load: "v = __ldcs(p);"},
	{Name: "ldcg", Restrict: true, Load: "v = __ldcg(p);"},
	{Name: "ldca", Restrict: true, Load: "v = __ldca(p);"},
	{Name: "ldlu", Restrict: true, Load: "v = __ldlu(p);"},
	{Name: "ldcv", Restrict: true, Load: "v = __ldcv(p);"},
	{Name: "ldg", Restrict: true, Load: "v = __ldg(p);"},
	{Name: "asm", Restrict: true, Load: asmLoad("", "")},
	{Name: "asm-nc", Restrict: true, Load: asmLoad(".nc", "")},
	{Name: "asm-cs", Restrict: true, Load: asmLoad(".cs", "")},
	{Name: "asm-cs-nc", Restrict: true, Load: asmLoad(".cs.nc", "")},
	{Name: "asm-l2-evict-first", Restrict: true, Load: asmLoad(".L2::evict_first", "")},
	{Name: "asm-nc-l2-evict-first", Restrict: true, Load: asmLoad(".nc.L2::evict_first", "")},
	{Name: "asm-l1-no-allocate", Restrict: true, Load: asmLoad(".L1::no_allocate", "")},
	{Name: "asm-l1-no-allocate-l2-evict-first", Restrict: true, Load: asmLoad(".L1::no_allocate.L2::evict_first", "")},
	{Name: "policy-evict-first", Restrict: true, Load: asmLoad(".L2::cache_hint", ", %5")},
	{Name: "policy-nc-evict-first", Restrict: true, Load: asmLoad(".nc.L2::cache_hint", ", %5")},
	{Name: "wide", Restrict: true, Load: asmWide(""), Wide: true},
	{Name: "wide-nc", Restrict: true, Load: asmWide(".nc"), Wide: true},
	{Name: "wide-l2-evict-first", Restrict: true, Load: asmWide(".L2::evict_first"), Wide: true},
	{Name: "wide-nc-l2-evict-first", Restrict: true, Load: asmWide(".nc.L2::evict_first"), Wide: true},
	{Name: "wide-l1-no-allocate-l2-evict-first", Restrict: true, Load: asmWide(".L1::no_allocate.L2::evict_first"), Wide: true},
}

Spellings lists the loads the device test compiles, disassembles and times. The first is the control: a plain load through a pointer the compiler may not assume is read-only.

Functions

func Agree

func Agree(a, b SMRank) float64

Agree returns the Spearman correlation of the SMs' medians in a and in b, over the SMs both have: whether two metrics, or one metric measured two ways, order the SMs alike.

func Buffers

func Buffers(l2, sms int) (map[string]int, error)

Buffers returns the size of each buffer in tiles per block, for an L2 of l2 bytes read by sms blocks: D, the stream that disturbs the cache, is 8 times L2; W, the weights, and X, the wrong guess, 4 times; A, the preceding operation's weights, half; M, the region that must stay, an eighth; K, 32 more regions of M's size, 4 times.

func CodeSource

func CodeSource(n int) string

CodeSource returns the source of a kernel that is n distinct straight-line statements long and runs them twice.

void pf_code(unsigned long long* out, unsigned seed)

Thread 0 of block k writes out[4k..4k+3]: the %globaltimer nanoseconds of the first pass and of the second, the SM number plus one, and the final state, which CodeState predicts. The first pass fetches the code; the second finds it where the first left it.

func CodeState

func CodeState(n int, seed uint32, block int) uint64

CodeState returns the state word thread 0 of the given block writes.

func Defines

func Defines(have map[string]bool) []string

Defines returns the macros to compile Source with when the named features work.

func Report

func Report(w io.Writer, samples []Sample, arms []Arm) error

Report prints the statistics of every arm, one table per probe. The verdict column is the arm's difference from its control, or "unresolved" when that is within the null spread.

func SMMetrics

func SMMetrics(samples []SMSample) []string

SMMetrics returns the distinct arms of the samples, in first-seen order.

func SMRankCompute

func SMRankCompute(seed uint32) uint32

SMRankCompute returns the result of the compute chain from seed.

func SMRankEnd

func SMRankEnd(nodes, steps int) uint32

SMRankEnd returns the node a chain of nodes nodes ends on after steps steps.

func SMRankEnds

func SMRankEnds(smid, nodes int) uint32

SMRankEnds returns the sum sr_map reports for the ends of its chains on SM smid, for a global chain of nodes nodes: the shared-memory chain, six global chains, and the control, which is twice as long on SMs whose number is a multiple of 4.

func Seed

func Seed(buf string) uint32

Seed returns the fill seed of a buffer: pf_fill writes word i as i*2654435761 + seed.

func SpellSource

func SpellSource(s Spelling) string

SpellSource returns the source of a kernel that streams a region through s.

void pf_spell(const uint4* w, unsigned long long vecs, unsigned long long* stamps, unsigned* sums)

Block k reads vecs 16-byte vectors starting k*vecs in, in groups of four, thread t taking the groups whose number is t mod 256. Thread 0 writes stamps[4k..4k+2], the %globaltimer before and after and the SM number plus one, and sums[k], the sum of the block's words.

func Sum

func Sum(seed uint32, r Region, sms, work int) uint32

Sum returns what the blocks' sums of one operation on r add up to, with sms blocks, a buffer filled with seed, and work passes over each tile.

func SumAll

func SumAll(seed uint32, r Region, sms int) uint32

SumAll returns the sum of every word of r, with sms blocks and a buffer filled with seed: what the blocks' sums of pf_spell add up to.

func Summary

func Summary(w io.Writer, ranks []SMRank) error

Summary writes one line per metric: the spread of the SMs' medians and the stability statistics, real and shuffled.

func TouchSum

func TouchSum(seed uint32, r Region, sms, line int) uint32

TouchSum returns what the spare warps' sums add up to when they read one word every line bytes of r.

Types

type Arm

type Arm struct {
	Name  string
	Needs []string
	Steps []Step
}

An Arm is a sequence of launches. Needs names the features it uses.

func Plan

func Plan(l2, sms int) ([]Arm, error)

Plan returns the arms for an L2 of l2 bytes read by sms blocks.

Example

The plan's first arm reads the smallest region cold: a stream of 8 times L2, then one operation on a sixteenth of L2.

package main

import (
	"fmt"
	"github.com/tmc/lib/nvidia/x/obs/diag/prefetchprobe"
)

func main() {
	arms, err := prefetchprobe.Plan(24<<20, 48)
	if err != nil {
		fmt.Println(err)
		return
	}
	a := arms[0]
	fmt.Println(a.Name)
	for _, st := range a.Steps {
		fmt.Printf("read %d tiles per block of %s\n", st.Op.B.Tiles, st.Op.B.Buf)
	}
}
Output:
a.cold/1.5MiB
read 128 tiles per block of D
read 1 tiles per block of W

type Emit

type Emit struct {
	Phase   byte   // 'A', 'B', or 'T' for the two together; of pf_code, 'F' or 'S' for the first or second pass
	Probe   string // "P6a" ... "P6e"
	Arm     string
	Control string // arm compared against; empty for a control
	Null    string // arm that repeats NullOf
	NullOf  string // if empty, Control
	IsNull  bool
}

An Emit names the sample one operation of a step produces.

type Feature

type Feature struct {
	ID     int
	Name   string
	Policy bool   // needs createpolicy
	Asm    string // the instruction tried
}

A Feature is an instruction the kernels use only if the device assembles and runs it. ID selects it in the kernel's pf_feat.

type GroupStat

type GroupStat struct {
	Group            string
	SMs              int
	Median, Min, Max float64 // of the group's SMs' medians
}

A GroupStat is the statistics of the SMs of one named group.

type Log

type Log struct {
	// contains filtered or unexported fields
}

A Log appends samples to a new JSONL file.

func Create

func Create(path string) (*Log, error)

Create creates the file at path. It fails if the file exists.

func (*Log) Close

func (l *Log) Close() error

Close closes the file.

func (*Log) Write

func (l *Log) Write(s Sample) error

Write appends s, setting its Order to its position in the file.

func (*Log) WriteSM

func (l *Log) WriteSM(s SMSample) error

WriteSM appends s, setting its Order to its position in the file.

type Op

type Op struct {
	A, B, P      Region
	AHint, BHint int
	Ahead        bool // B fills the ring one tile ahead
	Mode         int  // what the spare warp does with P
	Line         int  // bytes between the spare warp's per-line prefetches
	Work         int  // passes over each tile
	AWork        int  // passes over each tile of A; if zero, Work
}

An Op is one launch of pf_op: operation A, then operation B, with the spare warp working on P while A runs.

type Region

type Region struct {
	Buf   string
	Tiles int
	Off   int
}

A Region is each block's slice of a named buffer, in tiles. The slices are those of the part of the buffer that starts Off tiles per block in. The zero Region is empty.

type Result

type Result struct {
	Emit
	Stats
	Ratio float64 // median over the control's median; 0 for a control

	// NullSpread is how far the null arm, which repeats the control,
	// lands from the control, relative to the control's median: the
	// larger of the two medians' difference and the median difference
	// between the two in the same sample.
	NullSpread float64
	Resolved   bool // |Ratio-1| > NullSpread
}

A Result is one arm's statistics and its comparison with its control.

func Compare

func Compare(samples []Sample, arms []Arm) ([]Result, error)

Compare summarizes the samples of every emit of the arms, in plan order, skipping emits with no samples. An arm whose control or null has no samples is an error.

Example

An arm is compared with its control, and with how far a repeat of the control lands from it.

package main

import (
	"fmt"
	"github.com/tmc/lib/nvidia/x/obs/diag/prefetchprobe"
	"strings"
)

func main() {
	const jsonl = `
{"probe":"P6d","arm":"d.cold","sample":0,"ns":12000}
{"probe":"P6d","arm":"d.cold-null","null":true,"sample":0,"ns":12100}
{"probe":"P6d","arm":"d.warm","sample":0,"ns":2800}
{"probe":"P6d","arm":"d.cold","sample":1,"ns":12200}
{"probe":"P6d","arm":"d.cold-null","null":true,"sample":1,"ns":12000}
{"probe":"P6d","arm":"d.warm","sample":1,"ns":2700}
`
	samples, err := prefetchprobe.ReadSamples(strings.NewReader(jsonl))
	if err != nil {
		fmt.Println(err)
		return
	}
	arms, err := prefetchprobe.Plan(24<<20, 48)
	if err != nil {
		fmt.Println(err)
		return
	}
	res, err := prefetchprobe.Compare(samples, arms)
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, r := range res[1:] {
		fmt.Printf("%s: %.0f ns, %.2f of %s, null spread %.2f, resolved %v\n", r.Arm, r.Median, r.Ratio, r.Control, r.NullSpread, r.Resolved)
	}
}
Output:
d.cold-null: 12050 ns, 1.00 of d.cold, null spread 0.01, resolved false
d.warm: 2750 ns, 0.23 of d.cold, null spread 0.01, resolved true

type SMRank

type SMRank struct {
	Metric           string
	Runs             int
	SMs              []SMStat // by SM number
	Min, Median, Max float64  // of the SMs' medians
	Real             Stability
	// Shuffled is the same statistics with the SM labels permuted within
	// each run: what a table with no per-SM difference looks like.
	Shuffled Stability
}

An SMRank is a metric's per-SM table.

func Rank

func Rank(samples []SMSample, metric string) (SMRank, error)

Rank builds the per-SM table of one metric. Two samples of one SM in one run are averaged.

func (SMRank) Groups

func (r SMRank) Groups(groups map[string][]int) ([]GroupStat, float64)

Groups summarizes r over named sets of SMs, in name order, and returns with them the spread of the groups' medians over the median spread within a group, each spread a maximum less a minimum. The groups differ resolvably only if that ratio is above 1.

func (SMRank) Table

func (r SMRank) Table(w io.Writer) error

Table writes the per-SM table of r: each SM's median and its range over the runs.

type SMSample

type SMSample struct {
	Sample
	SMID int `json:"smid"`
	N    int `json:"n"` // steps the span was divided by
}

An SMSample is one line of the ranking's JSONL output: one metric of one SM in one run. Arm is the metric with its variant, such as "l2_hit_ns/all/r0": every SM measuring at once, address range 0. NS is device time per step.

func ReadSMSamples

func ReadSMSamples(r io.Reader) ([]SMSample, error)

ReadSMSamples reads JSONL samples from r.

type SMSection

type SMSection struct {
	Metric string
	Slot   int
	Ops    bool
	Shared bool
}

An SMSection is one timed section of sr_map: the output word that holds its span. The span is of SMRankOps steps if Ops is set, of SMRankSMNodes-1 if Shared is, and otherwise of one fewer than the global chain has nodes.

func (SMSection) Steps

func (s SMSection) Steps(nodes int) int

Steps returns the steps the section's span is divided by, for a global chain of nodes nodes.

type SMStat

type SMStat struct {
	SMID             int
	N                int
	Median, Min, Max float64
}

An SMStat is one SM's statistics for a metric over the runs.

type Sample

type Sample struct {
	Probe       string  `json:"probe"`
	Arm         string  `json:"arm"`
	Null        bool    `json:"null"`
	Sample      int     `json:"sample"`
	Order       int     `json:"order"`
	NS          float64 `json:"ns"`
	Unit        string  `json:"unit"`
	SMs         int     `json:"sms"`
	CodeBytes   int     `json:"code_bytes"`
	CubinSHA256 string  `json:"cubin_sha256"`
	Driver      string  `json:"driver"`
	Toolkit     string  `json:"toolkit"`
	Note        string  `json:"note"`
}

A Sample is one line of the probe's JSONL output. NS is device time.

func ReadSamples

func ReadSamples(r io.Reader) ([]Sample, error)

ReadSamples reads JSONL samples from r.

type Spelling

type Spelling struct {
	Name     string
	Restrict bool // the kernel's pointer is const and __restrict__
	Load     string
	Wide     bool
}

A Spelling is one way to write a 16-byte global load that asks for a cache policy. Load is a C statement that sets the uint4 v from the pointer p; it may use policy, an evict_first descriptor. A Wide spelling loads 32 bytes instead, the only width the assembler takes an L2 eviction priority on: its Load sets the four 64-bit words q0..q3.

type Stability

type Stability struct {
	// RankCorr is the Spearman correlation between the SMs' medians over
	// the odd runs and over the even runs: near 1 if the same SMs are
	// fast in both halves, near 0 if not.
	RankCorr float64
	// VarRatio is the variance of the SMs' means over the mean of the
	// SMs' run-to-run variances. Identical SMs give about 1/runs.
	VarRatio float64
}

Stability says whether the SMs differ from one another by more than each differs from itself.

type Stats

type Stats struct {
	N                int
	Median, Min, Max float64
	CV               float64 // standard deviation over mean
}

Stats summarizes one arm's samples.

type Step

type Step struct {
	Op    Op
	Emit  []Emit
	Reset bool
	Spell string
	Code  int
}

A Step is one launch and the samples taken from it: of pf_op, or with Spell set of that spelling's pf_spell over Op.B, or with Code set of the pf_code of that many statements. With Reset set there is no launch but a call of cuCtxResetPersistingL2Cache.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL