Reputation: 39
I have a question about sum
in matlab.
For a vector (1xN matrices), sum
seems to be parallelised. For example,
a=rand(1,100000000);
maxNumCompThreads(2);
tic;for ii=1:20;b=sum(a,2);end;toc
maxNumCompThreads(1);
tic;for ii=1:20;b=sum(a,2);end;toc
> Elapsed time is 1.219342 seconds.
> Elapsed time is 2.393047 seconds.
But if instead I consider a 2xN matrix,
a=rand(2,100000000);
maxNumCompThreads(2);
tic;for ii=1:20;b=sum(a,2);end;toc
maxNumCompThreads(1);
tic;for ii=1:20;b=sum(a,2);end;toc
> Elapsed time is 7.614303 seconds.
> Elapsed time is 7.432590 seconds.
In this case, sum
doesn't seem to benefit from the extra core.
Anyone came across this before? I'm wondering if this could be due to indexing overhead and whether it is possible to make sum
faster in the case of 2xN matrices.
Thanks a lot.
Upvotes: 3
Views: 688
Reputation: 668
This is something MATLAB is not very clear about. Anytime you create an array, MATLAB generates a row vector but behind the scene it actually prefers column vectors. So, summing an array in rows (1st dimension) would faster than in rows (2nd dimension). For your case, if you converted a
into a row-major representation and performed the sum
in the 1st dimension, the benefit would be seen. On my machine, I get the following
a = rand(100000000, 2);
maxNumCompThreads(2);
tic; for ii=1:20; b=sum(a,1); end; toc
maxNumCompThreads(1);
tic; for ii=1:20; b=sum(a,1); end; toc
> Elapsed time is 2.485628 seconds.
> Elapsed time is 4.381082 seconds.
Upvotes: 2