"Rakesh Kumar" wrote in message <l17052$78$1@newscl01ah.mathworks.com>...
> "Toan Cao" <toancv3010@gmail.com> wrote in message <l11tt6$c0q$1@newscl01ah.mathworks.com>...
> > "Rakesh Kumar" wrote in message <l0vnuf$kp5$1@newscl01ah.mathworks.com>...
> > > "Toan Cao" <toancv3010@gmail.com> wrote in message <l0vn9q$8pp$1@newscl01ah.mathworks.com>...
> > > > "Steven_Lord" <slord@mathworks.com> wrote in message <l0v8n7$so5$1@newscl01ah.mathworks.com>...
> > > > >
> > > > >
> > > > > "Rakesh Kumar" <r.kumar_spamhole@mathworks.com> wrote in message
> > > > > news:l0v783$po$1@newscl01ah.mathworks.com...
> > > > > > "Toan Cao" <toancv3010@gmail.com> wrote in message
> > > > > > <l0u1au$ei4$1@newscl01ah.mathworks.com>...
> > > > > >> Hi,
> > > > > >> I write an objective function in both mfile (fileA1.m) and C mex file
> > > > > >> (fileA2.c). Given an input to this objective function, computation time
> > > > > >> in C mex file is 15 times faster than that of m file. However, when i
> > > > > >> use the objective function in function lsqnonlin of Matlab, computation
> > > > > >> time which lsqnonlin gets a optimal solution is almost the same for both
> > > > > >> files (~101 minutes for C mex file and ~104 minutes for mfile).
> > > > > >> I hope when i use C mex file, i will save more time for optimization
> > > > > >> process, but it is not.
> > > > > >> Can you help me explain why lsqnonlin spends almost the same computation
> > > > > >> time?
> > > > > >> I would like to understand more how lsqnonlin of Matlab works to reduce
> > > > > >> its time?
> > > > > >> Thanks in advance!
> > > > > >>
> > > > > >> Toan
> > > > > >
> > > > > > Hi Toan,
> > > > > > This is unexpected. First, I would suggest to compare the #iterations and
> > > > > > #function evaluations in two scenarios (just to make sure we have same
> > > > > > implementation in MATLAB and C). If these numbers are similar, then I
> > > > > > would profile and see where the time is spent in two scenarios. Can you
> > > > > > report back here with your findings? Thanks!
> > > > >
> > > > > In addition, make sure you're comparing apples and oranges. Do fileA1.m and
> > > > > fileA2.$MEXEXT return the EXACT SAME, down to the last bit, results given
> > > > > the exact same inputs? Even a small difference in the return values could
> > > > > affect the path in the solution space the optimization routines follow, if I
> > > > > remember correctly.
> > > > >
> > > > > 
> > > > > Steve Lord
> > > > > slord@mathworks.com
> > > > > To contact Technical Support use the Contact Us link on
> > > > > http://www.mathworks.com
> > > >
> > > > Hi Rakesh,
> > > >
> > > > When i set just for 5 iteration , following outputs of optimization process make me more confusion:
> > > > 
> > > > mfile:
> > > > FirstOrder Norm of
> > > > Iteration Funccount Residual optimality Lambda step
> > > > 0 1 9.02706 1.1 0.02
> > > > 1 4 8.15864 0.775 2 0.715913
> > > > 2 6 7.12331 0.592 20 0.19392
> > > > 3 7 6.6927 0.546 2 0.147436
> > > > 4 9 6.4763 0.513 20 0.124842
> > > > 5 10 6.37183 0.486 2 0.112678
> > > > 6 12 6.28601 0.465 20 0.106625
> > > > Elapsed time is 3240.325396 seconds.
> > > >
> > > > C mex file:
> > > >
> > > > FirstOrder Norm of
> > > > Iteration Funccount Residual optimality Lambda step
> > > > 0 1 9.02706 1.1 0.02
> > > > 1 4 8.15864 0.775 2 0.715913
> > > > 2 6 7.12331 0.592 20 0.19392
> > > > 3 7 6.6927 0.546 2 0.147436
> > > > 4 9 6.4763 0.513 20 0.124842
> > > > 5 10 6.37183 0.486 2 0.112678
> > > > 6 12 6.28601 0.465 20 0.106625
> > > > Elapsed time is 8275.102153 seconds.
> > > > 
> > > > Uhmmmm, as you see every parameters are similar but it is terrible when elapsed time for C mex file is even longer than that of mfile for above case.
> > > >
> > > > Hi Steve Lord,
> > > > There are some things relating to what you say. In Jacobian matrix returned by the objective function, when i use Matlab to compare element by element between these two matrices of 2 files, Matlab indicates some different values among elements.
> > > > However, if i run comparison two times of the same output Jacobian matrices , locations of difference at each running time are not the same. Then, i check some locations , some values of 2 elements are different at last digit as Matlab indicates. (for example: 4.559150742630094 vs 4.559150742630095), some values are the same (for example: 1.641766402933731 vs 1.641766402933731).
> > > > Therefore, i think this is due to machine error between values returned by c mex file and mfile. But i can not still explain where is the problem relating computation time of lsqnonlin.
> > > >
> > > > Looking forward to your reply.
> > > > Toan
> > >
> > > What about the profiler report? Can you post the profiler report for top 10 selftime for functions and how many times they are called?
> >
> > Hi Rakesh,
> > Here my profiler when i run two optimization processes, where testCM is mfile storing program of optimization, the object function in mfile is MydeformfuncSub7Ext, and in C mex file is MydeformfuncSub7ExtC.
> > Profiler of optimzation using mfile:
> > http://i906.photobucket.com/albums/ac264/toancv3010/Mtime_zps92ae907a.jpg
> > Profiler of optimzation using C mexfile:
> > http://i906.photobucket.com/albums/ac264/toancv3010/Ctime_zpsbbbc5fb3.jpg
> > As seen, subfunction LevenbergMarquardt consumes almost time of optimization process.
> > Is there any solution to reduce computation time for LevenbergMarquardt ?
> >
> > Toan
>
> Hi Toan,
> First, in your previous post you mentioned these overall time for two runs.
> 8275 seconds (Cmex file)
> 3240 seconds (MATLAB file)
>
> The profiler result you posted shows these total time for testCM:
> 3249 seconds (Cmex file)
> 3234 seconds (MATLAB file).
>
> They don't match. Are we looking at the same data?
>
> In addition, the Cmex fie by itself takes very little time (1.188 seconds, 16 calls) compared to MATLAB file (18.90 in 16 calls). This is the expected speed up. However, if you look at the profiler results closely, the bottleneck appears to be the LevenbergMarquardt function and not your objective function.
>
> In this problem, most of the time is spent in LevenbergMarquardt function (MATLAB or MEX) and the speed up you get from MEX is lost by 10% slowdown of LevenbergMarquardt (painful!). In your profiler results, I see that LevenbergMarquardt takes more time in Cmex case (~ 10% more), which I can investigate further if you can please provide an example to me.
>
> BTW, What was your findings with 'trustregionreflective' algorithm (which is the default algorithm choice)? Was that slower or did not work?
>
> Thanks for posting the profiler results.
> Rakesh
Hi Rakesh,
The computation time of my two posts are different because i ran at two different time but the same number of iteration.
However, for the case where 8275 seconds for Cmex file and 3240 seconds for MATLAB file, i can not explain why computation time with Cmex file is bigger than although number of iteration is the same for both C mex file and mfile.
Relating to computing burden of LevenbergMartquardt, i think Matlab is slow because of my big sparse Jacobian matrix (10000x12000) to find movement direction at each iteration.
Toan
