\(\newcommand{\B}[1]{ {\bf #1} }\) \(\newcommand{\R}[1]{ {\rm #1} }\)
xam_main.py¶
View page sourceExample and Test Using Main Programs¶
Python¶
The python cmpad main program is
py_run_cmpad = 'python/bin/run_cmpad.py'
C++¶
The C++ main program is
cpp_run_cmpad = 'cpp/build/src/run_cmpad'
xam_main.csv¶
The xam_main.py program creates the file xam_main.csv .
If this file already exists, the previous version is removed
and a completely new version is created.
The meaning of the columns are described in csv_column .
Below is a display of an
example xam_man.csv .
The values n_arg and n_other determine the number of floating
point operations and are small for these cases so that the
program runs quickly:
rate |
min_time |
package |
algorithm |
n_arg |
n_other |
time_setup |
date |
compiler |
debug |
language |
special |
|---|---|---|---|---|---|---|---|---|---|---|---|
7.4e+06 |
0.500 |
none |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.1e+05 |
0.5 |
none |
det_by_minor |
9 |
0 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
1.4e+06 |
0.500 |
adept |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
4.0e+05 |
0.500 |
adolc |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.1e+06 |
0.500 |
autodiff |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.6e+03 |
0.500 |
codi |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
9.9e+05 |
0.500 |
cppad |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.5e+01 |
0.500 |
cppad_jit |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
8.5e+00 |
0.500 |
cppadcg |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.4e+06 |
0.500 |
sacado |
det_by_minor |
9 |
0 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.1e+03 |
0.5 |
autograd |
det_by_minor |
9 |
0 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
1.3e+04 |
0.5 |
cppad_py |
det_by_minor |
9 |
0 |
true |
2024-09-14 |
CPython-3.11.9 |
False |
python |
false |
1.1e+02 |
0.5 |
jax |
det_by_minor |
9 |
0 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
5.5e+03 |
0.5 |
torch |
det_by_minor |
9 |
0 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
3.5e+05 |
0.500 |
none |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
5.5e+02 |
0.5 |
none |
an_ode |
100 |
9 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
1.4e+04 |
0.500 |
adept |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.0e+03 |
0.500 |
adolc |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.6e+03 |
0.500 |
autodiff |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.4e+03 |
0.500 |
codi |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.0e+04 |
0.500 |
cppad |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.8e+00 |
0.500 |
cppad_jit |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
7.2e+00 |
0.500 |
cppadcg |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.0e+03 |
0.500 |
sacado |
an_ode |
100 |
9 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.7e+00 |
0.5 |
autograd |
an_ode |
100 |
9 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
5.4e+01 |
0.5 |
cppad_py |
an_ode |
100 |
9 |
true |
2024-09-14 |
CPython-3.11.9 |
False |
python |
false |
1.9e-01 |
0.5 |
jax |
an_ode |
100 |
9 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
1.2e+01 |
0.5 |
torch |
an_ode |
100 |
9 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
2.3e+06 |
0.500 |
none |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
4.5e+04 |
0.5 |
none |
llsq_obj |
9 |
100 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
6.9e+04 |
0.500 |
adept |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.0e+04 |
0.500 |
adolc |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
8.1e+04 |
0.500 |
autodiff |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.5e+03 |
0.500 |
codi |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.7e+04 |
0.500 |
cppad |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
4.6e+04 |
0.500 |
cppad |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
true |
8.7e-01 |
0.500 |
cppad_jit |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.9e+00 |
0.500 |
cppadcg |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.2e+04 |
0.500 |
sacado |
llsq_obj |
9 |
100 |
true |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.6e+03 |
0.5 |
autograd |
llsq_obj |
9 |
100 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
6.3e+02 |
0.5 |
cppad_py |
llsq_obj |
9 |
100 |
true |
2024-09-14 |
CPython-3.11.9 |
False |
python |
false |
1.1e+02 |
0.5 |
jax |
llsq_obj |
9 |
100 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
4.0e+03 |
0.5 |
torch |
llsq_obj |
9 |
100 |
true |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
8.1e+06 |
0.500 |
none |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.6e+05 |
0.5 |
none |
det_by_minor |
9 |
0 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
2.2e+06 |
0.500 |
adept |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
8.5e+05 |
0.500 |
adolc |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.1e+06 |
0.500 |
autodiff |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.0e+03 |
0.500 |
codi |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
4.2e+06 |
0.500 |
cppad |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
7.6e+06 |
0.500 |
cppad_jit |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
7.2e+06 |
0.500 |
cppadcg |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.4e+06 |
0.500 |
sacado |
det_by_minor |
9 |
0 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.8e+03 |
0.5 |
autograd |
det_by_minor |
9 |
0 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
4.6e+04 |
0.5 |
cppad_py |
det_by_minor |
9 |
0 |
false |
2024-09-14 |
CPython-3.11.9 |
False |
python |
false |
1.1e+02 |
0.5 |
jax |
det_by_minor |
9 |
0 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
5.2e+03 |
0.5 |
torch |
det_by_minor |
9 |
0 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
3.5e+05 |
0.500 |
none |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
5.5e+02 |
0.5 |
none |
an_ode |
100 |
9 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
1.5e+04 |
0.500 |
adept |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.2e+04 |
0.500 |
adolc |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.7e+03 |
0.500 |
autodiff |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.4e+03 |
0.500 |
codi |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
6.6e+05 |
0.500 |
cppad |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
9.6e+05 |
0.500 |
cppad_jit |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
9.2e+05 |
0.500 |
cppadcg |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.4e+03 |
0.500 |
sacado |
an_ode |
100 |
9 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.6e+00 |
0.5 |
autograd |
an_ode |
100 |
9 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
1.0e+04 |
0.5 |
cppad_py |
an_ode |
100 |
9 |
false |
2024-09-14 |
CPython-3.11.9 |
False |
python |
false |
1.8e-01 |
0.5 |
jax |
an_ode |
100 |
9 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
1.3e+01 |
0.5 |
torch |
an_ode |
100 |
9 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
2.7e+06 |
0.500 |
none |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
6.7e+04 |
0.5 |
none |
llsq_obj |
9 |
100 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
7.2e+04 |
0.500 |
adept |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.0e+05 |
0.500 |
adolc |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
8.2e+04 |
0.500 |
autodiff |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.5e+03 |
0.500 |
codi |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.3e+05 |
0.500 |
cppad |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
7.8e+04 |
0.500 |
cppad |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
true |
2.4e+06 |
0.500 |
cppad_jit |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
1.8e+06 |
0.500 |
cppadcg |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
3.1e+04 |
0.500 |
sacado |
llsq_obj |
9 |
100 |
false |
2024-9-14 |
GNU-14.2.1 |
false |
c++ |
false |
2.7e+03 |
0.5 |
autograd |
llsq_obj |
9 |
100 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
2.1e+04 |
0.5 |
cppad_py |
llsq_obj |
9 |
100 |
false |
2024-09-14 |
CPython-3.11.9 |
False |
python |
false |
9.6e+01 |
0.5 |
jax |
llsq_obj |
9 |
100 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
|
3.9e+03 |
0.5 |
torch |
llsq_obj |
9 |
100 |
false |
2024-09-14 |
CPython-3.11.9 |
python |
false |
Source Code¶
import sys
import os
import csv
import re
import subprocess
import importlib
import platform
#
# sys.path
python_version = platform.python_version()
(major, minor, patch) = python_version.split('.')
for lib in [ 'lib', 'lib64' ] :
directory = f'python/build/prefix/{lib}/python{major}.{minor}/site-packages'
if os.path.isdir(directory) :
sys.path.insert(0, os.getcwd() + '/' + directory)
#
def main() :
#
# program
program = 'bin/xam_main.py'
#
# configure_file
configure_file = 'cpp/include/cmpad/configure.hpp'
#
# check
if len(sys.argv) != 1 :
msg = f'{program}: does not expect any arguments'
sys.exit(msg)
if sys.argv[0] != program :
msg = f'{program}: must be executed from the top source directory'
sys.exit(msg)
if not os.path.isfile(configure_file) :
msg = f'{program}: {configure_file} does not exist.'
msg += '\nUse cpp/bin/run_cmake.sh to create it.'
sys.exit(msg)
if not os.path.isfile(cpp_run_cmpad ) :
msg = f'{program}: {cpp_run_cmpad} does not exist.'
msg += '\nUse use make in cpp/build to create it.'
sys.exit(msg)
#
# cpp_package_list
# use configure file to determine list of available packages
cpp_package_list = list()
file_obj = open(configure_file, 'r')
file_data = file_obj.read()
file_obj.close()
ad_package = 'adept,adolc,autodiff,codi,cppad,cppad_jit,cppadcg,sacado'
for package in ad_package.split(',') :
PACKAGE = package.upper()
pattern = f'# *define *CMPAD_HAS_{PACKAGE} *[01]'
match = re.search(pattern, file_data)
if match == None :
msg = f'{program}: {configure_file} cannot find the pattern'
msg += f'\n{pattern}'
sys.exit(msg)
if match.group(0)[-1] == '1' :
cpp_package_list.append(package)
print( f'cpp_package_list = {cpp_package_list}' )
#
# python_package_list
python_package_list = list()
for package in [ 'autograd', 'cppad_py', 'jax', 'torch' ] :
try :
importlib.import_module(package)
python_package_list.append(package)
except :
pass
print( f'python_package_list = {python_package_list}' )
#
# package_list
package_list = [ 'none' ] + cpp_package_list + python_package_list
#
# file_name
file_name = 'xam_main.csv'
if os.path.isfile(file_name) :
os.remove(file_name)
#
# default_min_time
default_min_time = 0.5
#
# n_arg_dict
n_arg_dict = {
'det_by_minor' : 9 ,
'an_ode' : 100 ,
'llsq_obj' : 9,
}
#
# n_other_dict
n_other_dict = {
'det_by_minor' : 0 ,
'an_ode' : 9 ,
'llsq_obj' : 100
}
#
# time_setup
for time_setup in [ True, False ] :
#
# algorithm
algorithm_list = [ 'det_by_minor' , 'an_ode' , 'llsq_obj' ]
for algorithm in algorithm_list :
#
# n_arg
n_arg = n_arg_dict[algorithm]
#
# n_other
n_other = n_other_dict[algorithm]
#
# package
for package in package_list :
#
# run_cmpad_list
if package == 'none' :
run_cmpad_list = [ cpp_run_cmpad, py_run_cmpad ]
elif package in cpp_package_list :
run_cmpad_list = [ cpp_run_cmpad ]
else :
assert package in python_package_list
run_cmpad_list = [ py_run_cmpad ]
#
# run_cmpad
for run_cmpad in run_cmpad_list :
#
# command
command = [
run_cmpad,
f'--package={package}',
f'--algorithm={algorithm}',
f'--n_arg={n_arg}',
f'--n_other={n_other}',
f'--file_name={file_name}',
]
if time_setup :
command.append('--time_setup')
#
# run command
print( ' '.join(command) )
result = subprocess.run(command)
if result.returncode != 0 :
msg = ' '.join(command)
msg += '\nxam_main.py: command above failed'
sys.exit(msg)
#
# file_obj
file_obj = open(file_name)
reader = csv.DictReader(file_obj)
for row in reader :
algorithm = row['algorithm']
assert float(row['min_time']) == default_min_time
assert int(row['n_arg']) == n_arg_dict[algorithm]
assert row['package'] in package_list
assert row['algorithm'] in algorithm_list
assert row['time_setup'] in [ 'true', 'false' ]
assert int( row['n_other'] ) == n_other_dict[algorithm]
#
print( f'{program}: OK')
#
main()