From: James Tursa on
"Aristeidis " <aris262(a)hotmail.com> wrote in message <i0ncv3$jl5$1(a)fred.mathworks.com>...
>
> I am the definition of a beginner with mex files. I ve reached a stage with my imaging algorithm where I just have to mex some cell computations for speed. I have optimised the m-code as much as I could but C/C++ seems now the only option to speed things up.

Why do you think a mex routine will speed things up? i.e., what specific calculation(s) are you doing that you think will be faster with a mex routine? For example, operating on a cell will not, in general, be any faster in a mex routine. So before you embark on a journey to convert m-code to C-code maybe you could explain why you think it will run faster. Things don't run faster just because it is C-code vs m-code ... it is for specific reasons like avoiding many temporary variables in a for-loop, or avoiding making copies of array slices, etc.

James Tursa
From: Aristeidis on
> Why do you think a mex routine will speed things up? i.e., what specific calculation(s) are you doing that you think will be faster with a mex routine? For example, operating on a cell will not, in general, be any faster in a mex routine. So before you embark on a journey to convert m-code to C-code maybe you could explain why you think it will run faster. Things don't run faster just because it is C-code vs m-code ... it is for specific reasons like avoiding many temporary variables in a for-loop, or avoiding making copies of array slices, etc.
>
> James Tursa

Actually, I can't be 100% certain that it will be (unless I see it for myself) and by all means maybe this discussion will help me understand if indeed i need C/C++. Your opinion will definitely help.

So here is my problem, I have created cells which hold 2D matrices of image pixel values at different sizes (i.e. 5x5, 8x8 etc) and scales (of the original image). I extract the maximum value of each of these matrices and thus cells. The computation speed is controlled, obviously, by the number of scaled versions of the image and the number of cells as a result (see them as filters of different sizes).
As a calculation i simply need to extract the maximum value, however cells within cells containing matrices as you can imagine needs m-code which contains a few for loops which we all know take a while in matlab. Multiply that by the dozens of images and you got an m-file which runs for many hours. Yes, I ve set the cell values beforehand (none growing in loops), I ve run and dealt with all the profiler instructions to optimise and used tic-toc to improve wherever possible. As an indication, I should tell you that the whole m-code program (excluding the bit which I want to MEX) runs at a "good" 5secs while including the bit to be MEX-ed at 35 secs (on average, of course depending on parameters and settings, more cells more time). C/C++ on the other hand, will handle the for loops in a better manner both in memory and speed, due to the extend of temporary variables and loops within loops.
I simply therefore, want to input these cells in a mex file which will only return the maximum values back to matlab and I if i can bring the total computation time to 10secs (arbitrary) or even 20 from 35secs I am a happy man. Do you think that this is a good reason why someone should MEX?
I look forward to your replies and many thanks.
From: Rune Allnor on
On 4 Jul, 13:35, "Aristeidis " <aris...(a)hotmail.com> wrote:
>  > Why do you think a mex routine will speed things up?  i.e., what specific calculation(s) are you doing that you think will be faster with a mex routine? For example, operating on a cell will not, in general, be any faster in a mex routine. So before you embark on a journey to convert m-code to C-code maybe you could explain why you think it will run faster.  Things don't run faster just because it is C-code vs m-code ... it is for specific reasons like avoiding many temporary variables in a for-loop, or avoiding making copies of array slices, etc.
>
>
>
> > James Tursa
>
> Actually, I can't be 100% certain that it will be (unless I see it for myself) and by all means maybe this discussion will help me understand if indeed i need C/C++. Your opinion will definitely help.
>
> So here is my problem, I have created cells which hold 2D matrices of image pixel values at different sizes (i.e. 5x5, 8x8 etc) and scales (of the original image). I extract the maximum value of each of these matrices and thus cells. The computation speed is controlled, obviously, by the number of scaled versions of the image and the number of cells as a result (see them as filters of different sizes).
> As a calculation i simply need to extract the maximum value, however cells within cells containing matrices as you can imagine needs m-code which contains a few for loops which we all know take a while in matlab. Multiply that by the dozens of images and you got an m-file which runs for many hours.. Yes, I ve set the cell values beforehand (none growing in loops), I ve run and dealt with all the profiler instructions to optimise and used tic-toc to improve wherever possible. As an indication, I should tell you that the whole m-code program (excluding the bit which I want to MEX) runs at a "good" 5secs while including the bit to be MEX-ed at 35 secs (on average, of course depending on parameters and settings, more cells more time). C/C++ on the other hand, will handle the for loops in a better manner both in memory and speed, due to the extend of temporary variables and loops within loops.
> I simply therefore, want to input these cells in a mex file which will only return the maximum values back to matlab and I if i can bring the total computation time to 10secs (arbitrary) or even 20 from 35secs I am a happy man. Do you think that this is a good reason why someone should MEX?
> I look forward to your replies and many thanks.

If I understand this correctly, you have a cell array containing
a number of matrices? And you only want to extract the maximum
values from these matrices?

Provided the matrices already exist in the cell array, there is
no reason to expect MEX to work significantly faster than something
like

x = max(max(c(n))));

Maybe you can gain 20% or so by mexing. With a *lot* of work.

The problem I would look for, is moving the arrays in and out of
the cell array. This is the kind of thing that would cause all
sorts of overhead, by allocating memory, moving the numbers,
and so on. This overhead will be the same whether or not you
use matlab or MEX.

The main question, then, is why you use the cell array in the
first place. My first guess will be that the main speed savings
might be gained by restructuring the program so that you avoid
the cell array at all.

Rune
From: Aristeidis on
> If I understand this correctly, you have a cell array containing
> a number of matrices? And you only want to extract the maximum
> values from these matrices?
>
> Provided the matrices already exist in the cell array, there is
> no reason to expect MEX to work significantly faster than something
> like
>
> x = max(max(c(n))));
>
> Maybe you can gain 20% or so by mexing. With a *lot* of work.
>
> The problem I would look for, is moving the arrays in and out of
> the cell array. This is the kind of thing that would cause all
> sorts of overhead, by allocating memory, moving the numbers,
> and so on. This overhead will be the same whether or not you
> use matlab or MEX.
>
> The main question, then, is why you use the cell array in the
> first place. My first guess will be that the main speed savings
> might be gained by restructuring the program so that you avoid
> the cell array at all.
>
> Rune

Yes, Rune you got the problem spot on and I do use X = max(max(y)); on all of these matrices which are contained in cells of cells.

By all means I' d gladly ditch cells if i can but If I don't use cells then what do you think can hold image data in a "better" and in a more efficient way computation-wise? I' ve examined using structures but this didn't seem to be alot different (in fact in terms of memory it is slightly "heavier").
From: Rune Allnor on
On 4 Jul, 14:06, "Aristeidis " <aris...(a)hotmail.com> wrote:
>By all means I' d gladly ditch cells if i can but If I don't use cells then what do you think can hold image data in a "better" and in a more efficient way computation-wise? I' ve examined using structures but this didn't seem to be alot different (in fact in terms of memory it is slightly "heavier").

The answer to that question depends entirely on what you
attempt to do, and why you need to have all the arrays
available at once.

I get the impression that you hight be in a position where
you might need to make an executive decision:

Either stick with matlab and accept the run-time penalty,
or ditch matlab alltogether and go pure C(++).

Rune