请考虑以下几点:
program main
integer, parameter :: n=10, m=20
integer ints(n,m)
real floats(m,n)
!... initialize ints
! ...
floats=transpose(ints)
!... do stuff with floats
end查看gfortran的文档,似乎transpose(ints)将返回一个整数数组,然后将其强制转换为实数。在这个操作中,编译器(gfortran)为转置的数组创建了一个临时数组,这似乎是一种浪费(用gfortran -O3 -Warray-temporaries -o test test.f90编译)。另请注意,如果将real数组“浮动”更改为integer数组,则警告将消失。
有没有办法在不生成临时数组的情况下做到这一点(对于任意类型)?(我也尝试过floats(:,:)=transpose(ints),因为我在某处读到它很重要... )。它在其他编译器上也是这样的吗?
发布于 2012-07-26 03:22:37
你可以试一试
floats = transpose(real(ints))但是,如果gfortran (或任何其他编译器)生成一个临时数组来实现这一点,我不会感到非常惊讶。如果没有的话,我会更惊讶的。
您也可以尝试
forall (J=1:N, K=1:M) floats(K, J) = real(ints(J, K))同样,如果编译器创建一个临时数组来实现这一点,我也不会感到惊讶。
发布于 2012-07-27 06:40:08
do i = 1, n
do j = 1, m
floats(j,i) = real(ints(i,j))
enddo
enddo您可以创建自己的transpose接口来处理不同的数据类型,尽管它必须是一个子例程而不是一个函数。
interface transpose_
module procedure transpose_ints_to_reals
end interface
subroutine transpose_ints_to_reals(ints_in, reals_out)
...
end subroutine
call transpose_(ints,floats)https://stackoverflow.com/questions/11655533
复制相似问题