Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use "use, only" in Fortran

Tags:

fortran

In python a statement like this:

from module import function354

makes sense, because Python is an interpreter language and I don't want python to load all 353 other functions.

Fortran has a similar construct:

use module, only : function354

Why would I use this? The compiler creates a *.mod file anyway, compiling all function. Is there any performance advantage (in compile or run time) if I specify an only-statement?

I can see that it might be helpful to avoid naming conflict, but other than that I don't really see the point.

like image 414
Stein Avatar asked Aug 04 '18 14:08

Stein


People also ask

What does use do in Fortran?

Any declared entities in a module that are intended to be used externally can be accessed by Fortran 90's 'USE' statement. The 'USE' statement can be included in any program unit, function or subroutine of a Fortran 90 code. Its syntax can be described in two forms.

How do I use statements in Fortran?

If a statement function is referenced, the defined calculations are inserted. The statement function argument list indicates the order, number, and type of arguments for the statement function. A statement function is referenced by using its name, along with its arguments, as an operand in an expression.

What is module in fortran 90?

Modules are containers for variables and subprograms. Modules are useful for creating global variables and grouping related subprograms. Modules consist of variables, data type definitions, subroutines, and functions.

What is a Fortran interface?

An abstract interface allows you to specify procedure characteristics and dummy argument names without declaring a procedure with those characteristics.


2 Answers

Two main reasons

  1. To avoid name conflicts, as you mention but seem to think unimportant. In a large, complex code anything that helps maintainability is a bonus, so use, only is a useful addition to aid this
  2. It automatically documents where an entity comes from. Given a large, complex code to read for the first time I can almost guarantee you'll be spending time working out what comes from which module, so use, only is a nice feature to aid code readability

You don't just want fast code - as important is maintainability, and more important is correctness!

like image 116
Ian Bush Avatar answered Nov 06 '22 00:11

Ian Bush


Disclaimer:

Some people has pointed (correctly) in the comments that use rename => name can be used independently of use, only. Like this:

use module, f354 => function354

I am keeping the answer because it could be useful as complementary information on the use statement for someone that lands here.


Original Answer:

Just for completeness, there is another feature that use, only provides, that is the ability to rename imported names with locally-bound names (only is not needed for this, see disclaimer above).

Like this:

use module, only: f354 => function354

That has proven useful for me in several different scenarios:

  1. Resolve specific name ambiguities when two used modules provide types or functions with the same name.
    Example:

    use module1, only: m1_init => initialize
    use module2, only: m2_init => initialize
    
  2. Use short names when the original name is too long, too cryptic or when it is used very often in your program.
    Example (from fgsl and lapack):

    use fgsl, only: stride => fgsl_aux_vector_double_stride      ! too long
    use lapack, only: solve => dgesvx                            ! too cryptic
    use iso_c_binding, only: i32 => c_int32_t, i64 => c_int64_t  ! too frequent
    
  3. Make it easier to refactor or use conditional compilation / templating:
    Example (from Fortran-lang/stdlib):

    use iso_fortran_env, only: sp => real32, dp => real64, qp => real128
    ! If we decide later to use iso_c_binding instead of iso_fortran_env:
    ! use iso_c_binding, only: sp => c_float, dp => c_double, qp => c_float128
    
like image 26
Rodrigo Rodrigues Avatar answered Nov 06 '22 00:11

Rodrigo Rodrigues