Hardware/Nvidia/NvidiaGroup.cs
author moel.mich
Sun, 04 Aug 2013 06:54:46 +0000
changeset 419 07d44b20bbd4
parent 298 96263190189a
permissions -rw-r--r--
Fixed an issue in the NCT677X class causing a crash when calling Update.
moel@1
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@344
     7
  Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System.Collections.Generic;
moel@166
    12
using System.Globalization;
moel@35
    13
using System.Text;
moel@1
    14
moel@1
    15
namespace OpenHardwareMonitor.Hardware.Nvidia {
moel@1
    16
moel@165
    17
  internal class NvidiaGroup : IGroup {
moel@35
    18
   
moel@298
    19
    private readonly List<Hardware> hardware = new List<Hardware>();
moel@195
    20
    private readonly StringBuilder report = new StringBuilder();
moel@1
    21
moel@165
    22
    public NvidiaGroup(ISettings settings) {
moel@1
    23
      if (!NVAPI.IsAvailable)
moel@1
    24
        return;
moel@1
    25
moel@35
    26
      report.AppendLine("NVAPI");
moel@35
    27
      report.AppendLine();
moel@35
    28
moel@140
    29
      string version;
moel@140
    30
      if (NVAPI.NvAPI_GetInterfaceVersionString(out version) == NvStatus.OK) {
moel@140
    31
        report.Append("Version: ");
moel@140
    32
        report.AppendLine(version);
moel@140
    33
      }
moel@140
    34
moel@1
    35
      NvPhysicalGpuHandle[] handles = 
moel@1
    36
        new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
moel@140
    37
      int count;
moel@115
    38
      if (NVAPI.NvAPI_EnumPhysicalGPUs == null) {
moel@115
    39
        report.AppendLine("Error: NvAPI_EnumPhysicalGPUs not available");
moel@115
    40
        report.AppendLine();
moel@115
    41
        return;
moel@140
    42
      } else {        
moel@140
    43
        NvStatus status = NVAPI.NvAPI_EnumPhysicalGPUs(handles, out count);
moel@140
    44
        if (status != NvStatus.OK) {
moel@195
    45
          report.AppendLine("Status: " + status);
moel@140
    46
          report.AppendLine();
moel@140
    47
          return;
moel@140
    48
        }
moel@115
    49
      }
moel@115
    50
moel@140
    51
      IDictionary<NvPhysicalGpuHandle, NvDisplayHandle> displayHandles =
moel@140
    52
        new Dictionary<NvPhysicalGpuHandle, NvDisplayHandle>();
moel@140
    53
moel@140
    54
      if (NVAPI.NvAPI_EnumNvidiaDisplayHandle != null &&
moel@140
    55
        NVAPI.NvAPI_GetPhysicalGPUsFromDisplay != null) 
moel@140
    56
      {
moel@140
    57
        NvStatus status = NvStatus.OK;
moel@140
    58
        int i = 0;
moel@140
    59
        while (status == NvStatus.OK) {
moel@140
    60
          NvDisplayHandle displayHandle = new NvDisplayHandle();
moel@140
    61
          status = NVAPI.NvAPI_EnumNvidiaDisplayHandle(i, ref displayHandle);
moel@140
    62
          i++;
moel@140
    63
moel@140
    64
          if (status == NvStatus.OK) {
moel@140
    65
            NvPhysicalGpuHandle[] handlesFromDisplay =
moel@140
    66
              new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
moel@140
    67
            uint countFromDisplay;
moel@140
    68
            if (NVAPI.NvAPI_GetPhysicalGPUsFromDisplay(displayHandle,
moel@140
    69
              handlesFromDisplay, out countFromDisplay) == NvStatus.OK) {
moel@140
    70
              for (int j = 0; j < countFromDisplay; j++) {
moel@140
    71
                if (!displayHandles.ContainsKey(handlesFromDisplay[j]))
moel@140
    72
                  displayHandles.Add(handlesFromDisplay[j], displayHandle);
moel@140
    73
              }
moel@140
    74
            }
moel@140
    75
          }
moel@140
    76
        }
moel@140
    77
      }
moel@1
    78
moel@35
    79
      report.Append("Number of GPUs: ");
moel@195
    80
      report.AppendLine(count.ToString(CultureInfo.InvariantCulture));
moel@195
    81
moel@195
    82
      for (int i = 0; i < count; i++) {
moel@140
    83
        NvDisplayHandle displayHandle;
moel@195
    84
        displayHandles.TryGetValue(handles[i], out displayHandle);
moel@195
    85
        hardware.Add(new NvidiaGPU(i, handles[i], displayHandle, settings));
moel@140
    86
      }
moel@140
    87
moel@35
    88
      report.AppendLine();
moel@1
    89
    }
moel@1
    90
moel@1
    91
    public IHardware[] Hardware {
moel@1
    92
      get {
moel@1
    93
        return hardware.ToArray();
moel@1
    94
      }
moel@1
    95
    }
moel@1
    96
moel@1
    97
    public string GetReport() {
moel@35
    98
      return report.ToString();
moel@1
    99
    }
moel@1
   100
moel@298
   101
    public void Close() {
moel@298
   102
      foreach (Hardware gpu in hardware)
moel@298
   103
        gpu.Close();      
moel@298
   104
    }
moel@1
   105
  }
moel@1
   106
}