MCPcopy Create free account
hub / github.com/cogentcore/core / SelectGPU

Method SelectGPU

vgpu/gpu.go:333–438  ·  view source on GitHub ↗
(gpus []vk.PhysicalDevice, gpuCount int)

Source from the content-addressed store, hash-verified

331}
332
333func (gp *GPU) SelectGPU(gpus []vk.PhysicalDevice, gpuCount int) int {
334 if gpuCount == 1 {
335 var properties vk.PhysicalDeviceProperties
336 vk.GetPhysicalDeviceProperties(gpus[0], &properties)
337 properties.Deref()
338 gp.DeviceName = gp.GetDeviceName(&properties, 0)
339 if Debug {
340 log.Printf("vgpu: selected only device named: %s\n", gp.DeviceName)
341 }
342 return 0
343 }
344 trgDevNm := ""
345 if ev := os.Getenv("MESA_VK_DEVICE_SELECT"); ev != "" {
346 trgDevNm = ev
347 } else if ev := os.Getenv("VK_DEVICE_SELECT"); ev != "" {
348 trgDevNm = ev
349 }
350 if gp.Compute {
351 if ev := os.Getenv("VK_COMPUTE_DEVICE_SELECT"); ev != "" {
352 trgDevNm = ev
353 }
354 }
355
356 if trgDevNm != "" {
357 idx, err := strconv.Atoi(trgDevNm)
358 if err == nil && idx >= 0 && idx < gpuCount {
359 curIndex := 0
360 for gi := 0; gi < gpuCount; gi++ {
361 var properties vk.PhysicalDeviceProperties
362 vk.GetPhysicalDeviceProperties(gpus[gi], &properties)
363 properties.Deref()
364 if properties.DeviceType == vk.PhysicalDeviceTypeDiscreteGpu {
365 if curIndex == idx {
366 gp.DeviceName = gp.GetDeviceName(&properties, gi)
367 if Debug {
368 log.Printf("vgpu: selected device named: %s, specified by index in *_DEVICE_SELECT environment variable, index: %d\n", gp.DeviceName, gi)
369 }
370 return gi
371 } else {
372 curIndex++
373 }
374 }
375 }
376 panic(fmt.Sprintf("vgpu: device specified by index in *_DEVICE_SELECT environment variable, index: %d, NOT FOUND\n", idx))
377 }
378 for gi := 0; gi < gpuCount; gi++ {
379 var properties vk.PhysicalDeviceProperties
380 vk.GetPhysicalDeviceProperties(gpus[gi], &properties)
381 properties.Deref()
382 if bytes.Contains(properties.DeviceName[:], []byte(trgDevNm)) {
383 devNm := gp.GetDeviceName(&properties, gi)
384 if Debug {
385 log.Printf("vgpu: selected device named: %s, specified in *_DEVICE_SELECT environment variable, index: %d\n", devNm, gi)
386 }
387 gp.DeviceName = devNm
388 return gi
389 }
390 }

Callers 1

ConfigMethod · 0.95

Calls 3

GetDeviceNameMethod · 0.95
PrintfMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected