LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2020, 04:23 PM   #1
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Knoppix
Posts: 1,813
Blog Entries: 14

Rep: Reputation: 282Reputation: 282Reputation: 282
Fortran interface usage


So: this is a bit of a philosophical or style question perhaps, or perhaps I have no clue!

From what I understand, in modern Fortran, or at least since F95, calling subroutines now either requires an explicit INTERFACE block in the calling program, or the subroutine has to be in a module with a corresponding USE statement in the calling program. Not having either doesn't seem to lead to compiler errors*, but it can lead to run time errors. (my personal recent experience and reading see for example http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html) From what I read, that helps the compiler find and report problems with argument passing errors. *Funny thing that the compiler (gfortran) didn't flag me when I wasn't initially using INTERFACES. Maybe I needed additional warning flags.

At any rate, my question is two fold: is there a preferred style, explicit INTERFACE or MODULE/USE?

For debugging purposes, it's definitely helpful to have subroutines in separate files, not all collected in a module, particularly if they are general purpose subroutines and don't necessarily group together naturally with others. Obviously some subroutines work great in modules together, and if the modules don't get huge, that seems like a nice approach. Of course that means you have to debug everything in the module together, or develop them separately first, then stick them in the module afterwards.

On the other hand, having to put in in INTERFACE statements, which are essentially copies of the first several lines of the subroutine, in every program and subprogram that calls the subroutine, leads to a lot of extra duplicated text in a lot of different places. Isn't that potentially a code development and maintenance issue? What if I have to change the subprogram's interface? Then I have to search through every file everywhere to change the INTERFACE blocks. A USE statement is at least a little easier to manage, not unlike a COMMON block, which are now deprecated.

Or am I supposed to put all the explicit INTERFACE blocks, needed or not, in one INCLUDE file and then put that in all my files that use any of the subprograms in the INCLUDE FILE? That's a little bit like a COMMON block too, but perhaps easier to maintain, and there would be a lot of extra INTERFACES added unnecessarily and I'm not sure that would increase readability.

For the philosophical part, which is probably above my head, and you are free to disagree, I hope politely:
If I were writing C++ stuff/graphics libraries or something, I'd understand the need for an API that doesn't change unless absolutely necessary, and I suppose that corresponds to the INTERFACE blocks, which I suppose are like an API. But Fortran is about FormulaTranslation, not API development, so it all seems a bit too C++-ish. I'm not writing an OS or a compiler, I'm just trying to crunch some numbers. Or maybe I'm just too much of an engineer/number cruncher/neanderthal and not enough of a programmer. Does anyone know what the Fortran modernizers were thinking? I was kind of busy in the 90's-00's and am just catching up now. Were they all C++ programmers (not that there's anything wrong with that )? Or am I missing something?
 
Old 08-10-2020, 08:12 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 324

Rep: Reputation: Disabled
If you'll forgive me for oversimplifying and abusing an old joke:
There are 10 kinds of programming languages. Those that favor compiler writers and those that favor programmers.

It's been a long time since I wrote any Fortran and I have no idea what its designers might have in mind but I would guess they are following the C route more then C++ and C is a language that favors compiler writers*. Modules allow programmers to break code up into smaller units that are easier to manage and interfaces allow the compiler to make assumptions about the correctness of external function calls. The compiler can check that you are using the function as specified although it is in a module external to the program.

*I suggest C++ favors programmers since it regularly gets a shiny new kitchen sink - allowing the programmer to do the same old thing in a new way and making compilers more complicated.
 
Old 08-11-2020, 12:19 AM   #3
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 2,059

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
Quote:
Originally Posted by mostlyharmless View Post
So: this is a bit of a philosophical or style question perhaps, or perhaps I have no clue!

From what I understand, in modern Fortran, or at least since F95, calling subroutines now either requires an explicit INTERFACE block in the calling program, or the subroutine has to be in a module with a corresponding USE statement in the calling program. Not having either doesn't seem to lead to compiler errors*, but it can lead to run time errors.
I'm not as familiar with F95 (and even later standards) as I should be but my quick skimming of the idea of "interface" statements seem to be similar to the prototypes you define in header files for C procedures. (That's probably a crude analogy---there's surely more to those than a one-line description can provide.)

The Fortran maintainers appear to have brought in some ideas that have been used by the C people for years and years.

Quote:
... having to put in in INTERFACE statements, which are essentially copies of the first several lines of the subroutine, in every program and subprogram that calls the subroutine, leads to a lot of extra duplicated text in a lot of different places. Isn't that potentially a code development and maintenance issue? What if I have to change the subprogram's interface? Then I have to search through every file everywhere to change the INTERFACE blocks.
The examples of interface blocks I've seen do seem to be rather verbose and repetitive.

Can't the interfaces be defined in an external file and included at compile time? (Rather like the practice in the F77 days of defining common blocks in separate files and included when compiling.)

Cheers...
 
Old 08-11-2020, 05:04 PM   #4
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Knoppix
Posts: 1,813

Original Poster
Blog Entries: 14

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Can't the interfaces be defined in an external file and included at compile time? (Rather like the practice in the F77 days of defining common blocks in separate files and included when compiling.)
yeah, I mentioned that I could put them in an include file, or several include files and then put the include file in the subprograms as needed. Very C-ish. Not sure what most people do... I didn't see that suggestion in my "Modern Fortran" book though.

Quote:
There are 10 kinds of programming languages. Those that favor compiler writers and those that favor programmers.
heh, It's really that there are 10 kinds of people, the programming languages don't have any say about their designers' choices. I suppose as a coder, of sorts, I have the illusion that the designers of languages should be aiming to make my job easier writing code, make coding easier to maintain, and, perhaps, help the compiler writers so that the code can be run more quickly and reliably. That's a number of constraints that perhaps can't be met simultaneously.

So far I've discovered I can do things that are perhaps more elegant in modern fortran (arrays operations, OOP, overloading etc.), do some things that were previously quite difficult (C-interoperability in particular), recursive subs, and a few new things, parallel processing, at the cost of a large number of new ways to have obscure bugs, and slightly harder to develop code, for me at least.

Thanks for listening & the replies
 
Old 08-12-2020, 12:02 AM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 2,059

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
Quote:
Originally Posted by mostlyharmless View Post
C-interoperability in particular
Interoperability was a piece of cake on VMS. DEC's language calling standard allowed you to write pieces of a program in whatever language was most suited and link them together. I recall writing VAX FORTRAN programs that had a BASIC routine to interact with the user to collect information/parameters/etc. before kicking off the number crunching. (FORTRAN could do that but not as easily.) We did the same thing with bit of real-time software (running under VAXELN): Ada + BASIC. If only it were that easy nowadays.

Cheers...
 
Old 08-15-2020, 06:39 PM   #6
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Knoppix
Posts: 1,813

Original Poster
Blog Entries: 14

Rep: Reputation: 282Reputation: 282Reputation: 282
Looking further into this, it seems that sometimes having an implicit interface within a module isn't sufficient. See https://stackoverflow.com/questions/...se-interfaces; quoting the relevent bits for the record (because I don't want the link to go bad one day)
Quote:
If you're using interface blocks to create generic identifiers you're probably doing so within modules, so the main reason to write such blocks when modules aren't about is for the explicit interfaces that they create in the scope in which they occur. It's these that allow those compiler checks and the use association with modules no longer happens.

However, there are times when an explicit interface is required, rather than being a nicety: if referencing a procedure with certain features an explicit interface will be required for your code to conform to the Fortran standard. Those features can be found in F2008, 12.4.2.2

A procedure other than a statement function shall have an explicit interface if it is referenced and

a reference to the procedure appears
(a) with an argument keyword (12.5.2), or
(b) in a context that requires it to be pure,
the procedure has a dummy argument that
(a) has the ALLOCATABLE, ASYNCHRONOUS, OPTIONAL, POINTER, TARGET, VALUE, or VOLATILE attribute,
(b) is an assumed-shape array,
(c) is a coarray,
(d) is of a parameterized derived type, or
(e) is polymorphic,
the procedure has a result that
(a) is an array,
(b) is a pointer or is allocatable, or
(c) has a nonassumed type parameter value that is not a constant expression,
the procedure is elemental, or
the procedure has the BIND attribute.

Beyond those, an implicit interface will be sufficient and the interface block not required. But could still helpful.
So it looks like I'll be writing interfaces for all functions and subroutines as well as declaring the type of all my variables, because it's the RIGHT way to do things. I'll take the above hints and place all my interfaces together in an include file, perhaps, to cut down on repetitive and hard to find duplicate INTERFACE statements. [SOLVED] thanks
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Interface C++ With Fortran Zssfssz Programming 2 02-05-2012 05:17 PM
g77 in gcc 4.1.0 not found only gfortran fortran 95 compiler! I need fortran 77. TheBrick Linux - Software 3 07-04-2007 06:39 AM
Gnu Fortran versus Intel Fortran tomatoefish Linux - Software 3 02-20-2006 01:31 PM
does linux fortran compiler in fedora 4 support VAX FORTRAN? terrence Programming 17 08-31-2005 08:59 AM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration