主要内容

选择基于线程和基于过程的环境

With Parallel Computing Toolbox™, you can run your parallel code in different parallel environments, such as thread-based or process-based environments. These environments offer different advantages.

笔记that thread-based environments support only a subset of the MATLAB®functions available for process workers. If you are interested in a function that is not supported, let theMathWorks技术支持金宝app团队知道。有关支持的更多信息,请参阅金宝appCheck Support for Thread-Based Environment

选择并行环境

根据您选择的并行环境的类型,功能在流程工作者或线程工作者上运行。要确定哪些环境适合您,请参阅以下图表和表。

  • 使用并行池功能,例如par或者Parfeval.,通过使用“在所选环境中创建并行池”parpool.功能。

    环境 Recommendation Example
    Thread-based environment on local machine

    使用此设置可简化内存使用率,更快的调度和更低的数据传输成本。

    parpool.('线程'的)

    笔记

    If you choose'线程'那check that your code is supported. For more information, seeCheck Support for Thread-Based Environment

    要了解从基于线程的池中获得充分的好处,请在基于过程的池中测量数据传输ticBytestocbytes.。If the data transfer is large, such as above 100 MB, then use'线程'

    基于过程的本地环境

    在缩放到集群或云之前,对大多数用例使用此设置和原型设计。

    parpool.('当地的'的)

    Process-based environment on remote cluster

    使用此设置缩放计算。

    parpool.('mycluster'的)
    在哪里mycluster.is the name of a cluster profile.

  • 使用群集功能,例如,通过使用使用的环境在所选环境中创建群集对象Parcluster.功能。请注意,仅在基于过程的环境中支持群集功能。金宝app

    环境 Recommendation Example
    基于过程的本地环境

    Use this setup if you have sufficient local resources, or to prototype before scaling to clusters or clouds.

    Parcluster('当地的'的)

    Process-based environment on remote cluster

    使用此设置缩放计算。

    Parcluster('mycluster'的)

    在哪里mycluster.is the name of a cluster profile.

Recommendation

建议使用默认为基于过程的环境。

  • 它们支持完金宝app整的并行语言。

  • 它们向后兼容以前的版本。

  • They are more robust in the event of crashes.

  • 外部库不需要是线程安全的。

Choose thread-based environments when:

  • 基于线程的环境支持您的并行代码。金宝app

  • You want reduced memory usage, faster scheduling and lower data transfer costs.

Compare Process Workers and Thread Workers

下面显示了用于利用线程工作者效率的示例的过程工人和线程工作者之间的性能比较。

创建一些数据。

X= rand(10000, 10000);

Create a parallel pool of process workers.

池= parpool('当地的'的);
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6).

执行和测量某些并行代码的数据传输时间。对于此示例,请使用aParfeval.执行。

ticbytes(池);tprocesses = timeit(@()fetchOutputs(parfeval(@ sum,1,x,'all')))tocbytes(池)
Tprocesses = 3.9060 BytessentToworks BytesreceivedFromWorkers ________________________________________ 0 0 0 2 0 0 3 0 0 4 0 0 0 5 5.6e + 09 16254 6 0 0总数5.6e + 09 16254

请注意,数据传输很大。为避免产生数据传输成本,可以使用线程工作者。删除当前并行池并创建基于线程的并行池。

delete(pool); pool = parpool('线程'的);

时间相同的代码运行了时间。

tthreads = timeit(@()fetchoutputs(parfeval(@ sum,1,x,'all'的)的)的)
tthreads = 0.0232.

比较时间。

FPRINTF('没有数据传输,这个例子是%.2fx更快。\ n',tprocesses / tthreads)
如果没有数据传输,则此示例速度速度为168.27倍。

线程工作者优于流程工作者,因为线程工人可以使用数据X不复制它,它们的安排开销较少。

Solve Optimization Problem in Parallel on Process-Based and Thread-Based Pool

此示例显示如何使用基于进程和基于基于线程的池来并行解决优化问题。基于线程的池进行优化,可针对较少的数据传输,更快的调度和减少内存使用情况进行优化,因此它们可能会导致应用程序中的性能增益。

问题描述

The problem is to change the position and angle of a cannon to fire a projectile as far as possible beyond a wall. The cannon has a muzzle velocity of 300 m/s. The wall is 20 m high. If the cannon is too close to the wall, it fires at too steep an angle, and the projectile does not travel far enough. If the cannon is too far from the wall, the projectile does not travel far enough. For full problem details, see并行优化ode(全局优化工具箱)或者视频的后一部分替代优化

matlab问题配方

To solve the problem, call thepatternsearchsolver from Global Optimization Toolbox. The objective function is in thecannonobjective辅助功能,计算射弹落在墙体之外的距离,以获得给定位置和角度。约束是大炮helper function, which calculates whether the projectile hits the wall, or even reaches the wall before hitting the ground. The helper functions are in separate files that you can view when you run this example.

设置以下输入patternsearchsolver. Note that, to use Parallel Computing Toolbox, you must set'使用指惯'真的在优化选项中。

lb = [-200; 0.05];UB = [-1; pi / 2-.05];x0 = [-30,pi / 3];opts = Optimoptions('patternsearch'......'USECOMPLETEPOLL', 真的,......'展示''off'......'使用指惯',真的);%没有线性约束,因此将这些输入设置为空:a = [];b = [];AEQ = [];beq = [];

解决基于过程的池

为了比较,首先解决基于过程的并行池的问题。

开始一个平行的流程工作台池。

p = parpool('当地的'的);
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6).

To reproduce the same computations later, seed the random generator with the default value.

rng.默认;

使用循环来解决问题几次并平均结果。

Tprocesses =零(5,1);为了重复= 1:数量(TProcesses)TIC [Xsolution,距离,EFLAG,OUTPT] = PatternSearch(@ Cannonobjective,X0,......A,B,AEQ,BEQ,LB,UB,@ Cannonstraint,OPTS);tProcesses(repetition) = toc;结尾TPROCESSES =平均值(TPROCESSES)
Tprocesses = 2.7677

要准备与基于线程的池进行比较,请删除当前并行池。

删除(p);

解决基于线程的池

开始一个平行的线程工人池。

p = parpool('线程'的);
Starting parallel pool (parpool) ... Connected to the parallel pool (number of workers: 6).

将随机数生成器恢复为默认设置,并与以前一起运行相同的代码。

rng.默认tThreads = zeros(5,1);为了重复= 1:numel(tthreads)tic [xsolution,距离,eflag,outpt] = patternsearch(@ cannonobjective,x0,......A,B,AEQ,BEQ,LB,UB,@ Cannonstraint,OPTS);tthreads(重复)= toc;结尾tthreads =卑鄙(tthreads)
tthreads = 1.5790.

比较线程工作者和流程工作者的表现。

FPRINTF('In this example, thread workers are %.2fx faster than process workers.\n',tprocesses / tthreads)
In this example, thread workers are 1.75x faster than process workers.

注意由于基于线程的池的优化,请注意性能收益。

使用计算完成后,删除并行池。

删除(p);

什么是基于线程的环境?

In thread-based environments, parallel language features run on workers that are backed by computing threads, which run code on cores on a machine. They differ from computing processes in that they coexist within the same process and can share memory.

基于线程的环境在基于过程的环境中具有以下优点。

  • 由于线程工作者可以共享内存,因此可以访问数字数据而无需复制,因此它们更高效。

  • 线程之间的通信耗时较少。因此,调度任务或工人间通信的开销较小。

When you use thread-based environments, keep the following considerations in mind.

  • Check that your code is supported for a thread-based environment. For more information, seeCheck Support for Thread-Based Environment

  • 如果您正在使用工人的外部库,则必须确保库函数是线程安全的。

什么是基于过程的环境?

In process-based environments, parallel language features run on workers that are backed by computing processes, which run code on cores on a machine. They differ from computing threads in that they are independent of each other.

基于过程的环境在基于线程的环境中具有以下优点。

  • They support all language features and are backwards compatible with previous releases.

  • They are more robust in the event of crashes. If a process worker crashes, then the MATLAB client does not crash. If a process worker crashes and your code does not useSPMD.或者distributed arrays, then the rest of the workers can continue running.

  • If you use external libraries from workers, then you do not need to pay attention to thread-safety.

  • You can use cluster features, such as

当您使用基于流程的环境时,请记住以下考虑。

  • 如果您的代码访问文件从工人,然后你must use additional options, such as'AttachedFiles'或者'AdditionalPaths',使数据可访问。

Check Support for Thread-Based Environment

Thread workers support only a subset of the MATLAB functions available for process workers. If you are interested in a function that is not supported, let theMathWorks技术支持金宝app团队知道。

在使用的独立应用程序中支持线程工作者金宝appMATLAB Compiler™和托管的Web应用程序MATLAB Web App Server™

有关线程工作人员支持的函数的更多信息,请参阅金宝app在基于线程的环境中运行matlab函数

也可以看看

|

相关话题