Test of returning array efficiency from Fortran subprograms

Fortran has two kinds of subprograms: subroutine and function. Usually, subroutine is a combination of several procedures generating side effects without returning values, while the purpose of function is to return values after some operations. In fact, returning values can be implemented in a subroutine by setting some of the formal parameter properties as intent(out) or intent(inout). Compared to function calling, an inconvenience for returning values from a subroutine is that we can’t adopt the form of natural assignment like this:

A = MathFunc(...)

Further, if the formula on the right side of the assignment involves several concatenated function calls, compiler optimization could be enabled for it:

A = MathFunc1(...) * MathFunc2(...) – Mathfunc3(...)

However, when considering about returning a large chunk of data from a subprogram, there may be some efficiency problems with function call. The return value of a function call is created on the function’s own stack when entering the function. When the function returns, the data held in the memory will be coped out into an external variable, like the ‘A‘ on the left of an assignment as shown in the above. After that, the data in the stack will be popped out and lost. It is obvious to see that if the data dimension is huge, for example, elemental or DOF data in a FEM analysis, this data copy operation will be time-consuming.

On the contrary, if the large matrix is returned as a subroutine parameter (as a function parameter is also ok) with its intent property set to out or inout, there will be no such a data copy operation. This is because in Fortran, the transfer of parameters into a subprogram is governed by the mechanism of passing by reference, which is different from the default behavior in C or C++.

To verify the above assumption, a test has been performed. In this test, two subprograms were written and both of them return a 100000×100000 matrix, with the difference that one of the subprogram is a function, in which the matrix is returned by copy and the other subprogram is a subroutine, in which the matrix is returned by reference as a parameter. The two subprograms are:

subroutine ret_by_para(AA)
  real(8), dimension(10000,10000), intent(out) :: AA
  integer m, n
  do m = 1, 10000
     do n = 1, 10000
        AA(m,n) = 10.
     end do
  end do
end subroutine ret_by_para

function ret_by_func()
  real(8), dimension(10000,10000) :: ret_by_func
  integer m, n
  do m = 1, 10000
     do n = 1, 10000
        ret_by_func(m,n) = 10.
     end do
  end do
end function ret_by_func

By calling these two subprograms for 1 and 10 times respectively, the running time collected by gprof is summarized as follows:

Call for 1 time


% time


Cumulative seconds


Self seconds


name


72.29


4.46


4.46


ret_by_func.1512


27.71


6.17


1.71


ret_by_para.1516

Call for 10 times


% time


Cumulative seconds


Self seconds


name


82.12


71.58


71.58


ret_by_func.1512


17.88


87.17


15.59


ret_by_para.1516

It can be seen that the time cost by returning value from a function is much larger than by returning reference from a subroutine. And this difference increases with the calling times. Therefore, it is suggested that if a parameter or return value is a large matrix, it had better be transferred by reference as a subroutine parameter instead of by function return value.

Test of returning array efficiency from Fortran subprograms,布布扣,bubuko.com

时间: 2024-11-04 23:52:43

Test of returning array efficiency from Fortran subprograms的相关文章

Returning array from function in C

以下为了通俗易懂,使用意译. I've here very intersting discussion about the best and common ways to return an array from a function..我最近很热衷于讨论从函数返回数组的最佳及常用方法Some solutions to use output parameter and copy the value of the array into the value of this output parame

FORTRAN & MATLAB 混合编程

[email protected] 第一部分:Fortran调用Matlab引擎 1  什么是Matlab引擎 所谓Matlab引擎(engine),是指一组Matlab提供的接口函数,支持C/C++.Fortran等语言,通过这些接口函数,用户可以在其它编程环境中实现对Matlab的控制.可以主要功能有: ★ 打开/关闭一个Matlab对话: ★ 向Matlab环境发送命令字符串: ★ 从Matlab环境中读取数据: ★ 向Matlab环境中写入数据. 与其它各种接口相比,引擎所提供的Matl

FindBugs规则整理

FindBugs规则整理 FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查.没有合理关闭资源.字符串相同判断错(==,而不是equals)等 一.Security 关于代码安全性防护 1.Dm: Hardcoded constant database password (DMI_CONSTANT_DB_PASSWORD) 代码中创建DB的密码时采

findbugs规则

FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查.没有合理关闭资源.字符串相同判断错(==,而不是equals)等 一.Security 关于代码安全性防护 1.Dm: Hardcoded constant database password (DMI_CONSTANT_DB_PASSWORD) 代码中创建DB的密码时采用了写死的密码. 2.Dm

FindBug使用[1]

FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查.没有合理关闭资源.字符串相同判断错(==,而不是equals)等 1.Dm: Hardcoded constant database password (DMI_CONSTANT_DB_PASSWORD) 代码中创建DB的密码时采用了写死的密码. 2.Dm: Empty database pass

Twitter OA prepare: Equilibrium index of an array

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A: A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0 3 is an equil

fortran与c

http://asc.2dark.org/node/45一.说明 本文多数内容是我读彭国伦<Fortran 95 程序设计>的笔记.只读到第九章,主要是3~9 章,都是最基本的用法(原书共16章).这里主要摘录了我看书过程中总结的一些Fortran和C不 同的地方,主要是语法方面.希望这份笔记能够给学过C但没有接触过Fortran的同学带去一些帮 助.要想得更清楚些,推荐看一下原书,觉得作者真的写得很好,很清楚:如果有C语言的基础, 看完前九应该很快的,花一两天就行了.觉得如果耐心看完本文,基

Swift Generic Array &#39;not identical&#39; error

Arrays in Swift are value types. That means that data is copied when passed into your exchangemethod, but you are trying to modify the copy to affect the original version. Instead you should do one of two things: 1. Define data as an inout parameter:

How to debug Fortran programs using gdb

Previously, I thought the debugging functionality provided by gdb for Fortran program was quite limited due to experiences of a couple of failed attempts to print allocatable arrays. However, having given several another tries today, I discovered tha