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

Compiler and execution performance with placement of use association

Norman_K_
New Contributor I
264 Views

I wonder if the community can help/advise me: does it matter where I declare use association?

Consider there two alternatives:

module mod_a
use mod_c
contains
subroutine mod_a_sub
!do something with mod_c
end subroutine mod_a_sub
end module mod_a

and this (in which use of mod_c is restricted to just the module subroutine...

module mod_a
contains
subroutine mod_a_sub
use mod_c
!do something
end subroutine mod_a_sub
end module mod_a

OK, the obvious; in the second example I restrict potential damage to public data in mod_c to just what I do in mod_a_sub, but in the first example other functions and subroutines all have (potentially lethal) access to public bits of mod_c.

In very big modules with large numbers of module procedures, is it 'better' in any sense to litter the use statements as and only when needed?  It is certainly clearer to put them all in one place, but is compiler or execution performance affected? 

BTW could we have a compiler warning to detect use association declared at module level which is (unnecessarily) repeated by a declaration of use association to the same module at module procedure level.?

Finally, what about submodules? Is there any advantage to 'hiding' use association down in submodule procedures, where possible?

So three questions, and I apologise in advance if they are silly questions.

Thanks

0 Kudos
4 Replies
andrew_4619
Honored Contributor II
264 Views

It is,  more work but I do USE modname, ONLY: a,b,c,d all the time and mostly within the routine that actually uses the stuff. This stop lots of name pollution potential and also when you look at a the header of a subroutine you can see at a glance where everything that is actually used comes from

0 Kudos
Steve_Lionel
Honored Contributor III
264 Views

Execution performance is not affected. Compiler performance is affected by the number of names brought in from a module and percolated down through USE levels. The way to go here really depends on the structure of your application and whether "lowering" the level of USE statements harms maintainability. 

Submodules are great for separating the implementation from the interface, so as to avoid "compilation cascades" if you change the implementation of a procedure. Andrew's mode of USE, ONLY is also good practice as it helps document what is used where.

0 Kudos
JVanB
Valued Contributor II
264 Views

The two examples of Quote #1 are semantically different in that in the first subroutine mod_a_sub gets access to the names in mod_c by host association and in the second by use association. The difference is that with host association mod_a_sub can override names in mod_c without rename clauses whereas with use association rename or only clauses would be necessary to override names in mod_c.

 

0 Kudos
Norman_K_
New Contributor I
264 Views

Thanks

The main points are well made: 1) use vs host association and name override, 2) be explicit by "use name. only: foo" partly as a form of code documentation and 3) no effect on execution performance but possible effect on compilation speed.

Your advice is much appreciated.

0 Kudos
Reply