Skip to content

Simplify nested loops in Fortran with this easy to use multi-dimensional iterator

License

Notifications You must be signed in to change notification settings

grofz/multidim_loop_iterator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-Dimensional Loop Iterator for Fortran

The module provides a versatile iterator for multi-dimensional loops with customizable lower bounds, upper bounds, and strides for each dimension. It simplifies the process of iterating over multi-dimensional arrays by abstracting the complexities of nested loops.

Motivation

To traverse items of a multidimensional array, nested loops are usualy used in the following manner:

do i1 = ...
  do i2 = ...
    do i3 = ...
      do i4 = ...
        :
      end do
    end do
  end do
end do

Although this approach is fine in two or three dimensions, it becomes unfeasible for higher dimensions. The purpose of this module is to avoid these nested loops and provide a more efficient and elegant solution for multidimensional array traversal.

Usage

  1. File loop.f90 provides a module loop_mod that can be used in your Fortran program:

    use loop_mod, only : loop_iterator_t
  2. Creating an Iterator: Use constructor to create an iterator instance. Provide arrays for lower bounds, upper bounds, and strides for each dimension

    type(loop_iterator_t) :: my_iterator
    integer, dimension(3) :: lbounds = [1, 1, 1]
    integer, dimension(3) :: ubounds = [5, 10, 3]
    integer, dimension(3) :: strides = [1, 2, 1]
    
    my_iterator = loop_iterator_t(lbounds, ubounds, strides)
  3. Iterating:

Use has_next_element() method to check if there are more elements to iterate.

Use get_next_element() method to retrieve the indices of the next element in the loop.

Example

Here is a simple example of using the loop_mod iterator:

program example2
  use loop_mod
  implicit none

  type(loop_iterator_t) :: iterator
  integer, allocatable :: i(:)

  iterator = loop_iterator_t([1,5,1],[3,1,1],[1,-2,1])
  do
    if (.not. iterator%has_next_element()) exit
    i = iterator%get_next_element()
    print *, i
  end do

end program

Another example in file example.f90 showcases the capabilities of the loop_mod module. It iterates through dimensions and calculates the count of k-th closest neighbours of an n-dimensional hypercube.

Example example3.f90 illustrates the iteration over all neighbours of a particular array element with out-of-bounds checks.

A simple script to build these examples is provided. The script assumes the gfortran compiler is available.

./build.sh
./example

License

This Fortran module is provided under the MIT License.

About

Simplify nested loops in Fortran with this easy to use multi-dimensional iterator

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published