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

This module contains the procedures and interfaces for computing the cumulative sum of the exponential of an array without undue numerical overflow. More...

Data Types

interface  getCumPropExp
 Generate and return the cumulative sum of the proportions of the exponential of the input array, optionally in the backward direction and, optionally reverse the output cumulative sum upon return. More...
 
interface  setCumPropExp
 Return the cumulative sum of the proportions of the exponential of the input array, optionally in the backward direction and, optionally reverse the output cumulative sum upon return. More...
 

Functions/Subroutines

pure real(RKG) function, dimension(lenArray) getCumPropExp_RK (array, maxArray, lenArray)
 [LEGACY code]
Generate and return the normalized cumulative sum (i.e., Cumulative Density Function (CDF)) of the exponentials of the input real vector robustly (without overflow or underflow). The last element of the returned vector is one. More...
 

Variables

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

Detailed Description

This module contains the procedures and interfaces for computing the cumulative sum of the exponential of an array without undue numerical overflow.

Benchmarks:


Benchmark :: The runtime performance of getCumPropExp vs. setCumPropExp

1! Test the performance of `getCumPropExp()` vs. `setCumPropExp()`.
2program benchmark
3
4 use iso_fortran_env, only: error_unit
5 use pm_bench, only: bench_type
6 use pm_kind, only: IK, RK, SK
7
8 implicit none
9
10 integer(IK) :: i
11 integer(IK) :: iarr
12 integer(IK) :: fileUnit
13 integer(IK) , parameter :: NARR = 11_IK
14 integer(IK) :: arraySize(NARR)
15 real(RK) :: dummySum = 0._RK
16 real(RK) :: maxArray
17 real(RK) , allocatable :: array(:)
18 real(RK) , allocatable :: cumPropExp(:)
19 type(bench_type), allocatable :: bench(:)
20
21 bench = [ bench_type(name = SK_"setCumPropExp", exec = setCumPropExp , overhead = setOverhead) &
22 , bench_type(name = SK_"getCumPropExp", exec = getCumPropExp , overhead = setOverhead) &
23 ]
24
25 arraySize = [( 2_IK**iarr, iarr = 1_IK, NARR )]
26
27 write(*,"(*(g0,:,' '))")
28 write(*,"(*(g0,:,' '))") "getCumPropExp() vs. setCumPropExp()"
29 write(*,"(*(g0,:,' '))")
30
31 open(newunit = fileUnit, file = "main.out", status = "replace")
32
33 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1, size(bench))
34
35 loopOverArraySize: do iarr = 1, NARR
36
37 allocate(array(arraySize(iarr)))
38 allocate(cumPropExp(arraySize(iarr)), source = 0._RK)
39 write(*,"(*(g0,:,' '))") "Benchmarking with array size", arraySize(iarr)
40
41 do i = 1, size(bench)
42 bench(i)%timing = bench(i)%getTiming(minsec = 0.1_RK)
43 end do
44 write(fileUnit,"(*(g0,:,','))") arraySize(iarr), (bench(i)%timing%mean, i = 1, size(bench))
45
46 deallocate(array, cumPropExp)
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 getArray()
62 call getDummy()
63 end subroutine
64
65 subroutine getArray()
66 call random_number(array)
67 maxArray = maxval(array)
68 end subroutine
69
70 subroutine getDummy()
71 dummySum = dummySum + cumPropExp(1)
72 end subroutine
73
74 subroutine getCumPropExp()
75 block
77 call getArray()
78 cumPropExp = getCumPropExp(array, maxArray)
79 call getDummy()
80 end block
81 end subroutine
82
83 subroutine setCumPropExp()
84 block
85 use pm_mathCumPropExp, only: setCumPropExp, sequence
86 call getArray()
87 call setCumPropExp(cumPropExp, array, maxArray, sequence)
88 call getDummy()
89 end block
90 end subroutine
91
92end program benchmark
Generate and return an object of type timing_type containing the benchmark timing information and sta...
Definition: pm_bench.F90:574
Generate and return the cumulative sum of the proportions of the exponential of the input array,...
Return the cumulative sum of the proportions of the exponential of the input array,...
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 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 module contains the procedures and interfaces for computing the cumulative sum of the exponentia...
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
7import os
8dirname = os.path.basename(os.getcwd())
9
10fontsize = 14
11
12df = pd.read_csv("main.out", delimiter = ",")
13colnames = list(df.columns.values)
14
15
18
19ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
20ax = plt.subplot()
21
22for colname in colnames[1:]:
23 plt.plot( df[colnames[0]].values
24 , df[colname].values
25 , linewidth = 2
26 )
27
28plt.xticks(fontsize = fontsize)
29plt.yticks(fontsize = fontsize)
30ax.set_xlabel(colnames[0], fontsize = fontsize)
31ax.set_ylabel("Runtime [ seconds ]", fontsize = fontsize)
32ax.set_title(" vs. ".join(colnames[1:])+"\nLower is better.", fontsize = fontsize)
33ax.set_xscale("log")
34ax.set_yscale("log")
35plt.minorticks_on()
36plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
37ax.tick_params(axis = "y", which = "minor")
38ax.tick_params(axis = "x", which = "minor")
39ax.legend ( colnames[1:]
40 #, loc='center left'
41 #, bbox_to_anchor=(1, 0.5)
42 , fontsize = fontsize
43 )
44
45plt.tight_layout()
46plt.savefig("benchmark." + dirname + ".runtime.png")
47
48
51
52ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
53ax = plt.subplot()
54
55plt.plot( df[colnames[0]].values
56 , np.ones(len(df[colnames[0]].values))
57 , linestyle = "--"
58 #, color = "black"
59 , linewidth = 2
60 )
61for colname in colnames[2:]:
62 plt.plot( df[colnames[0]].values
63 , df[colname].values / df[colnames[1]].values
64 , linewidth = 2
65 )
66
67plt.xticks(fontsize = fontsize)
68plt.yticks(fontsize = fontsize)
69ax.set_xlabel(colnames[0], fontsize = fontsize)
70ax.set_ylabel("Runtime compared to {}".format(colnames[1]), fontsize = fontsize)
71ax.set_title("Runtime Ratio Comparison. Lower means faster.\nLower than 1 means faster than {}().".format(colnames[1]), fontsize = fontsize)
72ax.set_xscale("log")
73#ax.set_yscale("log")
74plt.minorticks_on()
75plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
76ax.tick_params(axis = "y", which = "minor")
77ax.tick_params(axis = "x", which = "minor")
78ax.legend ( colnames[1:]
79 #, bbox_to_anchor = (1, 0.5)
80 #, loc = "center left"
81 , fontsize = fontsize
82 )
83
84plt.tight_layout()
85plt.savefig("benchmark." + dirname + ".runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface getCumPropExp are functions while the procedures under the generic interface setCumPropExp are subroutines.
    From the benchmark results, it appears that the functional interface performs significantly worse than the procedural interface.
    However, the difference appears to diminish toward larger array sizes.
Test:
test_pm_mathCumPropExp


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, April 25, 2015, 2:21 PM, National Institute for Fusion Studies, The University of Texas at Austin

Function/Subroutine Documentation

◆ getCumPropExp_RK()

pure real(RKG) function, dimension(lenArray) pm_mathCumPropExp::getCumPropExp_RK ( real(RKG), dimension(lenArray), intent(in)  array,
real(RKG)  maxArray,
integer(IK)  lenArray 
)

[LEGACY code]
Generate and return the normalized cumulative sum (i.e., Cumulative Density Function (CDF)) of the exponentials of the input real vector robustly (without overflow or underflow). The last element of the returned vector is one.

Parameters
[in]array: The input contiguous vector of log-values whose log-sum-exp must be computed.
[in]maxArray: The maximum of the input array argument (maxArray = maxval(array)).
[in]lenArray: The length of the input array.
Returns
cumPropExp : A real vector of the same length as the input array array.
Warning
This routine is only kept for backward compatibility and should not be used in production code. Instead, use the procedures under the generic interface setCumPropExp.


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.

Test:
test_pm_mathCumPropExp
Author:
Amir Shahmoradi, April 25, 2015, 2:21 PM, National Institute for Fusion Studies, The University of Texas at Austin

Definition at line 2128 of file pm_mathCumPropExp.F90.

References pm_kind::RK.

Referenced by test_pm_mathCumPropExp::test_setCumPropExp_RK().

Here is the caller graph for this function:

Variable Documentation

◆ MODULE_NAME

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

Definition at line 59 of file pm_mathCumPropExp.F90.