Coarray Examples

Small Coarray examples.

Some random collection of examples can be found at "Examples/test files" at http://users.physik.fu-berlin.de/~tburnus/coarray/README.txt

Examples from the testsuite

To check the correctness of the compiler, some coarray tests have been added. The main purpose is to check the diagnostics of the compiler (for invalid code) and the compile and run-time correctness. As such, the programs might be invalid (e.g. missing initialization for compile-time only tests) or incomplete. On the other hand, they might illustrate the use for some more special functions. They are located in the gfortran.dg/coarray/ directory and in files starting with coarray_ in the gcc/testsuite/gfortran.dg/ directory.

Hello World

A small program, which illustrates how to use coarrays. (A copy of this code example appears on Wikipedia at Examples.)

! Created by Tobias Burnus 2010.

program Hello_World
  implicit none
  integer :: i  ! Local variable
  character(len=20) :: name[*] ! scalar coarray
  ! Note: "name" is the local variable while "name[<index>]"
  ! accesses the variable on a remote image
 
  ! Interact with the user on Image 1
  if (this_image() == 1) then
    write(*,'(a)',advance='no') 'Enter your name: '
    read(*,'(a)') name
 
    ! Distribute information to other images
    do i = 2, num_images()
      name[i] = name
    end do
  end if
 
  sync all ! Barrier to make sure the data has arrived
 
  ! I/O from all nodes
  write(*,'(3a,i0)') 'Hello ',trim(name),' from image ', this_image()
end program Hello_world

The program above scales poorly because the loop that distributes information executes sequentially. Writing scalable programs often requires a sophisticated understanding of parallel algorithms, a detailed knowledge of the underlying network characteristics, and special tuning for application characteristics such as the size of data transfers. For most application developers, letting the compiler or runtime library decided the best algorithm proves more robust and high-performing. Fortran 2015 will offer collective communication subroutines that empower compiler and runtime library teams to encapsulate efficient parallel algorithms for collective communication and distributed computation in a set of collective subroutines. These subroutines and other new parallel programming features are summarized in a technical specification [4] that the Fortran standards committee has voted to incorporate into Fortran 2015. These enable the user to write a more efficient version of the above algorithm,

program Hello_World
  implicit none
  character(len=20) :: name[*] ! scalar coarray, one "name" for each image.
  ! Note: "name" is the local variable while "name[<index>]" accesses the
  ! variable in a specific image; "name[this_image()]" is the same as "name".

  ! Interact with the user on Image 1; execution for all others pass by.
  if (this_image() == 1) then   
    write(*,'(a)',advance='no') 'Enter your name: '
    read(*,'(a)') name
  end if
  ! Distribute information to all images
  call co_broadcast(name,source_image=1)

  ! I/O from all images, executing in any order, but each record written is intact. 
  write(*,'(3a,i0)') 'Hello ',trim(name),' from image ', this_image()
end program Hello_world

where the lack of explicit synchronization offers the potential for higher performance due to less coordination between the images. Furthermore, TS 18508 guarantees that "A transfer from an image cannot occur before the collective subroutine has been invoked on that image." This implies some partial synchronization inside co_broadcast, but could be higher performing than the "sync all" in the prior example. TS 18508 also incorporates several other new features that address issues targeted by the CAF 2.0 effort described in the Wikipedia article. Examples include teams of images and events.

None: CoarrayExample (last edited 2016-01-09 09:51:46 by PaulThomas)