N t
N t

Reputation: 271

Matlab optimisation

Apologies if this is too much like a 'do my homework' question. I am a bit stuck and this is my go to place. This is not code I wrote, but I am now maintaining it.

I have a tool that runs for around 8 hours on a bad day. I profiled it today. 91.4% of runtime is in one subfunction, and within that 70% of the time is spent on this one line of code. This is getting called effectively 60000 times in the profiled run due to being inside 3-4 nested for loops (unavoidable)

    testincrease = ones(183,3021)*.02;
    cRange = (0:177)'; %fix broken syntax highlighting --> '
    pRate = ones(183,3021)*.02;
    lengthA = 0;
    lengthP = 178;
    nSim = 3021;

    tic
    for i  = 1:100
    v_F = @(adj_N,adj_I) (((1+ adj_I+ testincrease(cRange+1,:))./(1 + adj_N + pRate(cRange+1,:))).^...
                repmat([cRange+0.5],[1 nSim]))...
                ./ ...
                (repmat(((1+adj_I+testincrease(lengthA+1,:))./(1 + adj_N+ pRate(lengthA+1,:))), [lengthP 1]).^...
                repmat([repmat(cRange(1), [lengthP 1])], [1 nSim]));
    v=v_F(0,0);
    end
    toc

Current Ideas:

I think that perhaps I could use bsxfun in place of repmat (and am trying that at the moment)

When cRange(1) = 0 (as it did in one part of my test data - the part I have put here) the second half is simply dividing by 1 - an expensive and meaningless calculation. So I want to use 'if' clauses around this to avoid unnecessary calculation.

Calling another language - this would be entirely new for me. Probably going to try it. There is apparently easy access to java, and also creating mex files. In the latter case I think the overhead of passing data will be to expensive unless I rewrite huge swathes of code rather than just this one line.

If you have any other thoughts I would be grateful to hear them. If not I'll keep doing some more research.

Edit: Just noticed that the final line of the function repmats are completely unnecessary, since you can .^ by a scalar value without needing to change it to a matrix

Secondary edit: This is what it looks like so far. 30% savings.

tic
for i  = 1:100
    if cRange(1) == 0

        v_F = @(adj_N,adj_I) bsxfun(@power,((1+ adj_I+ testincrease(cRange+1,:))./(1 + adj_N + pRate(cRange+1,:))),cRange+0.5);

    else
        v_F = @(adj_N,adj_I) bsxfun(@power,((1+ adj_I+ testincrease(cRange+1,:))./(1 + adj_N + pRate(cRange+1,:))),cRange+0.5)...
            ./ ...
            (repmat(((1+adj_I+testincrease(lengthA+1,:))./(1 + adj_N+ pRate(lengthA+1,:))).^cRange(1), [lengthP 1]));

    end
    v=v_F(0,0);
end
AllChange = toc;

But for some reason the bsxfun change that speeds it up in 2011a slows it down in 2008 where we compile. Bugger.

Upvotes: 0

Views: 192

Answers (1)

Trevor Boyd Smith
Trevor Boyd Smith

Reputation: 19223

Suggestion 1 - try rewriting code without repmat:

repmat makes tons of memory allocations and memory copies... so if you were to rewrite the code to be the same functionality but don't use repmat it might have huge speed increase. (FYI... that one line of code has 4 repmat function calls... one of the repmat calls has repmat INSIDE of it... that would be horribly slow.)

Suggestion 2 - try rewriting the one-liner:

rewrite this MASSIVE one-liner into multiple lines. then re-run your profiler. you will then be able to more clearly see exactly what is causing the slow down! then you can fix that bit of code. (pro tip: massive one liners are sometimes not the best thing to have in your code because of various reasons (readability, debugging, performance, etc)) – Trevor Boyd Smith 17 secs ago edit

Suggestion 3 - take advantage of parallelization:

I think most of the operations in your one line of code can be parallelized. So I suggest first trying Matlab's parallel toolboo. Then if that still isn't fast enough... try using GPU acceleration via an external third-party Matlab GPU acceleration software package like Jacket.


(Disclaimer: These are general tips... I haven't tried any of these yet myself... but I will when I get the time.)

Upvotes: 2

Related Questions