Hardware/Nvidia/NvidiaGroup.cs
author moel.mich
Sat, 22 Jan 2011 17:58:32 +0000
changeset 250 c19d56a0bcad
parent 182 4801e9eaf979
child 298 96263190189a
permissions -rw-r--r--
Fixed the Intel Sandy Bridge CPU core clock calculation.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System.Collections.Generic;
    39 using System.Globalization;
    40 using System.Text;
    41 
    42 namespace OpenHardwareMonitor.Hardware.Nvidia {
    43 
    44   internal class NvidiaGroup : IGroup {
    45    
    46     private readonly List<IHardware> hardware = new List<IHardware>();
    47     private readonly StringBuilder report = new StringBuilder();
    48 
    49     public NvidiaGroup(ISettings settings) {
    50       if (!NVAPI.IsAvailable)
    51         return;
    52 
    53       report.AppendLine("NVAPI");
    54       report.AppendLine();
    55 
    56       string version;
    57       if (NVAPI.NvAPI_GetInterfaceVersionString(out version) == NvStatus.OK) {
    58         report.Append("Version: ");
    59         report.AppendLine(version);
    60       }
    61 
    62       NvPhysicalGpuHandle[] handles = 
    63         new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
    64       int count;
    65       if (NVAPI.NvAPI_EnumPhysicalGPUs == null) {
    66         report.AppendLine("Error: NvAPI_EnumPhysicalGPUs not available");
    67         report.AppendLine();
    68         return;
    69       } else {        
    70         NvStatus status = NVAPI.NvAPI_EnumPhysicalGPUs(handles, out count);
    71         if (status != NvStatus.OK) {
    72           report.AppendLine("Status: " + status);
    73           report.AppendLine();
    74           return;
    75         }
    76       }
    77 
    78       IDictionary<NvPhysicalGpuHandle, NvDisplayHandle> displayHandles =
    79         new Dictionary<NvPhysicalGpuHandle, NvDisplayHandle>();
    80 
    81       if (NVAPI.NvAPI_EnumNvidiaDisplayHandle != null &&
    82         NVAPI.NvAPI_GetPhysicalGPUsFromDisplay != null) 
    83       {
    84         NvStatus status = NvStatus.OK;
    85         int i = 0;
    86         while (status == NvStatus.OK) {
    87           NvDisplayHandle displayHandle = new NvDisplayHandle();
    88           status = NVAPI.NvAPI_EnumNvidiaDisplayHandle(i, ref displayHandle);
    89           i++;
    90 
    91           if (status == NvStatus.OK) {
    92             NvPhysicalGpuHandle[] handlesFromDisplay =
    93               new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
    94             uint countFromDisplay;
    95             if (NVAPI.NvAPI_GetPhysicalGPUsFromDisplay(displayHandle,
    96               handlesFromDisplay, out countFromDisplay) == NvStatus.OK) {
    97               for (int j = 0; j < countFromDisplay; j++) {
    98                 if (!displayHandles.ContainsKey(handlesFromDisplay[j]))
    99                   displayHandles.Add(handlesFromDisplay[j], displayHandle);
   100               }
   101             }
   102           }
   103         }
   104       }
   105 
   106       report.Append("Number of GPUs: ");
   107       report.AppendLine(count.ToString(CultureInfo.InvariantCulture));
   108 
   109       for (int i = 0; i < count; i++) {
   110         NvDisplayHandle displayHandle;
   111         displayHandles.TryGetValue(handles[i], out displayHandle);
   112         hardware.Add(new NvidiaGPU(i, handles[i], displayHandle, settings));
   113       }
   114 
   115       report.AppendLine();
   116     }
   117 
   118     public IHardware[] Hardware {
   119       get {
   120         return hardware.ToArray();
   121       }
   122     }
   123 
   124     public string GetReport() {
   125       return report.ToString();
   126     }
   127 
   128     public void Close() { }
   129   }
   130 }