-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tibor Vass <[email protected]>
- Loading branch information
Tibor Vass
committed
Mar 20, 2019
1 parent
91339e1
commit 3dfc04f
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package opts | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/docker/docker/api/types/container" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// GpuOpts is a Value type for parsing mounts | ||
type GpuOpts struct { | ||
values []container.DeviceRequest | ||
} | ||
|
||
func parseCount(s string) (int, error) { | ||
if s == "all" { | ||
return -1, nil | ||
} | ||
i, err := strconv.Atoi(s) | ||
return i, errors.Wrap(err, "count must be an integer") | ||
} | ||
|
||
// Set a new mount value | ||
func (o *GpuOpts) Set(value string) error { | ||
csvReader := csv.NewReader(strings.NewReader(value)) | ||
fields, err := csvReader.Read() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req := container.DeviceRequest{Options: make(map[string]string), Capabilities: [][]string{{"gpu"}}} | ||
|
||
// Set writable as the default | ||
for _, field := range fields { | ||
parts := strings.SplitN(field, "=", 2) | ||
key := parts[0] | ||
|
||
if len(parts) == 1 { | ||
req.Count, err = parseCount(key) | ||
if err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
|
||
value := parts[1] | ||
switch key { | ||
case "driver": | ||
req.Driver = value | ||
case "count": | ||
req.Count, err = parseCount(value) | ||
if err != nil { | ||
return err | ||
} | ||
case "device": | ||
req.DeviceIDs = strings.Split(value, ",") | ||
case "capabilities": | ||
req.Capabilities = [][]string{append(strings.Split(value, ","), "gpu")} | ||
case "options": | ||
r := csv.NewReader(strings.NewReader(value)) | ||
optFields, err := r.Read() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read gpu options") | ||
} | ||
req.Options = ConvertKVStringsToMap(optFields) | ||
default: | ||
return fmt.Errorf("unexpected key '%s' in '%s'", key, field) | ||
} | ||
} | ||
|
||
o.values = append(o.values, req) | ||
return nil | ||
} | ||
|
||
// Type returns the type of this option | ||
func (o *GpuOpts) Type() string { | ||
return "gpu-request" | ||
} | ||
|
||
// String returns a string repr of this option | ||
func (o *GpuOpts) String() string { | ||
gpus := []string{} | ||
for _, gpu := range o.values { | ||
gpus = append(gpus, fmt.Sprintf("%v", gpu)) | ||
} | ||
return strings.Join(gpus, ", ") | ||
} | ||
|
||
// Value returns the mounts | ||
func (o *GpuOpts) Value() []container.DeviceRequest { | ||
return o.values | ||
} |