ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_option::getOption Interface Reference

Generate and return the value of the optional input argument if it is present, otherwise, return the input default value. More...

Detailed Description

Generate and return the value of the optional input argument if it is present, otherwise, return the input default value.

Parameters
[in]default: The input scalar, are array of arbitrary rank, of either
  1. type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU), or
  2. type logical of kind any supported by the processor (e.g., LK), or
  3. type integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64), or
  4. type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128), or
  5. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
or,
  1. a scalar assumed-length character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
whose value will be returned as the output if optional is missing among the input arguments.
[in]optional: The input value of the same type, kind, and rank as the input default argument, representing the optional input argument whose value will be returned if present.
(optional, default = default)
Returns
value : The output value of the same type, kind, and rank as the input default.
  1. If optional is present as an input argument, value takes the value of optional.
  2. If optional is missing as an input argument, value takes the value of default.
If value is of type character, then its length type parameter is len(default).


Possible calling interfaces

use pm_option, only: getOption
defaultValue = getOption(default, optional)
Generate and return the value of the optional input argument if it is present, otherwise,...
Definition: pm_option.F90:135
This module contains procedures, generic interfaces, and types for generating default values for opti...
Definition: pm_option.F90:34
Warning
The condition len(optional) <= len(default) must hold when the input arguments are of type character.
This is to ensure proper full assignment of the optional value to the output whose length is that of default.
This condition is verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
The pure procedure(s) documented herein become impure when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1.
By default, these procedures are pure in release build and impure in debug and testing builds.
Remarks
The procedures under discussion are elemental.
Note
The functions under this generic interface provide are solely intended to a provide convenient shortcut to evaluating whether an optional argument is present or not, and if missing, to return a default value.
Due to significant performance penalty associated with the use of these procedures, their usage should be limited to non-performance-critical sections of code.


Example usage

1program example
2
3 use pm_kind, only: LK
4 use pm_kind, only: SK ! All kinds are supported.
5 use pm_kind, only: IK ! All kinds are supported.
6 use pm_kind, only: CK ! All kinds are supported.
7 use pm_kind, only: RK ! All kinds are supported.
8 use pm_io, only: display_type
9 use pm_option, only: getOption
10
11 implicit none
12
13 character(3, SK), allocatable :: Optional_SK(:,:), Default_SK(:,:), Output_SK(:,:) ! Can be any processor-supported kind.
14 logical(LK) , allocatable :: Optional_LK(:,:), Default_LK(:,:) ! Can be any processor-supported kind.
15 integer(IK) , allocatable :: Optional_IK(:,:), Default_IK(:,:) ! Can be any processor-supported kind.
16 complex(CK) , allocatable :: Optional_CK(:,:), Default_CK(:,:) ! Can be any processor-supported kind.
17 real(RK) , allocatable :: Optional_RK(:,:), Default_RK(:,:) ! Can be any processor-supported kind.
18
19 integer(IK) :: i, j
20
21 type(display_type) :: disp
22 disp = display_type(file = "main.out.F90")
23
24 call disp%skip()
25 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
26 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
27 call disp%show("! Return the optional value if present, otherwise the default value.")
28 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
29 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
30 call disp%skip()
31
32 Optional_SK = reshape(["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"], shape = [2,3])
33 Optional_IK = reshape([1_IK, 2_IK, 3_IK, 4_IK, 5_IK, 6_IK], shape = [2,3])
34 Optional_RK = reshape([1._RK, 2._RK, 3._RK, 4._RK, 5._RK, 6._RK], shape = [2,3])
35 Optional_CK = reshape([(1._CK, -1._CK), (2._CK, -2._CK), (3._CK, -3._CK), (4._CK, -4._CK), (5._CK, -5._CK), (6._CK, -6._CK)], shape = [2,3])
36 Optional_LK = reshape([.false._LK, .true._LK, .false._LK, .true._LK, .false._LK, .true._LK], shape = [2,3])
37
38 Default_SK = reshape([((Optional_SK(i,j), i = size(Optional_SK,1), 1, -1), j = size(Optional_SK,2), 1, -1)], shape = shape(Optional_SK))
39 Default_IK = reshape([((Optional_IK(i,j), i = size(Optional_IK,1), 1, -1), j = size(Optional_IK,2), 1, -1)], shape = shape(Optional_IK))
40 Default_RK = reshape([((Optional_RK(i,j), i = size(Optional_RK,1), 1, -1), j = size(Optional_RK,2), 1, -1)], shape = shape(Optional_RK))
41 Default_CK = reshape([((Optional_CK(i,j), i = size(Optional_CK,1), 1, -1), j = size(Optional_CK,2), 1, -1)], shape = shape(Optional_CK))
42 Default_LK = reshape([((Optional_LK(i,j), i = size(Optional_LK,1), 1, -1), j = size(Optional_LK,2), 1, -1)], shape = shape(Optional_LK))
43
44 call disp%skip()
45 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
46 call disp%show("! Option character scalar/array.")
47 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
48 call disp%skip()
49
50 allocate(Output_SK, mold = Optional_SK)
51
52 call disp%skip()
53 call disp%show("Default_SK")
54 call disp%show( Default_SK, deliml = SK_"""" )
55 call disp%show("Optional_SK")
56 call disp%show( Optional_SK, deliml = SK_"""" )
57 call disp%show("Output_SK(:,:) = getOption(Default_SK)")
58 Output_SK(:,:) = getOption(Default_SK) ! bypass gfortran 10.3 bug
59 call disp%show("Output_SK(:,:)")
60 call disp%show( Output_SK(:,:), deliml = SK_"""" )
61 call disp%show("Output_SK(:,:) = getOption(Default_SK, Optional_SK)")
62 Output_SK(:,:) = getOption(Default_SK, Optional_SK) ! bypass gfortran 10.3 bug
63 call disp%show("Output_SK(:,:)")
64 call disp%show( Output_SK(:,:), deliml = SK_"""" )
65 call disp%skip()
66
67 call disp%skip()
68 call disp%show("Default_SK(:,1)")
69 call disp%show( Default_SK(:,1), deliml = SK_"""" )
70 call disp%show("Optional_SK(:,1)")
71 call disp%show( Optional_SK(:,1), deliml = SK_"""" )
72 call disp%show("Output_SK(:,1) = getOption(Default_SK(:,1))")
73 Output_SK(:,1) = getOption(Default_SK(:,1)) ! bypass gfortran 10.3 bug
74 call disp%show("Output_SK(:,1)")
75 call disp%show( Output_SK(:,1), deliml = SK_"""" )
76 call disp%show("Output_SK(:,1) = getOption(Default_SK(:,1), Optional_SK(:,1))")
77 Output_SK(:,1) = getOption(Default_SK(:,1), Optional_SK(:,1)) ! bypass gfortran 10.3 bug
78 call disp%show("Output_SK(:,1)")
79 call disp%show( Output_SK(:,1), deliml = SK_"""" )
80 call disp%skip()
81
82 call disp%skip()
83 call disp%show("Default_SK(1,1)")
84 call disp%show( Default_SK(1,1), deliml = SK_"""" )
85 call disp%show("Optional_SK(1,1)")
86 call disp%show( Optional_SK(1,1), deliml = SK_"""" )
87 call disp%show("Output_SK(1,1) = getOption(Default_SK(1,1))")
88 Output_SK(1,1) = getOption(Default_SK(1,1)) ! bypass gfortran 10.3 bug
89 call disp%show("Output_SK(1,1)")
90 call disp%show( Output_SK(1,1), deliml = SK_"""" )
91 call disp%show("Output_SK(1,1) = getOption(Default_SK(1,1), Optional_SK(1,1))")
92 Output_SK(1,1) = getOption(Default_SK(1,1), Optional_SK(1,1)) ! bypass gfortran 10.3 bug
93 call disp%show("Output_SK(1,1)")
94 call disp%show( Output_SK(1,1), deliml = SK_"""" )
95 call disp%skip()
96
97 call disp%skip()
98 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
99 call disp%show("! Option logical scalar/array.")
100 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
101 call disp%skip()
102
103 call disp%skip()
104 call disp%show("Default_LK")
105 call disp%show( Default_LK )
106 call disp%show("Optional_LK")
107 call disp%show( Optional_LK )
108 call disp%show("getOption(Default_LK)")
109 call disp%show( getOption(Default_LK) )
110 call disp%show("getOption(Default_LK, Optional_LK)")
111 call disp%show( getOption(Default_LK, Optional_LK) )
112 call disp%skip()
113
114 call disp%skip()
115 call disp%show("Default_LK(:,1)")
116 call disp%show( Default_LK(:,1) )
117 call disp%show("Optional_LK(:,1)")
118 call disp%show( Optional_LK(:,1) )
119 call disp%show("getOption(Default_LK(:,1))")
120 call disp%show( getOption(Default_LK(:,1)) )
121 call disp%show("getOption(Default_LK(:,1), Optional_LK(:,1))")
122 call disp%show( getOption(Default_LK(:,1), Optional_LK(:,1)) )
123 call disp%skip()
124
125 call disp%skip()
126 call disp%show("Default_LK(1,:)")
127 call disp%show( Default_LK(1,:) )
128 call disp%show("Optional_LK(1,:)")
129 call disp%show( Optional_LK(1,:) )
130 call disp%show("getOption(Default_LK(1,:))")
131 call disp%show( getOption(Default_LK(1,:)) )
132 call disp%show("getOption(Default_LK(1,:), Optional_LK(1,:))")
133 call disp%show( getOption(Default_LK(1,:), Optional_LK(1,:)) )
134 call disp%skip()
135
136 call disp%skip()
137 call disp%show("Default_LK(1,1)")
138 call disp%show( Default_LK(1,1) )
139 call disp%show("Optional_LK(1,1)")
140 call disp%show( Optional_LK(1,1) )
141 call disp%show("getOption(Default_LK(1,1))")
142 call disp%show( getOption(Default_LK(1,1)) )
143 call disp%show("getOption(Default_LK(1,1), Optional_LK(1,1))")
144 call disp%show( getOption(Default_LK(1,1), Optional_LK(1,1)) )
145 call disp%skip()
146
147 call disp%skip()
148 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
149 call disp%show("! Option integer scalar/array.")
150 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
151 call disp%skip()
152
153 call disp%skip()
154 call disp%show("Default_IK")
155 call disp%show( Default_IK )
156 call disp%show("Optional_IK")
157 call disp%show( Optional_IK )
158 call disp%show("getOption(Default_IK)")
159 call disp%show( getOption(Default_IK) )
160 call disp%show("getOption(Default_IK, Optional_IK)")
161 call disp%show( getOption(Default_IK, Optional_IK) )
162 call disp%skip()
163
164 call disp%skip()
165 call disp%show("Default_IK(:,1)")
166 call disp%show( Default_IK(:,1) )
167 call disp%show("Optional_IK(:,1)")
168 call disp%show( Optional_IK(:,1) )
169 call disp%show("getOption(Default_IK(:,1))")
170 call disp%show( getOption(Default_IK(:,1)) )
171 call disp%show("getOption(Default_IK(:,1), Optional_IK(:,1))")
172 call disp%show( getOption(Default_IK(:,1), Optional_IK(:,1)) )
173 call disp%skip()
174
175 call disp%skip()
176 call disp%show("Default_IK(1,:)")
177 call disp%show( Default_IK(1,:) )
178 call disp%show("Optional_IK(1,:)")
179 call disp%show( Optional_IK(1,:) )
180 call disp%show("getOption(Default_IK(1,:))")
181 call disp%show( getOption(Default_IK(1,:)) )
182 call disp%show("getOption(Default_IK(1,:), Optional_IK(1,:))")
183 call disp%show( getOption(Default_IK(1,:), Optional_IK(1,:)) )
184 call disp%skip()
185
186 call disp%skip()
187 call disp%show("Default_IK(1,1)")
188 call disp%show( Default_IK(1,1) )
189 call disp%show("Optional_IK(1,1)")
190 call disp%show( Optional_IK(1,1) )
191 call disp%show("getOption(Default_IK(1,1))")
192 call disp%show( getOption(Default_IK(1,1)) )
193 call disp%show("getOption(Default_IK(1,1), Optional_IK(1,1))")
194 call disp%show( getOption(Default_IK(1,1), Optional_IK(1,1)) )
195 call disp%skip()
196
197 call disp%skip()
198 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
199 call disp%show("! Option complex scalar/array.")
200 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
201 call disp%skip()
202
203 call disp%skip()
204 call disp%show("Default_CK")
205 call disp%show( Default_CK )
206 call disp%show("Optional_CK")
207 call disp%show( Optional_CK )
208 call disp%show("getOption(Default_CK)")
209 call disp%show( getOption(Default_CK) )
210 call disp%show("getOption(Default_CK, Optional_CK)")
211 call disp%show( getOption(Default_CK, Optional_CK) )
212 call disp%skip()
213
214 call disp%skip()
215 call disp%show("Default_CK(:,1)")
216 call disp%show( Default_CK(:,1) )
217 call disp%show("Optional_CK(:,1)")
218 call disp%show( Optional_CK(:,1) )
219 call disp%show("getOption(Default_CK(:,1))")
220 call disp%show( getOption(Default_CK(:,1)) )
221 call disp%show("getOption(Default_CK(:,1), Optional_CK(:,1))")
222 call disp%show( getOption(Default_CK(:,1), Optional_CK(:,1)) )
223 call disp%skip()
224
225 call disp%skip()
226 call disp%show("Default_CK(1,:)")
227 call disp%show( Default_CK(1,:) )
228 call disp%show("Optional_CK(1,:)")
229 call disp%show( Optional_CK(1,:) )
230 call disp%show("getOption(Default_CK(1,:))")
231 call disp%show( getOption(Default_CK(1,:)) )
232 call disp%show("getOption(Default_CK(1,:), Optional_CK(1,:))")
233 call disp%show( getOption(Default_CK(1,:), Optional_CK(1,:)) )
234 call disp%skip()
235
236 call disp%skip()
237 call disp%show("Default_CK(1,1)")
238 call disp%show( Default_CK(1,1) )
239 call disp%show("Optional_CK(1,1)")
240 call disp%show( Optional_CK(1,1) )
241 call disp%show("getOption(Default_CK(1,1))")
242 call disp%show( getOption(Default_CK(1,1)) )
243 call disp%show("getOption(Default_CK(1,1), Optional_CK(1,1))")
244 call disp%show( getOption(Default_CK(1,1), Optional_CK(1,1)) )
245 call disp%skip()
246
247 call disp%skip()
248 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%")
249 call disp%show("! Option real scalar/array.")
250 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%")
251 call disp%skip()
252
253 call disp%skip()
254 call disp%show("Default_RK")
255 call disp%show( Default_RK )
256 call disp%show("Optional_RK")
257 call disp%show( Optional_RK )
258 call disp%show("getOption(Default_RK)")
259 call disp%show( getOption(Default_RK) )
260 call disp%show("getOption(Default_RK, Optional_RK)")
261 call disp%show( getOption(Default_RK, Optional_RK) )
262 call disp%skip()
263
264 call disp%skip()
265 call disp%show("Default_RK(:,1)")
266 call disp%show( Default_RK(:,1) )
267 call disp%show("Optional_RK(:,1)")
268 call disp%show( Optional_RK(:,1) )
269 call disp%show("getOption(Default_RK(:,1))")
270 call disp%show( getOption(Default_RK(:,1)) )
271 call disp%show("getOption(Default_RK(:,1), Optional_RK(:,1))")
272 call disp%show( getOption(Default_RK(:,1), Optional_RK(:,1)) )
273 call disp%skip()
274
275 call disp%skip()
276 call disp%show("Default_RK(1,:)")
277 call disp%show( Default_RK(1,:) )
278 call disp%show("Optional_RK(1,:)")
279 call disp%show( Optional_RK(1,:) )
280 call disp%show("getOption(Default_RK(1,:))")
281 call disp%show( getOption(Default_RK(1,:)) )
282 call disp%show("getOption(Default_RK(1,:), Optional_RK(1,:))")
283 call disp%show( getOption(Default_RK(1,:), Optional_RK(1,:)) )
284 call disp%skip()
285
286 call disp%skip()
287 call disp%show("Default_RK(1,1)")
288 call disp%show( Default_RK(1,1) )
289 call disp%show("Optional_RK(1,1)")
290 call disp%show( Optional_RK(1,1) )
291 call disp%show("getOption(Default_RK(1,1))")
292 call disp%show( getOption(Default_RK(1,1)) )
293 call disp%show("getOption(Default_RK(1,1), Optional_RK(1,1))")
294 call disp%show( getOption(Default_RK(1,1), Optional_RK(1,1)) )
295 call disp%skip()
296
297end program example
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
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 CK
The default complex kind in the ParaMonte library: real64 in Fortran, c_double_complex in C-Fortran I...
Definition: pm_kind.F90:542
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
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

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

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4! Return the optional value if present, otherwise the default value.
5!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7
8
9!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10! Option character scalar/array.
11!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12
13
14Default_SK
15"FFF", "DDD", "BBB"
16"EEE", "CCC", "AAA"
17Optional_SK
18"AAA", "CCC", "EEE"
19"BBB", "DDD", "FFF"
20Output_SK(:,:) = getOption(Default_SK)
21Output_SK(:,:)
22"FFF", "DDD", "BBB"
23"EEE", "CCC", "AAA"
24Output_SK(:,:) = getOption(Default_SK, Optional_SK)
25Output_SK(:,:)
26"AAA", "CCC", "EEE"
27"BBB", "DDD", "FFF"
28
29
30Default_SK(:,1)
31"FFF", "EEE"
32Optional_SK(:,1)
33"AAA", "BBB"
34Output_SK(:,1) = getOption(Default_SK(:,1))
35Output_SK(:,1)
36"FFF", "EEE"
37Output_SK(:,1) = getOption(Default_SK(:,1), Optional_SK(:,1))
38Output_SK(:,1)
39"AAA", "BBB"
40
41
42Default_SK(1,1)
43"FFF"
44Optional_SK(1,1)
45"AAA"
46Output_SK(1,1) = getOption(Default_SK(1,1))
47Output_SK(1,1)
48"FFF"
49Output_SK(1,1) = getOption(Default_SK(1,1), Optional_SK(1,1))
50Output_SK(1,1)
51"AAA"
52
53
54!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55! Option logical scalar/array.
56!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
58
59Default_LK
60T, T, T
61F, F, F
62Optional_LK
63F, F, F
64T, T, T
65getOption(Default_LK)
66T, T, T
67F, F, F
68getOption(Default_LK, Optional_LK)
69F, F, F
70T, T, T
71
72
73Default_LK(:,1)
74T, F
75Optional_LK(:,1)
76F, T
77getOption(Default_LK(:,1))
78T, F
79getOption(Default_LK(:,1), Optional_LK(:,1))
80F, T
81
82
83Default_LK(1,:)
84T, T, T
85Optional_LK(1,:)
86F, F, F
87getOption(Default_LK(1,:))
88T, T, T
89getOption(Default_LK(1,:), Optional_LK(1,:))
90F, F, F
91
92
93Default_LK(1,1)
94T
95Optional_LK(1,1)
96F
97getOption(Default_LK(1,1))
98T
99getOption(Default_LK(1,1), Optional_LK(1,1))
100F
101
102
103!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104! Option integer scalar/array.
105!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107
108Default_IK
109+6, +4, +2
110+5, +3, +1
111Optional_IK
112+1, +3, +5
113+2, +4, +6
114getOption(Default_IK)
115+6, +4, +2
116+5, +3, +1
117getOption(Default_IK, Optional_IK)
118+1, +3, +5
119+2, +4, +6
120
121
122Default_IK(:,1)
123+6, +5
124Optional_IK(:,1)
125+1, +2
126getOption(Default_IK(:,1))
127+6, +5
128getOption(Default_IK(:,1), Optional_IK(:,1))
129+1, +2
130
131
132Default_IK(1,:)
133+6, +4, +2
134Optional_IK(1,:)
135+1, +3, +5
136getOption(Default_IK(1,:))
137+6, +4, +2
138getOption(Default_IK(1,:), Optional_IK(1,:))
139+1, +3, +5
140
141
142Default_IK(1,1)
143+6
144Optional_IK(1,1)
145+1
146getOption(Default_IK(1,1))
147+6
148getOption(Default_IK(1,1), Optional_IK(1,1))
149+1
150
151
152!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153! Option complex scalar/array.
154!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156
157Default_CK
158(+6.0000000000000000, -6.0000000000000000), (+4.0000000000000000, -4.0000000000000000), (+2.0000000000000000, -2.0000000000000000)
159(+5.0000000000000000, -5.0000000000000000), (+3.0000000000000000, -3.0000000000000000), (+1.0000000000000000, -1.0000000000000000)
160Optional_CK
161(+1.0000000000000000, -1.0000000000000000), (+3.0000000000000000, -3.0000000000000000), (+5.0000000000000000, -5.0000000000000000)
162(+2.0000000000000000, -2.0000000000000000), (+4.0000000000000000, -4.0000000000000000), (+6.0000000000000000, -6.0000000000000000)
163getOption(Default_CK)
164(+6.0000000000000000, -6.0000000000000000), (+4.0000000000000000, -4.0000000000000000), (+2.0000000000000000, -2.0000000000000000)
165(+5.0000000000000000, -5.0000000000000000), (+3.0000000000000000, -3.0000000000000000), (+1.0000000000000000, -1.0000000000000000)
166getOption(Default_CK, Optional_CK)
167(+1.0000000000000000, -1.0000000000000000), (+3.0000000000000000, -3.0000000000000000), (+5.0000000000000000, -5.0000000000000000)
168(+2.0000000000000000, -2.0000000000000000), (+4.0000000000000000, -4.0000000000000000), (+6.0000000000000000, -6.0000000000000000)
169
170
171Default_CK(:,1)
172(+6.0000000000000000, -6.0000000000000000), (+5.0000000000000000, -5.0000000000000000)
173Optional_CK(:,1)
174(+1.0000000000000000, -1.0000000000000000), (+2.0000000000000000, -2.0000000000000000)
175getOption(Default_CK(:,1))
176(+6.0000000000000000, -6.0000000000000000), (+5.0000000000000000, -5.0000000000000000)
177getOption(Default_CK(:,1), Optional_CK(:,1))
178(+1.0000000000000000, -1.0000000000000000), (+2.0000000000000000, -2.0000000000000000)
179
180
181Default_CK(1,:)
182(+6.0000000000000000, -6.0000000000000000), (+4.0000000000000000, -4.0000000000000000), (+2.0000000000000000, -2.0000000000000000)
183Optional_CK(1,:)
184(+1.0000000000000000, -1.0000000000000000), (+3.0000000000000000, -3.0000000000000000), (+5.0000000000000000, -5.0000000000000000)
185getOption(Default_CK(1,:))
186(+6.0000000000000000, -6.0000000000000000), (+4.0000000000000000, -4.0000000000000000), (+2.0000000000000000, -2.0000000000000000)
187getOption(Default_CK(1,:), Optional_CK(1,:))
188(+1.0000000000000000, -1.0000000000000000), (+3.0000000000000000, -3.0000000000000000), (+5.0000000000000000, -5.0000000000000000)
189
190
191Default_CK(1,1)
192(+6.0000000000000000, -6.0000000000000000)
193Optional_CK(1,1)
194(+1.0000000000000000, -1.0000000000000000)
195getOption(Default_CK(1,1))
196(+6.0000000000000000, -6.0000000000000000)
197getOption(Default_CK(1,1), Optional_CK(1,1))
198(+1.0000000000000000, -1.0000000000000000)
199
200
201!%%%%%%%%%%%%%%%%%%%%%%%%%%
202! Option real scalar/array.
203!%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205
206Default_RK
207+6.0000000000000000, +4.0000000000000000, +2.0000000000000000
208+5.0000000000000000, +3.0000000000000000, +1.0000000000000000
209Optional_RK
210+1.0000000000000000, +3.0000000000000000, +5.0000000000000000
211+2.0000000000000000, +4.0000000000000000, +6.0000000000000000
212getOption(Default_RK)
213+6.0000000000000000, +4.0000000000000000, +2.0000000000000000
214+5.0000000000000000, +3.0000000000000000, +1.0000000000000000
215getOption(Default_RK, Optional_RK)
216+1.0000000000000000, +3.0000000000000000, +5.0000000000000000
217+2.0000000000000000, +4.0000000000000000, +6.0000000000000000
218
219
220Default_RK(:,1)
221+6.0000000000000000, +5.0000000000000000
222Optional_RK(:,1)
223+1.0000000000000000, +2.0000000000000000
224getOption(Default_RK(:,1))
225+6.0000000000000000, +5.0000000000000000
226getOption(Default_RK(:,1), Optional_RK(:,1))
227+1.0000000000000000, +2.0000000000000000
228
229
230Default_RK(1,:)
231+6.0000000000000000, +4.0000000000000000, +2.0000000000000000
232Optional_RK(1,:)
233+1.0000000000000000, +3.0000000000000000, +5.0000000000000000
234getOption(Default_RK(1,:))
235+6.0000000000000000, +4.0000000000000000, +2.0000000000000000
236getOption(Default_RK(1,:), Optional_RK(1,:))
237+1.0000000000000000, +3.0000000000000000, +5.0000000000000000
238
239
240Default_RK(1,1)
241+6.0000000000000000
242Optional_RK(1,1)
243+1.0000000000000000
244getOption(Default_RK(1,1))
245+6.0000000000000000
246getOption(Default_RK(1,1), Optional_RK(1,1))
247+1.0000000000000000
248
249
Benchmarks:


Benchmark :: The runtime performance of getOption vs. direct optional choice

1program benchmark
2
3 use iso_fortran_env, only: error_unit
4 use pm_bench, only: bench_type
5 use pm_kind, only: IK, RK, SK
6
7 implicit none
8
9 integer(IK) :: i
10 integer(IK) :: isize
11 integer(IK) :: fileUnit
12 integer(IK) , parameter :: NSIZE = 14_IK
13 integer(IK) , parameter :: NBENCH = 2_IK
14 integer(IK) :: arraySize(0:NSIZE)
15 real(RK) :: dummy = 0._RK
16 real(RK) :: unifrnd
17 real(RK) , allocatable :: Array(:)
18 real(RK) , allocatable :: Default(:)
19 type(bench_type) :: bench(NBENCH)
20
21 bench(1) = bench_type(name = SK_"getOption", exec = getOption , overhead = setOverhead)
22 bench(2) = bench_type(name = SK_"direct", exec = direct , overhead = setOverhead)
23
24 arraySize = [0_IK, ( 2_IK**isize, isize = 0_IK, NSIZE - 1_IK)]
25
26 write(*,"(*(g0,:,' '))")
27 write(*,"(*(g0,:,' '))") "getOption() vs. direct()"
28 write(*,"(*(g0,:,' '))")
29
30 open(newunit = fileUnit, file = "main.out", status = "replace")
31
32 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1, NBENCH)
33
34 loopOverArraySize: do isize = 0_IK, NSIZE
35
36 write(*,"(*(g0,:,' '))") "Benchmarking with size", arraySize(isize)
37
38 allocate(Array(arraySize(isize)), Default(arraySize(isize)))
39 call random_number(Default)
40 call random_number(Array)
41 do i = 1, NBENCH
42 bench(i)%timing = bench(i)%getTiming(minsec = 0.07_RK)
43 end do
44 deallocate(Array, Default)
45
46 write(fileUnit,"(*(g0,:,','))") arraySize(isize), (bench(i)%timing%mean, i = 1, NBENCH)
47
48 end do loopOverArraySize
49 write(*,"(*(g0,:,' '))") dummy
50 write(*,"(*(g0,:,' '))")
51
52 close(fileUnit)
53
54contains
55
56 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 ! procedure wrappers.
58 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59
60 subroutine setOverhead()
61 call random_number(unifrnd)
62 if (arraySize(isize) > 0_IK) then
63 !if (unifrnd < 0.5_RK) then
64 ! call addOverhead_D1()
65 !else
66 ! call addOverhead_D1()
67 !end if
68 dummy = dummy + sum(Default)
69 else
70 dummy = dummy + unifrnd
71 !if (unifrnd < 0.5_RK) then
72 ! call addOverhead_D0()
73 !else
74 ! call addOverhead_D0()
75 !end if
76 end if
77 end subroutine
78
79 !subroutine addOverhead_D0()
80 ! dummy = dummy + unifrnd
81 !end subroutine
82 !
83 !subroutine addOverhead_D1()
84 ! dummy = dummy + sum(Default)
85 !end subroutine
86
87 subroutine getOption()
88 call random_number(unifrnd)
89 if (arraySize(isize) > 0_IK) then
90 if (unifrnd < 0.5_RK) then
91 call add_option_D1(Array)
92 else
93 call add_option_D1()
94 end if
95 else
96 if (unifrnd < 0.5_RK) then
97 call add_option_D0(unifrnd)
98 else
99 call add_option_D0()
100 end if
101 end if
102 end subroutine
103
104 subroutine direct()
105 call random_number(unifrnd)
106 if (arraySize(isize) > 0_IK) then
107 if (unifrnd < 0.5_RK) then
108 call add_direct_D1(Array)
109 else
110 call add_direct_D1()
111 end if
112 else
113 if (unifrnd < 0.5_RK) then
114 call add_direct_D0(unifrnd)
115 else
116 call add_direct_D0()
117 end if
118 end if
119 end subroutine
120
121 subroutine add_option_D0(value)
122 use pm_option, only: getOption
123 real(RK), intent(in), optional :: value
124 dummy = dummy + getOption(unifrnd, value)
125 end subroutine
126
127 subroutine add_direct_D0(value)
128 real(RK), intent(in), optional :: value
129 if (present(value)) then
130 dummy = dummy + value
131 else
132 dummy = dummy + unifrnd
133 end if
134 end subroutine
135
136 subroutine add_option_D1(Value)
137 use pm_option, only: getOption
138 real(RK), intent(in), optional :: Value(:)
139 dummy = dummy + sum(getOption(Default, Value))
140 end subroutine
141
142 subroutine add_direct_D1(Value)
143 real(RK), intent(in), optional :: Value(:)
144 if (present(Value)) then
145 dummy = dummy + sum(Value)
146 else
147 dummy = dummy + sum(Default)
148 end if
149 end subroutine
150
151end program benchmark
Generate and return an object of type timing_type containing the benchmark timing information and sta...
Definition: pm_bench.F90:574
This module contains abstract interfaces and types that facilitate benchmarking of different procedur...
Definition: pm_bench.F90:41
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 = ["direct", "getOption"]
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 + 1
22 , df[method].values
23 , linewidth = 2
24 )
25
26plt.xticks(fontsize = fontsize)
27plt.yticks(fontsize = fontsize)
28ax.set_xlabel("Array Size + 1", fontsize = fontsize)
29ax.set_ylabel("Runtime [ seconds ]", fontsize = fontsize)
30ax.set_title("getOption() vs. direct method.\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.getOption_vs_direct.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 + 1
54 , np.ones(len(df["arraySize"].values))
55 #, linestyle = "--"
56 , linewidth = 2
57 )
58plt.plot( df["arraySize"].values + 1
59 , df["getOption"].values / df["direct"].values
60 , linewidth = 2
61 )
62
63plt.xticks(fontsize = fontsize)
64plt.yticks(fontsize = fontsize)
65ax.set_xlabel("Array Size + 1", fontsize = fontsize)
66ax.set_ylabel("getOption() runtime compared to direct method", fontsize = fontsize)
67ax.set_title("getOption() to direct method Runtime Ratio.\nLower means faster. Lower than 1 means faster than direct method.", fontsize = fontsize)
68ax.set_xscale("log")
69ax.set_yscale("log")
70plt.minorticks_on()
71plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
72ax.tick_params(axis = "y", which = "minor")
73ax.tick_params(axis = "x", which = "minor")
74ax.legend ( ["Direct Method (Equality)", "getOption()"]
75 #, bbox_to_anchor = (1, 0.5)
76 #, loc = "center left"
77 , fontsize = fontsize
78 )
79
80plt.tight_layout()
81plt.savefig("benchmark.getOption_vs_direct.runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The benchmark results indicate that explicit if-blocks for optional arguments tends to be 100-1000 times faster than calling the convenience functions under the generic interface getOption.
    This performance difference tends to be about a factor of 10 times for scalar optional arguments and grows substantially to larger factors with switching to increasing-size array-like optional dummy arguments.
  2. Despite significant performance degradation with the use of getOption, note that the overall average overhead of calling getOption remains extremely small as of 2022 AD, on the order of nano to micro seconds depending on the size of the optional argument from scalar to large arrays.
  3. Additionally, the significance of performance degradation, if any, depends entirely on the ability of the compiler to inline the procedure.
    The observed performance degradations report here occur without any compiler inlining attempts.
  4. Consequently, there is practically no harm in using getOption in non-performance-critical sections of a codebase, that is, parts of a codebase that are to be called less than billions of times at runtime.
Test:
test_pm_option


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:
Amir Shahmoradi, September 1, 2020, 01:12 AM, Dallas, Texas

Definition at line 135 of file pm_option.F90.


The documentation for this interface was generated from the following file: