diff --git a/DigitDisplay/DigitDisplay.csproj b/DigitDisplay/DigitDisplay.csproj index 7c7af54..d1699e4 100644 --- a/DigitDisplay/DigitDisplay.csproj +++ b/DigitDisplay/DigitDisplay.csproj @@ -40,6 +40,13 @@ + + ..\packages\Microsoft.Tpl.Dataflow.4.5.24\lib\portable-net45+win8+wpa81\System.Threading.Tasks.Dataflow.dll + True + + + ..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll + @@ -103,6 +110,7 @@ ResXFileCodeGenerator Resources.Designer.cs + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/DigitDisplay/ParallelRecognizerControl.xaml.cs b/DigitDisplay/ParallelRecognizerControl.xaml.cs index bb447ab..a146a43 100644 --- a/DigitDisplay/ParallelRecognizerControl.xaml.cs +++ b/DigitDisplay/ParallelRecognizerControl.xaml.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.Threading.Tasks.Dataflow; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -35,11 +36,11 @@ public ParallelRecognizerControl(string classifierName, FSharpFunc + foreach (var s in rawData) { - int act = s.Split(',').Select(x => Convert.ToInt32(x)).First(); - int[] ints = s.Split(',').Select(x => Convert.ToInt32(x)).Skip(1).ToArray(); - var result = Recognizer.predict(ints, classifier); - - //predictions.Enqueue(new PredictionData() { - // prediction = result, actual = act.ToString(), imageData = s }); - - Task.Factory.StartNew(() => - CreateUIElements(result, act.ToString(), s, DigitsBox), - CancellationToken.None, - TaskCreationOptions.None, - options.TaskScheduler - ); + pipeline.Post(s); + } + + pipeline.Complete(); + await pipeline.Completion; + } + + private TransformBlock BuildPipeline() + { + var transform = new TransformBlock(s => + { + var split = s.Split(','); + var act = split.First(); + var ints = split.Skip(1).Select(x => Convert.ToInt32(x)).ToArray(); + return (act: act, ints: ints, s: s); }); - //Parallel.Invoke(options, () => - //{ - // PredictionData d; - // while (predictions.TryDequeue(out d)) - // { - // if (d.imageData != null) - // CreateUIElements(d.prediction, d.actual, d.imageData, DigitsBox); - // } - //}); + var compute = + new TransformBlock<(string act, int[] ints, string s), (string act, int[] ints, string s, string result) + >( + tuple => + { + var result = Recognizer.predict(tuple.ints, classifier); + return (act: tuple.act, ints: tuple.ints, s: tuple.s, result: result); + }, + new ExecutionDataflowBlockOptions + { + MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount - 1, 1) + }); + + var output = new ActionBlock<(string act, int[] ints, string s, string result)>(tuple => + { + Dispatcher.Invoke(() => CreateUIElements(tuple.result, tuple.act, tuple.s, DigitsBox)); + }); + + transform.LinkTo(compute); + compute.LinkTo(output); + + return transform; } private void CreateUIElements(string prediction, string actual, string imageData, diff --git a/DigitDisplay/packages.config b/DigitDisplay/packages.config new file mode 100644 index 0000000..0cac5c8 --- /dev/null +++ b/DigitDisplay/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file