Fortran 将共阵列子阵列传递给函数会导致阵列的错误部分

Fortran 将共阵列子阵列传递给函数会导致阵列的错误部分,fortran,parameter-passing,intel-fortran,fortran-coarrays,Fortran,Parameter Passing,Intel Fortran,Fortran Coarrays,我试图理解如何将多维共数组的一部分传递给函数。我想使用如下函数: function get_int_vec(vec_int_2get, rank) result(ret_val) implicit none integer, dimension(:), codimension[*], intent(in) :: vec_int_2get integer, intent(in) :: rank integer, allocatable, d

我试图理解如何将多维共数组的一部分传递给函数。我想使用如下函数:

    function get_int_vec(vec_int_2get, rank) result(ret_val)
      implicit none
      integer,  dimension(:), codimension[*], intent(in) :: vec_int_2get
      integer, intent(in) :: rank
      integer, allocatable, dimension(:) :: ret_val 

      ret_val = vec_int_2get(:)[rank]

    end function ! get_int_vec
它可以很好地获得整个阵列。 但是当经过一片丛林时,比如:

    vec_getA(:) = get_int_vec(matrix_A(n, :), rank)
其中
矩阵A
声明为

    integer, dimension(:, :), codimension[:],  allocatable :: matrix_A
如果分配得当,我总是得到矩阵A的第一列,而不是第n列

他们说:

“使用
-fcoarray=lib
[…]属于不可分配的coarray伪参数的令牌和偏移量作为隐藏参数沿着字符长度隐藏参数传递。令牌是标识coarray的不透明指针,偏移量是一种传递值整数
C\u PTRDIFF\u T
,表示coarr基址之间的字节偏移量ay和传递的标量或传递数组的第一个元素。“

因此,我希望函数也能很好地处理矩阵切片,因为从矩阵开始的偏移量应该传递给函数

我做错什么了


如果可能会引起一些兴趣:我使用的是英特尔并行工作室XE 2018群集版,而不是OpenCoArray版本的CoArray。这似乎是英特尔ifort 2018中的一个bug。代码的语法似乎符合Fortran 2008标准()。使用OpenCoArray和GFortran编译的相同代码产生了预期的结果。以下是对您的问题的一个(不是很小,但)有效实施:

module coarrayFunc
    implicit none
contains
    function get_int_vec(vec_int_2get, rank) result(ret_val)
        implicit none
        integer,  dimension(:), codimension[*], intent(in) :: vec_int_2get
        integer, intent(in) :: rank
        integer :: ret_val(3)
        !integer :: ret_val(size(vec_int_2get)) ! using this results in internal compiler error when compiled with ifort.
        !integer, allocatable :: ret_val(:) ! both ifort and OpenCoarrays (GFortran) compile with this declaration, however both ifort give wrong results.
        ret_val = vec_int_2get(:)[rank]
    end function ! get_int_vec
end module coarrayFunc

program testNoncontiguousCoarray
    use coarrayFunc
    implicit none
    integer, allocatable    :: matrix_A(:,:)[:], dummy(:)
    integer                 :: rank, n, i, j, image
    integer, parameter      :: ilower = 1, iupper = 5
    integer, parameter      :: jlower = 1, jupper = 3

    allocate( matrix_A(ilower:iupper,jlower:jupper)[*] )

    do i = ilower, iupper
        do j = jlower, jupper
            matrix_A(i,j) = this_image()*100 + i*10 + j
        end do
    end do

    ! print matrix_A on each image
    sync all
    if (this_image()==1) then
        do image = 1, num_images()
            write(*,"(*(g0))") "matrix_A on image ", image, ":"
            do i = ilower, iupper
                write(*,"(*(g8.1))") matrix_A(i,:)[image]
            end do
            write(*,"(*(g0))")
        end do
        sync images(*)
    else
        sync images(1)
    end if
    sync all

    n = iupper
    rank = this_image()
    !rank = num_images()

    sync all
    if (this_image()==1) then
        write(*,"(*(g0))")
        write(*,"(*(g0))") "On all images: "
        write(*,"(*(g0))") "n = ", n
        write(*,"(*(g0))")
    end if
    sync all

    if (this_image()==1) then
        write(*,"(*(g0,' '))") "On Image ", this_image(), ": matrix_A( n =", n, ", : )[",rank,"] = ", matrix_A(n,:)[rank]
        dummy = get_int_vec(matrix_A(n,:), rank)
        write(*,"(*(g0,' '))") "On Image ", this_image(), ": get_int_vec( matrix_A( n =", n, ", : ), rank =", rank, ") = " &
                               , dummy
    else
        sync images (this_image()-1)
        write(*,"(*(g0,' '))") "On Image ", this_image(), ": matrix_A( n =", n, ", : )[",rank,"] = ", matrix_A(n,:)[rank]
        dummy = get_int_vec(matrix_A(n,:), rank)
        write(*,"(*(g0,' '))") "On Image ", this_image(), ": get_int_vec( matrix_A( n =", n, ", : ), rank =", rank, ") = " &
                               , dummy
    end if
    call sleep(1)
    if (this_image()<num_images()) sync images (this_image()+1)

end program testNoncontiguousCoarray
输出一个人期望得到的结果。请注意,我已经调整了原始函数,使该函数的结果是一个自动数组,而不是可分配的(这似乎是OpenCoArray中的另一个bug,即可分配的输出返回错误的结果)。使用ifort 2018 Windows运行相同的代码会重现您在自己的实现中观察到的错误:

>set FOR_COARRAY_NUM_IMAGES=4

>ifort /Qcoarray=shared testNoncontiguousCoarray.f90 -o run.exe
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 18.0.2.185 Build 20180210
Copyright (C) 1985-2018 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.13.26129.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:run.exe
-subsystem:console
testNoncontiguousCoarray.obj

>run.exe
matrix_A on image 1:
     111     112     113
     121     122     123
     131     132     133
     141     142     143
     151     152     153

matrix_A on image 2:
     211     212     213
     221     222     223
     231     232     233
     241     242     243
     251     252     253

matrix_A on image 3:
     311     312     313
     321     322     323
     331     332     333
     341     342     343
     351     352     353

matrix_A on image 4:
     411     412     413
     421     422     423
     431     432     433
     441     442     443
     451     452     453


On all images:
n = 5

On Image  1 : matrix_A( n = 5 , : )[ 1 ] =  151 152 153
On Image  1 : get_int_vec( matrix_A( n = 5 , : ), rank = 1 ) =  111 112 113
On Image  2 : matrix_A( n = 5 , : )[ 2 ] =  251 252 253
On Image  2 : get_int_vec( matrix_A( n = 5 , : ), rank = 2 ) =  211 212 213
On Image  3 : matrix_A( n = 5 , : )[ 3 ] =  351 352 353
On Image  3 : get_int_vec( matrix_A( n = 5 , : ), rank = 3 ) =  311 312 313
On Image  4 : matrix_A( n = 5 , : )[ 4 ] =  451 452 453
On Image  4 : get_int_vec( matrix_A( n = 5 , : ), rank = 4 ) =  411 412 413

如在你的问题的评论中所提到的,考虑编写一个代码的最小工作示例,该代码复制你所得到的错误,并提交一张到英特尔iFuver编译器团队的票,以获得一个潜在的解决方案。您使用不相关的标记或没有人订阅的标记。标记在堆栈溢出中非常重要。所有Fortran问题都使用tag。对不起,请使用tag。我使用的是英特尔版本的CoArray,而不是OpenCoArray,正如我所说的,我使用的是英特尔编译器。好了,我现在可以看到混淆的原因了。这些约定不是Fortran标准约定,它们是特定于gfortran编译器的约定,当您停留在标准Fortran中时,它们实际上不应该太重要。您完全正确,我删掉了这一部分。谢谢你的评论,但我现在想知道引用是否相关,因为,正如你所说,这是一个特定于编译器的约定,我甚至没有使用这个编译器。我认为它不是真正相关的。与此相关的是Fortran标准文档。

>set FOR_COARRAY_NUM_IMAGES=4

>ifort /Qcoarray=shared testNoncontiguousCoarray.f90 -o run.exe
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 18.0.2.185 Build 20180210
Copyright (C) 1985-2018 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.13.26129.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:run.exe
-subsystem:console
testNoncontiguousCoarray.obj

>run.exe
matrix_A on image 1:
     111     112     113
     121     122     123
     131     132     133
     141     142     143
     151     152     153

matrix_A on image 2:
     211     212     213
     221     222     223
     231     232     233
     241     242     243
     251     252     253

matrix_A on image 3:
     311     312     313
     321     322     323
     331     332     333
     341     342     343
     351     352     353

matrix_A on image 4:
     411     412     413
     421     422     423
     431     432     433
     441     442     443
     451     452     453


On all images:
n = 5

On Image  1 : matrix_A( n = 5 , : )[ 1 ] =  151 152 153
On Image  1 : get_int_vec( matrix_A( n = 5 , : ), rank = 1 ) =  111 112 113
On Image  2 : matrix_A( n = 5 , : )[ 2 ] =  251 252 253
On Image  2 : get_int_vec( matrix_A( n = 5 , : ), rank = 2 ) =  211 212 213
On Image  3 : matrix_A( n = 5 , : )[ 3 ] =  351 352 353
On Image  3 : get_int_vec( matrix_A( n = 5 , : ), rank = 3 ) =  311 312 313
On Image  4 : matrix_A( n = 5 , : )[ 4 ] =  451 452 453
On Image  4 : get_int_vec( matrix_A( n = 5 , : ), rank = 4 ) =  411 412 413