6
\$\begingroup\$

I have created a Matlab Simulink model with a series of dot products.

Simulink

Embedded Coder generates a for loop for each dot product, as evidenced by this excerpt of the generated output:

/* DotProduct: '<S3>/Dot Product1' incorporates:
 *  Inport: '<Root>/LngPwr'
 */
EPAR_SPAF_dot_B.DotProduct1 = 0.0F;
for (temp_ = 0; temp_ < 24; temp_++) {
  EPAR_SPAF_dot_B.DotProduct1 += temp_DataTypeConversion[temp_] *
  LngPwr[temp_];
}
/* End of DotProduct: '<S3>/Dot Product1' */

/* DotProduct: '<S3>/Dot Product2' incorporates:
 *  Inport: '<Root>/ShrtPwr'
 */
EPAR_SPAF_dot_B.DotProduct2 = 0.0F;
for (temp_ = 0; temp_ < 24; temp_++) {
  EPAR_SPAF_dot_B.DotProduct2 += temp_DataTypeConversion[temp_] *
  ShrtPwr[temp_];
 }
/* End of DotProduct: '<S3>/Dot Product2' */

Can I improve the Simulink model such that it will compile to better code?

I want to avoid using embedded matlab or a S-function for the purposes of maintainability.

\$\endgroup\$
  • 4
    \$\begingroup\$ I find this kind of downvoting to be extremely frustrating because it communicates to me that there is a total lack of understanding of how MATLAB Simulink and Embedded Coder works. There IS Working code in the original post, it just happens to be graphical. The C-Code is auto generated. The question is identical to any kind of other Code efficiency question since the way you craft the simulink will directly affects the C-Code generated which in turns affects how it runs on the target. \$\endgroup\$ – Michael Coury May 6 '16 at 12:49
  • \$\begingroup\$ @MichaelCoury Stackoverflow has the tag matlab, why don't you try it there? There's also scicomp.stackexchange.com \$\endgroup\$ – Niklas R. May 6 '16 at 14:49
  • \$\begingroup\$ @Programmer400, Thanks for the constructive feedback, I'll see what the Stackoverflow community looks like. Having said that, CodeReview also has a matlab tag, so... I still pretty confident that this question falls into the realm of CodeReview IF the scope of CodeReview includes optimization. I am going to continue this conversation in the meta section.... \$\endgroup\$ – Michael Coury May 6 '16 at 15:24
  • 2
    \$\begingroup\$ This question is being discussed on Meta. \$\endgroup\$ – 200_success May 6 '16 at 16:21
5
\$\begingroup\$

There is a common mathematical operation involving many dot products: it's called matrix multiplication.

$$ \left[\begin{matrix}v_1 & v_2 & v_3 & v_4\end{matrix}\right] \left[\begin{matrix} a_1 & b_1 & c_1 \\ a_2 & b_2 & c_2 \\ a_3 & b_3 & c_3 \\ a_4 & b_4 & c_4 \end{matrix}\right] = \left[\begin{matrix}(\vec{v}\cdot\vec{a}) & (\vec{v}\cdot\vec{b}) & (\vec{v}\cdot\vec{c})\end{matrix}\right] $$

So, I recommend that you concatenate vectors \$\color{lightgreen}1\$ to \$\color{lightgreen}6\$ into a matrix and perform matrix multiplication.

Illustrated matrix multiplication

You can also transpose and swap the matrices:

$$ \left[\begin{matrix} a_1 & a_2 & a_3 & a_4 \\ b_1 & b_2 & b_3 & b_4 \\ c_1 & c_2 & c_3 & c_4 \\ \end{matrix}\right] \left[\begin{matrix}v_1 \\ v_2 \\ v_3 \\ v_4\end{matrix}\right] = \left[\begin{matrix}\vec{a}\cdot\vec{v} \\ \vec{b}\cdot\vec{v} \\ \vec{c}\cdot\vec{v}\end{matrix}\right] $$

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Gosh, I wish I was better at linear algebra... I'll put this into Simulink and Embedded Coder and see what the output looks like... \$\endgroup\$ – Michael Coury May 6 '16 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.