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

Codegen c multi-dimensional locals #21190

Merged
merged 5 commits into from Apr 8, 2021

Conversation

rols121
Copy link
Contributor

@rols121 rols121 commented Mar 29, 2021

Brief description of what is fixed or changed

Sympy did not allow for codegen with multi-dimensional arrays as arguments that would require a local variable copy. This change just enforces that multi-dimensional arrays are converted to a single dimension array with the equivalent amount of elements. The de-referencing of the new local array already happened correctly.

Other comments

This could be improved upon even more by having a flag for "copy arguments", which, if set has the current behavior, and if not then just uses the argument instead of making a copy and then operates on the argument. It may not be the neatest solution but contiguous multi-dim arrays could just be treated as a single dim array if the user is willing to pass the pointer to the very first element. This change just is just a starting point to allow the use of sympy Matrix types with more that one dimension for c code generation.

Release Notes

  • codegen
    • allowing for multi-dimensional arrays as arguments/locals in c code generation

@sympy-bot
Copy link

sympy-bot commented Mar 29, 2021

Hi, I am the SymPy bot (v161). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • codegen
    • allowing for multi-dimensional arrays as arguments/locals in c code generation (#21190 by @rols121)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.8.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### Brief description of what is fixed or changed
Sympy did not allow for codegen with multi-dimensional arrays as arguments that would require a local variable copy. This change just enforces that multi-dimensional arrays are converted to a single dimension array with the equivalent amount of elements. The de-referencing of the new local array already happened correctly.


#### Other comments
This could be improved upon even more by having a flag for "copy arguments", which, if set has the current behavior, and if not then just uses the argument instead of making a copy and then operates on the argument. It may not be the neatest solution but contiguous multi-dim arrays could just be treated as a single dim array if the user is willing to pass the pointer to the very first element. This change just is just a starting point to allow the use of sympy Matrix types with more that one dimension for c code generation.

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->

* codegen
  * allowing for multi-dimensional arrays as arguments/locals in c code generation

<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@bjodah
Copy link
Member

bjodah commented Mar 29, 2021

Thank you for your contribution. This will also need a test.

@bjodah
Copy link
Member

bjodah commented Mar 30, 2021

@gouarin I think you know the utilities.codegen module the best, would you mind taking a quick look here?

@gouarin
Copy link

gouarin commented Mar 30, 2021

@bjodah thanks. I will review this PR before the end of this week.

@rols121
Copy link
Contributor Author

rols121 commented Apr 7, 2021

Hi @bjodah and @gouarin, is there an update on this PR? Comments are welcome as this is my first contribution, so still learning the ins and outs.

@gouarin
Copy link

gouarin commented Apr 7, 2021

Hi @rols121, few days ago I added a comment in your test case.
Do you see it ?

@rols121
Copy link
Contributor Author

rols121 commented Apr 7, 2021

Hey @gouarin, no, unfortunately not. Where do I see comments? It still says "no reviews" at the top of this PR.

A = Matrix(A_sym)
b = Matrix(b_sym)
c = A*b
cgen = CCodeGen(project="test", cse=True)
Copy link

Choose a reason for hiding this comment

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

Why do you use cse here? The example is more complicated to read, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to use cse so that the code reached the section where c locals are printed. If I did not use cse, then there where no locals and the "raise CodeGenError("Only column vectors are supported in local variabels. Local result {} has dimensions {}".format(result, dims))" line was not reached.

The test is a bit simple as it does not show cse being used but it does however force an array from a multi-dimensional input.

Copy link

Choose a reason for hiding this comment

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

of course! Thanks.

@gouarin
Copy link

gouarin commented Apr 7, 2021

Sorry, my bad.

@bjodah bjodah merged commit 8adb88a into sympy:master Apr 8, 2021
@bjodah
Copy link
Member

bjodah commented Apr 8, 2021

Thank you @rols121 for your contribution, and thank you @gouarin for the review!

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.

None yet

4 participants