16

I'm looking for a reliable way to determine current GPU memory usage preferably in C++/C . I have found many ways of obtaining usage like the following methods:

  • Direct Draw
  • DxDiag
  • WMI
  • DXGI
  • D3D9

Those methods are not accurate enough (most off by a hundred megabytes). I tried nvapi.h but I didn't see anything that I could use to query for memory. I was thinking only the methods listed above were the only options but then I ran into a tool called GPU-Z that gives me accurate memory readings to the nearest megabyte even when OpenCL runs almost full load on my 580GTX. I can verify I am at the peak of my memory usage by allocating a few more megabytes before OpenCL returns Object_Allocation fail return code.

Looking at the imports from GPU-Z, I see nothing interesting other than:

kernel32.dll: LoadLibraryA, GetProcAddress, VirtualAlloc, VirtualFree

My guess is LoadLibraryA must be used to load a dll for querying the GPU memory and sensors. If this dll exists, where does it live? I'm looking for a solution for AMD and NVidia if possible (using different APIs is ok).

Cœur
  • 37,241
  • 25
  • 195
  • 267
roboto1986
  • 624
  • 2
  • 13
  • 28
  • 1
    "most off my a hundred megabytes" -- what's the known good reference that you're using? – Brian Cain May 10 '13 at 13:47
  • I am using GPU-Z as my reference since it gives me the accuracy I need. – roboto1986 May 10 '13 at 14:04
  • And what gives you confidence that GPU-Z right where the others are wrong? – Brian Cain May 10 '13 at 14:33
  • 2
    It's good to scrutinize GPU-Z (as I have also done) but as I mentioned on my post, if I am near the top of my memory usage on my 580-GTX which appears to have a 3GB limit, I get allocation failure with OpenCL. I also see that when I create a context for my gpu it occupies 60MB and when my gpu is not used, I get 0MB of memory usage (my 580 only computes while a 440gtx does the display). GPU-Z could very well be wrong but why is it different than the other methods? I also know from my algorithm how much each section of code my allocates what and it is consistent with GPU-Z readings. – roboto1986 May 10 '13 at 14:53
  • Did you find out how to do this for AMD cards? – Tsury Oct 11 '15 at 07:51
  • No, sorry. I remember doing an average using some methods listed above and that was good enough for me. – roboto1986 Oct 11 '15 at 10:09

3 Answers3

13

cudaMemGetInfo (documented here) requires nothing other than the cuda runtime API to get free memory and total memory on the current device.

And as Erik pointed out, there is similar functionality in NVML.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • Thank You! I will try this. I'll mark the correct answer soon. – roboto1986 May 10 '13 at 14:39
  • I tried it out and this method gives me the precision I want without having to instal other sdks :) Now, I'm still out of luck for ATI cards. If you have an idea, please let me know. Else, I might just go with DX methods. – roboto1986 May 27 '13 at 17:22
2

Check out the function nvmlDeviceGetMemoryInfo in NVIDIA Management Library https://developer.nvidia.com/nvidia-management-library-nvml:

"Retrieves the amount of used, free and total memory available on the device, in bytes."

Don't know if AMD has something equivalent.

Erik Smistad
  • 1,009
  • 8
  • 7
  • Thanks for the info. However, I was not able to get a proper device count using nvmlUnitGetCount() which returned 0 devices. I called nvmlInit() and the return status was successful and then I followed up with nvmlUnitGetCount() and its returned status was also successful but it returned 0 devices. Any ideas? – roboto1986 May 27 '13 at 17:00
  • When OpenCL gives me 0 devices/platforms, I reinstall the display driver, which usually works. Other than that I have no suggestions. Sorry. – Erik Smistad May 28 '13 at 13:38
2

D3DKMTQueryStatistics is what you need.

Similar question has been asked here: How to query GPU Usage in DirectX?

Community
  • 1
  • 1
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90