multithreading - Powershell Multithreaded math -


i'm working on self-inspired project learn powershell, , have been writing script generate prime numbers. stands, script works without issue, next goal increase it's processing speed.

cls $primes = @() $primes += 3 $targetnum = 5 $primesindex = 0 $numofprime = 3 while(1) {     if(($targetnum / 3) -lt 3)      {         $primes += $targetnum         $targetnum += 2         $numofprime += 1             }     else     {         if($primes[$primesindex] -le ($targetnum / ($primes[$primesindex])))          {             if($targetnum % $primes[$primesindex] -eq 0)             {                 $primesindex = 0                 $targetnum += 2              }             else             {                 $primesindex++             }         }         else         {             $primesindex = 0             $numofprime += 1             $primes += $targetnum             $targetnum += 2             if($targetnum -gt 100000){write-host $targetnum ", " $numofprime;break}         }     } } 

if execute statement measure-command {& ".\primes.ps1"} calculate first 100,000 primes in ≈ 9.1 seconds (for me anyway), performing calculations using single cpu thread. i've looked using start-job , start-processcommands implement sort of multi-threading, failing understand how work.

if moved prime testing calculation function, how go calling function across 4 of logical cores? perhaps creating second powershell script can pass value test, , start-process on that? above script solves average of 10,000 primes\sec in first 10 sec, powershell able start , stop worker scripts quickly?

there 2 terms must considered separately: asynchronous , parallel programming. first 1 provides simple background execution of arbitrary task, while latter obliges (as author of algorithm) split task several independent tasks able run them on separate calculating units (cores, processors, machines).

you can start asynchronous task function, won't give parallel calculation:

start-job -name "getprimes" -scriptblock {myprimesfunction} | wait-job | receive-job 

an easy way achieve parallelism split function chunks (for example, several number intervals in search primes) , run each chunk start-job:

$jobs = @()  # gather jobs array  $jobs += start-job -scriptblock {myprimesfunction1} $jobs += start-job -scriptblock {myprimesfunction2} $jobs += start-job -scriptblock {myprimesfunction3} $jobs += start-job -scriptblock {myprimesfunction4}  # wait jobs wait-job $jobs | out-null  # result arrays jobs $results = $jobs | receive-job  $primes = @()  # merge results single array foreach ($result in $results) {   $primes += $result } 

notice function must return result array of primes. , must rewrite function 4 times, each using different number intervals.

approach jobs relies on system process management (cause each job starts separate powershell.exe). approach use runspaces. can read several posts it.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -