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

This module contains procedures and generic interfaces for removing a pattern from arrays of various types at the specified instances of occurrence of pattern. More...

Data Types

interface  getRemoved
 Generate and return an allocatable array containing the remaining parts of the input array as a sequence after removing the input pattern at the requested occurrences. More...
 
interface  setRemoved
 Return the remaining parts of the input array as a sequence after removing the input pattern at the requested occurrences. More...
 

Variables

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

Detailed Description

This module contains procedures and generic interfaces for removing a pattern from arrays of various types at the specified instances of occurrence of pattern.

Benchmarks:


Benchmark :: The runtime performance of setRemoved for scalar vs. vector input pattern argument.

1! Test the performance of setRemoved() with a vector `pattern` vs. scalar `pattern`.
2program benchmark
3
4 use iso_fortran_env, only: error_unit
5 use pm_bench, only: bench_type
6 use pm_kind, only: IK, LK, RK, SK
7
8 implicit none
9
10 integer(IK) :: i
11 integer(IK) :: isize
12 integer(IK) :: fileUnit
13 integer(IK) , parameter :: NSIZE = 15_IK
14 integer(IK) , parameter :: NBENCH = 2_IK
15 integer(IK) :: arraySize(NSIZE)
16 logical(LK) :: dummy = .true._LK
17 integer(IK) , allocatable :: Array(:)
18 integer(IK) , parameter :: pattern(1) = 0_IK
19 type(bench_type) :: bench(NBENCH)
20
21 bench(1) = bench_type(name = SK_"scalarPattern", exec = scalarPattern , overhead = setOverhead)
22 bench(2) = bench_type(name = SK_"vectorPattern", exec = vectorPattern , overhead = setOverhead)
23
24 arraySize = [( 2_IK**isize, isize = 1_IK, NSIZE )]
25
26 write(*,"(*(g0,:,' '))")
27 write(*,"(*(g0,:,' '))") "scalarPattern() vs. vectorPattern()"
28 write(*,"(*(g0,:,' '))")
29
30 open(newunit = fileUnit, file = "main.out", status = "replace")
31
32 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1_IK, NBENCH)
33
34 loopOverArraySize: do isize = 1, NSIZE
35
36 write(*,"(*(g0,:,' '))") "Benchmarking with size", arraySize(isize)
37 allocate(Array(arraySize(isize)))
38
39 do i = 1_IK, NBENCH
40 bench(i)%timing = bench(i)%getTiming(minsec = 0.05_RK)
41 end do
42
43 deallocate(Array)
44 write(fileUnit,"(*(g0,:,','))") arraySize(isize), (bench(i)%timing%mean, i = 1_IK, NBENCH)
45
46 end do loopOverArraySize
47 write(*,"(*(g0,:,' '))") dummy
48 write(*,"(*(g0,:,' '))")
49
50 close(fileUnit)
51
52contains
53
54 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 ! procedure wrappers.
56 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
58 subroutine setOverhead()
59 call initialize()
60 call finalize()
61 end subroutine
62
63 subroutine initialize()
64 Array(:) = 1_IK
65 end subroutine
66
67 subroutine finalize()
68 dummy = dummy .and. size(Array, kind = IK) < 1_IK
69 end subroutine
70
71 subroutine scalarPattern()
72 use pm_arrayRemove, only: setRemoved
73 call initialize()
74 call setRemoved(Array, pattern(1))
75 call finalize()
76 end subroutine
77
78 subroutine vectorPattern()
79 block
80 use pm_arrayRemove, only: setRemoved
81 call initialize()
82 call setRemoved(Array, pattern)
83 call finalize()
84 end block
85 end subroutine
86
87end program benchmark
Return the remaining parts of the input array as a sequence after removing the input pattern at the r...
Generate and return an object of type timing_type containing the benchmark timing information and sta...
Definition: pm_bench.F90:574
This module contains procedures and generic interfaces for removing a pattern from arrays of various ...
This module contains abstract interfaces and types that facilitate benchmarking of different procedur...
Definition: pm_bench.F90:41
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 LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
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

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 = ["scalarPattern", "vectorPattern"]
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("Removing array segments with pattern(1) (scalar) vs. pattern(1:1) (vector).\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.setRemoved-scalarPattern_vs_vectorPattern.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["vectorPattern"].values / df["scalarPattern"].values
61 , linewidth = 2
62 )
63
64plt.xticks(fontsize = fontsize)
65plt.yticks(fontsize = fontsize)
66ax.set_xlabel("Array Size", fontsize = fontsize)
67ax.set_ylabel("Runtime compared to scalarPattern()", fontsize = fontsize)
68ax.set_title("Runtime Ratio: Remove pattern(1:1) / Remove pattern(1).\nLower means faster. Lower than 1 means faster than scalarPattern.", fontsize = fontsize)
69ax.set_xscale("log")
70#ax.set_yscale("log")
71plt.minorticks_on()
72plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
73ax.tick_params(axis = "y", which = "minor")
74ax.tick_params(axis = "x", which = "minor")
75ax.legend ( ["scalarPattern", "vectorPattern"]
76 #, bbox_to_anchor = (1, 0.5)
77 #, loc = "center left"
78 , fontsize = fontsize
79 )
80
81plt.tight_layout()
82plt.savefig("benchmark.setRemoved-scalarPattern_vs_vectorPattern.runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface setRemoved take both scalar and vector pattern arguments.
    As evidenced by the above benchmark, when the input pattern is vector of length 1, it is much faster, up to 4X, to pass pattern as a scalar instead of a whole array of length 1.
    Note that this benchmark is likely irrelevant to removing substrings from Fortran strings.


Benchmark :: The runtime performance of getRemoved vs. setRemoved

1! Test the performance of `getRemoved()` vs. `setRemoved()`.
2program benchmark
3
4 use iso_fortran_env, only: error_unit
5 use pm_kind, only: IK, LK, RK, SK
6 use pm_bench, only: bench_type
7
8 implicit none
9
10 integer(IK) :: i
11 integer(IK) :: isize
12 integer(IK) :: fileUnit
13 integer(IK) , parameter :: NSIZE = 12_IK
14 integer(IK) :: arraySize(NSIZE)
15 logical(LK) :: dummy = .true._LK
16 character(:, SK), allocatable :: array
17 character(*, SK), parameter :: pattern = "a"
18 type(bench_type), allocatable :: bench(:)
19
20 bench = [ bench_type(name = SK_"setRemoved", exec = setRemoved, overhead = setOverhead) &
21 , bench_type(name = SK_"getRemoved", exec = getRemoved, overhead = setOverhead) &
22 ]
23 arraySize = [( 2_IK**isize, isize = 1_IK, NSIZE )]
24
25 write(*,"(*(g0,:,' '))")
26 write(*,"(*(g0,:,' '))") "setRemoved() vs. getRemoved()"
27 write(*,"(*(g0,:,' '))")
28
29 open(newunit = fileUnit, file = "main.out", status = "replace")
30
31 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1, size(bench, 1, IK))
32
33 loopOverArraySize: do isize = 1, NSIZE
34
35 write(*,"(*(g0,:,' '))") "Benchmarking with size", arraySize(isize)
36
37 do i = 1, size(bench, 1, IK)
38 bench(i)%timing = bench(i)%getTiming(minsec = 0.05_RK)
39 end do
40
41 write(fileUnit,"(*(g0,:,','))") arraySize(isize), (bench(i)%timing%mean, i = 1, size(bench, 1, IK))
42
43 end do loopOverArraySize
44 write(*,"(*(g0,:,' '))") dummy
45 write(*,"(*(g0,:,' '))")
46
47 close(fileUnit)
48
49contains
50
51 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 ! procedure wrappers.
53 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54
55 subroutine setOverhead()
56 call initialize()
57 call finalize()
58 end subroutine
59
60 subroutine initialize()
61 array = repeat(pattern, arraySize(isize))
62 end subroutine
63
64 subroutine finalize()
65 dummy = dummy .and. 0_IK < len(array, IK)
66 deallocate(array)
67 end subroutine
68
69 subroutine setRemoved()
70 block
71 use pm_arrayRemove, only: setRemoved
72 call initialize()
73 call setRemoved(array, pattern)
74 call finalize()
75 end block
76 end subroutine
77
78 subroutine getRemoved()
79 block
80 use pm_arrayRemove, only: getRemoved
81 call initialize()
82 array = getRemoved(array, pattern)
83 call finalize()
84 end block
85 end subroutine
86
87end program benchmark
Generate and return an allocatable array containing the remaining parts of the input array as a seque...

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 = ["setRemoved", "getRemoved"]
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("setRemoved() vs. getRemoved()\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.getRemoved_vs_setRemoved.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["getRemoved"].values / df["setRemoved"].values
61 , linewidth = 2
62 )
63
64plt.xticks(fontsize = fontsize)
65plt.yticks(fontsize = fontsize)
66ax.set_xlabel("Array Size", fontsize = fontsize)
67ax.set_ylabel("Runtime compared to setRemoved()", fontsize = fontsize)
68ax.set_title("getRemoved / setRemoved\nLower means faster. Lower than 1 means faster than setRemoved().", fontsize = fontsize)
69ax.set_xscale("log")
70#ax.set_yscale("log")
71plt.minorticks_on()
72plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
73ax.tick_params(axis = "y", which = "minor")
74ax.tick_params(axis = "x", which = "minor")
75ax.legend ( ["setRemoved", "getRemoved"]
76 #, bbox_to_anchor = (1, 0.5)
77 #, loc = "center left"
78 , fontsize = fontsize
79 )
80
81plt.tight_layout()
82plt.savefig("benchmark.getRemoved_vs_setRemoved.runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface getRemoved are functions while the procedures under the generic interface setRemoved are subroutines.
    From the benchmark results, it appears that the functional interface performs slightly less efficiently than the subroutine interface, despite the two algorithms having the same implementation.
  2. Note that this benchmark does not even include the cost of repeated reallcations, that is, the allocation of Removed happen only once in all tests.
  3. Note that this benchmark considers the worst-case scenario where all elements of the input array match the input pattern and must be therefore, removed.
Test:
test_pm_arrayRemove
Bug:

Status: Unresolved
Source: Intel Classic Fortran Compiler ifort version 2021.2.0
Description: The Intel Classic Fortran Compiler ifort version 2021.2.0 has a bug for useing the following two modules simultaneously in the implementation of the procedures in this module,
Sort the input scalar string or contiguous vector in ascending order, or return the sorted indices of...
Generate and return a vector of unique values in the input array.
This module contains procedures and generic interfaces for various sorting tasks.
This module contains procedures and generic interfaces for finding unique values of an input array of...

The following is example error message from the Intel Classic Fortran Compiler ifort,

      pm_arrayRemove@routines@setRemoved_D1.inc.F90(196): error #6405: The same named entity from different modules and/or program units cannot be referenced.   [UNIQUE]
                      if (present(unique)) unique_def = unique
      ----------------------------^

Searching this error on the web points to the possibility that the internal representation of entities by Intel Classic Fortran Compiler ifort has a naming conflict.
Remedy (as of ParaMonte Library version 2.0.0): For now, the remedy was to isolate the use of one of the modules to exactly where it is needed, like,

block
InstanceNew = getUnique(InstanceNew(1:lenInstanceNew))
end block


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_arrayRemove::MODULE_NAME = "@pm_arrayRemove"

Definition at line 105 of file pm_arrayRemove.F90.