forked from jcbritobr/nvml-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
56 lines (51 loc) · 2.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Text;
using Nvidia.Nvml;
namespace Demo
{
class Program
{
private static string GetStringFromSByteArray(sbyte[] data)
{
if (data == null)
throw new SystemException("Data can't be null");
byte[] busIdData = Array.ConvertAll(data, (a) => (byte)a);
return Encoding.Default.GetString(busIdData).Replace("\0", "");
}
static void Main(string[] args)
{
Console.WriteLine("Initialiling nvml library..");
try
{
NvGpu.NvmlInitV2();
Console.WriteLine("Retrieving device count in this machine ...");
var deviceCount = NvGpu.NvmlDeviceGetCountV2();
Console.WriteLine("Listing devices ...");
for (int i = 0; i < deviceCount; i++)
{
var device = NvGpu.NvmlDeviceGetHandleByIndex((uint)i);
if (device == IntPtr.Zero)
{
throw new SystemException($"Something got wrong retrieving device {i}. Do you have a nvidia card?");
}
var deviceName = NvGpu.NvmlDeviceGetName(device);
Console.WriteLine($"The device name from index {i} is {deviceName}");
var info = NvGpu.NvmlDeviceGetPciInfoV3(device);
var busId = GetStringFromSByteArray(info.busId);
Console.WriteLine($"BusId is {busId}");
Console.WriteLine("Trying to change compute mode. This may fail if device not support the new mode.");
var computeMode = NvGpu.NvmlDeviceGetComputeMode(device);
Console.WriteLine($"Current compute mode is {computeMode}");
NvGpu.NvmlDeviceSetComputeMode(device, NvmlComputeMode.NVML_COMPUTEMODE_EXCLUSIVE_THREAD);
NvGpu.NvmlDeviceSetComputeMode(device, computeMode);
}
NvGpu.NvmlShutdown();
}
catch (SystemException se)
{
Console.WriteLine(se.ToString());
NvGpu.NvmlShutdown();
}
}
}
}