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

OPEN(NEWUNIT Set Unit To Positive Value

ScottBoyce
Novice
3,114 Views

I have a large program that reads in an input file that contains a list of additional input files and a unit number. I like to change the code so that it can take advantage of the Fortran 2008 NEWUNIT feature, but this always sets the unit number to a negative value. This is a problem because the code decides what features to employ based on a unit number being greater than zero.

Is there anyway to have something as the following:

[fortran]

LOGICAL::OPENFILE

...

IUNIT=0

IF(OPENFILE) OPEN(NEWUNIT=IUNIT,FILE='TEMP.txt')

[/fortran]

But have the unit number stored in IUNIT be greater than zero (i.e. IUNIT>0 if opened)

If this is not possible, it would be a great feature to employ in future versions of the Intel compiler because due to the complexity of the code it would be virtually impossible to change it from IF(IUNIT>0) to IF(IUNIT/=0).

Thanks

0 Kudos
8 Replies
andrew_4619
Honored Contributor III
3,114 Views

NEWUNIT being negatice appaers to be part of the rational of the new feature and is what the standard prescribes. There are many ways you can go this, for example make a ultilty routine wrappers around open and close that track avialable units for example.

"virtually impossible to change it from IF(IUNIT>0) to IF(IUNIT/=0)."  not an imposibility just some work!

0 Kudos
IanH
Honored Contributor III
3,114 Views

Note that the F2008 standard requires that the unit number that comes back with the NEWUNIT specifier be a negative number.  You can also get valid negative numbers for unit numbers in other ways too.

Edit: beaten to the punch...

0 Kudos
ScottBoyce
Novice
3,114 Views

I would say its pretty much inpossible given financing for the software and value of time. IUNIT is actually a vector of length 50 that gets sent to subroutines that set it to scalar variables that get passed to additional subroutines and the choas dominos from there.

0 Kudos
andrew_4619
Honored Contributor III
3,114 Views

So does the code currently work, and if so why do you want to change it?

0 Kudos
jimdempseyatthecove
Honored Contributor III
3,114 Views

Though the number of test for IUNIT may be large and disbursed throught the program, the number of places where you use OPEN and CLOSE may be small. If this is the case, the consider creating converter functions, the one to insert into the table strips the sign bit off the returned NEWUNIT number from OPEN, and one that propagates the next to sign bit to sign bit for the CLOSE/INQUIRE. This will have a minimal restriction of reducing the numbers of + or - unit numbers to 1 billion each (using 32-bit integer unit number).

Personally I would fix the test points. Perhapse insert a logical function for test (IF(isOpen(UnitTable)) ...)

Jim Dempsey

0 Kudos
lklawrie
Beginner
3,114 Views

I use the INQUIRE, OPENED option instead of relying on a specific value of a unit number.

If that would help.  I imagine the negative value for NEWUNIT is a compiler option -- other compilers might do it differently.

0 Kudos
mecej4
Honored Contributor III
3,114 Views

lklawrie wrote:
 I imagine the negative value for NEWUNIT is a compiler option -- other compilers might do it differently.
Actually, the Fortran 2008 standard controls the allowable values of NEWUNIT

9.5.6.12 NEWUNIT= speci fier in the OPEN statement
1 The variable is de fined with a processor determined NEWUNIT value if no error occurs during the execution of the OPEN statement. If an error occurs, the processor shall not change the value of the variable. 

2 A NEWUNIT value is a negative number, and shall not be equal to -1, any of the named constants ERROR_UNIT, INPUT_UNIT, or OUTPUT_UNIT from the intrinsic module ISO FORTRAN ENV (13.8.2), any value used by the processor for the unit argument to a de fined input/output procedure, nor any previous NEWUNIT value that identi fies a fi le that is connected.

0 Kudos
lklawrie
Beginner
3,114 Views

Sorry, have not memorized the F2008 standard.

0 Kudos
Reply