mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
Add GPU device request support (#84)
* add gpus to api. * add gpus to gui. * get gpus all configuration. * correctly parse gpu device requests.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package room
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -9,6 +11,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/cli/opts"
|
||||
dockerTypes "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
dockerMount "github.com/docker/docker/api/types/mount"
|
||||
@@ -388,6 +391,31 @@ func (manager *RoomManagerCtx) Create(settings types.RoomSettings) (string, erro
|
||||
paths[mount.ContainerPath] = true
|
||||
}
|
||||
|
||||
//
|
||||
// Set container device requests
|
||||
//
|
||||
|
||||
var deviceRequests []container.DeviceRequest
|
||||
|
||||
if len(settings.Resources.Gpus) > 0 {
|
||||
gpuOpts := opts.GpuOpts{}
|
||||
|
||||
// convert to csv
|
||||
var buf bytes.Buffer
|
||||
w := csv.NewWriter(&buf)
|
||||
if err := w.Write(settings.Resources.Gpus); err != nil {
|
||||
return "", err
|
||||
}
|
||||
w.Flush()
|
||||
|
||||
// set GPU opts
|
||||
if err := gpuOpts.Set(buf.String()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
deviceRequests = append(deviceRequests, gpuOpts.Value()...)
|
||||
}
|
||||
|
||||
//
|
||||
// Set container configs
|
||||
//
|
||||
@@ -430,9 +458,10 @@ func (manager *RoomManagerCtx) Create(settings types.RoomSettings) (string, erro
|
||||
Mounts: mounts,
|
||||
// Resources contains container's resources (cgroups config, ulimits...)
|
||||
Resources: container.Resources{
|
||||
CPUShares: settings.Resources.CPUShares,
|
||||
NanoCPUs: settings.Resources.NanoCPUs,
|
||||
Memory: settings.Resources.Memory,
|
||||
CPUShares: settings.Resources.CPUShares,
|
||||
NanoCPUs: settings.Resources.NanoCPUs,
|
||||
Memory: settings.Resources.Memory,
|
||||
DeviceRequests: deviceRequests,
|
||||
},
|
||||
// Privileged
|
||||
Privileged: isPrivilegedImage,
|
||||
@@ -556,11 +585,52 @@ func (manager *RoomManagerCtx) GetSettings(id string) (*types.RoomSettings, erro
|
||||
|
||||
var roomResources types.RoomResources
|
||||
if container.HostConfig != nil {
|
||||
gpus := []string{}
|
||||
for _, req := range container.HostConfig.DeviceRequests {
|
||||
var isGpu bool
|
||||
var caps []string
|
||||
for _, cc := range req.Capabilities {
|
||||
for _, c := range cc {
|
||||
if c == "gpu" {
|
||||
isGpu = true
|
||||
continue
|
||||
}
|
||||
caps = append(caps, c)
|
||||
}
|
||||
}
|
||||
if !isGpu {
|
||||
continue
|
||||
}
|
||||
|
||||
if req.Count > 1 {
|
||||
gpus = append(gpus, fmt.Sprintf("count=%d", req.Count))
|
||||
} else if req.Count == -1 {
|
||||
gpus = append(gpus, "all")
|
||||
}
|
||||
if req.Driver != "" {
|
||||
gpus = append(gpus, fmt.Sprintf("driver=%s", req.Driver))
|
||||
}
|
||||
if len(req.DeviceIDs) > 0 {
|
||||
gpus = append(gpus, fmt.Sprintf("device=%s", strings.Join(req.DeviceIDs, ",")))
|
||||
}
|
||||
if len(caps) > 0 {
|
||||
gpus = append(gpus, fmt.Sprintf("capabilities=%s", strings.Join(caps, ",")))
|
||||
}
|
||||
var opts []string
|
||||
for key, val := range req.Options {
|
||||
opts = append(opts, fmt.Sprintf("%s=%s", key, val))
|
||||
}
|
||||
if len(opts) > 0 {
|
||||
gpus = append(gpus, fmt.Sprintf("options=%s", strings.Join(opts, ",")))
|
||||
}
|
||||
}
|
||||
|
||||
roomResources = types.RoomResources{
|
||||
CPUShares: container.HostConfig.CPUShares,
|
||||
NanoCPUs: container.HostConfig.NanoCPUs,
|
||||
ShmSize: container.HostConfig.ShmSize,
|
||||
Memory: container.HostConfig.Memory,
|
||||
Gpus: gpus,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,10 +60,11 @@ type RoomMount struct {
|
||||
}
|
||||
|
||||
type RoomResources struct {
|
||||
CPUShares int64 `json:"cpu_shares"` // relative weight vs. other containers
|
||||
NanoCPUs int64 `json:"nano_cpus"` // in units of 10^-9 CPUs
|
||||
ShmSize int64 `json:"shm_size"` // in bytes
|
||||
Memory int64 `json:"memory"` // in bytes
|
||||
CPUShares int64 `json:"cpu_shares"` // relative weight vs. other containers
|
||||
NanoCPUs int64 `json:"nano_cpus"` // in units of 10^-9 CPUs
|
||||
ShmSize int64 `json:"shm_size"` // in bytes
|
||||
Memory int64 `json:"memory"` // in bytes
|
||||
Gpus []string `json:"gpus"` // gpu opts
|
||||
}
|
||||
|
||||
type RoomSettings struct {
|
||||
|
||||
Reference in New Issue
Block a user