Skip to content

Commit

Permalink
Add an "advanced" option to limit number of threads used by the paral…
Browse files Browse the repository at this point in the history
…lel loops
  • Loading branch information
Coding-Enthusiast committed Jul 12, 2023
1 parent 8c3835e commit 09466f3
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 48 deletions.
4 changes: 3 additions & 1 deletion Src/FinderOuter/Services/Base16Sevice.cs
Expand Up @@ -169,7 +169,9 @@ private unsafe void Loop()
{
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem, state) => Loop(firstItem, smallPub, state));

ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem, state) => Loop(firstItem, smallPub, state));
}
}

Expand Down
24 changes: 14 additions & 10 deletions Src/FinderOuter/Services/Base58Service.cs
Expand Up @@ -189,7 +189,8 @@ private void WifLoopMissingEnd(bool compressed)
report.SetTotal(diff);
report.SetProgressStep(loopCount);

Parallel.For(0, loopCount, (i, state) =>
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, loopCount, opts, (i, state) =>
WifLoopMissingEnd(sc, i, i == loopCount - 1 ? loopLastMax : WifEndDiv, comparer.Clone(), state));
}

Expand Down Expand Up @@ -357,7 +358,8 @@ private unsafe void LoopComp()
// That makes 5 the optimal number for using parallelization
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem) => LoopComp(ParallelPre(firstItem, 52), firstItem, 1));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem) => LoopComp(ParallelPre(firstItem, 52), firstItem, 1));
}
else
{
Expand Down Expand Up @@ -451,7 +453,8 @@ private unsafe void LoopUncomp()
{
// Same as LoopComp()
report.SetProgressStep(58);
Parallel.For(0, 58, (firstItem) => LoopUncomp(ParallelPre(firstItem, 51), firstItem, 1));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, 58, opts, (firstItem) => LoopUncomp(ParallelPre(firstItem, 51), firstItem, 1));
}
else
{
Expand Down Expand Up @@ -555,7 +558,8 @@ private unsafe void Loop21()
{
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem) => Loop21(ParallelPre21(firstItem), firstItem, 1));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem) => Loop21(ParallelPre21(firstItem), firstItem, 1));
}
else
{
Expand Down Expand Up @@ -669,7 +673,8 @@ private unsafe void Loop58()
{
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem) => Loop58(ParallelPre58(firstItem), firstItem, 1));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem) => Loop58(ParallelPre58(firstItem), firstItem, 1));
}
else
{
Expand Down Expand Up @@ -858,7 +863,8 @@ private unsafe bool SpecialLoopComp2(string key, bool comp)

Debug.Assert(pow[0] == 12);

Parallel.For(0, 58, (c1, state) =>
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, 58, opts, (c1, state) =>
{
for (int c2 = 0; c2 < 58; c2++)
{
Expand Down Expand Up @@ -1007,10 +1013,8 @@ private unsafe bool SpecialLoopComp3(string key)
}

CancellationTokenSource cancelToken = new();
ParallelOptions options = new()
{
CancellationToken = cancelToken.Token,
};
ParallelOptions options = report.BuildParallelOptions();
options.CancellationToken = cancelToken.Token;

try
{
Expand Down
8 changes: 5 additions & 3 deletions Src/FinderOuter/Services/Bip38Service.cs
Expand Up @@ -1649,25 +1649,27 @@ private void StartParallel()
{
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem, state) => MainLoop(firstItem, state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem, state) => MainLoop(firstItem, state));
}

private void StartParallelEC()
{
report.SetProgressStep(searchSpace.AllValues.Length);
ParallelOptions opts = report.BuildParallelOptions();
if (searchSpace.hasLot)
{
report.AddMessageSafe("EC mult mode with LOT/Sequence");
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem, state) => MainLoopECLot(firstItem, state));
Parallel.For(0, max, opts, (firstItem, state) => MainLoopECLot(firstItem, state));
}
else
{
report.AddMessageSafe("EC mult mode with no LOT/Sequence");
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem, state) => MainLoopECNoLot(firstItem, state));
Parallel.For(0, max, opts, (firstItem, state) => MainLoopECNoLot(firstItem, state));
}
}

Expand Down
9 changes: 6 additions & 3 deletions Src/FinderOuter/Services/MiniKeyService.cs
Expand Up @@ -173,7 +173,8 @@ private unsafe void Loop23()
// which makes it the optimal number for using parallelization
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem, state) => Loop23(firstItem, comparer.Clone(), state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem, state) => Loop23(firstItem, comparer.Clone(), state));
}
else
{
Expand Down Expand Up @@ -305,7 +306,8 @@ private unsafe void Loop27()
{
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem, state) => Loop27(firstItem, comparer.Clone(), state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem, state) => Loop27(firstItem, comparer.Clone(), state));
}
else
{
Expand Down Expand Up @@ -438,7 +440,8 @@ private unsafe void Loop31()
{
int max = searchSpace.PermutationCounts[0];
report.SetProgressStep(max);
Parallel.For(0, max, (firstItem, state) => Loop31(firstItem, comparer.Clone(), state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, max, opts, (firstItem, state) => Loop31(firstItem, comparer.Clone(), state));
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion Src/FinderOuter/Services/MnemonicExtensionService.cs
Expand Up @@ -453,7 +453,8 @@ public unsafe void MainLoop(ulong[] pads, byte[] salt, byte[] allValues, int pas
else
{
report.SetProgressStep(allValues.Length);
Parallel.For(0, allValues.Length,
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, allValues.Length, opts,
(firstItem, state) => LoopBip39(pads, ParallelSalt(salt, allValues[firstItem]), allValues, passLength, state));
}
}
Expand Down
18 changes: 12 additions & 6 deletions Src/FinderOuter/Services/MnemonicSevice.cs
Expand Up @@ -516,7 +516,8 @@ private unsafe void Loop24()
{
report.SetProgressStep(searchSpace.PermutationCounts[0]);
int firstIndex = searchSpace.MissingIndexes[0];
Parallel.For(0, searchSpace.PermutationCounts[0], (firstItem, state) => Loop24(firstItem, firstIndex, state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, searchSpace.PermutationCounts[0], opts, (firstItem, state) => Loop24(firstItem, firstIndex, state));
}
else
{
Expand Down Expand Up @@ -705,7 +706,8 @@ private unsafe void Loop21()
{
report.SetProgressStep(searchSpace.PermutationCounts[0]);
int firstIndex = searchSpace.MissingIndexes[0];
Parallel.For(0, searchSpace.PermutationCounts[0], (firstItem, state) => Loop21(firstItem, firstIndex, state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, searchSpace.PermutationCounts[0], opts, (firstItem, state) => Loop21(firstItem, firstIndex, state));
}
else
{
Expand Down Expand Up @@ -845,7 +847,8 @@ private unsafe void Loop18()
{
report.SetProgressStep(searchSpace.PermutationCounts[0]);
int firstIndex = searchSpace.MissingIndexes[0];
Parallel.For(0, searchSpace.PermutationCounts[0], (firstItem, state) => Loop18(firstItem, firstIndex, state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, searchSpace.PermutationCounts[0], opts, (firstItem, state) => Loop18(firstItem, firstIndex, state));
}
else
{
Expand Down Expand Up @@ -983,7 +986,8 @@ private unsafe void Loop15()
{
report.SetProgressStep(searchSpace.PermutationCounts[0]);
int firstIndex = searchSpace.MissingIndexes[0];
Parallel.For(0, searchSpace.PermutationCounts[0], (firstItem, state) => Loop15(firstItem, firstIndex, state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, searchSpace.PermutationCounts[0], opts, (firstItem, state) => Loop15(firstItem, firstIndex, state));
}
else
{
Expand Down Expand Up @@ -1121,7 +1125,8 @@ private unsafe void Loop12()
{
report.SetProgressStep(searchSpace.PermutationCounts[0]);
int firstIndex = searchSpace.MissingIndexes[0];
Parallel.For(0, searchSpace.PermutationCounts[0], (firstItem, state) => Loop12(firstItem, firstIndex, state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, searchSpace.PermutationCounts[0], opts, (firstItem, state) => Loop12(firstItem, firstIndex, state));
}
else
{
Expand Down Expand Up @@ -1295,7 +1300,8 @@ private unsafe void LoopElectrum(ElectrumMnemonic.MnemonicType mnType)
{
report.SetProgressStep(searchSpace.PermutationCounts[0]);
int firstIndex = searchSpace.MissingIndexes[0];
Parallel.For(0, searchSpace.PermutationCounts[0], (firstItem, state) => LoopElectrum(firstItem, firstIndex, mask, expected, state));
ParallelOptions opts = report.BuildParallelOptions();
Parallel.For(0, searchSpace.PermutationCounts[0], opts, (firstItem, state) => LoopElectrum(firstItem, firstIndex, mask, expected, state));
}
else
{
Expand Down
18 changes: 10 additions & 8 deletions Src/FinderOuter/ViewModels/MainWindowViewModel.cs
Expand Up @@ -17,20 +17,21 @@ public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
WinMan = new WindowManager();
Settings = new();

OptionList = new OptionVmBase[]
{
new MissingBase16ViewModel(),
new MissingBase58ViewModel(),
new MissingMiniPrivateKeyViewModel(),
new MissingBip38PassViewModel(),
new MissingMnemonicViewModel(),
new MissingMnemonicPassViewModel(),
new MissingBase16ViewModel(Settings),
new MissingBase58ViewModel(Settings),
new MissingMiniPrivateKeyViewModel(Settings),
new MissingBip38PassViewModel(Settings),
new MissingMnemonicViewModel(Settings),
new MissingMnemonicPassViewModel(Settings),
new MissingBip32PathViewModel(),
new MissingArmoryViewModel(),
new MissingEncodingViewModel(),
};

WinMan = new WindowManager();
}

private static Version Version => Assembly.GetExecutingAssembly().GetName().Version;
Expand Down Expand Up @@ -92,6 +93,7 @@ private void SelectedOption_PropertyChanged(object sender, PropertyChangedEventA
public HelpViewModel HelpVm => new();

public IWindowManager WinMan { get; set; }
public Settings Settings { get; set; }

public void OpenAbout() => WinMan.ShowDialog(new AboutViewModel());
}
Expand Down
3 changes: 2 additions & 1 deletion Src/FinderOuter/ViewModels/MissingBase16ViewModel.cs
Expand Up @@ -18,8 +18,9 @@ namespace FinderOuter.ViewModels
{
public class MissingBase16ViewModel : OptionVmBase
{
public MissingBase16ViewModel()
public MissingBase16ViewModel(Settings settings)
{
Result.Settings = settings;
b16Service = new Base16Sevice(Result);
searchSpace = new();

Expand Down
3 changes: 2 additions & 1 deletion Src/FinderOuter/ViewModels/MissingBase58ViewModel.cs
Expand Up @@ -18,8 +18,9 @@ namespace FinderOuter.ViewModels
{
public class MissingBase58ViewModel : OptionVmBase
{
public MissingBase58ViewModel()
public MissingBase58ViewModel(Settings settings)
{
Result.Settings = settings;
// Don't move this line, service must be instantiated here
b58Service = new Base58Service(Result);
searchSpace = new();
Expand Down
3 changes: 2 additions & 1 deletion Src/FinderOuter/ViewModels/MissingBip38PassViewModel.cs
Expand Up @@ -18,8 +18,9 @@ namespace FinderOuter.ViewModels
{
public class MissingBip38PassViewModel : OptionVmBase
{
public MissingBip38PassViewModel()
public MissingBip38PassViewModel(Settings settings)
{
Result.Settings = settings;
Bip38Service = new(Result);
CompareInputTypeList = ListHelper.GetEnumDescItems(new CompareInputType[] { CompareInputType.PrivateKey }).ToArray();
SelectedCompareInputType = CompareInputTypeList.First();
Expand Down
3 changes: 2 additions & 1 deletion Src/FinderOuter/ViewModels/MissingMiniPrivateKeyViewModel.cs
Expand Up @@ -18,8 +18,9 @@ namespace FinderOuter.ViewModels
{
public class MissingMiniPrivateKeyViewModel : OptionVmBase
{
public MissingMiniPrivateKeyViewModel()
public MissingMiniPrivateKeyViewModel(Settings settings)
{
Result.Settings = settings;
// Don't move this line, service must be instantiated here
miniService = new MiniKeyService(Result);
searchSpace = new();
Expand Down
3 changes: 2 additions & 1 deletion Src/FinderOuter/ViewModels/MissingMnemonicPassViewModel.cs
Expand Up @@ -17,8 +17,9 @@ namespace FinderOuter.ViewModels
{
public class MissingMnemonicPassViewModel : OptionVmBase
{
public MissingMnemonicPassViewModel()
public MissingMnemonicPassViewModel(Settings settings)
{
Result.Settings = settings;
WordListsList = ListHelper.GetAllEnumValues<BIP0039.WordLists>().ToArray();
MnemonicTypesList = ListHelper.GetAllEnumValues<MnemonicTypes>().ToArray();
CompareInputTypeList = ListHelper.GetEnumDescItems<CompareInputType>().ToArray();
Expand Down
3 changes: 2 additions & 1 deletion Src/FinderOuter/ViewModels/MissingMnemonicViewModel.cs
Expand Up @@ -20,8 +20,9 @@ namespace FinderOuter.ViewModels
{
public class MissingMnemonicViewModel : OptionVmBase
{
public MissingMnemonicViewModel()
public MissingMnemonicViewModel(Settings settings)
{
Result.Settings = settings;
WordListsList = ListHelper.GetAllEnumValues<BIP0039.WordLists>().ToArray();
MnemonicTypesList = ListHelper.GetAllEnumValues<MnemonicTypes>().ToArray();
ElectrumMnemonicTypesList = ListHelper.GetAllEnumValues<ElectrumMnemonic.MnemonicType>().ToArray();
Expand Down
44 changes: 34 additions & 10 deletions Src/FinderOuter/Views/MainWindow.axaml
Expand Up @@ -76,30 +76,54 @@
Margin="3"
Grid.Row="2"/>

<Grid ColumnDefinitions="auto,*" RowDefinitions="auto,auto,auto" Grid.Column="0" Grid.Row="3" Background="#e5e5e5">
<Grid ColumnDefinitions="auto,*" RowDefinitions="auto,auto,auto,auto" Grid.Column="0" Grid.Row="3" Background="#e5e5e5">
<Expander Header="Settings" Margin="5,0,0,10" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" IsExpanded="False">
<Grid ColumnDefinitions="auto,*,auto">
<TextBlock Text="Cores:"/>
<Slider Value="{Binding Settings.CoreCount}"
Minimum="1" Maximum="{Binding Settings.MaxCoreCount}"
VerticalAlignment="Center"
TickFrequency="1"
IsSnapToTickEnabled="True"
SmallChange="1" LargeChange="1"
Grid.Column="1">
<Slider.Styles>
<Style Selector="Slider /template/Thumb">
<Setter Property="ToolTip.Tip" Value="{Binding $parent[Slider].Value,Mode=OneWay,StringFormat='\{0:n0\} core'}" />
<Setter Property="ToolTip.Placement" Value="Top"/>
<Setter Property="ToolTip.VerticalOffset" Value="-5"/>
<Setter Property="ToolTip.HorizontalOffset" Value="-10"/>
</Style>
</Slider.Styles>
</Slider>
<TextBlock Text="{Binding Settings.CoreCount}" Grid.Column="2"/>
<!--Add override button here? Grid.Column="2"-->
</Grid>
</Expander>

<TextBlock Text="Checked:"
Margin="0,0,5,0"
Grid.Column="0" Grid.Row="0"/>
Margin="5,0,5,0"
Grid.Column="0" Grid.Row="1"/>
<TextBlock Text="{Binding SelectedOption.Result.TotalChecked, StringFormat=\{0:N0\}}"
ToolTip.Tip="Total keys checked"
MinWidth="20"
Grid.Column="1" Grid.Row="0"/>
Grid.Column="1" Grid.Row="1"/>

<TextBlock Text="Speed:"
Margin="0,0,5,0"
Grid.Column="0" Grid.Row="1"/>
Margin="5,0,5,0"
Grid.Column="0" Grid.Row="2"/>
<TextBlock Text="{Binding SelectedOption.Result.Speed, StringFormat=\{0:N0\}}"
ToolTip.Tip="Estimated speed (keys/second)"
MinWidth="20"
Grid.Column="1" Grid.Row="1"/>
Grid.Column="1" Grid.Row="2"/>

<TextBlock Text="Time left:"
Margin="0,0,5,0"
Grid.Column="0" Grid.Row="2"/>
Margin="5,0,5,0"
Grid.Column="0" Grid.Row="3"/>
<TextBlock Text="{Binding SelectedOption.Result.Remaining, StringFormat=\{0:hh\\:mm\\:ss\}}"
ToolTip.Tip="Estimated time remaining"
MinWidth="30"
Grid.Column="1" Grid.Row="2"/>
Grid.Column="1" Grid.Row="3"/>
</Grid>

<Grid ColumnDefinitions="auto,*" Grid.Column="0" Grid.Row="4" Background="#e5e5e5">
Expand Down

0 comments on commit 09466f3

Please sign in to comment.