SPMD与Parfor [英] SPMD vs. Parfor

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

问题描述

我对Matlab中的并行计算是陌生的.我有一个创建分类器(SVM)的函数,我想用几个数据集对其进行测试.我有一个2核工作站,所以我想并行运行测试.有人可以解释一下以下两者之间的区别吗:

I'm new about parallel computing in matlab. I have a function which creates a classifiers (SVM) and I'd like to test it with several dataset. I've got a 2 core workstation so I'd like to run test in parallel. Can someone explain me the difference between:

 dataset_array={dataset1, dataset2}
 matlabpool open 2
 spmd
      my_function(dataset(labindex));
 end

 dataset_array={dataset1, dataset2}
 matlabpool open 2
 parfor i:1=2
      my_function(dataset(i));
 end

推荐答案

spmd是一个并行区域,而parfor是一个并行的for循环.区别在于,在spmd区域中,您可以并行执行的任务具有更大的灵活性.您可以编写一个for循环,可以对分布式数组和向量进行操作.您可以对整个工作流程进行编程,该工作流程通常包含多个循环.这是有代价的:您需要了解有关在线程之间分配工作和数据的更多信息.例如,并行化循环需要在工作线程之间明确划分循环索引范围(通过使用 labindex 在代码中完成此操作),并可能创建分布式数组.

spmd is a parallel region, while parfor is a parallel for loop. The difference is that in spmd region you have a much larger flexibility when it comes to the tasks you can perform in parallel. You can write a for loop, you can operate on distributed arrays and vectors. You can program an entire work flow, which in general consists of more than loops. This comes at a price: you need to know more about distributing the work and the data among your threads. Parallelizing the loop for example requires explicitly dividing the loop index ranges amongst the workers (which you did in your code by using labindex), and maybe creating distributed arrays.

parfor仅这样做-并行化的for循环.您可以添加自动并行化的工作,因此工作可以通过MATLAB在工作人员之间进行划分.

parfor on the other hand only does this - a parallelized for loop. Automatically parallelized, you can add, so the work is divided between the workers by MATLAB.

如果只想并行运行一个循环,然后再在本地客户端上处理结果,则应使用parfor.如果要并行化整个MATLAB程序,则必须处理spmd和工作分配的复杂性.

If you only want to run a single loop in parallel and later work on the result on your local client, you should use parfor. If you want to parallelize your entire MATLAB program, you will have to deal with the complexities of spmd and work distribution.

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

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