Documentation
¶
Overview ¶
Package racedet reports shared memory data races in CUDA kernels in one form, whichever engine found them.
Three engines find races by happens-before:
- hostemu.CheckHazards runs a paired emulator source on host threads (FromHazard converts its races);
- hostemu's detsched runs it on deterministic fibers (FromDet);
- the device engine records a Trace of a launch's shared memory accesses and barriers.
Each reports a Race: two accesses to the same shared memory bytes of one CTA, at least one a write, that no happens-before edge orders. The only edges are program order and barriers; an access's memory order and scope are not recorded. So a Race is a conflict that no barrier orders, which is wider than a data race in the PTX memory model: two conflicting .volatile accesses (relaxed at system scope there) are morally strong and not a data race, but racedet reports them.
A Trace is the device engine's record of one launch: every shared memory load and store and every bar.sync of each thread, in the order the device logged them. Its binary form is versioned; Decode refuses a trace that dropped events, since a race among the dropped events would go unseen.
Replay checks a trace under detsched's rules: a fixed replay kernel, built through hostemu's public detsched API, makes each thread's accesses and barriers in program order, keeping the trace's order of accesses within each CTA. It covers shared memory and bar.sync 0 over the whole CTA, and refuses anything else. Replaying through detsched, not hazard.h, leaves cudahostemu.h untouched; detsched models no mbarrier, so mbarrier traces would need hazard.h.
Instrument is a ptxpass.Pass that inserts calls to a device runtime before every shared memory load and store and after every bar.sync 0: the off runtime does nothing, the trace runtime records a Trace.
Run is the device engine: it instruments a module with the trace runtime, links it with nvJitLink, launches it, reads the trace back and replays it. Trace mode is for small launches; a trace that dropped events voids the result (ErrVoid). Its checks stop at the PTX: no census yet maps the SASS memory instructions back to hooked PTX sites through lineinfo, so an access ptxas adds or moves is not caught. The trace lives in a racedet-owned buffer, not the IKFR flight ring, whose new record version would need a schema number.
The device tests need -tags racedet_physical and a GPU:
go test -tags racedet_physical -run Physical -count=1 ./x/obs/racedet
The Race type lives here rather than in hostemu so that hostemu's hazard.go and detsched.go stay unedited: the converters from their reports live in this package too.
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 ¶
const (
EventSize = 24 // bytes of one encoded event
)
The binary form is little endian: a header, then fixed-size events.
header: "RDTR" version:u32 grid:3×u32 block:3×u32 count:u64 dropped:u64 event: kind:u8 size:u8 barid:u8 0:u8 cta:u32 thread:u32 offset:u32 count:u32 ptxline:u32
The device runtime writes events in this form.
const TraceVersion = 1
TraceVersion is the version of the binary form Encode writes.
Variables ¶
var ErrDropped = errors.New("trace dropped events")
ErrDropped is returned by Decode for a trace whose device runtime ran out of room and dropped events.
var ErrVoid = errors.New("trace dropped events: race comparison is void")
ErrVoid is returned by Run when the trace buffer overflowed: the dropped events may hold races, so the launch's race set is void.
Functions ¶
This section is empty.
Types ¶
type Conflict ¶
type Conflict uint8
A Conflict is the kind of a race: the earlier access, then the later.
type Event ¶
type Event struct {
Kind EventKind
CTA int // linear CTA index in the grid
Thread int // linear thread index in the CTA
Offset int // byte offset in the CTA's shared memory (Load, Store)
Size int // bytes accessed (Load, Store)
BarID int // barrier id (Bar)
Count int // threads the barrier waits for, or 0 for the whole CTA (Bar)
PTXLine int // line of the instruction in the PTX module, or 0
}
An Event is one shared memory access or barrier of one thread.
type Instrument ¶
type Instrument struct {
Runtime Runtime
Partial bool // instrument what is in scope and count the rest in Uncovered
// Set by Run.
Accesses int // hooks inserted before loads and stores
Barriers int // hooks inserted after barriers
Uncovered map[string]int // unhooked constructs by opcode, under Partial
}
Instrument is a [ptxpass.Pass] that inserts a call to the runtime's __racedet_access before every shared memory load and store, and a call to __racedet_bar after every bar.sync 0 (or barrier.sync 0) over the whole CTA, and appends the runtime's functions to the module. A hook runs under its instruction's guard. Its arguments are the access's shared address, size and kind, or the barrier's id, and the instruction's line in the original PTX.
The accesses it hooks are ld.shared and st.shared, with .shared::cta, .volatile and .v2/.v4. A hook records no memory order, so a .volatile access is traced as a plain one. Constructs whose effect on shared memory or on ordering it does not model make Run return an error, unless Partial is set, in which case Run counts them in Uncovered and leaves them unhooked: atom and red on shared memory, cp.async and the bulk copies, mbarrier, named barriers and barriers with a thread count, bar.arrive, bar.red, bar.warp.sync, barrier.cluster, .shared::cluster accesses, ld and st with .relaxed, .acquire or .release, ldmatrix, stmatrix, wgmma, tcgen05, st.async and red.async, any other instruction with a .shared state space (such as wmma.load.shared, tensormap.replace and clusterlaunchcontrol.try_cancel), and ld, st, wmma.load, wmma.store and tensormap.replace with no state space, which may address shared memory. Address arithmetic (cvta, mapa, isspacep, getctarank) is not an access.
Synchronization through global memory is not modeled either: a handoff through a flag in global memory orders nothing in the trace, so its accesses are reported as races.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/tmc/lib/nvidia/x/isa/ptx/ptxast"
"github.com/tmc/lib/nvidia/x/isa/ptx/ptxpass"
"github.com/tmc/lib/nvidia/x/obs/racedet"
)
func main() {
const src = `.version 9.0
.target sm_121a
.address_size 64
.visible .entry k()
{
.reg .b32 %r<3>;
.shared .align 4 .b8 s[256];
mov.u32 %r1, %tid.x;
st.shared.u32 [s+12], %r1;
bar.sync 0;
ld.shared.u32 %r2, [s+12];
ret;
}
`
m, err := ptxast.ParseString(src)
if err != nil {
panic(err)
}
p := &racedet.Instrument{Runtime: racedet.RuntimeOff}
if _, err := ptxpass.Run(m, p); err != nil {
panic(err)
}
fmt.Println(p.Accesses, "accesses,", p.Barriers, "barriers")
for _, s := range m.Func("k").Body.Stmts {
if b, ok := s.(*ptxast.Block); ok {
last := b.Stmts[len(b.Stmts)-1].(*ptxast.Instr)
fmt.Println(strings.Join(strings.Fields(last.String()), " "))
}
}
}
Output: 2 accesses, 1 barriers call __racedet_access, (racedet_p0, racedet_p1, racedet_p2, racedet_p3); call __racedet_bar, (racedet_p0, racedet_p1); call __racedet_access, (racedet_p0, racedet_p1, racedet_p2, racedet_p3);
func (*Instrument) Name ¶
func (*Instrument) Name() string
type Launch ¶
type Launch struct {
Kernel string // the .entry to launch
Grid Dim3
Block Dim3
Args []unsafe.Pointer // kernel arguments, as for driver.Function.LaunchKernel
Arch string // nvJitLink target; "" means sm_121a
Capacity int // events the trace buffer holds; 0 means 1<<16
}
A Launch describes the kernel launch Run traces.
type Race ¶
type Race struct {
Conflict Conflict
Space string // "shared"
CTA int // linear index of the CTA whose shared memory both touch
Offset int // byte offset in that shared memory, or -1 if the engine does not report it
Size int // bytes both accesses touch from Offset, or 0 if unknown
Thread int // linear thread index in the CTA of the later access
Peer int // linear thread index in the CTA of the earlier access
Copy bool // either access is a cp.async or bulk copy's write
SiteA Site // the earlier access
SiteB Site // the later access
Engine Engine
Detail string // the engine's own account
}
A Race is a pair of accesses to the same shared memory bytes of one CTA, at least one a write, that no happens-before edge orders. Edges come from program order and barriers only, so a pair of conflicting .volatile accesses is a Race too; see the package documentation.
func FromDet ¶
FromDet converts a shared memory race detsched reported in a launch whose blocks have blockThreads threads. detsched numbers fibers across the grid and reports no offset, so Offset is -1. Its site ids hash a file and line; they are not inverted here.
func FromHazard ¶
FromHazard converts a race CheckHazards reported. CheckHazards reports the first racing byte of an access, so Size is 1, and it records no source sites.
func Replay ¶
Replay compiles a replay kernel in a temporary directory, replays tr and returns its races. See Replayer.Replay.
func (Race) String ¶
String formats r in the style of the Go race detector's reports.
Example ¶
package main
import (
"fmt"
"github.com/tmc/lib/nvidia/x/obs/racedet"
)
func main() {
r := racedet.Race{
Conflict: racedet.WAW,
Space: "shared",
Offset: 12,
Size: 4,
Thread: 32,
Peer: 0,
SiteA: racedet.Site{PTXLine: 30},
SiteB: racedet.Site{PTXLine: 31},
Engine: racedet.EngineDevice,
}
fmt.Print(r)
}
Output: ================== WARNING: DATA RACE (write-after-write, shared memory) Write at offset 12 (4 bytes) by thread 32 of CTA 0: PTX line 31 Previous write by thread 0 of CTA 0: PTX line 30 Engine: device ==================
type Replayer ¶
type Replayer struct {
// contains filtered or unexported fields
}
A Replayer checks traces under detsched's happens-before rules. It holds a compiled detsched worker; Close releases it.
func NewReplayer ¶
NewReplayer compiles the replay kernel in dir with the host C++ compiler. It returns an error wrapping hostemu.ErrNoCompiler when the host has none.
func (*Replayer) Replay ¶
Replay runs tr under detsched and returns the shared memory races it finds, with Engine set to EngineDevice and sites taken from the trace's PTX lines.
Each thread replays its own events in program order: a Load or Store is a tracked access of its bytes, and a Bar is __syncthreads. Accesses within a CTA keep the order of the trace, so a conflict's kind is the one the device's order gives. Since every access waits for its turn in that order and barriers complete by count, detsched's schedule cannot change the races, and the replay runs one fixed schedule.
Replay refuses, rather than skips, what detsched cannot model here: a barrier other than bar.sync 0 over the whole CTA, an access size other than 1, 2, 4, 8 or 16 bytes, an event outside the trace's grid or block, and a trace that dropped events. A trace whose barriers cannot all complete is an error too.
type Result ¶
A Result is a traced launch and the races Replay found in it.
func Run ¶
Run instruments the PTX module src with the trace runtime, links it with nvJitLink, launches it in the current CUDA context, reads the trace back and replays it (see Replay). Races carry Engine EngineDevice and the original module's PTX lines.
Trace mode records every shared memory access, so it is for small launches. If the buffer fills, Run returns the trace with ErrVoid. The instrumentation is checked against the PTX only: no census maps the SASS memory instructions back to hooked PTX sites yet, so an access ptxas introduces or a hook it moves is not detected.
Run needs a CUDA device and a host C++ compiler for the replay.
Example ¶
ExampleRun traces one launch of a kernel on the current CUDA context and prints its races. It needs a GPU, so it has no checked output.
package main
import (
"context"
"fmt"
"os"
"unsafe"
"github.com/tmc/lib/nvidia/driver"
"github.com/tmc/lib/nvidia/x/obs/racedet"
)
func main() {
src, err := os.ReadFile("testdata/litmus.sm_121a.ptx")
if err != nil {
panic(err)
}
var out driver.DevicePtr // a 256-byte device buffer, allocated by the caller
res, err := racedet.Run(context.Background(), string(src), racedet.Launch{
Kernel: "waw",
Grid: racedet.Dim3{X: 1, Y: 1, Z: 1},
Block: racedet.Dim3{X: 64, Y: 1, Z: 1},
Args: []unsafe.Pointer{unsafe.Pointer(&out)},
})
if err != nil {
panic(err)
}
for _, r := range res.Races {
fmt.Print(r)
}
}
Output:
type Site ¶
type Site struct {
File string
Line int
PTXLine int // line in the PTX module
SASSPC uint64 // offset of the SASS instruction in its function
}
A Site is the source location of an access. Its fields are zero when the engine does not know them.
type Trace ¶
type Trace struct {
Grid Dim3
Block Dim3
Events []Event
Dropped uint64 // events the device could not record
}
A Trace is a launch's shared memory accesses and barriers, in the order the device logged them. Each thread's events are in its program order.
func Decode ¶
Decode parses the binary form of a trace. It refuses a trace that dropped events, an unknown version or event kind, and input that is truncated or has trailing bytes.
Example ¶
package main
import (
"fmt"
"github.com/tmc/lib/nvidia/x/obs/racedet"
)
func main() {
tr := &racedet.Trace{
Grid: racedet.Dim3{X: 1, Y: 1, Z: 1},
Block: racedet.Dim3{X: 64, Y: 1, Z: 1},
Events: []racedet.Event{
{Kind: racedet.Store, Thread: 0, Offset: 12, Size: 4, PTXLine: 30},
{Kind: racedet.Bar, Thread: 0, PTXLine: 31},
},
}
b, err := tr.Encode()
if err != nil {
panic(err)
}
got, err := racedet.Decode(b)
if err != nil {
panic(err)
}
fmt.Println(len(b), "bytes")
for _, e := range got.Events {
fmt.Printf("%s thread %d offset %d size %d line %d\n", e.Kind, e.Thread, e.Offset, e.Size, e.PTXLine)
}
tr.Dropped = 1
b, _ = tr.Encode()
_, err = racedet.Decode(b)
fmt.Println(err)
}
Output: 96 bytes store thread 0 offset 12 size 4 line 30 bar thread 0 offset 0 size 0 line 31 decode trace: trace dropped events: 1