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

This module contains procedures and generic interfaces for selecting the \(k\)th smallest element in unsorted arrays of various types. More...

Data Types

interface  getSelected
 Generate and return the rankth smallest value in the input array by first sorting its elements in ascending order (optionally only between the specified indices [lb, ub]).
More...
 
interface  setSelected
 Return the rankth smallest (or ordered) value in the input array by first sorting its elements in ascending order (optionally only between the specified indices [lb, ub]).
More...
 

Variables

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

Detailed Description

This module contains procedures and generic interfaces for selecting the \(k\)th smallest element in unsorted arrays of various types.

Benchmarks:


Benchmark :: The runtime performance of getSelected vs. setSelected

1! Test the performance of `getSelected()` vs. `setSelected()`.
2program benchmark
3
4 use iso_fortran_env, only: error_unit
5 use pm_kind, only: IK, LK, RK, SK
6 use pm_arrayRange, only: getRange
7 use pm_bench, only: bench_type
8
9 implicit none
10
11 integer(IK) :: i
12 integer(IK) :: isize
13 integer(IK) :: fileUnit
14 integer(IK) , parameter :: NSIZE = 20_IK
15 integer(IK) , parameter :: NBENCH = 2_IK
16 integer(IK) :: arraySize(NSIZE)
17 logical(LK) :: dummy = .true._LK
18 real(RK) :: selection = 1._RK
19 real(RK) , allocatable :: array(:)
20 integer(IK) :: rank
21 type(bench_type) :: bench(NBENCH)
22
23 bench(1) = bench_type(name = SK_"setSelected", exec = setSelected , overhead = setOverhead)
24 bench(2) = bench_type(name = SK_"getSelected", exec = getSelected , overhead = setOverhead)
25
26 arraySize = [( 2_IK**isize, isize = 1_IK, NSIZE )]
27
28 write(*,"(*(g0,:,' '))")
29 write(*,"(*(g0,:,' '))") "setSelected() vs. getSelected()"
30 write(*,"(*(g0,:,' '))")
31
32 open(newunit = fileUnit, file = "main.out")
33
34 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1, NBENCH)
35
36 loopOverArraySize: do isize = 1, NSIZE
37
38 write(*,"(*(g0,:,' '))") "Benchmarking with size", arraySize(isize)
39
40 allocate(array(arraySize(isize)))
41 rank = 1_IK ! arraySize(isize)
42 do i = 1, NBENCH
43 bench(i)%timing = bench(i)%getTiming(minsec = 0.05_RK)
44 end do
45 deallocate(array)
46
47 write(fileUnit,"(*(g0,:,','))") arraySize(isize), (bench(i)%timing%mean, i = 1, NBENCH)
48
49 end do loopOverArraySize
50 write(*,"(*(g0,:,' '))") dummy
51 write(*,"(*(g0,:,' '))")
52
53 close(fileUnit)
54
55contains
56
57 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 ! procedure wrappers.
59 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60
61 subroutine setOverhead()
62 call initialize()
63 call finalize()
64 end subroutine
65
66 subroutine initialize()
67 call random_number(array)
68 end subroutine
69
70 subroutine finalize()
71 dummy = dummy .and. selection == 0.5_RK
72 end subroutine
73
74 subroutine setSelected()
75 block
77 call initialize()
78 call setSelected(selection, array, rank)
79 call finalize()
80 end block
81 end subroutine
82
83 subroutine getSelected()
84 block
86 call initialize()
87 selection = getSelected(array, rank)
88 call finalize()
89 end block
90 end subroutine
91
92end program benchmark
Generate minimally-spaced character, integer, real sequences or sequences at fixed intervals of size ...
Generate and return the rankth smallest value in the input array by first sorting its elements in asc...
Return the rankth smallest (or ordered) value in the input array by first sorting its elements in asc...
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 generating ranges of discrete character,...
This module contains procedures and generic interfaces for selecting the th smallest element in unsor...
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 = ["setSelected", "getSelected"]
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("setSelected() vs. getSelected()\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.getSelected_vs_setSelected.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["getSelected"].values / df["setSelected"].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 setSelected()", fontsize = fontsize)
68ax.set_title("getSelected() / setSelected()\nLower means faster. Lower than 1 means faster than setSelected().", 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 ( ["setSelected", "getSelected"]
76 #, bbox_to_anchor = (1, 0.5)
77 #, loc = "center left"
78 , fontsize = fontsize
79 )
80
81plt.tight_layout()
82plt.savefig("benchmark.getSelected_vs_setSelected.runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface getSelected are functions while the procedures under the generic interface setSelected are subroutines.
    From the benchmark results, it appears that the functional interface performs slightly less efficiently than the subroutine interface.
    This is entirely due to the fact that the functional interface makes a full copy of the input array to keep the input array intact.
  2. Note that in this benchmark rank = 1 was used to minimize the time spent on sorting, such that only the array copy time becomes the dominant factor.
    Nevertheless, the benchmark results point to the relatively minor effect of array copying on the runtime performance of functional vs. subroutine procedures.
  3. There is, however, a \(30\%-300\%\) performance loss with the use of the functional interface when size(array) < 10.
Test:
test_pm_arraySelect


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_arraySelect::MODULE_NAME = "@pm_arraySelect"

Definition at line 53 of file pm_arraySelect.F90.