ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_arrayReverse Module Reference

This module contains procedures and generic interfaces for reversing the order of elements in arrays of various types. More...

Data Types

interface  getReversed
 Generate and return an output array whose elements are the reversed-order elements of the input array, such that
  array = array(lenArray:1:-1),
  where lenArray = len(array) if array is a scalar character, or lenArray = size(array) is an array of rank 1. More...
 
interface  setReversed
 Reverse the order of the elements of the input array, such that
  array = array(lenArray:1:-1),
  where lenArray = len(array) if array is a scalar character, or lenArray = size(array) is an array of rank 1. More...
 

Variables

character(*, SK), parameter MODULE_NAME = "@pm_arrayReverse"
 

Detailed Description

This module contains procedures and generic interfaces for reversing the order of elements in arrays of various types.

Benchmarks:


Benchmark :: The runtime performance of setReversed vs. direct reversing

1! Test the performance of `getReversed()` vs. `setReversed()`.
2program benchmark
3
4 use iso_fortran_env, only: error_unit
6 use pm_distUnif, only: getUnifRand
7 use pm_bench, only: bench_type
8 use pm_kind, only: IK, RK, SK
9
10 implicit none
11
12 integer(IK) :: i
13 integer(IK) :: isize
14 integer(IK) :: fileUnit
15 integer(IK) , parameter :: nsize = 11_IK
16 integer(IK) , parameter :: NBENCH = 3_IK
17 integer(IK) :: arraySize(nsize)
18 real(RK) :: dummySum = 0._RK
19 real(RK) , allocatable :: array(:), arrayReverse(:)
20 type(bench_type) :: bench(NBENCH)
21
22 bench(1) = bench_type(name = SK_"getReversed", exec = getReversed, overhead = setOverhead)
23 bench(2) = bench_type(name = SK_"setReversed", exec = setReversed, overhead = setOverhead)
24 bench(3) = bench_type(name = SK_"setReversed_overwrite", exec = setReversed_overwrite , overhead = setOverhead)
25
26 arraySize = [( 2_IK**isize, isize = 1_IK, nsize )]
27
28 write(*,"(*(g0,:,' '))")
29 write(*,"(*(g0,:,' '))") "getReversed() vs. setReversed() vs. setReversed_overwrite()"
30 write(*,"(*(g0,:,' '))")
31
32 open(newunit = fileUnit, file = "main.out", status = "replace")
33
34 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1, NBENCH)
35
36 loopOverArraySize: do isize = 1, nsize
37
38 array = getUnifRand(1_IK, 9_IK, arraySize(isize))
39 call setResized(arrayReverse, arraySize(isize))
40 write(*,"(*(g0,:,' '))") "Benchmarking with rank", arraySize(isize)
41
42 do i = 1, NBENCH
43 bench(i)%timing = bench(i)%getTiming(minsec = 0.05_RK)
44 end do
45
46 write(fileUnit,"(*(g0,:,','))") arraySize(isize), (bench(i)%timing%mean, i = 1, NBENCH)
47
48 end do loopOverArraySize
49 write(*,"(*(g0,:,' '))") dummySum
50 write(*,"(*(g0,:,' '))")
51
52 close(fileUnit)
53
54contains
55
56 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 ! procedure wrappers.
58 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59
60 subroutine setOverhead()
61 call getDummy()
62 end subroutine
63
64 subroutine getDummy()
65 dummySum = dummySum + sum(array)
66 end subroutine
67
68 subroutine getReversed()
69 block
71 arrayReverse = getReversed(array)
72 call getDummy()
73 end block
74 end subroutine
75
76 subroutine setReversed()
77 block
79 call setReversed(array, arrayReverse)
80 call getDummy()
81 end block
82 end subroutine
83
84 subroutine setReversed_overwrite()
85 block
87 call setReversed(array)
88 call getDummy()
89 end block
90 end subroutine
91
92end program benchmark
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
Generate and return an output array whose elements are the reversed-order elements of the input array...
Reverse the order of the elements of the input array, such that   array = array(lenArray:1:-1),...
Generate and return an object of type timing_type containing the benchmark timing information and sta...
Definition: pm_bench.F90:574
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains procedures and generic interfaces for reversing the order of elements in arrays ...
This module contains abstract interfaces and types that facilitate benchmarking of different procedur...
Definition: pm_bench.F90:41
This module contains classes and procedures for computing various statistical quantities related to t...
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
Definition: pm_kind.F90:543
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
This is the class for creating benchmark and performance-profiling objects.
Definition: pm_bench.F90:386
subroutine bench(sort, arraySize)

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Postprocessing of the benchmark output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6
7fontsize = 14
8
9methods = ["setReversed_overwrite", "setReversed", "getReversed"]
10
11df = pd.read_csv("main.out")
12
13
16
17ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
18ax = plt.subplot()
19
20for method in methods:
21 plt.plot( df["arraySize"].values
22 , df[method].values
23 , linewidth = 2
24 )
25
26plt.xticks(fontsize = fontsize)
27plt.yticks(fontsize = fontsize)
28ax.set_xlabel("Array Size", fontsize = fontsize)
29ax.set_ylabel("Runtime [ seconds ]", fontsize = fontsize)
30ax.set_title("getReversed() vs. setReversed() vs. setReversed_overwrite()\nLower is better.", fontsize = fontsize)
31ax.set_xscale("log")
32ax.set_yscale("log")
33plt.minorticks_on()
34plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
35ax.tick_params(axis = "y", which = "minor")
36ax.tick_params(axis = "x", which = "minor")
37ax.legend ( methods
38 #, loc='center left'
39 #, bbox_to_anchor=(1, 0.5)
40 , fontsize = fontsize
41 )
42
43plt.tight_layout()
44plt.savefig("benchmark.getReversed_vs_setReversed.runtime.png")
45
46
49
50ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
51ax = plt.subplot()
52
53plt.plot( df["arraySize"].values
54 , np.ones(len(df["arraySize"].values))
55 #, linestyle = "--"
56 #, color = "black"
57 , linewidth = 2
58 )
59plt.plot( df["arraySize"].values
60 , df["setReversed"].values / df["setReversed_overwrite"].values
61 , linewidth = 2
62 )
63plt.plot( df["arraySize"].values
64 , df["getReversed"].values / df["setReversed_overwrite"].values
65 , linewidth = 2
66 )
67
68plt.xticks(fontsize = fontsize)
69plt.yticks(fontsize = fontsize)
70ax.set_xlabel("Array Size", fontsize = fontsize)
71ax.set_ylabel("Runtime compared to setReversed_overwrite()", fontsize = fontsize)
72ax.set_title("getReversed / setReversed_overwrite vs. setReversed / setReversed_overwrite\nLower means faster. Lower than 1 means faster than setReversed_overwrite().", fontsize = fontsize)
73ax.set_xscale("log")
74#ax.set_yscale("log")
75plt.minorticks_on()
76plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
77ax.tick_params(axis = "y", which = "minor")
78ax.tick_params(axis = "x", which = "minor")
79ax.legend ( ["setReversed_overwrite", "setReversed", "getReversed"]
80 #, bbox_to_anchor = (1, 0.5)
81 #, loc = "center left"
82 , fontsize = fontsize
83 )
84
85plt.tight_layout()
86plt.savefig("benchmark.getReversed_vs_setReversed.runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface setReversed with in-place array reversal (corresponding to setReversed_overwrite() in the benchmark) tend to be significantly faster than the functional-interface procedures getReversed.
    The reason is likely the extra copy required with the functional interface.
Test:
test_pm_arrayReverse


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Fatemeh Bagheri, Wednesday 12:20 AM, October 13, 2021, Dallas, TX

Variable Documentation

◆ MODULE_NAME

character(*, SK), parameter pm_arrayReverse::MODULE_NAME = "@pm_arrayReverse"

Definition at line 49 of file pm_arrayReverse.F90.