Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28615 Discussions

How to detect that a real or integer argument is not assigned (Fortran)

Simon_C
Novice
630 Views

Everyone,

I'm developing an application in which a Fortran subroutine (in a dll) is called by a Python, Matlab, or possibly R wrapper. All of the input data - passed as integer and real scalars and array arguments - need to be tested in the subroutine. The obvious tests, which I am not asking about, are whether the various input values are within the expected ranges and therefore can be accepted as valid data. That is trivial. My question is: what happens if the Python or Matlab user had failed to assign values to the arguments? What then would the Fortran routine find in these arguments when called? NaNs, huge/tiny numbers, garbage? And how can I detect that (without the routine failing)?

I should add that for various reasons it may be impractical to test the values of the input arguments to the Fortran routine before it is called. I say 'may' because the wrapper code is being produced for me (and I don't know any C, which appears to be involved).  So it is possible the user error - failure to assign values - might best be detected in the wrapper code.

I'd be very grateful for suggestions as to the right approach to take.

Thank you.

 

 

0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
603 Views

The proper place for the check for uninitialized variable, in the case of Python, MATLAB, R calling the external procedure (Fortran in this case) would be at the point of call (or somewhere else) withing the Python, MATLAB, R program. Else expect GIGO (garbage in, garbage out) or any other unexpected behavior (possibly program crash or data/code corruption elsewhere).

Just how is an external procedure to know that the passed arguments are not the intended arguments?

If you are to add into your calling program(s) initialization code to indicate uninitialized, for REALs you could use signaling NaN's. For integers, there isn't a signaling not a number, but with cooperation with the called procedure, you could pre-initialized to a "magic number" which is known to be not accepted or virtually impossible to be a proper argument. For integers, I would suggest using hexadecimal constant (or binary) initialization that sets the sign bit to 1 and the remaining bits 0. This number represents an integer value of -0.

 

An alternate way, for situations where the API is calling by reference (Fortran default argument passing is by reference) would be to initialize the references to the address NULL or perhaps address -1. Either of those addresses (-1 in particular) should cause a segment fault (should your Fortran code de-reference it instead of testing for it). Note, this does require cooperation between the programmer writing the calling code to assure the desired outcome.

 

Jim Dempsey

 

View solution in original post

0 Kudos
3 Replies
Arjen_Markus
Honored Contributor I
616 Views

Uninitialised variables an have any value, whether they originate in C or Fortran, there is no specific value that they are initlialised to, unless you friendly ask the compiler to do so (various compilers have options for this). As the variables will actually come from a very different language like Python or MATLAB or R, as you indicate, you will have to consult the documentation for these languages on the issue. Chances are that the value is simply zero, but you should not rely on that and in many applications a zero is a completely valid value.

In short: it is highly unlikely that you can make a robust check.

0 Kudos
jimdempseyatthecove
Honored Contributor III
604 Views

The proper place for the check for uninitialized variable, in the case of Python, MATLAB, R calling the external procedure (Fortran in this case) would be at the point of call (or somewhere else) withing the Python, MATLAB, R program. Else expect GIGO (garbage in, garbage out) or any other unexpected behavior (possibly program crash or data/code corruption elsewhere).

Just how is an external procedure to know that the passed arguments are not the intended arguments?

If you are to add into your calling program(s) initialization code to indicate uninitialized, for REALs you could use signaling NaN's. For integers, there isn't a signaling not a number, but with cooperation with the called procedure, you could pre-initialized to a "magic number" which is known to be not accepted or virtually impossible to be a proper argument. For integers, I would suggest using hexadecimal constant (or binary) initialization that sets the sign bit to 1 and the remaining bits 0. This number represents an integer value of -0.

 

An alternate way, for situations where the API is calling by reference (Fortran default argument passing is by reference) would be to initialize the references to the address NULL or perhaps address -1. Either of those addresses (-1 in particular) should cause a segment fault (should your Fortran code de-reference it instead of testing for it). Note, this does require cooperation between the programmer writing the calling code to assure the desired outcome.

 

Jim Dempsey

 

0 Kudos
Simon_C
Novice
591 Views

Thank you both for your replies. Jim, I have added your suggestions to my document describing the work to be done. They make sense to me, and I expect that is the solution we will implement.

Simon.

0 Kudos
Reply