From b90c52e85e9be83d164ba6f801b01fb30097156f Mon Sep 17 00:00:00 2001 From: Josef Hrabal <hrabal.josef@gmail.com> Date: Sat, 25 Feb 2017 13:32:01 -0500 Subject: [PATCH] Added OpenCoarrays: docs.it4i/salomon/software/numerical-languages/opencoarrays.md Added 'Fortran' section under salomon/software/numerical-languages Added 'OpenCoarrays' page under /salomon/software/numerical-language/fortran --- .../numerical-languages/opencoarrays.md | 121 ++++++++++++++++++ mkdocs.yml | 2 + 2 files changed, 123 insertions(+) create mode 100644 docs.it4i/salomon/software/numerical-languages/opencoarrays.md diff --git a/docs.it4i/salomon/software/numerical-languages/opencoarrays.md b/docs.it4i/salomon/software/numerical-languages/opencoarrays.md new file mode 100644 index 000000000..d708bf21f --- /dev/null +++ b/docs.it4i/salomon/software/numerical-languages/opencoarrays.md @@ -0,0 +1,121 @@ +# OpenCoarrays + +## Introduction + +Coarray Fortran (CAF) is an extension of Fortran language and offers a simple interface for parallel processing and memory sharing. +The advantage is that only small changes are required to convert existing Fortran code to support a robust and potentially efficient parallelism. + +A CAF program is interpreted as if it was replicated a number of times and all copies were executed asynchronously. +The number of copies is decided at execution time. Each copy (called *image*) has its own private variables. +The variable syntax of Fortran language is extended with indexes in square brackets (called *co-dimension*), which represents a reference to data distributed across images. + +By default, the CAF is using Message Passing Interface (MPI) for lower-level communication, so there are some similarities with MPI. + +Read more on <http://www.opencoarrays.org/> + +### Indexing of coarray images + +Indexing of individual images can be shown on the simple *Hello World* program: + +```fortran + program hello_world + implicit none + print *, 'Hello world from image ', this_image() , 'of', num_images() + end program hello_world +``` +* num_images() - returns the number of all images +* this_image() - returns the image index - numbered from 1 to num_images() + +### Co-dimension variables declaration + +Coarray variables can be declared with the **codimension[*]** attribute or by adding trailing index **[*]** after the variable name. +Notice, the ***** character always has to be in the square brackets. + +```fortran + integer, codimension[*] :: scalar + integer :: scalar[*] + real, dimension(64), codimension[*] :: vector + real :: vector(64)[*] +``` + +### Images synchronization + +Because each image is running on its own, the image synchronization is needed to ensure, that all altered data are distributed to all images. +Synchronization can be done across all images or only between selected images. Be aware, that selective synchronization can lead to the race condition problems like deadlock. + +Example program: + +```fortran + program synchronization_test + implicit none + integer :: i ! Local variable + integer :: numbers[*] ! Scalar coarray + + ! Genereate random number on image 1 + if (this_image() == 1) then + numbers = floor(rand(1) * 1000) + ! Distribute information to other images + do i = 2, num_images() + numbers[i] = numbers + end do + end if + + sync all ! Barrier to synchronize all images + + print *, 'The random number is', numbers + end program synchronization_test +``` + +* sync all - Synchronize all images between each other +* sync images(*) - Synchronize this image to all other +* sync images(*index*) - Synchronize this image to image with *index* + +!!! note + **number** is the local variable while **number[*index*]** accesses the variable in a specific image. + **number[this_image()]** is the same as **number**. + +### Compiling CAF program + +The preferred method for compiling a CAF program is by invoking the *caf* compiler wrapper. +The above mentioned *Hello World* program can be compiled as follows: + +```bash + $ caf hello_world.f90 -o hello_world.x +``` + +!!! warning + The input file extension has to be **.f90** or **.F90** to be interpreted as *Fortran 90*. + If the input file extension is **.f** or **.F** the source code will be interpreted as *Fortran 77*. + +Another method for compiling is by invoking the *mpif90* compiler wrapper directly: + +```bash + $ mpif90 hello_world.f90 -o hello_world.x -L $CAF_LIBRARY_PATH -fcoarray=lib -lcaf_mpi +``` + + +### Running CAF program + +The preferred method for running a CAF program is by invoking the *cafrun* wrapper: + +```bash + $ cafrun -np 4 ./hello_world.x + Hello world from image 1 of 4 + Hello world from image 2 of 4 + Hello world from image 3 of 4 + Hello world from image 4 of 4 +``` + +where **-np 4** is number of images to run. + +Another method is using the *mpiexec* directly: + +```bash + $ mpiexec -np 4 ./synchronization_test.x + The random number is 242 + The random number is 242 + The random number is 242 + The random number is 242 +``` + +The parameters of **cafrun** and **mpiexec** are the same. \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 0c557765e..216f2b109 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -116,6 +116,8 @@ pages: - Matlab: salomon/software/numerical-languages/matlab.md - Octave: salomon/software/numerical-languages/octave.md - R: salomon/software/numerical-languages/r.md + - 'Fortran': + - OpenCoarrays: salomon/software/numerical-languages/opencoarrays.md - Operating System: salomon/software/operating-system.md - Anselm Software: - Available Modules: modules-anselm.md -- GitLab