Skip to content

Commit

Permalink
fix the work flow
Browse files Browse the repository at this point in the history
  • Loading branch information
gongchen618 committed Jan 26, 2024
1 parent 86bde55 commit ce185e1
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 120 deletions.
25 changes: 17 additions & 8 deletions accelsim_tracing/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 3 additions & 12 deletions accelsim_tracing/driver/driver_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,21 @@ 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
}

func (d *DriverBuilder) Build() (*Driver, error) {
return &Driver{
benchmark: d.benchmark,
gpu: d.gpu,
gpu: d.gpu,
}, nil
}
6 changes: 2 additions & 4 deletions accelsim_tracing/trace/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"path"
"strings"

"github.com/sarchlab/mgpusim/v3/accelsim_tracing/gpu"
)

type TraceParser struct {
Expand All @@ -18,7 +16,7 @@ type TraceParser struct {

type TraceExecs interface {
Type() string
Exec(*gpu.GPU) error
File() string
}

func NewTraceParser(path string) *TraceParser {
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion accelsim_tracing/trace/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
19 changes: 5 additions & 14 deletions accelsim_tracing/trace/kernel.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 0 additions & 7 deletions accelsim_tracing/trace/memory_copy.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions accelsim_tracing/trace/thread_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
74 changes: 8 additions & 66 deletions accelsim_tracing/trace/trace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,33 @@ 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
for tg.scanner.Scan() {
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())
Expand Down
52 changes: 52 additions & 0 deletions accelsim_tracing/trace/trace_group_builder.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion accelsim_tracing/trace/trace_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type traceHeader struct {
parent *traceGroup
parent *traceGroupReader
rawContext struct {
kernelName string
kernelID string
Expand Down
2 changes: 1 addition & 1 deletion accelsim_tracing/trace/warp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type warp struct {
parent *threadBlock
parent *ThreadBlock
rawContext struct {
warpID string
instsCount string
Expand Down
4 changes: 2 additions & 2 deletions accelsim_tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ce185e1

Please sign in to comment.