11using System . Diagnostics ;
22using System . Numerics ;
3- using BenchmarkDotNet . Jobs ;
4- using BenchmarkDotNet . Toolchains . CsProj ;
5- using BenchmarkDotNet . Toolchains . InProcess . NoEmit ;
6- using CommunityToolkit . HighPerformance ;
73using Schedulers ;
84using Schedulers . Benchmarks ;
95using Schedulers . Utils ;
@@ -14,7 +10,6 @@ public struct EmptyJob : IJob
1410{
1511 public void Execute ( )
1612 {
17-
1813 }
1914}
2015
@@ -69,7 +64,7 @@ public HeavyCalculationJob(int first, int second)
6964
7065 public void Execute ( )
7166 {
72- for ( var i = 0 ; i < 100 ; i ++ )
67+ for ( var i = 0 ; i < 1 ; i ++ )
7368 {
7469 _first = double . Sqrt ( _second ) ;
7570 _second = double . Sqrt ( _first ) + 1 ;
@@ -105,7 +100,7 @@ public void RunVectorized(int index, int end)
105100
106101 public void RunSingle ( int index )
107102 {
108- if ( ! acceptsNewEntries ) throw new ( "Should not accept new entries" ) ;
103+ if ( ! acceptsNewEntries ) throw new ( $ "Should not accept new entries { index } ") ;
109104 var newValue = Interlocked . Increment ( ref total ) ;
110105 // Console.WriteLine($" {index} {newValue}");
111106 }
@@ -131,24 +126,28 @@ public long End(int jobs, string type)
131126
132127public class Benchmark
133128{
134- private const int jobCount = 200000 ;
135- private const int loopCount = 100 ;
129+ private const int jobCount = 2000 ;
130+ private const int loopCount = 1000 ;
131+ private static JobScheduler jobScheduler = new ( ) ;
136132
137133 private static void CorrectnessTestJob ( )
138134 {
139- using var jobScheduler = new JobScheduler ( ) ;
140135 var timer = new JobTimer ( ) ;
141136 for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
142137 {
143138 TestCorrectnessJob . total = 0 ;
144139 TestCorrectnessJob . acceptsNewEntries = true ;
145- var job = new ParallelJobProducer < TestCorrectnessJob > ( jobCount , new ( ) , jobScheduler ) ;
140+ var job = new ParallelJobProducer < TestCorrectnessJob > ( 0 , jobCount , new ( ) , jobScheduler ) ;
141+ job . CheckAndSplit ( ) ;
142+ jobScheduler . Flush ( job . GetHandle ( ) ) ;
146143 jobScheduler . Wait ( job . GetHandle ( ) ) ;
147- TestCorrectnessJob . acceptsNewEntries = false ;
148- var expected = jobCount ;
149- if ( TestCorrectnessJob . total != expected )
144+ // Thread.Sleep(1);
145+ // Console.WriteLine($"UnfinishedJobs {job.GetHandle().UnfinishedJobs} total {TestCorrectnessJob.total}");
146+ // TestCorrectnessJob.acceptsNewEntries = false;
147+ var total = TestCorrectnessJob . total ;
148+ if ( total != jobCount )
150149 {
151- throw new ( $ "{ TestCorrectnessJob . total } != { expected } ") ;
150+ throw new ( $ "{ total } != { jobCount } ") ;
152151 }
153152 }
154153
@@ -166,8 +165,7 @@ private static void BenchB()
166165 {
167166 var job = new HeavyCalculationJob ( index , index ) ;
168167 var handle = jobScheduler . Schedule ( job ) ;
169- handle . Parent = parentHandle . Index ;
170- handle . SetDependsOn ( parentHandle ) ;
168+ handle . SetParent ( parentHandle ) ;
171169 jobScheduler . Flush ( handle ) ;
172170 }
173171
@@ -184,7 +182,7 @@ private static void BenchC()
184182 var timer = new JobTimer ( ) ;
185183 for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
186184 {
187- var job = new ParallelJobProducer < HeavyCalculationJob > ( jobCount , new ( ) , jobScheduler ) ;
185+ var job = new ParallelJobProducer < HeavyCalculationJob > ( 0 , jobCount , new ( ) , jobScheduler ) ;
188186 jobScheduler . Wait ( job . GetHandle ( ) ) ;
189187 }
190188
@@ -206,22 +204,28 @@ private static void BenchD()
206204 timer . End ( jobCount * loopCount , "Just Parallel.For" ) ;
207205 }
208206
209- private static long BenchVector ( bool dontUseVector )
207+ private static long BenchVector ( bool useVector )
210208 {
211- using var jobScheduler = new JobScheduler ( ) ;
212209 var timer = new JobTimer ( ) ;
213210 var data = new VectorCalculationJob { a = new float [ jobCount ] , b = new float [ jobCount ] , result = new float [ jobCount ] , Repetitions = 500 } ;
211+ var parentJob = jobScheduler . Schedule ( ) ;
214212 for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
215213 {
216- var job = new ParallelJobProducer < VectorCalculationJob > ( jobCount , data , jobScheduler , 16 , ! dontUseVector ) ;
217- jobScheduler . Wait ( job . GetHandle ( ) ) ;
214+ var job = new ParallelJobProducer < VectorCalculationJob > ( 0 , jobCount , data , jobScheduler , 16 , ! useVector ) ;
215+ job . CheckAndSplit ( ) ;
216+ job . GetHandle ( ) . SetParent ( parentJob ) ;
217+ jobScheduler . Flush ( job . GetHandle ( ) ) ;
218218 }
219-
220- return timer . End ( jobCount * loopCount , $ "Use vector: { ! dontUseVector } ") ;
219+ jobScheduler . Flush ( parentJob ) ;
220+ jobScheduler . Wait ( parentJob ) ;
221+ return timer . End ( jobCount * loopCount , $ "Use vector: { useVector } ") ;
221222 }
222223
223224 private static void Main ( string [ ] args )
224225 {
226+ // new JobHierarchyTest();
227+ // return;
228+
225229 // var config = DefaultConfig.Instance.AddJob(Job.Default
226230 // .WithWarmupCount(2)
227231 // .WithMinIterationCount(10)
@@ -232,7 +236,7 @@ private static void Main(string[] args)
232236 // config = config.WithOptions(ConfigOptions.DisableOptimizationsValidator);
233237 // BenchmarkRunner.Run<JobSchedulerBenchmark>(config);
234238 // return;
235- for ( var i = 0 ; ; i ++ )
239+ for ( var i = 0 ; i < 20 ; i ++ )
236240 {
237241 // CorrectnessTestJob();
238242 // BenchB();
@@ -242,6 +246,8 @@ private static void Main(string[] args)
242246 var nonVectorized = BenchVector ( false ) ;
243247 Console . WriteLine ( $ "Ratio { ( double ) nonVectorized / vectorized } ") ;
244248 }
249+ jobScheduler . Dispose ( ) ;
250+
245251 //using var jobScheduler = new JobScheduler();
246252
247253 // Spawn massive jobs and wait for finish
0 commit comments