Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Param(
- [int]$bufferSize=100,
- [int]$numThreads=4,
- [string]$sampleFileLocation="c:\users\1530983\desktop\ass2-20000.txt"
- );
- set-strictmode -version 2.0;
- [int]$global:threadBufferSize = ($bufferSize / $numThreads);
- $global:jobSubTotals = @{};
- $global:threadScript = {
- Param($subThreads, [int[]]$items, $threadName);
- #Checks if number is prime
- function isPrime([int]$num) {
- if ($num % 2 -eq 0) {
- return $false;
- }
- for ($i = 3; $i * $i -lt $num; $i += 2) {
- if ($num % $i -eq 0) {
- return $false;
- }
- }
- return $true;
- }
- #Calculate subtotal value for this thread
- [int]$subTotal = 0;
- foreach($item in $items) {
- if ((isPrime $item)) {
- $subTotal += $item;
- }
- }
- Add-Content ("c:\users\1530983\desktop\thread_" + $threadName + ".txt") $subTotal;
- Add-Content ("c:\users\1530983\desktop\threadWait_" + $threadName + ".txt") "\n";
- $subThreads.Reverse();#Reverse sub threads to start by getting oldest thread first
- foreach($job in $subThreads) {
- Add-Content ("c:\users\1530983\desktop\threadWait_" + $threadName + ".txt") ("Waiting on " + $job.Name);
- Wait-Job -Job $job;
- Add-Content ("c:\users\1530983\desktop\threadWait_" + $threadName + ".txt") ("Got job " + $subThreadName + " " + $job.Name);
- $subTotal += Receive-Job -Job $job;
- }
- Add-Content ("c:\users\1530983\desktop\thread_" + $threadName + ".txt") "Done";
- return $subTotal;
- };
- function StartThreads {
- Param([int[]]$items, $threadName);
- [int]$subThreadCount = 0;
- $subThreads = New-Object System.Collections.ArrayList;
- while ($items.Count -gt $threadBufferSize + 1) {#Loop until this thread has proper buffer size
- [int]$subThreadSize = $items.Count / 2;
- #Start thread using first have of $items
- [int[]]$subItems = @(0) * $subThreadSize;
- [int]$subItemIndex = 0;
- foreach ($subItemIndex in 0..($subThreadSize - 1)) {
- $subItems[$subItemIndex] = $items[$subItemIndex];
- }
- #Start sub thread
- StartThreads -items $subItems -threadName ($threadName + "-" + $subThreadCount);
- $job = Get-Job -Name ($threadName + "-" + $subThreadCount);
- $subThreads.Add($job);
- $subThreadCount++;
- #Get new $items using second have of current $items
- [int[]]$subItems = @(0) * ($items.Count - $subThreadSize);
- $subItemIndex++;
- $newItemIndex = 0;
- foreach ($subItemIndex in $subItemIndex..($items.Count - 1)) {
- $subItems[$newItemIndex] = $items[$subItemIndex];
- $newItemIndex++;
- }
- $items = $subItems;
- }
- Start-Job -ScriptBlock $threadScript -ArgumentList @($subThreads, $items, $threadName) -Name $threadName;
- }
- $file = New-Object System.IO.StreamReader -Arg $sampleFileLocation;
- [int[]]$arr = @();
- [int]$index = 0;
- [int]$total = 0;
- while ($line = $file.ReadLine()) {
- $arr += $line;
- $index++;
- if ($index -eq $bufferSize) {#Buffer full, time to process
- $mainThreadName = "MainJob";
- StartThreads -items $arr -threadName $mainThreadName;
- $job = Get-Job -Name $mainThreadName;
- Wait-Job -Job $job;
- $total += Receive-Job -Job $job;
- Remove-Job *;
- #Reset buffer
- $index = 0;
- $arr = @();
- }
- }
- echo $total;
- $file.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement