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 < 10 ; i ++ )
7368 {
7469 _first = double . Sqrt ( _second ) ;
7570 _second = double . Sqrt ( _first ) + 1 ;
@@ -86,7 +81,7 @@ public void RunVectorized(int index, int end)
8681
8782 public void RunSingle ( int index )
8883 {
89- throw new NotImplementedException ( ) ;
84+ Execute ( ) ;
9085 }
9186}
9287
@@ -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,26 @@ 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 = 200 ;
130+ private const int loopCount = 100000 ;
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+ ParallelForJobCommon . GlobalScheduler . Flush ( job . GetHandle ( ) ) ;
141+ ParallelForJobCommon . GlobalScheduler . Wait ( job . GetHandle ( ) ) ;
142+ // Thread.Sleep(1);
143+ // Console.WriteLine($"UnfinishedJobs {job.GetHandle().UnfinishedJobs} total {TestCorrectnessJob.total}");
144+ // TestCorrectnessJob.acceptsNewEntries = false;
145+ var total = TestCorrectnessJob . total ;
146+ if ( total != jobCount )
150147 {
151- throw new ( $ "{ TestCorrectnessJob . total } != { expected } ") ;
148+ throw new ( $ "{ total } != { jobCount } ") ;
152149 }
153150 }
154151
@@ -166,8 +163,7 @@ private static void BenchB()
166163 {
167164 var job = new HeavyCalculationJob ( index , index ) ;
168165 var handle = jobScheduler . Schedule ( job ) ;
169- handle . Parent = parentHandle . Index ;
170- handle . SetDependsOn ( parentHandle ) ;
166+ handle . SetParent ( parentHandle ) ;
171167 jobScheduler . Flush ( handle ) ;
172168 }
173169
@@ -178,50 +174,31 @@ private static void BenchB()
178174 timer . End ( jobCount * loopCount , "Every calculation job is its own handle" ) ;
179175 }
180176
181- private static void BenchC ( )
182- {
183- using var jobScheduler = new JobScheduler ( ) ;
184- var timer = new JobTimer ( ) ;
185- for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
186- {
187- var job = new ParallelJobProducer < HeavyCalculationJob > ( jobCount , new ( ) , jobScheduler ) ;
188- jobScheduler . Wait ( job . GetHandle ( ) ) ;
189- }
190-
191- timer . End ( jobCount * loopCount , "ParallelJobProducer" ) ;
192- }
193-
194- private static void BenchD ( )
195- {
196- var timer = new JobTimer ( ) ;
197- for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
198- {
199- Parallel . For ( 0 , jobCount , i =>
200- {
201- var job = new HeavyCalculationJob ( i , i ) ;
202- job . Execute ( ) ;
203- } ) ;
204- }
205-
206- timer . End ( jobCount * loopCount , "Just Parallel.For" ) ;
207- }
208-
209- private static long BenchVector ( bool dontUseVector )
177+ private static long BenchVector ( bool useVector )
210178 {
211- using var jobScheduler = new JobScheduler ( ) ;
212179 var timer = new JobTimer ( ) ;
213180 var data = new VectorCalculationJob { a = new float [ jobCount ] , b = new float [ jobCount ] , result = new float [ jobCount ] , Repetitions = 500 } ;
181+ var parentJob = ParallelForJobCommon . GlobalScheduler . Schedule ( ) ;
214182 for ( var sindex = 0 ; sindex < loopCount ; sindex ++ )
215183 {
216- var job = new ParallelJobProducer < VectorCalculationJob > ( jobCount , data , jobScheduler , 16 , ! dontUseVector ) ;
217- jobScheduler . Wait ( job . GetHandle ( ) ) ;
184+ var job = new ParallelJobProducer < VectorCalculationJob > ( 0 , jobCount , data , loopSize : 16 , onlySingle : ! useVector ) ;
185+ job . GetHandle ( ) . SetParent ( parentJob ) ;
186+ ParallelForJobCommon . GlobalScheduler . Flush ( job . GetHandle ( ) ) ;
218187 }
219-
220- return timer . End ( jobCount * loopCount , $ "Use vector: { ! dontUseVector } ") ;
188+ ParallelForJobCommon . GlobalScheduler . Flush ( parentJob ) ;
189+ ParallelForJobCommon . GlobalScheduler . Wait ( parentJob ) ;
190+ return timer . End ( jobCount * loopCount , $ "Use vector: { useVector } ") ;
221191 }
222192
223193 private static void Main ( string [ ] args )
224194 {
195+ ParallelJobBenchmark . Benchmark ( ) ;
196+ return ;
197+ ParallelForJobCommon . SetScheduler ( new ( ) ) ;
198+ // new JobHierarchyTest();
199+ // ParallelForJobCommon.DisposeScheduler();
200+ // return;
201+
225202 // var config = DefaultConfig.Instance.AddJob(Job.Default
226203 // .WithWarmupCount(2)
227204 // .WithMinIterationCount(10)
@@ -232,16 +209,24 @@ private static void Main(string[] args)
232209 // config = config.WithOptions(ConfigOptions.DisableOptimizationsValidator);
233210 // BenchmarkRunner.Run<JobSchedulerBenchmark>(config);
234211 // return;
235- for ( var i = 0 ; ; i ++ )
212+ var continiousRatio = 0d ;
213+ for ( var i = 0 ; i < 200000 ; i ++ )
236214 {
237215 // CorrectnessTestJob();
238216 // BenchB();
239- // BenchC();
240- // BenchD();
241- var vectorized = BenchVector ( true ) ;
242- var nonVectorized = BenchVector ( false ) ;
243- Console . WriteLine ( $ "Ratio { ( double ) nonVectorized / vectorized } ") ;
217+ // var vectorized = BenchVector(true);
218+ // var nonVectorized = BenchVector(false);
219+ // var ratio = (double)nonVectorized / vectorized;
220+ // Console.WriteLine($"Ratio {ratio}");
221+ // continiousRatio += ratio;
222+ // if (i % 10 == 0)
223+ {
224+ Console . WriteLine ( $ "Continious ratio: { continiousRatio / ( i + 1 ) } ") ;
225+ }
226+ Thread . Sleep ( 1 ) ;
244227 }
228+ ParallelForJobCommon . DisposeScheduler ( ) ;
229+
245230 //using var jobScheduler = new JobScheduler();
246231
247232 // Spawn massive jobs and wait for finish
0 commit comments