2019-10-22 21:36:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2021-11-09 19:19:56 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
allDriverCapabilities = DriverCapabilities("compute,compat32,graphics,utility,video,display,ngx")
|
|
|
|
defaultDriverCapabilities = DriverCapabilities("utility,compute")
|
|
|
|
|
|
|
|
none = DriverCapabilities("")
|
|
|
|
all = DriverCapabilities("all")
|
2019-10-22 21:36:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func capabilityToCLI(cap string) string {
|
|
|
|
switch cap {
|
|
|
|
case "compute":
|
|
|
|
return "--compute"
|
|
|
|
case "compat32":
|
|
|
|
return "--compat32"
|
|
|
|
case "graphics":
|
|
|
|
return "--graphics"
|
|
|
|
case "utility":
|
|
|
|
return "--utility"
|
|
|
|
case "video":
|
|
|
|
return "--video"
|
|
|
|
case "display":
|
|
|
|
return "--display"
|
2020-06-24 17:53:24 +00:00
|
|
|
case "ngx":
|
|
|
|
return "--ngx"
|
2019-10-22 21:36:22 +00:00
|
|
|
default:
|
|
|
|
log.Panicln("unknown driver capability:", cap)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-11-09 19:19:56 +00:00
|
|
|
|
|
|
|
// DriverCapabilities is used to process the NVIDIA_DRIVER_CAPABILITIES environment
|
|
|
|
// variable. Operations include default values, filtering, and handling meta values such as "all"
|
|
|
|
type DriverCapabilities string
|
|
|
|
|
|
|
|
// Intersection returns intersection between two sets of capabilities.
|
|
|
|
func (d DriverCapabilities) Intersection(capabilities DriverCapabilities) DriverCapabilities {
|
|
|
|
if capabilities == all {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
if d == all {
|
|
|
|
return capabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup := make(map[string]bool)
|
|
|
|
for _, c := range d.list() {
|
|
|
|
lookup[c] = true
|
|
|
|
}
|
|
|
|
var found []string
|
|
|
|
for _, c := range capabilities.list() {
|
|
|
|
if lookup[c] {
|
|
|
|
found = append(found, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
intersection := DriverCapabilities(strings.Join(found, ","))
|
|
|
|
return intersection
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of the driver capabilities
|
|
|
|
func (d DriverCapabilities) String() string {
|
|
|
|
return string(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// list returns the driver capabilities as a list
|
|
|
|
func (d DriverCapabilities) list() []string {
|
|
|
|
var caps []string
|
|
|
|
for _, c := range strings.Split(string(d), ",") {
|
|
|
|
trimmed := strings.TrimSpace(c)
|
|
|
|
if len(trimmed) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
caps = append(caps, trimmed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps
|
|
|
|
}
|