As pointed out by @Jonas, there are a few options available in MATLAB, and which works best depends on a few factors such as:
- How large is your problem
- How many machines you have available
- Do you have a GPU
- Does MATLAB already multithread the operations
Many elementwise operations are multithreaded in MATLAB now - in which case, there's generally little point using PARFOR (unless you have multiple machines and MATLAB Distributed Computing Server licences available).
Truly huge problems that need the memory of multiple machines can benefit from distributed arrays.
Using the GPU can beat the multithreaded performance of a single machine if your problem is of a suitable size and type for GPU computation. Vectorized code tends to be the most natural fit for parallelization via the GPU. For example, you could write your code using gpuArray
s from Parallel Computing Toolbox like so and have everything run on the GPU.
x = parallel.gpu.GPUArray.colon(-2,0.01,2);
y = x;
[xx,yy] = meshgrid(x,y); % xx and yy are on the GPU
z = arrayfun( @(u, v) sin(u.*u-v.*v), xx, yy );
I converted the final line there into an arrayfun
call as that is more efficient when using gpuArray
s.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…