0
votes

I’m looking for some advice in a project in which speed and performance is of great importance.

I created a model which consists of an outer function with 1 for-loop (25 000 iterations) which invokes several custom-made functions. To increase model’s performance, I rewrote the custom-made functions into MEX-functions. This already gave a good speed-up. However, I would also like to place the outer for-loop in a MEX-function. But I have a problem passing data from MATLAB to the MEX-functions. (Note that it’s not possible to vectorize the for-loop).

The data which is known prior to the model’s simulation is saved to a large nested structure of arrays (built in the form of “data.Q.position1” with “position1” an array of 10000 doubles). All arrays containing doubles have the same length. The results of the model will also be written to this structure of arrays (different fieldnames of course, and pre-allocation is done before the for-loop)

So currently, the model is looking like this:

[data]  = function model(data)
for i=1:10000
    data.Q.position1(i) = MEX-function1(data.WL.position4(i), data.WL.position3(i), …);
    data.Q.position2(i) = MEX-function1(data.WL.position6(i), data.WL.position12(i), …);
    …
    data.WL.position7(i) = MEX-function2(data.Q.position1(i), …);
     …
end

I chose for the structure of arrays so I can point easily at the required variables for the function inputs.

So my question is, how can I place the for-loop in a MEX function and what do I have to do with the structure? I’m really new to MEX and C-language, so maybe there’s a straightforward solution...

Secondly, does this whole concept looks good with regard to performance, or are better solutions available?

Thanks in advance!

1
I suppose you got an error in your snippet as you use the input with index i which haven't been defined. Though should this be correc you can use parfor on it. - bdecaf

1 Answers

0
votes

Start with mxGetField and mxSetField and follow the doc trail from there...