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 4.9 kB

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