3

I need to get the GPU-Power from a server . This should be done with nvidia-smi.

  def getGpuPower(self):
      splitedGpuPower = os.popen("nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits").read().replace("\n", ",").split(",")
      for x in range(4):
        self.gpuPower += float(splitedGpuPower[x])
      return self.gpuPower

I need a float number like 250,00

I actually get

(  File "test1.py", line 22, in getGpuPower
    self.gpuPower += float(splitedGpuPower[x])
ValueError: could not convert string to float:)

The output looks like this

$ nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits

8.50
7.43
11.04
4
  • Can you print splitedGpuPower[x] and check if it is actually a float? Commented Apr 23, 2019 at 7:42
  • Please show us the output of the nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits command. Commented Apr 23, 2019 at 7:42
  • 1
    Your string is empty, that's why you can't convert it to float: float("") is what you are actually doing here. Commented Apr 23, 2019 at 7:47
  • You need to iterate on splitedGpuPower without assuming a range, and skip empty strings, check my answer if it makes sense to you! Commented Apr 23, 2019 at 7:56

1 Answer 1

2

Assuming your output of os.popen("nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits").read() is 8.50\n7.43\n11.04, the following should work.

def getGpuPower():
      #Split on newline
      splitedGpuPower = "8.50\n7.43\n11.04".split("\n")
      gpuPower = 0
      #Iterate through the list
      for power in splitedGpuPower:
          #If string is non empty, convert to float and add
          if power.strip() != '':
              gpuPower += float(power)
      print(gpuPower)

The output will be

getGpuPower()
#26.97
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.