Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. Param(
  2. [int]$bufferSize=100,
  3. [int]$numThreads=4,
  4. [string]$sampleFileLocation="c:\users\1530983\desktop\ass2-20000.txt"
  5. );
  6. set-strictmode -version 2.0;
  7.  
  8. [int]$global:threadBufferSize = ($bufferSize / $numThreads);
  9.  
  10. $global:jobSubTotals = @{};
  11.  
  12. $global:threadScript = {
  13. Param($subThreads, [int[]]$items, $threadName);
  14.  
  15. #Checks if number is prime
  16. function isPrime([int]$num) {
  17. if ($num % 2 -eq 0) {
  18. return $false;
  19. }
  20.  
  21. for ($i = 3; $i * $i -lt $num; $i += 2) {
  22. if ($num % $i -eq 0) {
  23. return $false;
  24. }
  25. }
  26. return $true;
  27. }
  28.  
  29. #Calculate subtotal value for this thread
  30. [int]$subTotal = 0;
  31. foreach($item in $items) {
  32. if ((isPrime $item)) {
  33. $subTotal += $item;
  34. }
  35. }
  36.  
  37. Add-Content ("c:\users\1530983\desktop\thread_" + $threadName + ".txt") $subTotal;
  38. Add-Content ("c:\users\1530983\desktop\threadWait_" + $threadName + ".txt") "\n";
  39.  
  40. $subThreads.Reverse();#Reverse sub threads to start by getting oldest thread first
  41. foreach($job in $subThreads) {
  42. Add-Content ("c:\users\1530983\desktop\threadWait_" + $threadName + ".txt") ("Waiting on " + $job.Name);
  43. Wait-Job -Job $job;
  44. Add-Content ("c:\users\1530983\desktop\threadWait_" + $threadName + ".txt") ("Got job " + $subThreadName + " " + $job.Name);
  45.  
  46. $subTotal += Receive-Job -Job $job;
  47. }
  48. Add-Content ("c:\users\1530983\desktop\thread_" + $threadName + ".txt") "Done";
  49. return $subTotal;
  50. };
  51.  
  52. function StartThreads {
  53. Param([int[]]$items, $threadName);
  54. [int]$subThreadCount = 0;
  55. $subThreads = New-Object System.Collections.ArrayList;
  56.  
  57. while ($items.Count -gt $threadBufferSize + 1) {#Loop until this thread has proper buffer size
  58. [int]$subThreadSize = $items.Count / 2;
  59.  
  60. #Start thread using first have of $items
  61. [int[]]$subItems = @(0) * $subThreadSize;
  62. [int]$subItemIndex = 0;
  63. foreach ($subItemIndex in 0..($subThreadSize - 1)) {
  64. $subItems[$subItemIndex] = $items[$subItemIndex];
  65. }
  66.  
  67. #Start sub thread
  68. StartThreads -items $subItems -threadName ($threadName + "-" + $subThreadCount);
  69. $job = Get-Job -Name ($threadName + "-" + $subThreadCount);
  70. $subThreads.Add($job);
  71. $subThreadCount++;
  72.  
  73. #Get new $items using second have of current $items
  74. [int[]]$subItems = @(0) * ($items.Count - $subThreadSize);
  75. $subItemIndex++;
  76. $newItemIndex = 0;
  77. foreach ($subItemIndex in $subItemIndex..($items.Count - 1)) {
  78. $subItems[$newItemIndex] = $items[$subItemIndex];
  79. $newItemIndex++;
  80. }
  81. $items = $subItems;
  82. }
  83.  
  84. Start-Job -ScriptBlock $threadScript -ArgumentList @($subThreads, $items, $threadName) -Name $threadName;
  85. }
  86.  
  87. $file = New-Object System.IO.StreamReader -Arg $sampleFileLocation;
  88. [int[]]$arr = @();
  89. [int]$index = 0;
  90. [int]$total = 0;
  91.  
  92. while ($line = $file.ReadLine()) {
  93. $arr += $line;
  94. $index++;
  95.  
  96. if ($index -eq $bufferSize) {#Buffer full, time to process
  97. $mainThreadName = "MainJob";
  98. StartThreads -items $arr -threadName $mainThreadName;
  99.  
  100. $job = Get-Job -Name $mainThreadName;
  101. Wait-Job -Job $job;
  102.  
  103. $total += Receive-Job -Job $job;
  104. Remove-Job *;
  105.  
  106.  
  107. #Reset buffer
  108. $index = 0;
  109. $arr = @();
  110. }
  111. }
  112. echo $total;
  113.  
  114. $file.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement