<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339</link>
    <title>MATLAB Central Newsreader - HOWTO: Accelerate processing algorithm</title>
    <description>Feed for thread: HOWTO: Accelerate processing algorithm</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 05 Jul 2009 11:18:49 -0400</pubDate>
      <title>HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662688</link>
      <author>Jose Antonio</author>
      <description>Hi to everyone&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I am new to this forum, but I thought it would be a good idea to try to get this problem solved by many minds rather than continue getting stucked once and again myself :)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
My problem is that I am trying to implement an algorithm to iteratively find the solution for an optimization problem. And the data that I have to handle is quite large, since I have to simultaneously work with 10 to 20 images of say 700x600 pixels.&lt;br&gt;
&lt;br&gt;
I have found the main blottle-neck in the program, which is the following:&lt;br&gt;
&lt;br&gt;
for (m = 1:256)&lt;br&gt;
&amp;nbsp;&amp;nbsp;[indR, indC] = find(y+1 == m); %Em rows pixels, cols ims&lt;br&gt;
&amp;nbsp;&amp;nbsp;I(m, iteration+1) = 1/length(indR) * ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sum(exposures(indC).' .* x(indR, chan));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Basically, y is the [(700x600) 16] matrix, iteration is the current step, and I need to index the variable exposures with the # of the image (1 to 16, returned in indC), and x with both the # of the pixel (indR) and the color channel (chan).&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I have tried to speed things up using sparse matrices:&lt;br&gt;
&lt;br&gt;
exposuresMat = repmat(exposures, size(y)./size(exposures));&lt;br&gt;
&lt;br&gt;
xMat = repmat(x, size(y)./size(x));&lt;br&gt;
&lt;br&gt;
[ySort, indices] = sort(y(:), 'ascend');&lt;br&gt;
exposuresMat = exposuresMat(indices);&lt;br&gt;
xMat = xMat(indices);&lt;br&gt;
S = sparse(1:size(ySort), double((ySort+1).'), xMat.*exposuresMat);&lt;br&gt;
S = sum(S);&lt;br&gt;
I(1:length(S), iteration+1) = S;&lt;br&gt;
&lt;br&gt;
This last code works (for what I have checked!) but, since I do not have enough memory, I think it needs to access the HD to allocate virtual memory, and everyting slows down again.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I am thinking of trying to implement the for loop in C, and then use a MEX implementation. But I really have no idea how to do this. &lt;br&gt;
&lt;br&gt;
More than an explanation of MEX, that I think I can learn from the documentation, I would like to know if anybody has a different suggestion, or even if somebody knows wether the MEX solution would accelerate things.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Thanks in advance&lt;br&gt;
Jose</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 11:51:01 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662691</link>
      <author>Bruno Luong</author>
      <description>Jose Antonio &amp;lt;juriguen@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; S = sparse(1:size(ySort), double((ySort+1).'), xMat.*exposuresMat);&lt;br&gt;
&amp;gt; S = sum(S);&lt;br&gt;
&lt;br&gt;
The two lines above can be replaced by :&lt;br&gt;
&lt;br&gt;
v = xMat.*exposuresMat;&lt;br&gt;
S = accumarray(ySort(:)+1,v(:));&lt;br&gt;
&lt;br&gt;
It also avoid to create S and the required memory&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 11:57:35 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662694</link>
      <author>Rune Allnor</author>
      <description>On 5 Jul, 13:18, Jose Antonio &amp;lt;jurig...@gmail.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; I am thinking of trying to implement the for loop in C, and then use a MEX implementation. But I really have no idea how to do this.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; More than an explanation of MEX, that I think I can learn from the documentation, I would like to know if anybody has a different suggestion, or even if somebody knows wether the MEX solution would accelerate things.&lt;br&gt;
&lt;br&gt;
Memory managent is the first thing to check.&lt;br&gt;
If you have many images in memory at the same time,&lt;br&gt;
and do a global search over all frames for all pixels&lt;br&gt;
with some value, cache misses etc might cause your&lt;br&gt;
algorithm to take a lot longer than you think it&lt;br&gt;
would.&lt;br&gt;
&lt;br&gt;
So the first work-around would be to accumulate&lt;br&gt;
whatever information you are looking for, from&lt;br&gt;
single frame to single frame.&lt;br&gt;
&lt;br&gt;
This approach has the added benefit that you don't&lt;br&gt;
push memory requirements as much as you do now,&lt;br&gt;
since only one frame is passed between functions,&lt;br&gt;
and less space is needed for temporary internal&lt;br&gt;
variables. Which in turn means less probability&lt;br&gt;
of needing disk swap spaces etc, which would&lt;br&gt;
significantly slow you down.&lt;br&gt;
&lt;br&gt;
This goes for both matlab and a C(++) MEX implementation.&lt;br&gt;
&lt;br&gt;
As for MEX - do you know how to program C or C++?&lt;br&gt;
&lt;br&gt;
If 'yes', then check out the 'External Interfaces'&lt;br&gt;
parts of the matlab documentation. There are a few&lt;br&gt;
example files there, on how to achieve what you want.&lt;br&gt;
&lt;br&gt;
If you don't already know C or C++ - be aware that&lt;br&gt;
these languages are demanding to learn. You need to&lt;br&gt;
know and understand the inner workings of the computer&lt;br&gt;
in some detail before you can start programming C&lt;br&gt;
with any benefit, compared to matlab. Similarly,&lt;br&gt;
you will not be able to exploit the power of C++&lt;br&gt;
until you learn how to select and use algorithms&lt;br&gt;
and data structures in some detail.&lt;br&gt;
&lt;br&gt;
So depending on previous programming experience and&lt;br&gt;
available time, your best choise might be to stick&lt;br&gt;
with matlab but leave the computer to do its job&lt;br&gt;
in your time off; over night, over weekends, etc.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 12:29:25 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662698</link>
      <author>Jose Antonio</author>
      <description>Thanks a lot!&lt;br&gt;
&lt;br&gt;
Apparently it works fine :) I had another error in the code, so I haven't been able to check all the results yet.&lt;br&gt;
&lt;br&gt;
I have seen the improvement is from around 35s to 6s, to it's great. However, do you know if it could be even faster implementing the operation in C?&lt;br&gt;
&lt;br&gt;
Thanks!</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 12:41:01 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662700</link>
      <author>Bruno Luong</author>
      <description>Jose Antonio &amp;lt;juriguen@gmail.com&amp;gt; wrote in message &amp;lt;9281730.75571.1246796996212.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; Thanks a lot!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Apparently it works fine :) I had another error in the code, so I haven't been able to check all the results yet.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have seen the improvement is from around 35s to 6s, to it's great. However, do you know if it could be even faster implementing the operation in C?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
To whom you ask this question? Me or Rune?&lt;br&gt;
&lt;br&gt;
If it's to me, my opinion is not bother with MEX. Using ACCUMARRAY, you can remove even the SORT command, and the code pretty much comes down to few basic commands that would be hard for MEX to beat it by much.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 13:01:00 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662701</link>
      <author>Rune Allnor</author>
      <description>On 5 Jul, 14:29, Jose Antonio &amp;lt;jurig...@gmail.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; I have seen the improvement is from around 35s to 6s, to it's great. However, do you know if it could be even faster implementing the operation in C?&lt;br&gt;
&lt;br&gt;
Somebody who really understand *both* C *and* the&lt;br&gt;
problem you are trying to solve, might be able to&lt;br&gt;
squeeze the performance a bit yet. But then, don't&lt;br&gt;
expect a C newbie to be able to obtain significant&lt;br&gt;
improvment.&lt;br&gt;
&lt;br&gt;
The question is, however, *why* one would want to&lt;br&gt;
boost performance. You mentioned optimization, so&lt;br&gt;
I assume this is a function that will be called&lt;br&gt;
over and over.&lt;br&gt;
&lt;br&gt;
Again: Leave the computer to work over night, and&lt;br&gt;
the performance boost you already got might be&lt;br&gt;
enough.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 13:20:27 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662703</link>
      <author>Jose Antonio</author>
      <description>Hi Bruno&lt;br&gt;
&lt;br&gt;
The 1st question was for you, so thanks for answering. I guess I will fix to Matlab now, which is performing fast enough!&lt;br&gt;
&lt;br&gt;
Thanks</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 13:23:59 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662704</link>
      <author>Jose Antonio</author>
      <description>Hi Rune&lt;br&gt;
&lt;br&gt;
Thanks for the advice. I will fix to Matlab now, since I am pretty much a newbie to C now :)&lt;br&gt;
&lt;br&gt;
I want the code to run as fast as possible because I am using it to perform a series of operations on a set of images and I want to check the results until I code everything properly.&lt;br&gt;
&lt;br&gt;
Furthermore, the algorithm should be efficient, since it is not meant to be a long postprocessing stage in the project that I am trying to develop.&lt;br&gt;
&lt;br&gt;
Thanks again!&lt;br&gt;
Jose</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 13:53:07 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662705</link>
      <author>Rune Allnor</author>
      <description>On 5 Jul, 15:23, Jose Antonio &amp;lt;jurig...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi Rune&lt;br&gt;
&lt;br&gt;
&amp;gt; I want the code to run as fast as possible because I am using it to perform a series of operations on a set of images and I want to check the results until I code everything properly.&lt;br&gt;
&lt;br&gt;
Use smaller data sets (smaller images, fewer images)&lt;br&gt;
for implementation checks.&lt;br&gt;
&lt;br&gt;
&amp;gt; Furthermore, the algorithm should be efficient, since it is not meant to be a long postprocessing stage in the project that I am trying to develop.&lt;br&gt;
&lt;br&gt;
This boils down to a cost/benefit analysis: How much&lt;br&gt;
effort does it take on your part to achieve the fast&lt;br&gt;
algorithm, and what pay-offs does this provide.&lt;br&gt;
&lt;br&gt;
If you spend 6-9 months learning C just to cut a&lt;br&gt;
processing routine from 5 min to 3 min - is it&lt;br&gt;
worth it in the end?&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 13:53:45 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662706</link>
      <author>Jose Antonio</author>
      <description>Thanks a lot again&lt;br&gt;
&lt;br&gt;
Just used Bruno's last suggestion, and my running times are now around 2s, rather than 30s tht took with the loop.&lt;br&gt;
&lt;br&gt;
Absolutely great!</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 14:02:01 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662708</link>
      <author>Bruno Luong</author>
      <description>Rune Allnor &amp;lt;allnor@tele.ntnu.no&amp;gt; wrote in message &amp;lt;efb4c71f-e507-4294-a5b6-1d212c68bc4f@j32g2000yqh.googlegroups.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you spend 6-9 months learning C just to cut a&lt;br&gt;
&amp;gt; processing routine from 5 min to 3 min - is it&lt;br&gt;
&amp;gt; worth it in the end?&lt;br&gt;
&lt;br&gt;
Learning is priceless.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 15:00:52 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255339#662712</link>
      <author>Jose Antonio</author>
      <description>Rune&lt;br&gt;
&lt;br&gt;
I agree with both Bruno and you. On one hand I want to finish this coding asap but, on the other hand, I am doing it for my MSc project.&lt;br&gt;
&lt;br&gt;
Thus, the most I can learn, in general, including remembering C stuff and even how to embed it into Matlab, the best.&lt;br&gt;
&lt;br&gt;
Thanks for your comments&lt;br&gt;
Jose</description>
    </item>
  </channel>
</rss>

