Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DigitDisplay/DigitDisplay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.5.24.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Tpl.Dataflow.4.5.24\lib\portable-net45+win8+wpa81\System.Threading.Tasks.Dataflow.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -103,6 +110,7 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
71 changes: 43 additions & 28 deletions DigitDisplay/ParallelRecognizerControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,11 +36,11 @@ public ParallelRecognizerControl(string classifierName, FSharpFunc<int[], string
Loaded += RecognizerControl_Loaded;
}

private void RecognizerControl_Loaded(object sender, RoutedEventArgs e)
private async void RecognizerControl_Loaded(object sender, RoutedEventArgs e)
{

ClassifierText.Text = classifierName;
PopulatePanel(rawData);
await PopulatePanel(rawData);
}

private struct PredictionData
Expand All @@ -49,39 +50,53 @@ private struct PredictionData
public string imageData;
}

private void PopulatePanel(string[] rawData)
private async Task PopulatePanel(string[] rawData)
{
startTime = DateTime.Now;

var options = new ParallelOptions();
options.TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
var pipeline = BuildPipeline();

var loopResult = Parallel.ForEach(rawData, s =>
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<string>(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<string, (string act, int[] ints, string s)> BuildPipeline()
{
var transform = new TransformBlock<string, (string act, int[] ints, string s)>(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<string>(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,
Expand Down
5 changes: 5 additions & 0 deletions DigitDisplay/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Tpl.Dataflow" version="4.5.24" targetFramework="net45" />
<package id="System.ValueTuple" version="4.3.0" targetFramework="net45" />
</packages>