Skip to content

Instantly share code, notes, and snippets.

@Ryan2065
Created March 3, 2016 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryan2065/f411cc1862e417c5f80b to your computer and use it in GitHub Desktop.
Save Ryan2065/f411cc1862e417c5f80b to your computer and use it in GitHub Desktop.
Function Start-EphingThreads {
Param($ScriptBlock, $threads)
$SyncTable.ThreadsInProg = New-Object System.Collections.ArrayList
$Synctable.Runspaces = @{}
$SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$SessionState.ApartmentState = "STA"
$SessionState.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'SyncTable', $SyncTable, ""))
$SyncTable.RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Threads, $SessionState, $Host)
$SyncTable.RunspacePool.Open()
for ($i = 0; $i -lt $Threads; $i++) {
$Synctable.Runspaces["Script$i"] = [Powershell]::Create().AddScript($Scriptblock).AddArgument($i).AddArgument($Directory)
$Synctable.Runspaces["Script$i"].RunspacePool = $SyncTable.RunspacePool
$Synctable.Runspaces["Handle$i"] = $Synctable.Runspaces["Script$i"].BeginInvoke()
}
}
$ScriptBlock = {
$WhiteWin = 0
$BlackWin = 0
$Draw = 0
Do {
[System.Threading.Mutex]$Mutex
Try {
[Bool]$Created = $false
$Mutex = New-Object System.Threading.Mutex($true, "MyMutex", [ref] $Created)
If (!$Created) {$Mutex.WaitOne()}
$FileList = $SyncTable.FileList
$FileListCount = $FileList.Count
$FileName = $SyncTable.FileList[0]
$SyncTable.FileList.RemoveAt(0)
}
Finally {
$Mutex.ReleaseMutex()
$Mutex.Dispose()
}
$file = New-Object System.IO.StreamReader -ArgumentList $FileName
$temphash = @{}
:loop while ($true )
{
try
{
$line = $null
$line = $file.ReadLine()
if ($line -eq $null)
{
$file.close()
break loop
}
if($line.StartsWith('[Result'))
{
$temphash[$line]+=1
}
}
catch
{
$file.close()
break loop
}
}
[System.Threading.Mutex]$Mutex
Try {
[Bool]$Created = $false
$Mutex = New-Object System.Threading.Mutex($true, "MyMutex", [ref] $Created)
If (!$Created) {$Mutex.WaitOne()}
$Wwin = $SyncTable.WhiteWin
$SyncTable.WhiteWin = $Wwin + $temphash['[Result "0-1"]']
$BWin = $SyncTable.BlackWin
$SyncTable.BlackWin = $BWin + $temphash['[Result "1-0"]']
$dr = $SyncTable.Draw
$SyncTable.Draw = $dr + $temphash['[Result "1/2-1/2"]']
}
Finally {
$Mutex.ReleaseMutex()
$Mutex.Dispose()
}
} while ($FileListCount -gt 1)
}
$TotalThreads = 6
$Global:SyncTable = [HashTable]::Synchronized(@{})
$SyncTable.FileList = New-Object System.Collections.Arraylist
Get-ChildItem 'C:\temp\ChessData-master' -Recurse -Filter '*.pgn' | ForEach-Object { $null = $SyncTable.FileList.Add($_.FullName) }
$SyncTable.WhiteWin = 0
$SyncTable.BlackWin = 0
$SyncTable.Draw = 0
Start-EphingThreads -ScriptBlock $ScriptBlock -threads $TotalThreads
$EndScriptCount = 0
do {
$EndScriptCount = 0
for ($i = 0; $i -le 20; $i++) {If ($SyncTable.Runspaces["Handle$i"].IsCompleted -eq $true) {$EndScriptCount++}}
Start-Sleep 1
} while ($EndScriptCount -lt $TotalThreads)
$WWin = $SyncTable.WhiteWin
$BWin = $SyncTable.BlackWin
$Draw = $SyncTable.Draw
Write-Host "W: $WWin B: $BWin D: $Draw"
@Ryan2065
Copy link
Author

Ryan2065 commented Mar 3, 2016

This gets me:

W: 1493574 B: 2013976 D: 1848775

Days : 0
Hours : 0
Minutes : 1
Seconds : 18
Milliseconds : 728
Ticks : 787280400
TotalDays : 0.000911204166666667
TotalHours : 0.0218689
TotalMinutes : 1.312134
TotalSeconds : 78.72804
TotalMilliseconds : 78728.04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment