diff --git a/accelsim_tracing/driver/driver.go b/accelsim_tracing/driver/driver.go index 7050229a..40ab3bf2 100644 --- a/accelsim_tracing/driver/driver.go +++ b/accelsim_tracing/driver/driver.go @@ -4,24 +4,33 @@ import ( "errors" "github.com/sarchlab/mgpusim/v3/accelsim_tracing/gpu" + trace "github.com/sarchlab/mgpusim/v3/accelsim_tracing/trace" ) type Driver struct { - benchmark *Benchmark - gpu *gpu.GPU + gpu *gpu.GPU } -func (d *Driver) Exec() error { - if d.benchmark == nil { +func (d *Driver) Exec(bm *Benchmark) error { + if bm == nil { return errors.New("no trace parser specified") } else if d.gpu == nil { return errors.New("no gpu specified") } - for _, e := range *d.benchmark.TraceExecs { - err := e.Exec(d.gpu) - if err != nil { - return err + for _, e := range *bm.TraceExecs { + if e.Type() == "memcopy" { + // [todo] implement + } else if e.Type() == "kernel" { + builder := trace.NewTraceGroupReaderBuilder().WithFilePath(e.File()) + tgReader := builder.Build() + + tgReader.ParseThreadBlocks() + for it := tgReader.ThreadBlockQueue.Front(); it != nil; it = it.Next() { + d.gpu.RunThreadBlock(it.Value.(*trace.ThreadBlock).GenerateNVThreadBlock()) + } + + tgReader.File.Close() } } return nil diff --git a/accelsim_tracing/driver/driver_builder.go b/accelsim_tracing/driver/driver_builder.go index 34708e2c..9811329b 100644 --- a/accelsim_tracing/driver/driver_builder.go +++ b/accelsim_tracing/driver/driver_builder.go @@ -3,22 +3,14 @@ package driver import "github.com/sarchlab/mgpusim/v3/accelsim_tracing/gpu" type DriverBuilder struct { - benchmark *Benchmark - gpu *gpu.GPU + gpu *gpu.GPU } func NewDriverBuilder() *DriverBuilder { return &DriverBuilder{ - benchmark: nil, - gpu: nil, + gpu: nil, } } - -func (d *DriverBuilder) WithBenchmark(b *Benchmark) *DriverBuilder { - d.benchmark = b - return d -} - func (d *DriverBuilder) WithGPU(g *gpu.GPU) *DriverBuilder { d.gpu = g return d @@ -26,7 +18,6 @@ func (d *DriverBuilder) WithGPU(g *gpu.GPU) *DriverBuilder { func (d *DriverBuilder) Build() (*Driver, error) { return &Driver{ - benchmark: d.benchmark, - gpu: d.gpu, + gpu: d.gpu, }, nil } diff --git a/accelsim_tracing/trace/builder.go b/accelsim_tracing/trace/builder.go index bed97c97..f7fae5d2 100644 --- a/accelsim_tracing/trace/builder.go +++ b/accelsim_tracing/trace/builder.go @@ -7,8 +7,6 @@ import ( "os" "path" "strings" - - "github.com/sarchlab/mgpusim/v3/accelsim_tracing/gpu" ) type TraceParser struct { @@ -18,7 +16,7 @@ type TraceParser struct { type TraceExecs interface { Type() string - Exec(*gpu.GPU) error + File() string } func NewTraceParser(path string) *TraceParser { @@ -77,7 +75,7 @@ func parseListToTraceExecs(rawText string, trace *TraceParser) TraceExecs { format : kernel name example : kernel_0 */ - k := &kernel{ + k := &Kernel{ rawText: rawText, fileName: rawText, filePath: path.Join(trace.traceDirPath, rawText), diff --git a/accelsim_tracing/trace/convert.go b/accelsim_tracing/trace/convert.go index aa1fdca2..a592cb32 100644 --- a/accelsim_tracing/trace/convert.go +++ b/accelsim_tracing/trace/convert.go @@ -2,7 +2,7 @@ package trace import "github.com/sarchlab/mgpusim/v3/accelsim_tracing/nvidia" -func (tb *threadBlock) generateNVThreadBlock() *nvidia.ThreadBlock { +func (tb *ThreadBlock) GenerateNVThreadBlock() *nvidia.ThreadBlock { nvtb := &nvidia.ThreadBlock{ WarpNum: len(tb.warps), } diff --git a/accelsim_tracing/trace/kernel.go b/accelsim_tracing/trace/kernel.go index 0ff5f012..d5087ecb 100644 --- a/accelsim_tracing/trace/kernel.go +++ b/accelsim_tracing/trace/kernel.go @@ -1,25 +1,16 @@ package trace -import ( - "github.com/sarchlab/mgpusim/v3/accelsim_tracing/gpu" -) - -type kernel struct { // trace execs interface +type Kernel struct { // trace execs interface rawText string fileName string filePath string - traceGroup *traceGroup + traceGroup *traceGroupReader } -func (te *kernel) Type() string { +func (te *Kernel) Type() string { return "kernel" } -func (te *kernel) Exec(gpu *gpu.GPU) error { - tg := NewTraceGroup().WithFilePath(te.filePath) - tg.Build() - - err := tg.Exec(gpu) - - return err +func (te *Kernel) File() string { + return te.fileName } diff --git a/accelsim_tracing/trace/memory_copy.go b/accelsim_tracing/trace/memory_copy.go index b2e0bea9..065d1373 100644 --- a/accelsim_tracing/trace/memory_copy.go +++ b/accelsim_tracing/trace/memory_copy.go @@ -1,7 +1,5 @@ package trace -import "github.com/sarchlab/mgpusim/v3/accelsim_tracing/gpu" - type memCopy struct { // trace execs interface rawText string h2d bool @@ -16,8 +14,3 @@ func (te *memCopy) Type() string { func (te *memCopy) File() string { return "" } - -func (te *memCopy) Exec(g *gpu.GPU) error { - // [todo] implement - return nil -} diff --git a/accelsim_tracing/trace/thread_block.go b/accelsim_tracing/trace/thread_block.go index 9c96537c..78d6c6e4 100644 --- a/accelsim_tracing/trace/thread_block.go +++ b/accelsim_tracing/trace/thread_block.go @@ -8,8 +8,8 @@ import ( "github.com/sarchlab/mgpusim/v3/accelsim_tracing/nvidia" ) -type threadBlock struct { - parent *traceGroup +type ThreadBlock struct { + parent *traceGroupReader rawContext struct { blockDim string } @@ -18,8 +18,8 @@ type threadBlock struct { warps []*warp } -func parseThreadBlocks(lines []string) *threadBlock { - tb := &threadBlock{} +func parseThreadBlocks(lines []string) *ThreadBlock { + tb := &ThreadBlock{} dim := parseThreadBlockDim(lines) tb.threadBlockDim = *dim for i, line := range lines { diff --git a/accelsim_tracing/trace/trace_group.go b/accelsim_tracing/trace/trace_group.go index edd930a1..7d18255c 100644 --- a/accelsim_tracing/trace/trace_group.go +++ b/accelsim_tracing/trace/trace_group.go @@ -3,83 +3,25 @@ package trace import ( "bufio" "container/list" - "log" "os" "strings" "github.com/sarchlab/mgpusim/v3/accelsim_tracing/gpu" ) -type traceGroup struct { - filePath string - file *os.File - scanner *bufio.Scanner - hasParsedTraceHeader bool - traceHeader *traceHeader - threadBlockQueue *list.List +type traceGroupReader struct { + File *os.File + scanner *bufio.Scanner + traceHeader *traceHeader + ThreadBlockQueue *list.List } -func NewTraceGroup() *traceGroup { - return &traceGroup{ - filePath: "", - threadBlockQueue: list.New(), - } -} - -func (tg *traceGroup) WithFilePath(path string) *traceGroup { - tg.filePath = path - return tg -} - -func (tg *traceGroup) Build() { - tg.buildFileScanner() - tg.parseTraceHeader() -} - -func (tg *traceGroup) Exec(gpu *gpu.GPU) error { +func (tg *traceGroupReader) Read(gpu *gpu.GPU) error { // [todo] threadblocks can be parallelized to save memory - tg.parseThreadBlocks() - - for it := tg.threadBlockQueue.Front(); it != nil; it = it.Next() { - gpu.RunThreadBlock(it.Value.(*threadBlock).generateNVThreadBlock()) - } - - tg.file.Close() return nil } -func (tg *traceGroup) buildFileScanner() { - file, err := os.Open(tg.filePath) - if err != nil { - log.Panic(err) - } - tg.file = file // [note] close after exec - tg.scanner = bufio.NewScanner(file) -} - -func (tg *traceGroup) parseTraceHeader() { - if tg.hasParsedTraceHeader { - return - } - - headerLines := make([]string, 0) - for tg.scanner.Scan() { // [note] get prefix lines that start with "-" - if strings.HasPrefix(tg.scanner.Text(), "-") { - headerLines = append(headerLines, tg.scanner.Text()) - } else if tg.scanner.Text() != "" { - break - } - } - - tg.traceHeader = parseHeaderParam(headerLines) - tg.hasParsedTraceHeader = true - tg.traceHeader.parent = tg -} - -func (tg *traceGroup) parseThreadBlocks() { - if !tg.hasParsedTraceHeader { - log.Panic("Trace header has not been parsed") - } +func (tg *traceGroupReader) ParseThreadBlocks() { for tg.scanner.Scan() { if strings.TrimSpace(tg.scanner.Text()) == "#BEGIN_TB" { threadBlocklines := make([]string, 0) // [note] store whole lines of a thread block @@ -87,7 +29,7 @@ func (tg *traceGroup) parseThreadBlocks() { if strings.TrimSpace(tg.scanner.Text()) == "#END_TB" { tb := parseThreadBlocks(threadBlocklines) tb.parent = tg - tg.threadBlockQueue.PushBack(tb) + tg.ThreadBlockQueue.PushBack(tb) break } threadBlocklines = append(threadBlocklines, tg.scanner.Text()) diff --git a/accelsim_tracing/trace/trace_group_builder.go b/accelsim_tracing/trace/trace_group_builder.go new file mode 100644 index 00000000..c5667bcd --- /dev/null +++ b/accelsim_tracing/trace/trace_group_builder.go @@ -0,0 +1,52 @@ +package trace + +import ( + "bufio" + "log" + "os" + "strings" +) + +type traceGroupReaderBuilder struct { + filePath string +} + +func NewTraceGroupReaderBuilder() *traceGroupReaderBuilder { + return &traceGroupReaderBuilder{ + filePath: "", + } +} + +func (tg *traceGroupReaderBuilder) WithFilePath(path string) *traceGroupReaderBuilder { + tg.filePath = path + return tg +} + +func (tg *traceGroupReaderBuilder) Build() *traceGroupReader { + tgReader := &traceGroupReader{} + tg.buildFileScanner(tgReader) + tg.parseTraceHeader(tgReader) + return tgReader +} + +func (tg *traceGroupReaderBuilder) buildFileScanner(tgReader *traceGroupReader) { + file, err := os.Open(tg.filePath) + if err != nil { + log.Panic(err) + } + tgReader.File = file // [note] close after exec + tgReader.scanner = bufio.NewScanner(file) +} + +func (tg *traceGroupReaderBuilder) parseTraceHeader(tgReader *traceGroupReader) { + headerLines := make([]string, 0) + for tgReader.scanner.Scan() { // [note] get prefix lines that start with "-" + if strings.HasPrefix(tgReader.scanner.Text(), "-") { + headerLines = append(headerLines, tgReader.scanner.Text()) + } else if tgReader.scanner.Text() != "" { + break + } + } + + tgReader.traceHeader = parseHeaderParam(headerLines) +} diff --git a/accelsim_tracing/trace/trace_header.go b/accelsim_tracing/trace/trace_header.go index d43b7462..7024b27a 100644 --- a/accelsim_tracing/trace/trace_header.go +++ b/accelsim_tracing/trace/trace_header.go @@ -9,7 +9,7 @@ import ( ) type traceHeader struct { - parent *traceGroup + parent *traceGroupReader rawContext struct { kernelName string kernelID string diff --git a/accelsim_tracing/trace/warp.go b/accelsim_tracing/trace/warp.go index a95515dc..600bcfa0 100644 --- a/accelsim_tracing/trace/warp.go +++ b/accelsim_tracing/trace/warp.go @@ -10,7 +10,7 @@ import ( ) type warp struct { - parent *threadBlock + parent *ThreadBlock rawContext struct { warpID string instsCount string diff --git a/accelsim_tracing/tracer.go b/accelsim_tracing/tracer.go index 54ea8f1d..9f4dd703 100644 --- a/accelsim_tracing/tracer.go +++ b/accelsim_tracing/tracer.go @@ -42,9 +42,9 @@ func main() { myGPU := buildAmpereGPU() - driverBuilder := driver.NewDriverBuilder().WithBenchmark(myBenchmark).WithGPU(myGPU) + driverBuilder := driver.NewDriverBuilder().WithGPU(myGPU) myDriver, _ := driverBuilder.Build() - myDriver.Exec() + myDriver.Exec(myBenchmark) } func buildAmpereGPU() *gpu.GPU {