Powershell 多线程数学 [英] Powershell Multithreaded math

查看:135
本文介绍了Powershell 多线程数学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开展一个自我启发的项目来学习 powershell,并且一直在编写一个脚本来生成素数.就目前而言,该脚本可以正常运行,但我的下一个目标是提高其处理速度.

I'm currently working on a self-inspired project to learn powershell, and have been writing a script to generate prime numbers. As it stands, the script works without issue, but my next goal is to 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}
        }
    }
}

如果我执行语句 Measure-command {&".\primes.ps1"} 它将在 ≈ 9.1 秒内计算前 100,000 个素数(无论如何对我来说),但这只是使用单个 CPU 线程执行计算.我研究过使用 start-jobstart-process 命令来实现某种多线程,但我不明白它们是如何工作的.

If I Execute the statement Measure-command {& ".\primes.ps1"} it will calculate the first 100,000 primes in ≈ 9.1 seconds (for me anyway), but this is only performing the calculations using a single CPU thread. I've looked into using start-job and start-processcommands to implement some sort of multi-threading, but I am failing to understand how they work.

如果我将质数测试计算移到一个函数中,我将如何在我的所有 4 个逻辑核心上调用该函数?也许创建第二个 powershell 脚本,我可以传递一个值来测试,然后启动进程?上面的脚本在前 10 秒内平均解决了 10,000 个质数\秒,powershell 甚至能够快速启动和停止一些工作脚本吗?

If I moved the prime testing calculation to a function, how would I go about calling that function across all 4 of my logical cores? Perhaps creating a second powershell script that I can pass a value to test, and start-process on that? The above script solves an average of 10,000 primes\sec in the first 10 sec, will powershell even be able to start and stop some worker scripts that quickly?

推荐答案

有两个术语必须分开考虑:异步并行编程.第一个提供任意任务的简单后台执行,而后者要求您(作为算法的作者)将您的任务拆分为几个独立的任务,以便能够在单独的计算单元(内核、处理器、机器)上运行它们.

There are two terms that must be considered separately: asynchronous and parallel programming. First one provides simple background execution of an arbitrary task, while latter obliges you (as the author of the algorithm) to split your task into several independent tasks to be able to run them on separate calculating units (cores, processors, machines).

您可以轻松地使用您的函数启动异步任务,但它不会为您提供并行计算:

You can easily start asynchronous task with your function, but it won't give you parallel calculation:

Start-Job -Name "GetPrimes" -ScriptBlock {MyPrimesFunction} | Wait-Job | Receive-Job

实现并行性的一种简单方法是将您的函数拆分为多个块(例如,通过几个数字间隔来搜索素数),然后使用 Start-Job 运行每个块:

An easy way to achieve parallelism is to split your function into chunks (for example, by several number intervals in which it will search primes) and then run each chunk with Start-Job:

$jobs = @()

# gather all jobs into an array 
$jobs += Start-Job -ScriptBlock {MyPrimesFunction1}
$jobs += Start-Job -ScriptBlock {MyPrimesFunction2}
$jobs += Start-Job -ScriptBlock {MyPrimesFunction3}
$jobs += Start-Job -ScriptBlock {MyPrimesFunction4}

# wait for all jobs
Wait-Job $jobs | Out-Null

# get result arrays from jobs
$results = $jobs | Receive-Job

$primes = @()

# merge results into single array
foreach ($result in $results) {
  $primes += $result
}

请注意,您的函数必须以素数数组的形式返回结果.并且您必须重写您的函数 4 次,每次使用不同的数字间隔.

Notice that your function must return a result as an array of primes. And you must rewrite your function 4 times, each using different number intervals.

作业方法依赖于系统进程管理(因为每个作业启动单独的 powershell.exe).另一种方法是使用运行空间.您可以阅读关于它的几篇博文.

Approach with jobs relies on system process management (cause each job starts separate powershell.exe). Another approach is to use Runspaces. You can read several posts about it.

这篇关于Powershell 多线程数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆