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

MultiThreadedUnitTestExecuter.cs 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. namespace TensorFlowNET.UnitTest
  5. {
  6. public delegate void MultiThreadedTestDelegate(int threadid);
  7. /// <summary>
  8. /// Creates a synchronized eco-system of running code.
  9. /// </summary>
  10. public class MultiThreadedUnitTestExecuter : IDisposable
  11. {
  12. public int ThreadCount { get; }
  13. public Thread[] Threads { get; }
  14. private readonly SemaphoreSlim barrier_threadstarted;
  15. private readonly ManualResetEventSlim barrier_corestart;
  16. private readonly SemaphoreSlim done_barrier2;
  17. public Action<MultiThreadedUnitTestExecuter> PostRun { get; set; }
  18. #region Static
  19. [DebuggerHidden]
  20. public static void Run(int threadCount, MultiThreadedTestDelegate workload)
  21. {
  22. if (workload == null) throw new ArgumentNullException(nameof(workload));
  23. if (threadCount <= 0) throw new ArgumentOutOfRangeException(nameof(threadCount));
  24. new MultiThreadedUnitTestExecuter(threadCount).Run(workload);
  25. }
  26. [DebuggerHidden]
  27. public static void Run(int threadCount, params MultiThreadedTestDelegate[] workloads)
  28. {
  29. if (workloads == null) throw new ArgumentNullException(nameof(workloads));
  30. if (workloads.Length == 0) throw new ArgumentException("Value cannot be an empty collection.", nameof(workloads));
  31. if (threadCount <= 0) throw new ArgumentOutOfRangeException(nameof(threadCount));
  32. new MultiThreadedUnitTestExecuter(threadCount).Run(workloads);
  33. }
  34. [DebuggerHidden]
  35. public static void Run(int threadCount, MultiThreadedTestDelegate workload, Action<MultiThreadedUnitTestExecuter> postRun)
  36. {
  37. if (workload == null) throw new ArgumentNullException(nameof(workload));
  38. if (postRun == null) throw new ArgumentNullException(nameof(postRun));
  39. if (threadCount <= 0) throw new ArgumentOutOfRangeException(nameof(threadCount));
  40. new MultiThreadedUnitTestExecuter(threadCount) {PostRun = postRun}.Run(workload);
  41. }
  42. #endregion
  43. /// <summary>Initializes a new instance of the <see cref="T:System.Object"></see> class.</summary>
  44. public MultiThreadedUnitTestExecuter(int threadCount)
  45. {
  46. if (threadCount <= 0)
  47. throw new ArgumentOutOfRangeException(nameof(threadCount));
  48. ThreadCount = threadCount;
  49. Threads = new Thread[ThreadCount];
  50. done_barrier2 = new SemaphoreSlim(0, threadCount);
  51. barrier_corestart = new ManualResetEventSlim();
  52. barrier_threadstarted = new SemaphoreSlim(0, threadCount);
  53. }
  54. [DebuggerHidden]
  55. public void Run(params MultiThreadedTestDelegate[] workloads)
  56. {
  57. if (workloads == null)
  58. throw new ArgumentNullException(nameof(workloads));
  59. if (workloads.Length != 1 && workloads.Length % ThreadCount != 0)
  60. throw new InvalidOperationException($"Run method must accept either 1 workload or n-threads workloads. Got {workloads.Length} workloads.");
  61. if (ThreadCount == 1)
  62. {
  63. new Thread(() =>
  64. {
  65. workloads[0](0);
  66. done_barrier2.Release(1);
  67. }).Start();
  68. done_barrier2.Wait();
  69. PostRun?.Invoke(this);
  70. return;
  71. }
  72. //thread core
  73. void ThreadCore(MultiThreadedTestDelegate core, int threadid)
  74. {
  75. barrier_threadstarted.Release(1);
  76. barrier_corestart.Wait();
  77. //workload
  78. core(threadid);
  79. done_barrier2.Release(1);
  80. }
  81. //initialize all threads
  82. if (workloads.Length == 1)
  83. {
  84. var workload = workloads[0];
  85. for (int i = 0; i < ThreadCount; i++)
  86. {
  87. var i_local = i;
  88. Threads[i] = new Thread(() => ThreadCore(workload, i_local));
  89. }
  90. } else
  91. {
  92. for (int i = 0; i < ThreadCount; i++)
  93. {
  94. var i_local = i;
  95. var workload = workloads[i_local % workloads.Length];
  96. Threads[i] = new Thread(() => ThreadCore(workload, i_local));
  97. }
  98. }
  99. //run all threads
  100. for (int i = 0; i < ThreadCount; i++) Threads[i].Start();
  101. //wait for threads to be started and ready
  102. for (int i = 0; i < ThreadCount; i++) barrier_threadstarted.Wait();
  103. //signal threads to start
  104. barrier_corestart.Set();
  105. //wait for threads to finish
  106. for (int i = 0; i < ThreadCount; i++) done_barrier2.Wait();
  107. //checks after ended
  108. PostRun?.Invoke(this);
  109. }
  110. public void Dispose()
  111. {
  112. barrier_threadstarted.Dispose();
  113. barrier_corestart.Dispose();
  114. done_barrier2.Dispose();
  115. }
  116. }
  117. }