Boost logo

Ublas :

From: Sorkin Dima (dsorkin_at_[hidden])
Date: 2008-01-12 05:18:20

>> Can anybody tell me the reason, why Matlab get faster, the bigger the
>> program gets?

I will add my part on Matlab vs. uBLAS:

1) As it was previously said Matlab uses optimized libraries. Moreover,
  Matlab developers add homemade changes in these libs, to make them even
  faster (but I'm not sure about precision in corner cases). For example,
  I've payed attention that Matlab uses at-home-modified version of
  LAPACK's SVD for dense matrices (see the documentation for 'svd' on their
  site, when it will return back to normal operation). It will be very hard
  to beat Matlab for large matrices (Our local gurus recommended to learn
  Fortran for such tasks ... :)

2) Two reasons made me abandon Matlab programming and switch to C++/Boost:
   a) Code maintenance in Matlab was too hard for me. If you write object
      oriented code in Matlab, each method is in a separate file. I
      couldn't stand this scattering of code in many tiny files. Maybe
      I got it wrong.
   b) In Matlab arguments are passed to functions by value (for good
      reasons - this allows very effective debug and inspection of
      state in case something fails). This has little effect on
      performance, since _actually_ objects are not copied.
      But this also has one exception which heavily harms performance
      of many algorithms: if you want to modify small part of large
      object (object containing large matrix some entries of which
      you want to modify) - you will pay for copying of the whole
      matrix. See the below attached message for details.


I last checked it 3 years ago, things could be improved since,
you can rerun the code by yourself on your MAtlab and examine the results.
Below attached the reply from Matlab support and my original message:

Sun, 23 May 2004 13:22:31 +0300 (IDT)
From: Batia Perry
To: Sorkin Dmitri


Following the answer I have received from the support of Matlab

MATLAB does not support passing arguments by reference (i.e., pointers).
Functions can change only their private, temporary copy of an object.
Therefore, to change an existing object, you must create a new one, and
then replace the old one.

The MATLAB parser will pass parameters by reference because it is faster
to pass a pointer to a chunk of data as opposed to passing the whole chunk
of data. However, the MATLAB parser will pass parameters by value if it
knows that the function is going to modify the chunk of data in some way.

In the case of passing structures or cell arrays, only the field or cell
data being modified by the function will be passed by value. When the
function returns, the calling workspace's copy of the structure or cell
array is replaced by the function's copy efficiently, such that a minimal
amount of copying occurs.

Batia Perry D.Sc.
Taub Computer Center, Technion, Haifa, Israel

On Wed, 19 May 2004, Sorkin Dmitri wrote:
> Hi.
> I have 2 (advanced) questions on matlab.
> 1.Am I right that matlab implements it's objects
> using a "reference counting with copy on write" technique ?
> 2.Regarding "A = subsasgn(A,S,B)" method :
> When calling
> >> myObj.field = 10 ;
> ,is there any way to make matlab to not to
> duplicate a copy of the object "myObj" in memory ?
> I have a problem with encapsulating large data structures.
> Each attemt to modify "large" object makes Matlab to
> duplicate the whole obj in memory , which hurts the performance
> (As I think).
> See example below for more information.
> Thank you.
> Dmitri.
> Example:
> ----------------------------
> @myclass/myclass.m:
> function result = myclass(N,M);
> d.m = cell(N,M);
> result = class(d,'myclass');
> @myclass/subsasgn.m:
> function A = subsasgn(A,S,B);
> A.m{1,1} = 'hello world' ;
> @myclass/dummy_method.m:
> function result = dummy_method(A);
> result = A.m{1,1};
> -------
> Running
> >> N = 10000 ; M = 1000; ; L = 100;
> >> Q = myclass(N,M);
> >> for i = 1:1:L
> >> Q.use_of_subsasgn = 10 ; % dummy use, only for example
> >> end
> has the complexity of O(L*M*N), because of copying.
> Running
> >> for i=1:1:L
> >> qqq = dummy_method(Q);
> >> end
> has the complexity of O(L) , because of referencecounting.