using System; using System.Runtime.InteropServices; public static class NvmlWrapper { [DllImport("nvml.dll", EntryPoint = "nvmlInit_v2")] public static extern int NvmlInit(); [DllImport("nvml.dll", EntryPoint = "nvmlShutdown")] public static extern int NvmlShutdown(); // Get device count /* [DllImport("nvml.dll", CallingConvention = CallingConvention.Cdecl)] private static extern int nvmlDeviceGetCount_v2(ref uint deviceCount); */ /* public static int NvmlDeviceGetCount(ref uint deviceCount) { return nvmlDeviceGetCount_v2(ref deviceCount); } */ [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetHandleByIndex_v2")] public static extern int NvmlDeviceGetHandleByIndex(int index, out IntPtr device); [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetUtilizationRates")] public static extern int NvmlDeviceGetUtilizationRates(IntPtr device, out NvmlUtilization utilization); [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetTemperature")] public static extern int NvmlDeviceGetTemperature(IntPtr device, uint sensorType, out uint temp); [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetFanSpeed")] public static extern int NvmlDeviceGetFanSpeed(IntPtr device, out uint speed); [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetMemoryInfo")] public static extern int NvmlDeviceGetMemoryInfo(IntPtr device, out NvmlMemory memory); [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetPowerUsage")] public static extern int NvmlDeviceGetPowerUsage(IntPtr device, out uint power); [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetName")] public static extern int NvmlDeviceGetName(IntPtr device, byte[] name, uint length); [StructLayout(LayoutKind.Sequential)] public struct NvmlUtilization { public uint Gpu; public uint Memory; } [StructLayout(LayoutKind.Sequential)] public struct NvmlMemory { public ulong Total; public ulong Free; public ulong Used; } }