相关疑难解决方法(0)

fortran运算符重载:函数或子例程

我最近将我的.f90代码更新为.f03,我期待看到加速,因为我的旧版本涉及许多分配和解除分配(7个3D阵列 - 45x45x45)在do循环内的每次迭代(总共4000个).使用派生类型,我在模拟开始时分配这些数组,并在结束时释放它们.我以为我会看到加速,但它实际上运行得慢得多(30分钟而不是23分钟).

我运行了一个分析器,看起来加/减/乘/除运算符需要相对较长的时间.除了标准变化的变化之外,就我所知,运营商是唯一的区别.我想知道这是否是因为函数在每次操作期间都返回了字段数量的新副本.

所以这是我的问题:如果我将函数更改为子例程以便这些字段通过引用传递(我认为?),它可能运行得更快吗?此外,如果这更快,更优选,那么为什么所有这些示例都显示运算符重载的函数而不是使用子例程?我觉得我错过了什么.

具有运算符重载的函数的引用:

http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/6-Fortran/operators.html

http://research.physics.illinois.edu/ElectronicStructure/498-s97/comp_info/overload.html

https://web.stanford.edu/class/me200c/tutorial_90/13_extra.html

https://www.ibm.com/developerworks/community/blogs/b10932b4-0edd-4e61-89f2-6e478ccba9aa/entry/object_oriented_fortran_does_fortran_support_operator_overloading55?lang=en

这是我的一个运营商的例子:

    function vectorVectorDivide(f,g) result(q)
      implicit none
      type(vectorField),intent(in) :: f,g
      type(vectorField) :: q
      q%x = f%x / g%x; q%y = f%y / g%y; q%z = f%z / g%z
      q%sx = f%sx; q%sy = f%sy; q%sz = f%sz
    end function
Run Code Online (Sandbox Code Playgroud)

任何帮助或信息都非常感谢!

fortran function operator-overloading gfortran subroutine

4
推荐指数
1
解决办法
898
查看次数

Fortran 中的多态数组分配例程

目标是创建一个单一的分配例程,它可以处理任何类型的一级分配。然后,我们的代码库可以使用标准化的错误捕获进行一次调用。

编译器错误如下:

generic_allocation.f08:32:27:

         call myAllocator ( array_int, source_int, lambda )
                           1
Error: Actual argument to ‘myarray’ at (1) must be polymorphic
generic_allocation.f08:33:27:

         call myAllocator ( array_real, source_real, lambda )
                           1
Error: Actual argument to ‘myarray’ at (1) must be polymorphic
Run Code Online (Sandbox Code Playgroud)

这段代码可以修改吗?

测试代码尝试分配一个整数数组,然后分配一个实数数组:

module mAllocator
    implicit none
contains
    subroutine myAllocator ( myArray, source_type, lambda )
        class ( * ), allocatable, intent ( inout ) :: myArray ( : )
        class ( * ),              intent ( in )    :: source_type
        integer,                  intent …
Run Code Online (Sandbox Code Playgroud)

arrays polymorphism fortran allocation gfortran

1
推荐指数
1
解决办法
506
查看次数