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

Modules and memory

pms549
Beginner
1,368 Views
The following two modules...

a)
MODULE a_module
CONTAINS
	SUBROUTINE do_something
		IMPLICIT NONE
		REAL, PARAMETER  :: a=1.,b=1.
		REAL :: c
		c=a+b
	END SUBROUTINE do_something

	SUBROUTINE do_something_else
		IMPLICIT NONE
		REAL, PARAMETER  :: a=1.,b=1.
		REAL :: c
		c=a+b
	END SUBROUTINE do_something_else
END MODULE a_module


b)
MODULE a_module
IMPLICIT NONE
REAL, PARAMETER  :: a=1.,b=1.
REAL :: c
CONTAINS
	SUBROUTINE do_something
		IMPLICIT NONE
		c=a+b
	END SUBROUTINE do_something

	SUBROUTINE do_something_else
		IMPLICIT NONE
		c=a+b
	END SUBROUTINE do_something_else
END MODULE a_module


... would behave in exactly the same way when used in the following (pointless) program:

PROGRAM a_program
USE a_module
IMPLICIT NONE
CALL do_something
CALL do_something_else
END PROGRAM a_program


However, would use of one of either a) or b) actually require less stack space and therefore be more memory efficient? Does a) require two separate copies of the variables a and b in memory; one for each procedure do_something and do_sometrhing else, whereas b) only needs one copy for the whole module? Also, as for variable c, is it the case that: For a) it is added to the stack as procedures do_something and do_something else are called, and removed when the procedures return, whereas in b) its memory is always reserved on the stack?

I realise this might be a compiler dependent question, but it would be handy if someone could give me a feeling for how allocation of stack space works with modules and procedures general.

Cheers, Paul.
0 Kudos
1 Reply
james1
Beginner
1,368 Views
Answers to this in general are difficult, as degree of compiler optimization plays a big part. In this case, the compiler may simply optimize all your floating point variables away, after all, nothing is done with them. If you were to do something with the results of the calculations, the compiler may simply store the result in a readonly data section of the executable, and depending upon how smart it is, that may be one or two copies.

Your result "c" is global in one case and local in the other case, thus in theory you would keep an extra piece of virtual memory in the second case and move data in and out of it, whereas in the other case everything could be kept in registers. Doubtful that this actually happens in your example as nothing is done with the result.

James
0 Kudos
Reply