Run Batch Parallel Jobs

Run a Batch Job

To offload work from your MATLAB®会话在另一个会话中运行在后台,可以使用batch命令在脚本。

  1. To create the script, type:

    editmywave
  2. In the MATLAB Editor, create afor-loop:

    fori = 1:1024 A(i) = sin(i*2*pi/1024);end
  3. Save the file and close the Editor.

  4. Use thebatchcommand in the MATLAB Command Window to run your script on a separate MATLAB worker:

    作业=批处理('mywave')

  5. batchdoes not block MATLAB and you can continue working while computations take place. If you need to block MATLAB until the job finishes, use thewaitfunction on the job object.

    wait(job)
  6. After the job finishes, you can retrieve and view its results. Theloadcommand transfers variables created on the worker to the client workspace, where you can view the results:

    load(job,'A') plot(A)
  7. 作业完成后,永久删除其数据并从工作区中删除其引用:

    delete(job) clear工作

batchruns your code on a local worker or a cluster worker, but does not require a parallel pool.

You can usebatchto run either scripts or functions. For more details, see thebatch参考页面。

使用并行池运行批处理作业

您可以将功能组合卸载作业并在并行池中运行循环。此示例结合了两个创建简单批处理parfor-loop.

  1. 要创建脚本,请键入:

    editmywave
  2. In the MATLAB Editor, create aparfor-loop:

    parfori = 1:1024 A(i) = sin(i*2*pi/1024);end
  3. Save the file and close the Editor.

  4. Run the script in MATLAB with thebatchcommand. Indicate that the script should use a parallel pool for the loop:

    作业=批处理('mywave','Pool',3)

    This command specifies that three workers (in addition to the one running the batch script) are to evaluate the loop iterations. Therefore, this example uses a total of four local workers, including the one worker running the batch script. Altogether, there are five MATLAB sessions involved, as shown in the following diagram.

  5. To view the results:

    等待(工作)加载(工作,'A') plot(A)

    The results look the same as before, however, there are two important differences in execution:

    • The work of defining theparfor-loop and accumulating its results are offloaded to another MATLAB session bybatch.

    • The loop iterations are distributed from one MATLAB worker to another set of workers running simultaneously ('Pool'parfor), so the loop might run faster than having only one worker execute it.

  6. 作业完成后,永久删除其数据并从工作区中删除其引用:

    delete(job) clear工作

从当前文件夹浏览器运行脚本作为批处理作业

From the Current Folder browser, you can run a MATLAB script as a batch job by browsing to the file’s folder, right-clicking the file, and selectingRun Script as Batch Job. The batch job runs on the cluster identified by the default cluster profile. The following figure shows the menu option to run the script filescript1.m:

Running a script as a batch from the browser uses only one worker from the cluster. So even if the script contains aparfor循环或spmdblock, it does not open an additional pool of workers on the cluster. These code blocks execute on the single worker used for the batch job. If your batch script requires opening an additional pool of workers, you can run it from the command line, as described in使用并行池运行批处理作业.

When you run a batch job from the browser, this also opens the Job Monitor. The Job Monitor is a tool that lets you track your job in the scheduler queue. For more information about the Job Monitor and its capabilities, seeJob Monitor.

See Also

相关话题