Skip to content

Commit

Permalink
Merge pull request #1403 from cogentcore/gpudev
Browse files Browse the repository at this point in the history
shorten GPU_DEVICE_SELECT -> GPU_DEVICE; use custom adapter selection logic again
  • Loading branch information
kkoreilly authored Dec 25, 2024
2 parents 3bf7f7e + 95757ee commit 0a17ddf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions gpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ $ go run cogentcore.org/core/gpu/cmd/webgpuinfo@latest

The following environment variables can be set to specifically select a particular device by device number or name (`deviceName`):

* `GPU_DEVICE_SELECT`, for GUI and compute usage.
* `GPU_COMPUTE_DEVICE_SELECT`, only used for compute, if present, will override above, so you can use different GPUs for graphics vs compute.
* `GPU_DEVICE`, for GUI and compute usage.
* `GPU_COMPUTE_DEVICE`, only used for compute, if present, will override above, so you can use different GPUs for graphics vs compute.

* `GPU` represents the hardware `Adapter` and maintains global settings, info about the hardware.

Expand Down
7 changes: 7 additions & 0 deletions gpu/cmd/webgpuinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import (
"fmt"

"cogentcore.org/core/base/reflectx"
"cogentcore.org/core/gpu"
"github.com/cogentcore/webgpu/wgpu"
)

func main() {
instance := wgpu.CreateInstance(nil)

gpus := instance.EnumerateAdapters(nil)
gp := gpu.NewGPU(nil)
gpIndex := gp.SelectGPU(gpus)
props := gpus[gpIndex].GetInfo()
fmt.Println("Default WebGPU Adapter number:", gpIndex, " Type:", props.AdapterType.String(), " Backend:", props.BackendType.String())
fmt.Println("Set the GPU_DEVICE environment variable to an adapter number to select a different GPU")

for i, a := range gpus {
props := a.GetInfo()
fmt.Println("\n#####################################################################")
Expand Down
37 changes: 21 additions & 16 deletions gpu/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"strconv"
"strings"

"cogentcore.org/core/base/errors"
"cogentcore.org/core/base/reflectx"
"github.com/cogentcore/webgpu/wgpu"
)
Expand Down Expand Up @@ -86,7 +85,7 @@ type GPU struct {
Limits wgpu.SupportedLimits

// ComputeOnly indicates if this GPU is only used for compute,
// which determines if it listens to GPU_COMPUTE_DEVICE_SELECT
// which determines if it listens to GPU_COMPUTE_DEVICE
// environment variable, allowing different compute devices to be
// selected vs. graphics devices.
ComputeOnly bool
Expand Down Expand Up @@ -115,7 +114,7 @@ func NewGPU(sf *wgpu.Surface) *GPU {

// NewComputeGPU returns a new GPU, configured and ready to use,
// for purely compute use, which causes it to listen to
// use the GPU_COMPUTE_DEVICE_SELECT variable for which GPU device to use.
// use the GPU_COMPUTE_DEVICE variable for which GPU device to use.
func NewComputeGPU() *GPU {
gp := &GPU{}
gp.ComputeOnly = true
Expand All @@ -132,15 +131,21 @@ func (gp *GPU) init(sf *wgpu.Surface) error {
gpIndex = gp.SelectGPU(gpus)
gp.GPU = gpus[gpIndex]
} else {
opts := &wgpu.RequestAdapterOptions{
CompatibleSurface: sf,
PowerPreference: wgpu.PowerPreferenceHighPerformance,
}
ad, err := inst.RequestAdapter(opts)
if errors.Log(err) != nil {
return err
}
gp.GPU = ad
gpus := inst.EnumerateAdapters(nil)
gpIndex = gp.SelectGPU(gpus)
gp.GPU = gpus[gpIndex]
// note: below is a more standard way of doing it, but until we fix the issues
// with NVIDIA adapters on linux (#1247), we are using our custom logic.
//
// opts := &wgpu.RequestAdapterOptions{
// CompatibleSurface: sf,
// PowerPreference: wgpu.PowerPreferenceHighPerformance,
// }
// ad, err := inst.RequestAdapter(opts)
// if errors.Log(err) != nil {
// return err
// }
// gp.GPU = ad
}
gp.Properties = gp.GPU.GetInfo()
gp.DeviceName = adapterName(&gp.Properties)
Expand Down Expand Up @@ -213,11 +218,11 @@ func (gp *GPU) SelectGPU(gpus []*wgpu.Adapter) int {
return 0
}
trgDevNm := ""
if ev := os.Getenv("GPU_DEVICE_SELECT"); ev != "" {
if ev := os.Getenv("GPU_DEVICE"); ev != "" {
trgDevNm = ev
}
if gp.ComputeOnly {
if ev := os.Getenv("GPU_COMPUTE_DEVICE_SELECT"); ev != "" {
if ev := os.Getenv("GPU_COMPUTE_DEVICE"); ev != "" {
trgDevNm = ev
}
}
Expand All @@ -236,13 +241,13 @@ func (gp *GPU) SelectGPU(gpus []*wgpu.Adapter) int {
if strings.Contains(pnm, trgDevNm) {
devNm := props.Name
if Debug {
log.Printf("wgpu: selected device named: %s, specified in *_DEVICE_SELECT environment variable, index: %d\n", devNm, gi)
log.Printf("gpu: selected device named: %s, specified in GPU_DEVICE or GPU_COMPUTE_DEVICE environment variable, index: %d\n", devNm, gi)
}
return gi
}
}
if Debug {
log.Printf("vgpu: unable to find device named: %s, specified in *_DEVICE_SELECT environment variable\n", trgDevNm)
log.Printf("gpu: unable to find device named: %s, specified in GPU_DEVICE or GPU_COMPUTE_DEVICE environment variable\n", trgDevNm)
}
}

Expand Down

0 comments on commit 0a17ddf

Please sign in to comment.