Why does the Fortran coarray can not access an element in a different image

My following coarray code does not show the expected output

program caftest
  implicit none
  integer, dimension(5), codimension[*] :: a = 1
  
  ! Image 1  
  a(1)[1] = 11
  sync all
  
  ! Image 2
  a(2)[2] = a(1)[1]
  sync all
  
  ! Only Image 1 prints
  if (this_image() == 1) then
    print*, 'a(2)[2] (Image 2 a(2)):', a(2)[2]  ! Should be 11
    print*, 'a(1)[1] (Image 1 a(1)):', a(1)[1]  ! Should be 11
  end if

end program

output

 a(2)[2] (Image 2 a(2)):           1
 a(1)[1] (Image 1 a(1)):          11

However, modifying it provides the expected output

program caftest
  implicit none
  integer, dimension(5), codimension[*] :: a = 1
  
  ! Image 1
  if (this_image() == 1) a(1) = 11
  sync all
  
  ! Image 2
  if (this_image() == 2) a(2) = a(1)[1]
  sync all
  
  ! Only Image 1 prints
  if (this_image() == 1) then
    print*, 'a(2)[2] (Image 2 a(2)):', a(2)[2]  ! Should be 11
    print*, 'a(1)[1] (Image 1 a(1)):', a(1)[1]  ! Should be 11
  end if
end program

output

 a(2)[2] (Image 2 a(2)):          11
 a(1)[1] (Image 1 a(1)):          11

can’t we use the shorter version (first code) of accessing data among different images? I run the codes windows 10.

What compiler are you using? Both the NAG and Intel compilers give the expected result with your original example. (Normally having all images write to the same location on the same image will create a race condition, but since they’re all writing the same value it doesn’t matter. But you’d never want to do that in practice.)

1 Like