Initial import

This commit is contained in:
Guilhem Lavaux 2023-05-29 10:41:03 +02:00
commit 56a50eead3
820 changed files with 192077 additions and 0 deletions

View file

@ -0,0 +1,113 @@
#+
# ARES/HADES/BORG Package -- ./extra/hades/scripts/generate_benchmark_tests.py
# Copyright (C) 2019 Guilhem Lavaux <guilhem.lavaux@iap.fr>
#
# Additional contributions from:
# Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
#
#+
import os
import sys
output_cmake_file = sys.argv[1]
input_config = sys.argv[2]
base_dir = sys.argv[3]
input_config_path = os.path.dirname(os.path.abspath(input_config))
with open(input_config, mode="r") as f:
config = {}
exec(f.read(), {}, config)
config = config['tests']
tests = config['tests']
with open(output_cmake_file, "wt") as f:
for test_name in tests:
test = tests[test_name]
includes = test['includes']
likelihood = test['likelihood']
info_code = test.get('info_code', '')
downgrade = test.get('downgrade', 1)
model_code=""
f.write("""
add_executable(benchmark_gradient_%(test_name)s %(base_dir)s/%(test_source)s)
target_link_libraries(benchmark_gradient_%(test_name)s test_library_LSS LSS ${LIBS})
ares_add_test_targets(benchmark_gradient_%(test_name)s)
target_include_directories(benchmark_gradient_%(test_name)s PRIVATE %(input_config_path)s)
"""
% dict(test_name=test_name, base_dir=base_dir, input_config_path=input_config_path, test_source="benchmark_%s.cpp" % (test_name,)
))
includes_str = \
"\n".join(
map(lambda x: "#include \"%s\"" % (x,), includes)
)
if 'model' in test:
if type(test['model']) is list:
model_string = ""
args = test.get(
'model_args', ['comm, box,box, 0.001'] + ['comm, box, box, 1']*(len(test['model'])-1))
extra_code = test.get(
'model_extra', [""]*len(test['model'])
)
extra_code_prev = test.get(
'model_extra_prev', [""]*len(test['model'])
)
for m, a, e, eprev in zip(test['model'], args, extra_code, extra_code_prev):
model_string += f"""
{{
{eprev}
auto m = std::make_shared<{m}>({a});
{e}
chain->addModel(m);
}}"""
model_code = f"""
auto buildModel(LibLSS::MPI_Communication * comm, LibLSS::MarkovState& state, LibLSS::BoxModel box, LibLSS::BoxModel box2) {{
using namespace LibLSS;
auto chain = std::make_shared<ChainForwardModel>(comm, box);
{model_string}
return chain;
}}
"""
else:
args = test.get('model_args', 'comm, box, box, 0.001')
model_code="auto buildModel(LibLSS::MPI_Communication *comm, LibLSS::MarkovState& state, LibLSS::BoxModel const& box, LibLSS::BoxModel box2) { return std::make_shared<%(model)s>(%(model_args)s); }" % dict(model=test['model'],model_args=args )
if 'data_setup' in test:
data_setup='#include "%s"\n#define DATA_SETUP %s\n'%(test['data_setup'])
else:
data_setup=''
with open(os.path.join(base_dir,"benchmark_%s.cpp" % (test_name,)), mode="wt") as f2:
f2.write("""%(includes)s
#include "libLSS/physics/likelihoods/base.hpp"
%(data_setup_header)s
static const int DOWNGRADE_DATA = %(downgrade)d;
namespace L = LibLSS::Likelihood;
using LibLSS::LikelihoodInfo;
auto makeLikelihood(LikelihoodInfo& info) {
%(additional_info_code)s
return std::make_shared<%(likelihood)s>(info);
}
%(model_code)s
static constexpr bool RUN_RSD_TEST = false;
static std::string testName = "%(test_name)s";
#include "generic_gradient_benchmark.cpp"
""" % dict(includes=includes_str,data_setup_header=data_setup, additional_info_code=info_code,likelihood=likelihood,model_code=model_code,test_name=test_name,downgrade=downgrade))
# ARES TAG: authors_num = 1
# ARES TAG: name(0) = Guilhem Lavaux
# ARES TAG: email(0) = guilhem.lavaux@iap.fr
# ARES TAG: year(0) = 2019

View file

@ -0,0 +1,152 @@
# +
# ARES/HADES/BORG Package -- -- ./scripts/generate_tests_cmake.py
# Copyright (C) 2019 Guilhem Lavaux <guilhem.lavaux@iap.fr>
#
# Additional contributions from:
# Guilhem Lavaux <guilhem.lavaux@iap.fr> (2019)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of either the CeCILL license or the GNU General Public
# license, as included with the software package.
#
# The text of the license is located in Licence_CeCILL_V2.1-en.txt
# and GPL.txt in the root directory of the source package.
# +
import os
import sys
import filecmp
output_cmake_file = sys.argv[1]
input_config = sys.argv[2]
base_dir = sys.argv[3]
input_config_path = os.path.dirname(os.path.abspath(input_config))
with open(input_config, mode="r") as f:
config = {}
exec(f.read(), {}, config)
config = config['tests']
tests = config['tests']
with open(output_cmake_file, "wt") as f:
for test_name in tests:
test = tests[test_name]
base_code = test.get("base_code", "generic_gradient_test.cpp")
includes = test['includes']
likelihood = test['likelihood']
info_code = test.get('info_code', '')
model_code = ""
f.write("""
add_executable(test_gradient_%(test_name)s %(base_dir)s/%(test_source)s)
target_link_libraries(test_gradient_%(test_name)s test_library_LSS LSS ${LIBS})
ares_add_test_targets(test_gradient_%(test_name)s)
target_include_directories(test_gradient_%(test_name)s PRIVATE %(input_config_path)s)
""" % dict(test_name=test_name,
base_dir=base_dir,
input_config_path=input_config_path,
test_source="test_%s_gradient.cpp" % (test_name, )))
includes_str = \
"\n".join(
map(lambda x: "#include \"%s\"" % (x,), includes)
)
downgrade = test.get('downgrade', 1)
if 'model' in test:
if isinstance(test['model'], list):
model_string = ""
args = test.get('model_args', ['comm, box,box, 0.001'] +
['comm, box, box, 1'] *
(len(test['model']) - 1))
extra_code = test.get('model_extra', [""] * len(test['model']))
extra_code_prev = test.get('model_extra_prev', [""] * len(test['model']))
for m, a, e, eprev in zip(test['model'], args, extra_code, extra_code_prev):
model_string += f"""
{{
{eprev}
auto m = std::make_shared<{m}>({a});
{e}
chain->addModel(m);
}}"""
model_code = f"""
auto makeModel(LibLSS::MPI_Communication * comm, LibLSS::MarkovState& state, LibLSS::BoxModel box, LibLSS::BoxModel box2) {{
using namespace LibLSS;
auto chain = std::make_shared<ChainForwardModel>(comm, box);
{model_string}
return chain;
}}
"""
else:
args = test.get('model_args', 'comm, box, box, 0.001')
extra_prev = test.get('model_extra_prev', "")
extra = test.get('model_extra', "")
model_code = f"""
auto makeModel(LibLSS::MPI_Communication * comm, LibLSS::MarkovState& state, LibLSS::BoxModel box, LibLSS::BoxModel box2) {{
using namespace LibLSS;
{extra_prev}
auto m = std::make_shared<{test['model']}>({args});
{extra}
return m;
}}
"""
else:
assert 'model_code' in test
model_code = test['model_code']
if 'data_setup' in test:
data_setup = '#include "%s"\n#define DATA_SETUP %s\n' % (
test['data_setup'])
else:
data_setup = ''
newfile = os.path.join(base_dir,
"test_%s_gradient.cpp_tmp" % (test_name, ))
finalfile = os.path.join(base_dir,
"test_%s_gradient.cpp" % (test_name, ))
with open(newfile, mode="wt") as f2:
f2.write("""%(includes)s
#include "libLSS/samplers/rgen/hmc/hmc_density_sampler.hpp"
#include "libLSS/physics/likelihoods/base.hpp"
%(data_setup_header)s
namespace L = LibLSS::Likelihood;
using LibLSS::LikelihoodInfo;
using LibLSS::HMCDensitySampler;
static const int DOWNGRADE_DATA = %(downgrade)d;
HMCDensitySampler::Likelihood_t makeLikelihood(LikelihoodInfo& info) {
%(additional_info_code)s
return std::make_shared<%(likelihood)s>(info);
}
%(model_code)s
#include "%(base_code)s"
""" % dict(includes=includes_str,
data_setup_header=data_setup,
additional_info_code=info_code,
likelihood=likelihood,
model_code=model_code,
downgrade=downgrade,
base_code=base_code))
try:
os.stat(finalfile)
if not filecmp.cmp(finalfile, newfile):
os.rename(newfile, finalfile)
else:
os.unlink(newfile)
except FileNotFoundError:
os.rename(newfile, finalfile)
# ARES TAG: authors_num = 1
# ARES TAG: name(0) = Guilhem Lavaux
# ARES TAG: email(0) = guilhem.lavaux@iap.fr
# ARES TAG: year(0) = 2019

View file

@ -0,0 +1,76 @@
#+
# ARES/HADES/BORG Package -- ./extra/hades/scripts/generate_tests_forward_models_cmake.py
# Copyright (C) 2019 Guilhem Lavaux <guilhem.lavaux@iap.fr>
# Copyright (C) 2009-2020 Jens Jasche <jens.jasche@fysik.su.se>
#
# Additional contributions from:
# Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
#
#+
import os
import sys
output_cmake_file = sys.argv[1]
input_config = sys.argv[2]
base_dir = sys.argv[3]
with open(input_config, mode="r") as f:
config = {}
exec(f.read(), {}, config)
config = config['tests']
if not 'forward_tests' in config:
sys.exit(1)
tests = config['forward_tests']
with open(output_cmake_file, "wt") as f:
for test_name in tests:
test = tests[test_name]
includes = test['includes']
model = test['model']
model_args = test.get('model_args', 'comm, box, 0.001');
model_code=""
f.write("""
add_executable(test_forward_%(test_name)s %(base_dir)s/%(test_source)s)
target_link_libraries(test_forward_%(test_name)s test_library_LSS LSS ${LIBS})
ares_add_test_targets(test_forward_%(test_name)s)
"""
% dict(test_name=test_name, base_dir=base_dir, test_source="test_forward_%s.cpp" % (test_name,)
))
includes_str = \
"\n".join(
map(lambda x: "#include \"%s\"" % (x,), includes)
)
extra_code_prev = test.get('model_extra_prev', "")
with open(os.path.join(base_dir,"test_forward_%s.cpp" % (test_name,)), mode="wt") as f2:
f2.write("""%(includes)s
#include "libLSS/samplers/rgen/hmc/hmc_density_sampler.hpp"
#include "libLSS/physics/likelihoods/base.hpp"
#include "libLSS/physics/forward_model.hpp"
namespace L = LibLSS::Likelihood;
using LibLSS::LikelihoodInfo;
using LibLSS::HMCDensitySampler;
using LibLSS::MarkovState;
using LibLSS::BoxModel;
auto makeModel(MarkovState& state, BoxModel const& box, LikelihoodInfo& info) {
auto comm = L::getMPI(info);
%(extra_prev)s
return std::make_shared<%(model)s>(%(model_args)s);
}
#include "libLSS/tests/generic_borg_fwd_test.cpp"
""" % dict(includes=includes_str,model_args=model_args,model=model,extra_prev=extra_code_prev))
# ARES TAG: num_authors = 1
# ARES TAG: name(0) = Guilhem Lavaux
# ARES TAG: email(0) = guilhem.lavaux@iap.fr
# ARES TAG: year(0) = 2019

View file

@ -0,0 +1,45 @@
function(hades_add_forward_test testnames testbase)
set(_forward_test_cmake ${CMAKE_BINARY_DIR}/CMakeFiles/hades_forward-test_${testnames}.cmake)
set(_base_tmp_forward_files ${CMAKE_BINARY_DIR}/CMakeFiles/forward_tests)
if (NOT EXISTS ${_base_tmp_forward_files})
FILE(MAKE_DIRECTORY ${_base_tmp_forward_files})
endif()
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/extra/hades/scripts/generate_tests_forward_models_cmake.py
${_forward_test_cmake} ${testbase} ${_base_tmp_forward_files}
RESULT_VARIABLE _generate_result)
if (NOT _generate_result EQUAL 0)
cmessage(FATAL_ERROR "Could not automatically generate gradient tests.")
endif()
include(${_forward_test_cmake})
endfunction()
function(hades_add_gradient_test testnames testbase)
set(_gradient_test_cmake ${CMAKE_BINARY_DIR}/CMakeFiles/hades_gradient_test_${testnames}.cmake)
set(_benchmark_gradient_test_cmake ${CMAKE_BINARY_DIR}/CMakeFiles/hades_gradient_benchmark_test_${testnames}.cmake)
set(_base_tmp_gradient_files ${CMAKE_BINARY_DIR}/CMakeFiles/gradient_tests)
if (NOT EXISTS ${_base_tmp_gradient_files})
FILE(MAKE_DIRECTORY ${_base_tmp_gradient_files})
endif()
execute_process(COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/extra/hades/scripts/generate_tests_cmake.py
${_gradient_test_cmake} ${testbase} ${_base_tmp_gradient_files} RESULT_VARIABLE _generate_result)
if (NOT _generate_result EQUAL 0)
cmessage(FATAL_ERROR "Could not automatically generate gradient tests.")
endif()
execute_process(COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/extra/hades/scripts/generate_benchmark_tests.py
${_benchmark_gradient_test_cmake} ${testbase} ${_base_tmp_gradient_files} RESULT_VARIABLE _generate_result)
if (NOT _generate_result EQUAL 0)
cmessage(FATAL_ERROR "Could not automatically generate gradient tests.")
endif()
include(${_gradient_test_cmake})
include(${_benchmark_gradient_test_cmake})
endfunction()

View file

@ -0,0 +1,14 @@
macro(hades_register_forward_models)
SET(_ALL_MODELS ${CMAKE_BINARY_DIR}/libLSS/physics/forwards/all_models.hpp)
get_property(_init GLOBAL PROPERTY HADES_FORWARD_MODEL_INIT)
if (NOT _init)
file(WRITE ${_ALL_MODELS} "#pragma once\n")
set_property(GLOBAL PROPERTY HADES_FORWARD_MODEL_INIT 1)
endif()
foreach(model_header ${ARGN})
cmessage(STATUS "Registering forward model ${model_header}")
file(APPEND ${_ALL_MODELS} "#include \"${model_header}\"\n")
endforeach()
endmacro()