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

Docs on ccall and array order #41986

Open
oxinabox opened this issue Aug 24, 2021 · 4 comments
Open

Docs on ccall and array order #41986

oxinabox opened this issue Aug 24, 2021 · 4 comments
Labels
domain:docs This change adds or pertains to documentation

Comments

@oxinabox
Copy link
Contributor

When ccalling some code that returned an arrays in C order, I found that my results was all out of order.
Adding some permutedims fixed it.

But I can't find any docs on C vs Fortran order when doing ccalls.
It isn't mentioned at all on
https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/index.html

The only place it is mentioned as far as i cabn tell is in the performance tips section.

@vtjnash vtjnash added the domain:docs This change adds or pertains to documentation label Aug 24, 2021
@vtjnash
Copy link
Sponsor Member

vtjnash commented Aug 24, 2021

I think it would be worth pointing out there that Julia uses Fortran-standard order with columns first, while C codes often use rows first, although there is no standard.

@fingolfin
Copy link
Contributor

I don't think there is anything to be done here: there is no standard way to store matrices in C; so you simply need to know about the C code you are calling and what data format it uses. At best, one could add a sentence somewhere pointing out just that.

@oxinabox
Copy link
Contributor Author

oxinabox commented Sep 25, 2021

although there is no standard.

...

there is no standard way to store matrices in C;

What do yous mean? It is literally part of the C standard. No?

From the C-2x standard working draft September 25, 2019 ISO/IEC 9899:202x (E)

6.5.2.1 Array subscripting
...
3. ... It follows from this that arrays are stored in row-major order (last subscript varies fastest).

Even if we go back to the C-89 standard, i.e. ANSI C.

3.3.2.1 Array subscripting
...
Semantics
... It follows from this that arrays are stored in row-major order (last subscript varies fastest).

This has always been part of the standard.


I don't think there is anything to be done here

I feel like there unquestionably is.
No where in the very long docs on ccall does it ever mention that my arrays might come out in funny order on one side or the other.

Regardless, we absolutely can have a short paragraph or two explaining how julia stores things in memory, and contrasting that to what you will normally get out of a C or Fortran compiler.
And we can even mention that some libraries are specifically written with a particular expectation regardless of the language they are written in.
Including things that accept both orders and take a flag.

@fingolfin
Copy link
Contributor

Sorry I misread (100% my fault resp that of my brain) your question as pertaining to matrices; not double/multi indexed arrays; those are indeed standarized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

3 participants