使用python ctypes与nvapi交互(跟随演示代码) [英] use python ctypes to interface with nvapi (follow up with demonstration code)

查看:228
本文介绍了使用python ctypes与nvapi交互(跟随演示代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我先前的问题的后续解答:有关python ctypes和nvapi的帮助

This is a follow up to my previous question: help with python ctypes and nvapi

我要发布另一个问题,因为我已经在上一个问题中选择了一个答案,并且该问题现在已经改变了方向。

I am posting another question because I already selected an answer in my previous question and the question has shifted direction now.

我正在使用python查询nvapi.dll以获取我的gpu使用值。

Using python, I am trying to query nvapi.dll to get my gpu usage value.

这是在C#中的操作方法(在下面引用): http://code.google.com/p/open-hardware-monitor/source/browse/trunk/Hardware/Nvidia /NVAPI.cs

This is how you would do it in C# (referenced below): http://code.google.com/p/open-hardware-monitor/source/browse/trunk/Hardware/Nvidia/NVAPI.cs

因为我只需要一小部分功能,但我并不想真正换成使用另一种语言。我认为ctypes应该能够做到这一点……我只是不知道该怎么做。我以前从未真正使用过Python ctypes。

Because I only need a small subset of the functionality I don't really want to swap into using another language. I think that ctypes should be able to do this... I just cannot figure out how to do it. I have never really used Python ctypes before.

为了查询gpu的使用情况,我首先需要调用NvAPI_Initialize函数。还必须使用查询接口来引用该函数:

In order to query for the gpu usage I first need to call the NvAPI_Initialize function. One must also use the query interface to reference the function:

from ctypes import *
nvapi = WinDLL("nvapi.dll")
nvapi_QueryInterface = nvapi.nvapi_QueryInterface

使用上述代码,我可以访问到nvapi_QueryInterface,但我无法弄清楚如何复制此部分:

Using the above code, I have access to the nvapi_QueryInterface, but I cannot figure out how to replicate this portion:

private static void GetDelegate<T>(uint id, out T newDelegate)
  where T : class
{
    IntPtr ptr = nvapi_QueryInterface(id);
    if (ptr != IntPtr.Zero)
    {
        newDelegate =
          Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)) as T;
    }
    else
    {
        newDelegate = null;
    }
}

引用初始化和用法函数:

to reference the init and usage functions:

GetDelegate(0x0150E828, out NvAPI_Initialize);
GetDelegate(0x189A1FDF, out NvAPI_GPU_GetUsages);

我对这的理解可能仍然是错误的,但是我花了很多时间尝试弄清楚这一点,所以我希望有人可以帮助我朝正确的方向发展,因为我对如何进行工作有些迷茫。

My understanding of this may still be wrong, but I've spent a good deal of time trying to figure this out so I hope someone can help push me in the right direction because I am a little lost as to how to proceed.

有人可以帮助我了解我如何做使这部分代码直接在python中工作?我只需要在一天结束时调用NvAPI_GPU_GetUsages函数即可。

Can someone help me understand how I can make this small portion of the code work directly in python? I just need to be able to call the NvAPI_GPU_GetUsages function at the end of the day.

谢谢。

推荐答案


为了查询gpu的使用情况,我
首先需要调用
NvAPI_Initialize函数。

In order to query for the gpu usage I first need to call the NvAPI_Initialize function.

实际上涉及两个初始化函数:静态库nvapi.lib中的一个初始化函数试图动态加载nvapi.dll,找到一些函数并调用第二个初始化函数动态链接库中的函数。从 static 库调用NvApi_Initialize涉及的步骤大致如下:

There are actually two initialization functions involved: the one from the static library nvapi.lib, which tries to dynamically load nvapi.dll, locates some functions and calls the second initialization function from the dynamic link library. The steps involved when calling NvApi_Initialize from the static library are roughly as follows:


  1. 检查nvapi .dll已加载
    ,如果没有加载,则加载它。

  1. Check if nvapi.dll is already loaded, if not, load it

使用GetProcAddress获取指向
的指针DLL
中的nvapi_QueryInterface
使用新获得的函数以
查询具有ID
0x150E828 的接口。这将是
动态库中的第二个
初始化函数

Use GetProcAddress to get a pointer to nvapi_QueryInterface from the DLL Use the newly obtained function to query for the interface with the Id 0x150E828. This will be the second initialization function from the dynamic library

调用获得的初始化函数。如果
函数成功(以返回值0表示
),则通过
nvapi_QueryInterface 新函数指针>,使用
id 0x33C7358C (我们将其称为ENTER)和 0x593E8644 (退出)。

Call the obtained initialization function. If the function was successful (indicated by a return value of 0), obtain two new function pointers via nvapi_QueryInterface, using the ids 0x33C7358C (let's call this ENTER) and 0x593E8644 (EXIT).

因此,如果要保留纯Python,则基本上必须使用ctypes复制这些步骤,这非常容易。完成后,nvapi.dll中将提供清理功能,您可以使用ID 0xD22BDD7E 进行查询。如果您已完成使用该库的操作,请记住调用此函数。

So, if you want to stay pure Python, you basically have to replicate these steps using ctypes, which would be pretty easy. After you are done, there is a cleanup function in nvapi.dll, which you can query using the id 0xD22BDD7E. If you are done using the library, remember to call this function.

现在要解决真正的问题:不幸的是,没有函数 NvAPI_GPU_GetUsages 中的2010年12月NVIDIA SDK。但是,有 NvAPI_GPU_GetDynamicPstatesInfoEx 允许您请求要填充的类型为 NV_GPU_DYNAMIC_PSTATES_INFO_EX 的结构数组。反过来,将包含给定域(即GPU,帧缓冲区和视频引擎)在最后一秒内的利用率,作为时间的百分比。

Now on to the real problem: unfortunately, there is no function NvAPI_GPU_GetUsages in the December 2010 NVIDIA SDK. There is, however, NvAPI_GPU_GetDynamicPstatesInfoEx which will allow you to request an array of structures of type NV_GPU_DYNAMIC_PSTATES_INFO_EX to be filled, which in turn will contain the utilization as percentage of time during the last second for a given domain (i.e. GPU, frame buffer and video engine).

基本上,如果您想检索GPU使用情况信息,将 NV_GPU_DYNAMIC_PSTATES_INFO_EX 结构传输到Python(请参阅结构和联合),创建至少包含3个结构的数组,并将该数组传递给 NvAPI_GPU_GetDynamicPstatesInfoEx 函数,您必须先使用id 0x60DED2ED 从动态链接库中查询该函数。

Basically, if you want to retrieve GPU usage information, you transfer the NV_GPU_DYNAMIC_PSTATES_INFO_EX struct to Python (see Structures and unions in the ctypes documentation), create an array of at least 3 structures and pass that array to the NvAPI_GPU_GetDynamicPstatesInfoEx function, which you would have to query from the dynamic link library first, using the id 0x60DED2ED.

这是,您在初始化期间查询这两个函数的位置-这是一个解决方案t的锁定机构。虽然不是绝对必要的(例如,您可以在通话过程中忽略它),但还是建议您致电它们,即

This is, by the way, where the two functions come into play you queries during initialization - it's a sort of locking mechanism. While not strictly necessary (i.e. you can omit this during calling), you are encouraged to call them, i.e.

Enter()
NvAPI_GPU_GetDynamicPstatesInfoEx(array_of_structs)
Exit()

您应该能够通过查看NVAPI_GPU_UTILIZATION_DOMAIN_GPU宏(或常量),可以找到在阵列中哪个位置存储了GPU使用情况信息,不幸的是,SDK的头文件中似乎没有声明。不过,您可以通过将代码结果与显示GPU使用情况信息的NVIDIA系统工具进行比较来轻松地验证这一点。

You are supposed to be able to find at at which position in the array GPU usage information is stored by looking at the NVAPI_GPU_UTILIZATION_DOMAIN_GPU macro (or constant), unfortunately, there does not seem to be a declaration included in the header files for the SDK. Still, you could easily verify this by comparing the results of your code with the NVIDIA system tool that displays GPU usage information.

希望有帮助。

这篇关于使用python ctypes与nvapi交互(跟随演示代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆