Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MEX and structure of arrays

Subject: MEX and structure of arrays

From: Vincent

Date: 12 Feb, 2013 16:07:19

Message: 1 of 2

Hi all,

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 very easy answer =)

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

Thanks in advance!

Subject: MEX and structure of arrays

From: James Tursa

Date: 12 Feb, 2013 16:50:25

Message: 2 of 2

"Vincent" wrote in message <kfdpbn$kee$1@newscl01ah.mathworks.com>...
> Hi all,
>
> 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 very easy answer =)
>
> Secondly, does this whole concept looks good with regard to performance, or are better solutions available?
>
> Thanks in advance!

Cross-Posted in Answers. See my response there ...

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us