Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate state (Kin) quantities exploiting CasADi code-generation #10

Merged
merged 2 commits into from
Jun 17, 2022

Conversation

FabioBergonti
Copy link
Member

@FabioBergonti FabioBergonti commented Jun 17, 2022

This PR decreases the computational time for evaluating jacobian, ...

@FabioBergonti FabioBergonti changed the title Evaluate state quantities exploiting CasADi code-generation Evaluate state (Kin) quantities exploiting CasADi code-generation Jun 17, 2022
@FabioBergonti FabioBergonti self-assigned this Jun 17, 2022
@FabioBergonti
Copy link
Member Author

I compared 4s of a kinematic simulation of an 8x8 mesh W/O codegen.
Using codegen the computational time is lower.

image

If we increase the length of the simulation 10s the gap increase even more

image

This PR could be beneficial also for #2

Copy link
Member

@gabrielenava gabrielenava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting! so the mex file is generated every time you run a new simulation and then deleted at the end? Do you think it may be convenient to keep also the generated mex files somewhere or not?

Copy link

@Giulero Giulero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of comments for curiosity ;)
It looks fine to me btw!

C.add(obj.csdFn.rC_from_mBodyTwist0_2_jointsAngVelPJ);
C.add(obj.csdFn.get_mBodyVelQuat0_from_mBodyTwist0)
C.generate();
mex mystica_stateKin.c -largeArrayDims
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For curiosity :)
Once the function mystica_stateKin is generated, does it live in the workspace and be called in every part of the code?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it computationally expensive to generate it?
I might understand why you don't check if the function already exists.
To check if the content of the function changed, you have to generate it again :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generate the MEX files in the current folder. So, until you do not change the current directory, they can be called everywhere

@@ -59,6 +59,7 @@
%% Saving Workspace

clear ans k kVec motorsCurrent mBodyPosQuat_0 tout dataLiveStatistics
mystica.utils.deleteGeneratedMEX
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it also work without parenthesis?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In matlab yes :)

C.add(obj.csdFn.rC_from_mBodyTwist0_2_jointsAngVelPJ);
C.add(obj.csdFn.get_mBodyVelQuat0_from_mBodyTwist0)
C.generate();
mex mystica_stateKin.c -largeArrayDims
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it computationally expensive to generate it?
I might understand why you don't check if the function already exists.
To check if the content of the function changed, you have to generate it again :D

@FabioBergonti
Copy link
Member Author

@gabrielenava

interesting! so the mex file is generated every time you run a new simulation and then deleted at the end?

Exactly!

Do you think it may be convenient to keep also the generated mex files somewhere or not?

The generated MEX files are model dependent. If I want to keep them I have to add a check to verify that the model is not changed.
For the moment I decided to regenerate them every time also because it requires only ~10s

@FabioBergonti
Copy link
Member Author

@Giulero @gabrielenava

Also, is it computationally expensive to generate it?

~10s but it depends on the dimension of the problem

I might understand why you don't check if the function already exists.
To check if the content of the function changed, you have to generate it again :D

The time-demanding operation is the generation of the MEX files
Theoretically, I can compare the mystica_stateKin.c file and if they are different I can generate the MEX file
image

@FabioBergonti
Copy link
Member Author

Thanks @gabrielenava and @Giulero
Merging!

@FabioBergonti FabioBergonti merged commit 552b896 into main Jun 17, 2022
@traversaro
Copy link
Contributor

If you are using mex, probably you need to also add in your dependencies a supported C compiler for mex (see https://it.mathworks.com/support/requirements/supported-compilers.html). It is kind of obvious having that on Linux/macOS, but it may be not obvious to have it on Windows.

@FabioBergonti FabioBergonti deleted the casadi-cogen branch June 17, 2022 12:30
FabioBergonti added a commit that referenced this pull request Jun 17, 2022
FabioBergonti added a commit that referenced this pull request Jun 17, 2022
FabioBergonti added a commit that referenced this pull request Jun 18, 2022
FabioBergonti added a commit that referenced this pull request Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants