| | |
| | | "encoding/json" |
| | | "errors" |
| | | "fmt" |
| | | "sort" |
| | | "strconv" |
| | | "strings" |
| | | "unsafe" |
| | |
| | | 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 { |
| | |
| | | |
| | | // GpuUnitInfo gpu card info |
| | | type GpuUnitInfo struct { |
| | | GpuIndex int `json:"index"` |
| | | GpuUtilization int `json:"utilization"` |
| | | GpuTemperature int `json:"temperature"` |
| | | GpuDecoderUtil int `json:"decoder"` |
| | |
| | | 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]) |