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 ¶
- Constants
- Variables
- func Agree(a, b SMRank) float64
- func Buffers(l2, sms int) (map[string]int, error)
- func CodeSource(n int) string
- func CodeState(n int, seed uint32, block int) uint64
- func Defines(have map[string]bool) []string
- func Report(w io.Writer, samples []Sample, arms []Arm) error
- func SMMetrics(samples []SMSample) []string
- func SMRankCompute(seed uint32) uint32
- func SMRankEnd(nodes, steps int) uint32
- func SMRankEnds(smid, nodes int) uint32
- func Seed(buf string) uint32
- func SpellSource(s Spelling) string
- func Sum(seed uint32, r Region, sms, work int) uint32
- func SumAll(seed uint32, r Region, sms int) uint32
- func Summary(w io.Writer, ranks []SMRank) error
- func TouchSum(seed uint32, r Region, sms, line int) uint32
- type Arm
- type Emit
- type Feature
- type GroupStat
- type Log
- type Op
- type Region
- type Result
- type SMRank
- type SMSample
- type SMSection
- type SMStat
- type Sample
- type Spelling
- type Stability
- type Stats
- type Step
Examples ¶
Constants ¶
const ( NoHint = iota EvictFirst EvictLast EvictNormal )
Hints on a load, as the kernel numbers them.
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.
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.
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.
const CodeKernel = "pf_code"
CodeKernel is the name of the kernel in CodeSource.
const SpellKernel = "pf_spell"
SpellKernel is the name of the kernel in SpellSource.
const Tile = 32768
Tile is the size in bytes of one bulk copy, and of one ring slot.
Variables ¶
var CodeSizes = []int{512, 4096}
CodeSizes is the statement counts of the code kernels in the plan.
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.
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.
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.
var Source string
Source is the CUDA source of the probe's kernels.
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 ¶
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 ¶
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 ¶
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 Report ¶
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 SMRankCompute ¶
SMRankCompute returns the result of the compute chain from seed.
func SMRankEnds ¶
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 SpellSource ¶
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 ¶
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 ¶
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.
Types ¶
type Arm ¶
An Arm is a sequence of launches. Needs names the features it uses.
func Plan ¶
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.
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 ¶
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 ¶
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 ¶
Rank builds the per-SM table of one metric. Two samples of one SM in one run are averaged.
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.
type SMSection ¶
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.
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.
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.