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,27 @@ 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 ;
136131
137132 private static void CorrectnessTestJob ( )
138133 {
139- using var jobScheduler = new JobScheduler ( ) ;
140134 var timer = new JobTimer ( ) ;
141135 for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
142136 {
143137 TestCorrectnessJob . total = 0 ;
144138 TestCorrectnessJob . acceptsNewEntries = true ;
145- var job = new ParallelJobProducer < TestCorrectnessJob > ( jobCount , new ( ) , jobScheduler ) ;
146- jobScheduler . Wait ( job . GetHandle ( ) ) ;
147- TestCorrectnessJob . acceptsNewEntries = false ;
148- var expected = jobCount ;
149- if ( TestCorrectnessJob . total != expected )
139+ var job = new ParallelJobProducer < TestCorrectnessJob > ( 0 , jobCount , new ( ) ) ;
140+ job . CheckAndSplit ( ) ;
141+ ParallelForJobCommon . GlobalScheduler . Flush ( job . GetHandle ( ) ) ;
142+ ParallelForJobCommon . GlobalScheduler . Wait ( job . GetHandle ( ) ) ;
143+ // Thread.Sleep(1);
144+ // Console.WriteLine($"UnfinishedJobs {job.GetHandle().UnfinishedJobs} total {TestCorrectnessJob.total}");
145+ // TestCorrectnessJob.acceptsNewEntries = false;
146+ var total = TestCorrectnessJob . total ;
147+ if ( total != jobCount )
150148 {
151- throw new ( $ "{ TestCorrectnessJob . total } != { expected } ") ;
149+ throw new ( $ "{ total } != { jobCount } ") ;
152150 }
153151 }
154152
@@ -166,8 +164,7 @@ private static void BenchB()
166164 {
167165 var job = new HeavyCalculationJob ( index , index ) ;
168166 var handle = jobScheduler . Schedule ( job ) ;
169- handle . Parent = parentHandle . Index ;
170- handle . SetDependsOn ( parentHandle ) ;
167+ handle . SetParent ( parentHandle ) ;
171168 jobScheduler . Flush ( handle ) ;
172169 }
173170
@@ -184,7 +181,7 @@ private static void BenchC()
184181 var timer = new JobTimer ( ) ;
185182 for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
186183 {
187- var job = new ParallelJobProducer < HeavyCalculationJob > ( jobCount , new ( ) , jobScheduler ) ;
184+ var job = new ParallelJobProducer < HeavyCalculationJob > ( 0 , jobCount , new ( ) ) ;
188185 jobScheduler . Wait ( job . GetHandle ( ) ) ;
189186 }
190187
@@ -206,22 +203,29 @@ private static void BenchD()
206203 timer . End ( jobCount * loopCount , "Just Parallel.For" ) ;
207204 }
208205
209- private static long BenchVector ( bool dontUseVector )
206+ private static long BenchVector ( bool useVector )
210207 {
211- using var jobScheduler = new JobScheduler ( ) ;
212208 var timer = new JobTimer ( ) ;
213209 var data = new VectorCalculationJob { a = new float [ jobCount ] , b = new float [ jobCount ] , result = new float [ jobCount ] , Repetitions = 500 } ;
210+ var parentJob = ParallelForJobCommon . GlobalScheduler . Schedule ( ) ;
214211 for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
215212 {
216- var job = new ParallelJobProducer < VectorCalculationJob > ( jobCount , data , jobScheduler , 16 , ! dontUseVector ) ;
217- jobScheduler . Wait ( job . GetHandle ( ) ) ;
213+ var job = new ParallelJobProducer < VectorCalculationJob > ( 0 , jobCount , data , loopSize : 16 , onlySingle : ! useVector ) ;
214+ job . CheckAndSplit ( ) ;
215+ job . GetHandle ( ) . SetParent ( parentJob ) ;
216+ ParallelForJobCommon . GlobalScheduler . Flush ( job . GetHandle ( ) ) ;
218217 }
219-
220- return timer . End ( jobCount * loopCount , $ "Use vector: { ! dontUseVector } ") ;
218+ ParallelForJobCommon . GlobalScheduler . Flush ( parentJob ) ;
219+ ParallelForJobCommon . GlobalScheduler . Wait ( parentJob ) ;
220+ return timer . End ( jobCount * loopCount , $ "Use vector: { useVector } ") ;
221221 }
222222
223223 private static void Main ( string [ ] args )
224224 {
225+ ParallelForJobCommon . SetScheduler ( new ( ) ) ;
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+ ParallelForJobCommon . DisposeScheduler ( ) ;
250+
245251 //using var jobScheduler = new JobScheduler();
246252
247253 // Spawn massive jobs and wait for finish
0 commit comments