Vectorized code in Matlab runs much faster than a for loop (see Parallel computing in Octave on a single machine -- package and example for concrete results in Octave)
With that said, is there a way to vectorize the code shown next in Matlab or Octave?
x = -2:0.01:2;
y = -2:0.01:2;
[xx,yy] = meshgrid(x,y);
z = sin(xx.^2-yy.^2);
In Matlab, the only way to get built-in vectorized functions to multithread is to wait for MathWorks to implement them as such.
Alternatively, you can write the vectorized computation as a loop, and run them in parallel using
parfor
.Finally, a number of functions are GPU-enabled, so with access to the parallel processing toolbox you can parallelize these operations, including the subtraction and the element-wise power.
Vectorization for
meshgrid
andndgrid
If you are still interested in finding a vectorized implementation to make the
meshgrid
based code in the problem faster, let me suggest a vectorized method withbsxfun
and it's GPU ported version. I strongly believe that people must look intovectorization with GPUs
as a promising option to speed upMATLAB
codes. Codes that employmeshgrid
orndgrid
and whose outputs are to be operated with some elementwise operation setup a perfect ground to employbsxfun
into those codes. To add to that, the use of GPU withbsxfun
, that lets it work on the elements independently with hundreds and thousands of CUDA cores available, makes it just perfect for GPU implementation.For your specific problem, the inputs were -
Next, you had -
With
bsxfun
, this becomes a one-liner -Benchmarking
GPU benchmarking tips were taken from Measure and Improve GPU Performance.
Associated functions
Results
Conclusions
As the results show, vectorized method with GPU shows good performance improvement which is about
4.3x
against the vectorized CPU code and6x
against the original code. Please keep in mind that GPU has to overcome a minimum overhead that is required with it's setting up, so at least a decent sized input is needed to see the improvement. Hopefully, people would explore more ofvectorization with GPUs
, as it could not be stressed enough!As pointed out by @Jonas, there are a few options available in MATLAB, and which works best depends on a few factors such as:
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.I converted the final line there into an
arrayfun
call as that is more efficient when usinggpuArray
s.