Skip to content

Remove C-Wrapper for MPI_Init #16

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

Merged
merged 3 commits into from
Mar 21, 2025
Merged

Remove C-Wrapper for MPI_Init #16

merged 3 commits into from
Mar 21, 2025

Conversation

adit4443ya
Copy link
Collaborator

@adit4443ya adit4443ya commented Mar 18, 2025

@gxyd
Copy link
Collaborator

gxyd commented Mar 18, 2025

I haven't given a look at the changes closely, but one important thing for this PR would be the addition of tests, so we definitely need a test case which makes use of MPI_Init, or atleast check it locally for any of the test program already present in tests/ directory to see if it still continues to work with these changes or not.

@adit4443ya
Copy link
Collaborator Author

Added a simple Test
LEt's hope CI passes

@adit4443ya adit4443ya requested a review from gxyd March 18, 2025 14:30
@adit4443ya
Copy link
Collaborator Author

Haha Tests Passed
@gxyd Now you can take a closer look and see if we can merge it

function c_mpi_init(argc, argv) bind(C, name="MPI_Init")
use iso_c_binding, only : c_int, c_ptr
integer(c_int), value :: argc
type(c_ptr), value :: argv
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do both the arguments have to be pass-by-value here? What if I keep it intent(inout)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would like to see an explanation for why this needs to be passed with value attribute with it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought that passing pointers/reference would make it complex , Hence to keep it safe i used value
Although i didn't checked with intent(inout)
So this is the current situation

Wihout value attribute all tests passes locally
with intent(inout) lfortran fails
with this error

code generation error: asr_to_llvm: module failed verification. Error:
Call parameter type does not match function signature!
  %argv = alloca void*, align 8
 void*  %0 = call i32 @MPI_Init(i32* %argc, void** %argv)

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, I tried locally, and when no intent is specified, then ./run_tests.sh passes with both LFortran and GFortran.

While, when intent(inout) is specified, then LFortran fails, but GFortran succeeds. By default, when no intent is specified, then the intent is actually intent(inout) I think.

So, there is anyways a bug in LFortran, which should be extracted and reported against LFortran.

I thought that passing pointers/reference would make it complex

I don't really understand this. @certik any thoughts on this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm reading: https://docs.open-mpi.org/en/v5.0.x/man-openmpi/man3/MPI_Init.3.html#description, which says:

All MPI programs must contain a call to MPI_Init or MPI_Init_thread. Open MPI accepts the C argc and argv arguments to main, but neither modifies, interprets, nor distributes them:

Copy link
Collaborator Author

@adit4443ya adit4443ya Mar 20, 2025

Choose a reason for hiding this comment

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

Let's Do one thing
We know that LFortran gives error in the intent(inout) declaration

So for now let me create an MRE and open an issue on LFortran and till then we would use value parameter,
Once that issue is resolved we will remove this workaround and things won't get blocked.

Here is the MRE:-lfortran/lfortran#6696

What's your view @gxyd

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me create issue in this repo too so that we do not skip it
Here:- #27

Copy link
Collaborator

@gxyd gxyd Mar 20, 2025

Choose a reason for hiding this comment

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

While, when intent(inout) is specified, then LFortran fails, but GFortran succeeds. By default, when no intent is specified, then the intent is actually intent(inout) I think.

Like I mentioned here, LFortran doesn't fail when no intent is specified, so we still need to answer, whether value is the correct way or not specifying any intent at all.

Copy link
Collaborator Author

@adit4443ya adit4443ya Mar 20, 2025

Choose a reason for hiding this comment

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

I guess maybe we need to look at any documentation for what to use
My intuition is not to use value or intent at all, (also i used GROK to do some research and the first thing it told me is not to use it)
maybe i read it somewhere, but i did not found the exact line stating that which parameter is ideal enough.

As with the value approach we might add an extra instruction of storing and then loading
Also i referred this https://gcc.gnu.org/onlinedocs/gfortran/Interoperable-Subroutines-and-Functions.html
It does not use intent(),
Also intent should not affect the passing of the objects at the runtime right?
Hence that issue iss the one which we have to fix at lfortran, but what i feel what ideal to do is not to use anything at all, until unless something comes up again with similar argument

Copy link
Collaborator

Choose a reason for hiding this comment

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

As with the value approach we might add an extra instruction of storing and then loading
Also i referred this https://gcc.gnu.org/onlinedocs/gfortran/Interoperable-Subroutines-and-Functions.html
It does not use intent()

This is good, it makes it clear that we don't need value attribute specified here.

Copy link
Collaborator

@gxyd gxyd left a comment

Choose a reason for hiding this comment

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

Looks perfect now, I've removed the value attribute from the arguments of MPI_Init

@gxyd gxyd merged commit ac78bd0 into lfortran:main Mar 21, 2025
8 checks passed
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.

2 participants