Skip to content

Commit

Permalink
run path needs to run the Config, and added goal.Args to access args …
Browse files Browse the repository at this point in the history
…instead of trying to use variable.
  • Loading branch information
rcoreilly committed Dec 23, 2024
1 parent 7148389 commit 5d45b49
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
6 changes: 3 additions & 3 deletions goal/GPU.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The use of massively parallel _Graphical Processsing Unit_ (_GPU_) hardware has revolutionized machine learning and other fields, producing many factors of speedup relative to traditional _CPU_ (_Central Processing Unit_) computation. However, there are numerous challenges for supporting GPU-based computation, relative to the more flexible CPU coding.

The Goal framework provides a solution to these challenges that enables the same Go-based code to work efficiently and reasonably naturally on both the GPU and CPU (i.e., standard Go execution), via the [gosl](gosl) package. Debugging code on the GPU is notoriously difficult because the usual tools are not directly available (not even print statements), so the ability to run exactly the same code on the CPU is invaluable, in addition to the benefits in portability across platforms without GPU hardware.
The Goal framework provides a solution to these challenges that enables the same Go-based code to work efficiently and reasonably naturally on both the GPU and CPU (i.e., standard Go execution), via the [gosl](../gosl) package. Debugging code on the GPU is notoriously difficult because the usual tools are not directly available (not even print statements), so the ability to run exactly the same code on the CPU is invaluable, in addition to the benefits in portability across platforms without GPU hardware.

See the [gosl](gosl) documentation for the details on how to write code that works on the GPU. The remainder of this document provides an overview of the overall approach in relation to other related tools.
See the [gosl](../gosl) documentation for the details on how to write code that works on the GPU. The remainder of this document provides an overview of the overall approach in relation to other related tools.

The two most important challenges are:

Expand Down Expand Up @@ -49,7 +49,7 @@ We assume that multiple kernels will in general be required, and that there is l

Even though the GPU kernels must each be compiled separately into a single distinct WGSL _shader_ file that is run under WebGPU, they can `import` a shared codebase of files, and thus replicate the same overall shared code structure as the CPU versions.

The GPU code can only handle a highly restricted _subset_ of Go code, with data structures having strict alignment requirements, and no `string` or other composite variable-length data structures (maps, slices etc). Thus, the [gosl](gosl) package recognizes `//gosl:start` and `//gosl:end` comment directives surrounding the GPU-safe (and relevant) portions of the overall code. Any `.go` or `.goal` file can contribute GPU relevant code, including in other packages, and the gosl system automatically builds a shadow package-based set of `.wgsl` files accordingly.
The GPU code can only handle a highly restricted _subset_ of Go code, with data structures having strict alignment requirements, and no `string` or other composite variable-length data structures (maps, slices etc). Thus, the [gosl](../gosl) package recognizes `//gosl:start` and `//gosl:end` comment directives surrounding the GPU-safe (and relevant) portions of the overall code. Any `.go` or `.goal` file can contribute GPU relevant code, including in other packages, and the gosl system automatically builds a shadow package-based set of `.wgsl` files accordingly.

> Each kernel function is marked with a `//gosl:kernel` directive, and the name of the function is used to create the name of the GPU shader file.
Expand Down
2 changes: 1 addition & 1 deletion goal/cmd/goal/testdata/make
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ command test {
println("running the test command")
}

goal.RunCommands(args)
goal.RunCommands(goal.Args())

12 changes: 10 additions & 2 deletions goal/goal.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ type Goal struct {
// Both can be nil.
Cancel func()

// Errors is a stack of runtime errors
// Errors is a stack of runtime errors.
Errors []error

// CliArgs are input arguments from the command line.
CliArgs []any

// Ctx is the context used for cancelling current shell running
// a single chunk of code, typically from the interpreter.
// We are not able to pass the context around so it is set here,
Expand Down Expand Up @@ -304,7 +307,12 @@ func (gl *Goal) OpenHistory(file string) error {
return nil
}

// AddCommand adds given command to list of available commands
// Args returns the command line arguments.
func (gl *Goal) Args() []any {
return gl.CliArgs
}

// AddCommand adds given command to list of available commands.
func (gl *Goal) AddCommand(name string, cmd func(args ...string)) {
gl.Commands[name] = cmd
}
Expand Down
5 changes: 3 additions & 2 deletions goal/interpreter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"cogentcore.org/core/base/fsx"
"cogentcore.org/core/base/logx"
"cogentcore.org/lab/goal"
"cogentcore.org/lab/goal/goalib"
"github.com/cogentcore/yaegi/interp"
)

Expand Down Expand Up @@ -57,12 +58,13 @@ type Config struct {
func Run(c *Config) error { //cli:cmd -root
in := NewInterpreter(interp.Options{})
if len(c.Args) > 0 {
in.Eval("args := goalib.StringsToAnys(" + fmt.Sprintf("%#v)", c.Args))
in.Goal.CliArgs = goalib.StringsToAnys(c.Args)
}

if c.Input == "" {
return c.InteractiveFunc(c, in)
}
in.Config()
code := ""
if errors.Log1(fsx.FileExists(c.Input)) {
b, err := os.ReadFile(c.Input)
Expand All @@ -77,7 +79,6 @@ func Run(c *Config) error { //cli:cmd -root
}
code += c.Expr + "\n"
}

_, _, err := in.Eval(code)
if err == nil {
err = in.Goal.TrState.DepthError()
Expand Down
1 change: 1 addition & 0 deletions goal/interpreter/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (in *Interpreter) ImportGoal() {
"Start": reflect.ValueOf(in.Goal.Start),
"AddCommand": reflect.ValueOf(in.Goal.AddCommand),
"RunCommands": reflect.ValueOf(in.Goal.RunCommands),
"Args": reflect.ValueOf(in.Goal.Args),
},
})
}
36 changes: 22 additions & 14 deletions goal/transpile/transpile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,29 @@ func TestTranspile(t *testing.T) {
func TestCommand(t *testing.T) {
// logx.UserLevel = slog.LevelDebug
tests := []exIn{
/* {
`command list {
ls -la args...
}`,
`goal.AddCommand("list", func(args ...string) {
goal.Run("ls", "-la", "args...")
})`},
{
` ss.GUI.AddToolbarItem(p, egui.ToolbarItem{
Label: "Reset RunLog",
})
`,
`ss.GUI.AddToolbarItem(p, egui.ToolbarItem {
Label: "Reset RunLog",
} )
`},
*/
{
`command list {
ls -la args...
}`,
`goal.AddCommand("list", func(args ...string) {
goal.Run("ls", "-la", "args...")
})`},
{
` ss.GUI.AddToolbarItem(p, egui.ToolbarItem{
Label: "Reset RunLog",
})
`#!/usr/bin/env goal
command build {
`,
`ss.GUI.AddToolbarItem(p, egui.ToolbarItem {
Label: "Reset RunLog",
} )
`
goal.AddCommand("build", func(args ...string) {
`},
}

Expand All @@ -228,7 +236,7 @@ Label: "Reset RunLog",
func TestCur(t *testing.T) {
// logx.UserLevel = slog.LevelDebug
tests := []exIn{
{"# x := 1", `x := tensor.Tensor(tensor.NewIntScalar(1))`},
{"#!/usr/bin/env goal", ``},
}
st := NewState()
st.MathRecord = false
Expand Down

0 comments on commit 5d45b49

Please sign in to comment.