Vectorization for meshgrid in Matlab (or Octave)

Vectorized code in Matlab is 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 below 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);
+5
source share
3 answers

As pointed out by @Jonas, in MATLAB there are several options that best depend on several factors, such as:

  • How big is your problem.
  • How many cars do you have
  • You have a graphics processor
  • MATLAB is already multithreaded in operations

MATLAB , PARFOR ( MATLAB Distributed Computing Server).

, , .

GPU , . . , gpuArray Parallel Computing Toolbox, , 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 );

arrayfun, gpuArray s.

+6

Matlab - , MathWorks .

parfor.

, GPU-enabled, , .

+5

meshgrid ndgrid

, meshgrid, bsxfun . , vectorization with GPUs MATLAB. , meshgrid ndgrid, - , bsxfun . , bsxfun, CUDA, GPU.

-

x = -2:0.01:2;
y = -2:0.01:2;

-

[xx,yy] = meshgrid(x,y);
z = sin(xx.^2-yy.^2);

bsxfun -

z = sin(bsxfun(@minus,x.^2,y.^2.'));

GPU .

%// Warm up GPU call with insignificant small scalar inputs
temp1 = sin_sqdiff_vect2(0,0);

N_arr = [50 100 200 500 1000 2000 3000]; %// array elements for N (datasize)
timeall = zeros(3,numel(N_arr));

for k = 1:numel(N_arr)
    N = N_arr(k);
    x = linspace(-20,20,N);
    y = linspace(-20,20,N);

    f = @() sin_sqdiff_org(x,y);%// Original CPU code
    timeall(1,k) = timeit(f);
    clear f

    f = @() sin_sqdiff_vect1(x,y);%// Vectorized CPU code
    timeall(2,k) = timeit(f);
    clear f

    f = @() sin_sqdiff_vect2(x,y);%// Vectorized GPU(GTX 750Ti) code
    timeall(3,k) = gputimeit(f);
    clear f
end

%// Display benchmark results
figure,hold on, grid on
plot(N_arr,timeall(1,:),'-b.')
plot(N_arr,timeall(2,:),'-ro')
plot(N_arr,timeall(3,:),'-kx')
legend('Original CPU','Vectorized CPU','Vectorized GPU (GTX 750 Ti)')
xlabel('Datasize (N) ->'),ylabel('Time(sec) ->')

%// Original code
function z = sin_sqdiff_org(x,y)
[xx,yy] = meshgrid(x,y);
z = sin(xx.^2-yy.^2);
return;

%// Vectorized CPU code
function z = sin_sqdiff_vect1(x,y)
z = sin(bsxfun(@minus,x.^2,y.^2.')); %//'
return;

%// Vectorized GPU code
function z = sin_sqdiff_vect2(x,y)
gx = gpuArray(x);
gy = gpuArray(y);
gz = sin(bsxfun(@minus,gx.^2,gy.^2.')); %//'
z =  gather(gz);
return;

enter image description here

, , 4.3x 6x . , GPU , , . , vectorization with GPUs, !

+5

All Articles