Skip to content

Instantly share code, notes, and snippets.

@rafaelcn
Created July 1, 2022 21:53
Show Gist options
  • Save rafaelcn/404ba1628a269ad7f5eed8a40b903124 to your computer and use it in GitHub Desktop.
Save rafaelcn/404ba1628a269ad7f5eed8a40b903124 to your computer and use it in GitHub Desktop.
A program to start other programs using the GPU
package main
// keep in mind that applications using OpenGL needs the -gl flag to be
// correctly executed with the GPU. If that doesn't work add the -w flag as I
// noticed in some applications to make it work when -gl alone was not enough.
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
)
var (
gl = flag.Bool("gl", false, "use glx")
wait = flag.Bool("w", false, "wait for program to exit")
)
func main() {
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
env := []string{}
options := []string{}
for i, v := range flag.Args() {
if i > 0 {
options = append(options, v)
}
}
env = append(env, os.Environ()...)
env = append(env, "__NV_PRIME_RENDER_OFFLOAD=1")
env = append(env, "__VK_LAYER_NV_optimus=NVIDIA_only")
env = append(env, "__NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0")
if *gl {
env = append(env, "__GLX_VENDOR_LIBRARY_NAME=nvidia")
}
fmt.Printf("program: %s options: %s\n", flag.Arg(0), options)
buff := bytes.Buffer{}
c := exec.Cmd{
Path: flag.Arg(0),
Args: options,
Env: env,
Stdout: &buff,
}
filepath, err := exec.LookPath(c.Path)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to find %s\n", c.Path)
os.Exit(1)
}
c.Path = filepath
if *wait {
err = c.Run()
} else {
err = c.Start()
}
if err != nil {
fmt.Fprintf(os.Stderr, "failed to run %s, reason %v\n", c.Path, err)
}
if *wait {
fmt.Printf("output: %s\n", buff.String())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment