Parallel Computing with MATLAB(并行计算)

合集下载

MATLAB并行计算的实现方法与技巧

MATLAB并行计算的实现方法与技巧

MATLAB并行计算的实现方法与技巧1. 引言MATLAB是一种非常强大的数值计算和科学编程工具,但在处理大规模数据以及复杂计算任务时,单个计算机资源的限制会导致计算效率低下。

为了充分利用计算资源,提高计算速度,我们可以使用并行计算技术。

本文将介绍MATLAB并行计算的实现方法与技巧,帮助读者合理利用多个计算核心,提升计算效率。

2. 并行计算概述并行计算是将一个任务划分为多个子任务,同时在多个计算核心上进行计算,以加快计算速度。

在MATLAB中,我们可以使用Parallel Computing Toolbox来实现并行计算。

其主要方式有两种:任务并行计算和数据并行计算。

3. 任务并行计算任务并行计算是指将一个大任务分解成多个子任务,每个子任务被分配到不同的计算核心上执行。

MATLAB提供了parfor循环来方便开发者实现任务并行计算。

下面是一个简单的示例,展示了如何使用parfor实现任务并行计算:```matlabparfor i = 1:100output(i) = myFunction(input(i));end```上述代码中,myFunction是一个需要计算的函数,input是输入数据,output是输出结果。

通过parfor循环,每个循环迭代都会在不同的计算核心上运行,从而提高计算速度。

需要注意的是,parfor循环中的每个迭代必须是独立的,不依赖于其他迭代的结果。

否则,parfor将无法正确处理结果的依赖关系。

4. 数据并行计算数据并行计算是指将一个大任务中的数据划分成多个部分,每个部分在不同的计算核心上进行计算。

MATLAB提供了spmd语句(single program multiple data),使得数据并行计算更容易实现。

下面是一个简单的示例,展示了如何使用spmd语句实现数据并行计算:```matlabspmddata = labindex:2:100;output = myFunction(data);end```上述代码中,labindex是一个特殊的变量,表示当前计算核心的索引。

Matlab中的并行计算方法介绍

Matlab中的并行计算方法介绍

Matlab中的并行计算方法介绍引言Matlab作为一种功能强大的科学计算工具,在各个领域的应用都不可忽视。

但是,随着数据规模的增加和计算复杂度的提升,单机计算已经无法满足研究者和工程师的需求。

这就需要使用并行计算的方法来实现更高效的计算。

本文将介绍一些常用的Matlab中的并行计算方法,包括如何使用Parallel Computing Toolbox中的函数、Parallel Computing Toolbox中的工具以及Parallel Computing Toolbox结合其他工具一起使用的方法。

一、Parallel Computing Toolbox函数的使用Parallel Computing Toolbox是Matlab中用于进行并行计算的工具箱,它提供了一系列方便易用的函数来实现并行计算。

其中主要的函数包括parfor、parpool和spmd。

1. parfor函数parfor函数是Matlab中用于实现循环并行计算的函数。

它可以将一个循环分解成多个子任务,并在多个处理器上同时执行这些子任务,从而大大提高计算效率。

使用parfor函数的方法如下所示:```matlabparfor i = 1:N% 子任务的计算过程end```在这个例子中,N表示循环的迭代次数。

使用parfor函数的时候,需要注意以下几点:- 子任务之间的计算不能相互依赖,也就是说每个子任务之间不存在数据的读取和写入操作。

- 子任务的计算过程尽量保持相对独立,避免不必要的数据交互。

2. parpool函数parpool函数用于创建一个并行计算的池子,其中包含多个工作进程。

使用这些工作进程可以实现对大规模计算任务的分布式处理。

使用parpool函数的方法如下所示:```matlabparpool('local', N)```在这个例子中,N表示要创建的工作进程的数量。

使用parpool函数的时候,需要注意以下几点:- 工作进程的数量应根据实际情况进行调整,以保证计算效率和资源的合理利用。

Matlab中的并行计算技术简介

Matlab中的并行计算技术简介

Matlab中的并行计算技术简介随着计算机科学的迅速发展,对高性能计算的需求日益增长。

为了提高计算过程的效率,许多编程语言都引入了并行计算的概念。

Matlab作为一种强大的科学计算软件,同样也提供了并行计算的技术来加速计算过程。

一、并行计算的基本概念并行计算是指将一个任务分解成多个子任务,并在多个处理单元上同时执行,以提高计算速度和效率。

在传统的串行计算中,任务按照顺序执行,每个任务必须等待上一个任务完成后才能开始。

而并行计算充分利用了计算机系统中的多核处理器、多台计算机等资源,将任务并发执行,加快了计算过程。

二、Matlab中的并行计算1. 并行计算工具箱(Parallel Computing Toolbox)Matlab提供了并行计算工具箱,该工具箱包括了一些并行计算的函数和工具,方便用户在Matlab环境下进行并行计算。

通过该工具箱,用户可以轻松地将串行代码转化为并行代码,利用多核处理器提高计算速度。

2. 并行循环(parfor)Matlab中的并行循环(parfor)是一种常用的并行计算技术,它可以将循环中的迭代任务分配给不同的处理核心并行执行。

使用parfor关键字来替代传统的for循环关键字,用户可以轻松地在循环中实现并行计算。

在parfor循环中,每个迭代任务之间是独立的,不同迭代任务可以并行执行,从而提高整体的计算速度。

3. Matlab分布式计算服务器(MATLAB Distributed Computing Server)Matlab分布式计算服务器是一种能够跨多台计算机实现并行计算的技术。

通过搭建分布式计算服务器集群,可以将任务分发到不同的计算节点上进行并行计算。

这种方式在大规模计算和处理复杂计算问题时非常有效,可以大大提高计算速度和效率。

三、并行计算的优势与应用1. 提高计算速度并行计算可以充分利用计算机系统中的多个处理核心,并行执行任务,从而大幅度提高计算速度。

特别是对于科学计算和复杂数据处理,通过并行计算可以显著缩短计算时间,提高工作效率。

MATLAB并行计算工具箱的使用方法

MATLAB并行计算工具箱的使用方法

MATLAB并行计算工具箱的使用方法1. 引言在当今大数据时代,处理海量数据和复杂计算成为了一项重要的任务。

MATLAB并行计算工具箱为用户提供了解决这一挑战的强大工具。

本文将介绍MATLAB并行计算工具箱的使用方法,包括并行计算的基本概念、并行计算模型、代码优化和调试技巧等。

2. 并行计算概述并行计算是一种通过同时执行多个任务来加快计算速度的方法。

在传统计算中,任务按照顺序逐个执行,而在并行计算中,多个任务可以同时进行。

这样可以大大减少计算时间,提高计算效率。

3. 并行计算模型MATLAB并行计算工具箱提供了两种常见的并行计算模型:数据并行和任务并行。

3.1 数据并行数据并行指的是将数据分成多个部分,分别发送给多个计算核心进行并行处理。

这种模型适用于数据量很大,但每个子问题相对较简单的情况。

数据并行可以通过使用parallel Computing Toolbox中的parfor语句实现。

parfor语句类似于for循环,但是可以自动分配任务给不同的计算核心。

3.2 任务并行任务并行指的是将多个任务分配给不同的计算核心并行执行。

这种模型适用于多个独立任务的情况。

任务并行可以通过使用parallel Computing Toolbox中的spmd语句实现。

spmd语句可以将程序分成多个独立的部分,并在不同的计算核心上同时运行。

4. 并行计算的基本操作在使用MATLAB并行计算工具箱时,可以使用以下几个基本操作实现并行计算。

4.1 启动并行计算首先,需要通过在MATLAB命令行窗口输入"parpool"命令来启动并行计算。

这将根据计算机的处理器核心数量自动创建一个并行计算池,供并行计算使用。

使用"parpool"命令可以最大程度地利用计算机的多核处理能力。

4.2 并行计算的语法在使用并行计算时,需要使用一些特殊的语法来表示并行操作。

如前所述,parfor语句用于实现数据并行,spmd语句用于实现任务并行。

如何在Matlab中实现并行计算任务

如何在Matlab中实现并行计算任务

如何在Matlab中实现并行计算任务导言:随着计算机技术的不断进步,日常生活中越来越多的任务需要进行大规模的数据处理和计算。

传统的串行计算方式已经无法满足高效的需求,而并行计算技术的应用则成为了当今计算领域的热点之一。

本文将重点讨论如何在Matlab中实现并行计算任务,以帮助读者更加深入地了解相关的概念和操作。

一、并行计算的基本概念并行计算可以简单地理解为同一时间内多个任务同时进行,以提高计算效率和节约时间。

在Matlab中,我们可以通过并行计算工具箱来实现这一目标。

并行计算工具箱提供了一系列的函数和工具,用于将单个任务分解为多个子任务,并在不同的处理单元上并行执行,最后将结果合并。

使用并行计算工具箱可以极大地提高计算速度和减少资源占用。

二、并行计算的基本流程在使用Matlab进行并行计算任务之前,需要先了解一些基本的流程和准备工作。

首先,需要确定是否需要并行计算来加速任务执行。

一般来说,较为复杂的计算任务或者需要处理大量数据的任务适合采用并行计算。

其次,要确定计算任务的分解方式,即将任务划分为多个更小的子任务。

可以根据任务的性质和需求,将数据划分为相等的子集或者根据相关性进行划分。

最后,需要合理地分配计算资源,包括处理器核心数、内存大小等。

根据实际情况合理分配资源可以最大程度地提高计算效率。

三、并行计算的实现方法在Matlab中,实现并行计算主要有两种方式:基于进程的并行计算和基于线程的并行计算。

基于进程的并行计算通过启动多个独立的Matlab进程来执行任务,每个进程拥有独立的内存空间和资源。

这种方式适用于解决大规模的问题,但需要额外的内存和时间开销。

基于线程的并行计算则通过创建多个线程来执行任务,所有线程共享同一内存空间和资源。

这种方式适用于规模较小的问题,但可节省内存和时间开销。

四、并行计算在Matlab中的具体操作在Matlab中实现并行计算任务需要使用到Parallel Computing Toolbox工具箱。

matlab并行计算代码

matlab并行计算代码

matlab并行计算代码在数字化时代的今天,计算机科学技术的快速发展为我们提供了更高效、更快速的计算方法。

而在众多的编程语言中,MATLAB作为一种强大的科学计算软件,被广泛应用于工程、科学研究以及数据分析等领域。

然而,随着问题的复杂性不断增加,单机计算已经不能满足我们的需求。

因此,并行计算技术的应用成为了提升计算效率的重要途径。

本文将介绍MATLAB并行计算的相关内容,并提供一些并行计算代码示例。

一、MATLAB并行计算的概述在传统的单机计算中,MATLAB通过串行方式执行代码,一次仅处理一个任务。

然而,在一些大规模计算、大数据处理以及复杂算法实现等情况下,串行计算往往会导致计算效率低下。

并行计算技术通过同时利用多个处理器或计算节点,将任务划分成多个子任务,并通过并行执行提高计算速度。

在MATLAB中,我们可以利用Parallel Computing Toolbox工具箱来实现并行计算。

二、并行计算的基本原理和方式1. 并行计算基本原理并行计算基于任务划分和数据分发的思想,将一个大任务划分为多个小任务,每个小任务在不同处理器上或不同计算节点上并行执行,最后将各个小任务的计算结果合并得到最终结果。

2. 并行计算的方式(1)任务并行:将一个大任务划分为多个独立的小任务,每个小任务分配给不同的处理器或计算节点并行执行,最后将各个小任务的结果合并。

(2)数据并行:将数据划分为多个子数据集,每个处理器或计算节点分别处理一个子数据集,最后将各个处理器或计算节点的结果合并。

三、MATLAB并行计算代码示例下面给出两个简单的MATLAB并行计算代码示例,以帮助读者更好地理解并行计算的应用。

1. 任务并行示例```matlabmatlabpool open 4 % 打开一个拥有4个工作进程的并行计算池spmd% 在每个工作进程上执行的任务a = [1 2 3 4];b = labindex; % 获取当前工作进程编号c = a + b;disp(c);endmatlabpool close % 关闭并行计算池```上述代码中,我们使用`matlabpool`命令打开一个具有4个工作进程的并行计算池。

MATLAB并行运算和分布式运算的基本操作-图解

MATLAB并行运算和分布式运算的基本操作-图解

MATLAB并行运算和分布式运算的基本操作操作举例用的是MATLAB 7.11(R2010b)。

并行运算和分布式运算的三个典型适用情形:parallel for‐loop、batch job和large data sets。

1、 并行运算(Parallel Computing)1.1 编写包含并行命令的代码以parallel for‐loop为例,如果程序中for循环(如for i=1:100)的每次迭代式相互独立的,即i每次取值进行运算的结果,不会影响的下一次取值的运算,就可以把“for”改成“parfor”。

1.2 打开matlabpool,确定worker数量以启动本地2个worker为例,在MATLAB主界面的“Command Window”里输入命令:matlabpool open local 2启动的worker数量根据用户的需求以及计算机的CPU(core)的数量来确定,根据本人的试验,启动的worker数不能超过计算机的CPU(core)数量。

1.3 运行程序通过计时器可看出并行运算所需的时间比串行运算明显少很多。

1.4 关闭matlabpool在MATLAB主界面的Command Window里键入命令:matlabpool close每次运行完之后要关闭matlabpool,以防下次需要开启不同数量worker时产生冲突。

2、 分布式运算(Distributed Computing)2.1 编写包含并行命令的代码同1.1。

2.2 开启mdce服务参与分布式运算集群里的所有计算机都必须开启mdce服务。

首先在MATLAB主界面的“Current Folder”里进入路径:MATLAB根目录\toolbox\distcomp\bin然后在“Command Window”里输入命令:!mdce install装载完成后再输入命令:!mdce start注意前面的“!”。

2.3 创建、配置job manager首先在计算机本地路径MATLAB根目录\toolbox\distcomp\bin里打开“admincenter”文件。

Matlab中的并行计算与多线程编程

Matlab中的并行计算与多线程编程

Matlab中的并行计算与多线程编程一、介绍在计算科学和工程领域,计算速度是一个重要的考量因素。

随着硬件技术的进步,计算机处理器变得越来越强大,能同时执行多个线程的多核处理器也越来越常见。

为了充分利用这些硬件资源,许多编程语言提供了并行计算和多线程编程的支持。

而在Matlab中,我们也可以使用并行计算和多线程编程来加速计算过程。

二、并行计算概述并行计算指的是将一个问题同时分解为多个子问题,并在不同的处理器上同时执行这些子问题。

通过将计算任务分配给多个处理器,可以大大提高计算速度。

在Matlab中,我们可以使用Parallel Computing Toolbox来实现并行计算。

三、并行计算的基本概念1. 并行循环并行循环是实现并行计算的一个基本概念。

在Matlab中,我们可以使用parfor 命令来创建一个并行循环。

parfor命令会自动将循环的迭代过程分配给不同的处理器来执行,从而加速计算过程。

例如,我们可以使用以下代码来计算一个向量的平方和:```matlabvec = [1, 2, 3, 4, 5];sum = 0;parfor i = 1:length(vec)sum = sum + vec(i)^2;enddisp(sum);```2. 并行函数除了并行循环,Matlab还提供了一些并行函数,可以在并行计算中使用。

这些函数可以同时对多个元素进行操作,提高计算效率。

例如,我们可以使用以下代码计算两个向量的点积:```matlabvec1 = [1, 2, 3, 4, 5];vec2 = [5, 4, 3, 2, 1];product = zeros(size(vec1));parfor i = 1:length(vec1)product(i) = vec1(i) * vec2(i);enddisp(sum(product));```四、多线程编程概述多线程编程是指同时执行多个线程的编程方式。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Getting Started with Parallel Computing usingMATLAB: Interactive and Scheduled ApplicationsCreated by S. Zaranek, E. Johnson and A. Chakravarti1.ObjectivesThis user guide provides an end user with instructions on how to get started running parallel MATLAB applications using a desktop computer or a cluster.2.AssumptionsUser has access to MATLAB and Parallel Computing Toolbox on the desktopcomputer or head node of the cluster.If running on a cluster:MATLAB Distributed Computing Server has been installed by an administrator on the cluster.The desktop MATLAB client has been configured to connect to the cluster. If this hasnot been done, you should contact the cluster administrator.3. Getting the Example FilesUnzip the demoFiles.zip file that was provided along with this guide. You can add the files to the MATLAB path by running the addpath command in MATLAB.>> addpath <location of files>4. Examples Running LocallyIn this section, you will be running and submitting jobs using the localconfiguration.If your workflow will ultimately involve submitting jobs to a cluster, you canfollow this section by switching the default configuration from local to that ofyour cluster and running these jobs again. This is described in Section 5.You can set the configuration to local, either at the command-line>> defaultParallelConfig('local')or by using the user interface found in the parallel menu. See screen snapshot below.For more information on configurations and programming with user configurations, see:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/f5-16141.html#f5-165401. Using an Interactive MATLAB poolTo interactively run your parallel code, you first need to open a MATLAB pool. This reserves a collection of MATLAB worker sessions to run your code. The MATLAB pool can consist of MATLAB sessions running on your local machine or on a remote cluster. In this case, we are initially running on your local machine. You can use matlabpool open to start an interactive worker pool. If the number of workers is not defined, the default number defined in your configuration will be used. A good rule of thumb is to not open more workers then cores available. If the Configuration argument is not provided, matlabpool will use the default configuration as setup in the beginning of this section. When you are finished running with your MATLAB pool, you can close it using matlabpool close. Two of the main parallel constructs that can be run on a MATLAB pool are parfor loops (parallel for-loops) and spmd blocks (single program - multiple data blocks). Both constructs allow for a straight-forward mixture of serial and parallel code.parfor loops are used for task-parallel (i.e. embarrassingly parallel) applications. parfor is used to speed up your code. Below is a simple for loop converted into a parfor to run in parallel, with different iterations of the loop running ondifferent workers. The code outside the parfor loop executes as traditional MATLAB code (serially, in your client MATLAB session).Note: The example below is located in the m-file, ‘parforExample1.m’.matlabpool open2% can adjust according to your resourcesN = 100;M = 200;a = zeros(N,1);tic; % serial (regular) for-loopfor i = 1:Na(i) = a(i) + max(eig(rand(M)));endtoc;tic; % parallel for-loopparfor i = 1:Na(i) = a(i) + max(eig(rand(M)));endtoc;matlabpool closespmd blocks are a single program multiple data (SPMD) language construct. The "single program" aspect of spmd means that the identical code runs on multiple labs. The code within the spmd body executes simultaneously on the MATLAB workers. The "multiple data" aspect means that even though the spmd statement runs identical code on all workers, each worker can have different, unique data for that code.spmd blocks are useful when dealing with large data that cannot fit on a single machine. Unlike parfor, spmd blocks support inter-worker communication. They allow:Arrays (and operations on them) to be distributed across multiple workersMessages to be explicitly passed amongst workers.The example below creates a distributed array (different parts of the array arelocated on different workers) and computes the svd of this distributed array. The spmd block returns the data in the form of a composite object (behaves similarly to cells in serial MATLAB. For specifics, see the documentation link below).Note: The example below is located in the m-file, ‘spmdExample1.m’.matlabpool open2% can adjust according to your resourcesM = 200;spmdN = rand(M,M,codistributor); % 200x100 chunk per workerA = svd(N);endA = max(A{1}); % Indexing into the composite objectdisp(A)clear Nmatlabpool closeFor information on matlabpool, see:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/matlabpool.htmlFor information about getting started using parfor loops, see:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/brb2x2l-1.htmlFor information about getting started using spmd blocks, see:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/brukbno-2.htmlFor information regarding composite objects:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/brukctb-1.htmlFor information regarding distributed arrays:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/bqi9fln-1.html2a. Using Batch to Submit Serial Code (Best for Scripts)batch sends your serial scriptto run on one worker in yourcluster. All of the variables inyour client workspace (e.g. theMATLAB process you aresubmitting from) are sent to the worker by default. You can alternatively pass a subset of these variables by defining the Workspace argument and passing the desired variables in a structure.After your job has finished, you can use the load command to retrieve the results from the worker-workspace back into your client-workspace. In this and all examples following, we use a wait to ensure the job is done before we load back in worker-workspace. This is optional, but you can not load the data from a task or job until that task or job is finished. So, we use wait to block the MATLAB command line until that occurs.If the Configuration argument is not provided, batch will use the default configuration that was set up above.Note: For this example to work, you will need ‘testBatch.m’ on the machine that you are submitting from (i.e. the client machine). This example below is located in the m-file, ‘submitJob2a.m’.%% This script submits a serial script using batchjob2a = batch('testBatch');wait(job2a); % only can load when job is finishedsprintf('Finished Running Job')load(job2a); % loads all variables backsprintf('Loaded Variables into Workspace')% load(job2a, 'A'); % only loads variable Adestroy(job2a) % permanently removes job datasprintf('Test Completed')If you have submitted successfully, you should see the following variables appear in your client workspace:For more information on batch, see:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/batch.htmland here:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/brjw1e5-1.html#brjw1fx-32b. Using Batch to Submit Scripts that Run Using a MATLAB poolbatch with the 'matlabpool'option sends scripts containing parfor or spmd to run on workers via a MATLAB pool. In this process, one worker behaves like a MATLAB client process that facilitates the distribution of the job amongst the workers in the pool and runs the serial portion of the script. Therefore, specifying a 'matlabpool' of size N actually will result in N+1 workers being used.Just like in step 2a, all variables are automatically sent from your client workspace (i.e. the workspace of the MATLAB you are submitting from) to the worker’s workspace on the cluster. load then brings the results from your worker’s workspace back into your client’s workspace. If a configuration is not specified, batch uses the default configuration as defined in the beginning of this section.Note: For this example to work, you will need ‘testParforBatch.m’ on the machine that you are submitting from (i.e. the client machine). This example below is located in the m-file, submitJob2b.m.%% This script submits a parfor script using batchjob2b = batch('testParforBatch','matlabpool',2);wait(job2b); % only can load when job is finishedsprintf('Finished Running Job')load(job2b); % loads all variables backsprintf('Loaded Variables into Workspace')% load(job2b, 'A'); % only loads variable Adestroy(job2b) % permanently removes job datasprintf('Test Completed')If you have submitted successfully, you should see the following variables appear in your client workspace:The above code submitted a script containing a parfor. You can submit a script containing a spmd block in the same fashion by changing the name of the submission script in the batch command.Note: For this example to work, you will need ‘testSpmdBatch.m’ on the machine that you are submitting from (i.e. the client machine). This example below is located in the m-file, submitJob2b_spmd.m.%% This script submits a spmd script using batchjob2b = batch('testSpmdBatch','matlabpool',2);wait(job2b); % only can load when job is finishedsprintf('Finished Running Job')load(job2b); % loads all variables backsprintf('Loaded Variables into Workspace')% load(job2b, 'A'); % only loads variable Adestroy(job2b) % permanently removes job datasprintf('Test Completed')If you have submitted successfully, you should see the following variables appear in your client workspace:3. Run Task-Parallel Example with Jobs and TasksIn this example, we are sending a task parallel job with multiple tasks. Each task evaluates the built-in MATLAB function. The createTask function in the below example is passed the job, the function to be run in the form of a function handle (@sum), the number of output arguments of the function (1), and the input argument to the sum function in the form of a cell array ({[1 1]});If not given a configuration, findResource uses the scheduler found in the default configuration defined in the beginning of this section.Note: This example is located in the m-file, ‘submitJob3a.m’.%% This script submits a job with 3 taskssched = findResource();job3a = createJob(sched);createTask(job3a, @sum, 1, {[1 1]});createTask(job3a, @sum, 1, {[2 2]});createTask(job3a, @sum, 1, {[3 3]});submit(job3a)waitForState(job3a, 'finished') %optionalsprintf('Finished Running Job')results = getAllOutputArguments(job3a);sprintf('Got Output Arguments')destroy(job3a) % permanently removes job datasprintf('Test Completed')If you have submitted successfully, you should see the following variables appear in your client workspace:results should contain the following:You can also call a user-created function in the same way as shown above. In that case, you will need to make sure that any scripts, files, or functions that the task function uses are accessible to the cluster. You can do this by sending those files to the cluster via the FileDependencies property or by directing the worker to a shared directory containing those files via the PathDependencies property. An example of using FileDependencies is shown below:Note: you will need to have a ‘testTask.m’ file on the machine you are submitting from for this example to work. This example is located in the m-file,‘submitJob3b.m’.% This script submits a job with 3 taskssched = findResource();job3b = createJob(sched,'FileDependencies',{'testTask.m'});createTask(job3b, @testTask, 1, {1,1});createTask(job3b, @testTask, 1, {2,2});createTask(job3b, @testTask, 1, {3,3});submit(job3b)waitForState(job3b, 'finished') % optionalsprintf('Finished Running Job')results = getAllOutputArguments(job3b);sprintf('Got Output Arguments')destroy(job3b) % permanently removes job datasprintf('Test Completed')If you have submitted successfully, you should see the following variables appear in your client workspace:results should contain the following:For more information on File and Path Dependencies, see the below documentation.File Dependencies:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/filedependencies.htmlPath Dependencies:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/pathdependencies.htmlMore general overview about sharing code between client and workers:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/bqur7ev-2.html#bqur7ev-94. Run Task-Parallel Example with a MATLAB pool job (best for parfor or spmd in functions)In this example, we are sending a MATLAB pool job with a single task. This is nearly equivalent to sending a batch job (see step 2b) with a parfor or a spmd block, except this method is best used when sending functions and not scripts. It behaves just like jobs/tasks explained in step 3. The function referenced in the task contains a parfor.Note: For this example to work, you will need ‘testParforJob.m’ on the machine that you are submitting from (i.e. the client machine). This example is located in the m-file, ‘submitJob4.m’.% This script submits a function that contains parforsched = findResource();job4 = createMatlabPoolJob(sched,'FileDependencies',...{'testParforJob.m'});createTask(job4, @testParforJob, 1, {});set(job4, 'MaximumNumberOfWorkers', 3);set(job4, 'MinimumNumberOfWorkers', 3);submit(job4)waitForState(job4, 'finished') % optionalsprintf('Finished Running Job')results = getAllOutputArguments(job4);sprintf('Got Output Arguments')sprintf('Test Completed')If you have submitted successfully, you should see the following variables appear in your client workspace:results{1} should contain a [50x1 double].For more information on creating and submitting MATLAB pool jobs, see /access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/creatematlabpooljob.html5. Run Data-Parallel ExampleIn this step, we are sending a data parallel job with a single task. The format is similar to that of jobs/tasks (see step 3). For parallel jobs, you only have one task. That task refers to a function that uses distributed arrays,labindex, or some mpi functionality. In this case, we are running a simple built in function (labindex) which takes no inputs and returns a single output.labindex returns the ID value for each of worker processes that ran the it . The value of labindex spans from 1 to n, where n is the number of labs running the current jobNote: This example is located in the m-file, ‘submitJob5.m’.%% Script submits a data parallel job, with one tasksched = findResource();job5 = createParallelJob(sched);createTask(job5, @labindex, 1, {});set(job5, 'MaximumNumberOfWorkers', 3);set(job5, 'MinimumNumberOfWorkers', 3);submit(job5)waitForState(job5, 'finished') % optionalsprintf('Finished Running Job')results = getAllOutputArguments(job5);sprintf('Got Output Arguments')sprintf('Test Completed')If you have submitted successfully, you should see the following variables appear in your client workspace:results should contain the following:For more information on creating and submitting data parallel jobs, see:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/createparalleljob.htmlFor more information on, labindex, see:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/labindex.html5 Submitting Jobs to Your ClusterRerun the above section (Section 4) with your configuration changed from local to the configuration name corresponding to your cluster.6. Summary Chart for Scheduling Options✓7. Next StepsRefer to the documentation for the Parallel Computing Toolbox to learn about more functionality for solving your MATLAB problems in parallel. A good place to start is here:/access/helpdesk/help/toolbox/distcomp/index.html?/a ccess/helpdesk/help/toolbox/distcomp/f3-6010.htmlYou can also consider taking a training course through The MathWorks to learnmore in focused, hands-on environment./services/training/index.html。

相关文档
最新文档