You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
7.0 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. //----------------------------------------------------------------------------
  2. // Copyright (C) 2004-2019 by EMGU Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using Emgu.Util;
  9. namespace Emgu.CV.Cuda
  10. {
  11. /// <summary>
  12. /// The Cuda device information
  13. /// </summary>
  14. public class CudaDeviceInfo : UnmanagedObject
  15. {
  16. private int _deviceID;
  17. /// <summary>
  18. /// Query the information of the gpu device that is currently in use.
  19. /// </summary>
  20. public CudaDeviceInfo()
  21. : this(CudaInvoke.GetDevice())
  22. {
  23. }
  24. /// <summary>
  25. /// Query the information of the cuda device with the specific id.
  26. /// </summary>
  27. /// <param name="deviceId">The device id</param>
  28. public CudaDeviceInfo(int deviceId)
  29. {
  30. _ptr = CudaInvoke.cudaDeviceInfoCreate(ref deviceId);
  31. _deviceID = deviceId;
  32. }
  33. /// <summary>
  34. /// The id of the device
  35. /// </summary>
  36. public int ID
  37. {
  38. get { return _deviceID; }
  39. }
  40. /// <summary>
  41. /// The name of the device
  42. /// </summary>
  43. public String Name
  44. {
  45. get
  46. {
  47. StringBuilder buffer = new StringBuilder(1024);
  48. CudaInvoke.cudaDeviceInfoDeviceName(_ptr, buffer, 1024);
  49. return buffer.ToString();
  50. }
  51. }
  52. /// <summary>
  53. /// The compute capability
  54. /// </summary>
  55. public Version CudaComputeCapability
  56. {
  57. get
  58. {
  59. int major = 0, minor = 0;
  60. CudaInvoke.cudaDeviceInfoComputeCapability(_ptr, ref major, ref minor);
  61. return new Version(major, minor);
  62. }
  63. }
  64. /// <summary>
  65. /// The number of single multi processors
  66. /// </summary>
  67. public int MultiProcessorCount
  68. {
  69. get
  70. {
  71. return CudaInvoke.cudaDeviceInfoMultiProcessorCount(_ptr);
  72. }
  73. }
  74. /// <summary>
  75. /// Get the amount of free memory at the moment
  76. /// </summary>
  77. public ulong FreeMemory
  78. {
  79. get
  80. {
  81. UIntPtr f = new UIntPtr();
  82. CudaInvoke.cudaDeviceInfoFreeMemInfo(_ptr, ref f);
  83. return f.ToUInt64();
  84. }
  85. }
  86. /// <summary>
  87. /// Get the amount of total memory
  88. /// </summary>
  89. public ulong TotalMemory
  90. {
  91. get
  92. {
  93. UIntPtr t = new UIntPtr();
  94. CudaInvoke.cudaDeviceInfoTotalMemInfo(_ptr, ref t);
  95. return t.ToUInt64();
  96. }
  97. }
  98. /// <summary>
  99. /// Indicates if the device has the specific feature
  100. /// </summary>
  101. /// <param name="feature">The device feature</param>
  102. /// <returns>True if the feature is supported</returns>
  103. public bool Supports(GpuFeature feature)
  104. {
  105. return CudaInvoke.cudaDeviceInfoSupports(_ptr, feature);
  106. }
  107. /// <summary>
  108. /// Checks whether the Cuda module can be run on the given device
  109. /// </summary>
  110. public bool IsCompatible
  111. {
  112. get
  113. {
  114. return CudaInvoke.cudaDeviceInfoIsCompatible(_ptr);
  115. }
  116. }
  117. /// <summary>
  118. /// GPU feature
  119. /// </summary>
  120. public enum GpuFeature
  121. {
  122. /// <summary>
  123. /// Cuda compute 1.0
  124. /// </summary>
  125. Compute10 = 10,
  126. /// <summary>
  127. /// Cuda compute 1.1
  128. /// </summary>
  129. Compute11 = 11,
  130. /// <summary>
  131. /// Cuda compute 1.2
  132. /// </summary>
  133. Compute12 = 12,
  134. /// <summary>
  135. /// Cuda compute 1.3
  136. /// </summary>
  137. Compute13 = 13,
  138. /// <summary>
  139. /// Cuda compute 2.0
  140. /// </summary>
  141. Compute20 = 20,
  142. /// <summary>
  143. /// Cuda compute 2.1
  144. /// </summary>
  145. Compute21 = 21,
  146. /// <summary>
  147. /// Global Atomic
  148. /// </summary>
  149. GlobalAtomics = Compute11,
  150. /// <summary>
  151. /// Shared Atomic
  152. /// </summary>
  153. SharedAtomics = Compute12,
  154. /// <summary>
  155. /// Native double
  156. /// </summary>
  157. NativeDouble = Compute13,
  158. }
  159. /// <summary>
  160. /// Release the unmanaged resource related to the GpuDevice
  161. /// </summary>
  162. protected override void DisposeObject()
  163. {
  164. CudaInvoke.cudaDeviceInfoRelease(ref _ptr);
  165. }
  166. }
  167. public static partial class CudaInvoke
  168. {
  169. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  170. internal extern static IntPtr cudaDeviceInfoCreate(ref int deviceId);
  171. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  172. internal extern static void cudaDeviceInfoRelease(ref IntPtr di);
  173. /// <summary>
  174. /// Get the compute capability of the device
  175. /// </summary>
  176. /// <param name="device">The device</param>
  177. /// <param name="major">The major version of the compute capability</param>
  178. /// <param name="minor">The minor version of the compute capability</param>
  179. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  180. internal static extern void cudaDeviceInfoComputeCapability(IntPtr device, ref int major, ref int minor);
  181. /// <summary>
  182. /// Get the number of multiprocessors on device
  183. /// </summary>
  184. /// <param name="device">The device</param>
  185. /// <returns>The number of multiprocessors on device</returns>
  186. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  187. internal static extern int cudaDeviceInfoMultiProcessorCount(IntPtr device);
  188. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  189. internal static extern void cudaDeviceInfoFreeMemInfo(IntPtr device, ref UIntPtr free);
  190. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  191. internal static extern void cudaDeviceInfoTotalMemInfo(IntPtr device, ref UIntPtr total);
  192. /// <summary>
  193. /// Get the device name
  194. /// </summary>
  195. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  196. internal static extern void cudaDeviceInfoDeviceName(
  197. IntPtr device,
  198. [MarshalAs(CvInvoke.StringMarshalType)]
  199. StringBuilder buffer,
  200. int maxSizeInBytes);
  201. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  202. [return: MarshalAs(CvInvoke.BoolMarshalType)]
  203. internal static extern bool cudaDeviceInfoSupports(IntPtr device, CudaDeviceInfo.GpuFeature feature);
  204. [DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  205. [return: MarshalAs(CvInvoke.BoolMarshalType)]
  206. internal static extern bool cudaDeviceInfoIsCompatible(IntPtr device);
  207. }
  208. }