zhangmeng
2019-07-11 197411370251b04e97a9a60e25b565cdfedbabac
update
1个文件已修改
33 ■■■■■ 已修改文件
gogpu.go 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
gogpu.go
@@ -12,6 +12,7 @@
    "encoding/json"
    "errors"
    "fmt"
    "sort"
    "strconv"
    "strings"
    "unsafe"
@@ -22,11 +23,37 @@
    return int(C.get_idle_gpu(C.int(memSize)))
}
// GPURank sort
type GPURank []GpuUnitInfo
func (a GPURank) Len() int           { return len(a) }
func (a GPURank) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a GPURank) Less(i, j int) bool { return a[i].GpuMemoryFree < a[j].GpuMemoryFree }
// RankGPU rank
func RankGPU() []int {
    info, err := Info()
    if err != nil {
        fmt.Println("RankGPU no gpu")
        return nil
    }
    var ret []int
    if info.Count == 1 {
        ret = append(ret, 0)
    } else {
        sort.Sort(GPURank(info.Info))
        for _, v := range info.Info {
            ret = append(ret, v.GpuIndex)
        }
    }
    return ret
}
// IdleGPU idle most
func IdleGPU() int {
    info, err := Info()
    if err != nil {
        fmt.Println("no gpu")
        fmt.Println("IdleGPU no gpu")
        return -1
    }
    if info.Count == 1 {
@@ -46,6 +73,7 @@
// GpuUnitInfo gpu card info
type GpuUnitInfo struct {
    GpuIndex       int   `json:"index"`
    GpuUtilization int   `json:"utilization"`
    GpuTemperature int   `json:"temperature"`
    GpuDecoderUtil int   `json:"decoder"`
@@ -91,10 +119,11 @@
    var count int
    for i := 0; i < length; i++ {
        if infos[i] == "gpu" {
            u := GpuUnitInfo{}
            u.GpuIndex = count
            count++
            i++
            u := GpuUnitInfo{}
            u.GpuUtilization, _ = strconv.Atoi(infos[i])
            i++
            u.GpuTemperature, _ = strconv.Atoi(infos[i])