PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Cudafy/Cudafy.Math/RAND/GPGPURAND.cs

#
C# | 274 lines | 80 code | 33 blank | 161 comment | 6 complexity | f722bbc637a6f086e1fdc585a1993383 MD5 | raw file
  1. /*
  2. CUDAfy.NET - LGPL 2.1 License
  3. Please consider purchasing a commerical license - it helps development, frees you from LGPL restrictions
  4. and provides you with support. Thank you!
  5. Copyright (C) 2011 Hybrid DSP Systems
  6. http://www.hybriddsp.com
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Diagnostics;
  24. using Cudafy.Host;
  25. namespace Cudafy.Maths.RAND
  26. {
  27. /// <summary>
  28. /// RAND wrapper for Cuda GPUs.
  29. /// </summary>
  30. public abstract class GPGPURAND
  31. {
  32. internal GPGPURAND()
  33. {
  34. _lock = new object();
  35. }
  36. /// <summary>
  37. /// Releases unmanaged resources and performs other cleanup operations before the
  38. /// <see cref="GPGPURAND"/> is reclaimed by garbage collection.
  39. /// </summary>
  40. ~GPGPURAND()
  41. {
  42. Dispose(false);
  43. }
  44. /// <summary>
  45. /// Releases unmanaged and - optionally - managed resources
  46. /// </summary>
  47. public void Dispose()
  48. {
  49. Dispose(true);
  50. // This object will be cleaned up by the Dispose method.
  51. // Therefore, you should call GC.SupressFinalize to
  52. // take this object off the finalization queue
  53. // and prevent finalization code for this object
  54. // from executing a second time.
  55. GC.SuppressFinalize(this);
  56. }
  57. private object _lock;
  58. // Track whether Dispose has been called.
  59. private bool _disposed = false;
  60. /// <summary>
  61. /// Releases unmanaged and - optionally - managed resources
  62. /// </summary>
  63. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  64. protected virtual void Dispose(bool disposing)
  65. {
  66. lock (_lock)
  67. {
  68. Debug.WriteLine(string.Format("GPGPURAND::Dispose({0})", disposing));
  69. // Check to see if Dispose has already been called.
  70. if (!this._disposed)
  71. {
  72. Debug.WriteLine("Disposing");
  73. // If disposing equals true, dispose all managed
  74. // and unmanaged resources.
  75. if (disposing)
  76. {
  77. // Dispose managed resources.
  78. }
  79. // Call the appropriate methods to clean up
  80. // unmanaged resources here.
  81. // If disposing is false,
  82. // only the following code is executed.
  83. Shutdown();
  84. // Note disposing has been done.
  85. _disposed = true;
  86. }
  87. else
  88. Debug.WriteLine("Already disposed");
  89. }
  90. }
  91. /// <summary>
  92. /// Creates an instance based on the specified gpu with pseudo random generator.
  93. /// </summary>
  94. /// <param name="gpu">The gpu.</param>
  95. /// <param name="host">if set to <c>true</c> the uses generator on the host (if applicable).</param>
  96. /// <returns>New instance.</returns>
  97. public static GPGPURAND Create(GPGPU gpu, bool host = false)
  98. {
  99. return Create(gpu, curandRngType.CURAND_RNG_PSEUDO_DEFAULT, host);
  100. }
  101. /// <summary>
  102. /// Creates an instance based on the specified gpu.
  103. /// </summary>
  104. /// <param name="gpu">The gpu.</param>
  105. /// <param name="rng_type">The type of generator.</param>
  106. /// <param name="host">if set to <c>true</c> the uses generator on the host (if applicable).</param>
  107. /// <returns>New instance.</returns>
  108. public static GPGPURAND Create(GPGPU gpu, curandRngType rng_type, bool host = false)
  109. {
  110. GPGPURAND rand;
  111. if (!host && gpu is CudaGPU)
  112. rand = new CudaDeviceRAND(gpu, rng_type);
  113. else if (gpu is CudaGPU)
  114. rand = new CudaHostRAND(gpu, rng_type);
  115. else
  116. rand = new HostRAND(gpu, rng_type);
  117. return rand;
  118. }
  119. /// <summary>
  120. /// Shutdowns this instance.
  121. /// </summary>
  122. protected abstract void Shutdown();
  123. /// <summary>
  124. /// Sets the pseudo random generator seed.
  125. /// </summary>
  126. /// <param name="seed">The seed.</param>
  127. public abstract void SetPseudoRandomGeneratorSeed(ulong seed);
  128. /// <summary>
  129. /// Generates random data.
  130. /// </summary>
  131. /// <param name="array">The array.</param>
  132. /// <param name="n">Count</param>
  133. public abstract void GenerateUniform(float[] array, int n = 0);
  134. /// <summary>
  135. /// Generates random data.
  136. /// </summary>
  137. /// <param name="array">The array.</param>
  138. /// <param name="n">Count</param>
  139. public abstract void GenerateUniform(double[] array, int n = 0);
  140. /// <summary>
  141. /// Generates random data.
  142. /// </summary>
  143. /// <param name="array">The array.</param>
  144. /// <param name="n">Count</param>
  145. public abstract void Generate(uint[] array, int n = 0);
  146. /// <summary>
  147. /// Generates random data.
  148. /// </summary>
  149. /// <param name="array">The array.</param>
  150. /// <param name="mean">The mean.</param>
  151. /// <param name="stddev">The stddev.</param>
  152. /// <param name="n">Count</param>
  153. public abstract void GenerateLogNormal(float[] array, float mean, float stddev, int n = 0);
  154. /// <summary>
  155. /// Generates random data.
  156. /// </summary>
  157. /// <param name="array">The array.</param>
  158. /// <param name="mean">The mean.</param>
  159. /// <param name="stddev">The stddev.</param>
  160. /// <param name="n">Count</param>
  161. public abstract void GenerateLogNormal(double[] array, double mean, double stddev, int n = 0);
  162. /// <summary>
  163. /// Generates random data.
  164. /// </summary>
  165. /// <param name="array">The array.</param>
  166. /// <param name="n">Count</param>
  167. public abstract void Generate(ulong[] array, int n = 0);
  168. /// <summary>
  169. /// Generates random data.
  170. /// </summary>
  171. /// <param name="array">The array.</param>
  172. /// <param name="mean">The mean.</param>
  173. /// <param name="stddev">The stddev.</param>
  174. /// <param name="n">Count</param>
  175. public abstract void GenerateNormal(float[] array, float mean, float stddev, int n = 0);
  176. /// <summary>
  177. /// Generates random data.
  178. /// </summary>
  179. /// <param name="array">The array.</param>
  180. /// <param name="mean">The mean.</param>
  181. /// <param name="stddev">The stddev.</param>
  182. /// <param name="n">Count</param>
  183. public abstract void GenerateNormal(double[] array, float mean, float stddev, int n = 0);
  184. /// <summary>
  185. /// Generates seeds.
  186. /// </summary>
  187. public abstract void GenerateSeeds();
  188. /// <summary>
  189. /// Gets the direction vectors for 32-bit.
  190. /// </summary>
  191. /// <param name="set">The set.</param>
  192. /// <returns></returns>
  193. public abstract RandDirectionVectors32 GetDirectionVectors32(curandDirectionVectorSet set);
  194. /// <summary>
  195. /// Gets the direction vectors for 64-bit.
  196. /// </summary>
  197. /// <param name="set">The set.</param>
  198. /// <returns></returns>
  199. public abstract RandDirectionVectors64 GetDirectionVectors64(curandDirectionVectorSet set);
  200. /// <summary>
  201. /// Gets the scramble constants for 32-bit.
  202. /// </summary>
  203. /// <param name="n">Count</param>
  204. /// <returns></returns>
  205. public abstract uint[] GetScrambleConstants32(int n);
  206. /// <summary>
  207. /// Gets the scramble constants for 64-bit.
  208. /// </summary>
  209. /// <param name="n">Count</param>
  210. /// <returns></returns>
  211. public abstract ulong[] GetScrambleConstants64(int n);
  212. /// <summary>
  213. /// Gets the version of library.
  214. /// </summary>
  215. /// <returns></returns>
  216. public abstract int GetVersion();
  217. /// <summary>
  218. /// Sets the generator offset.
  219. /// </summary>
  220. /// <param name="offset">The offset.</param>
  221. public abstract void SetGeneratorOffset(ulong offset);
  222. /// <summary>
  223. /// Sets the generator ordering.
  224. /// </summary>
  225. /// <param name="order">The order.</param>
  226. public abstract void SetGeneratorOrdering(curandOrdering order);
  227. /// <summary>
  228. /// Sets the quasi random generator dimensions.
  229. /// </summary>
  230. /// <param name="num_dimensions">The num_dimensions.</param>
  231. public abstract void SetQuasiRandomGeneratorDimensions(uint num_dimensions);
  232. /// <summary>
  233. /// Sets the stream.
  234. /// </summary>
  235. /// <param name="streamId">The stream id.</param>
  236. public abstract void SetStream(int streamId);
  237. }
  238. }