15#ifdef DEAL_II_WITH_SYMENGINE
20# include <boost/archive/text_iarchive.hpp>
21# include <boost/archive/text_oarchive.hpp>
30#ifdef DEAL_II_WITH_SYMENGINE
37 template <
typename ReturnType>
41 , ready_for_value_extraction(false)
42 , has_been_serialized(false)
47 template <
typename ReturnType>
58 template <
typename ReturnType>
61 : method(other.method)
63 , independent_variables_symbols(other.independent_variables_symbols)
64 , dependent_variables_functions(other.dependent_variables_functions)
65 , dependent_variables_output(0)
66 , map_dep_expr_vec_entry(other.map_dep_expr_vec_entry)
67 , ready_for_value_extraction(false)
68 , has_been_serialized(false)
73 template <
typename ReturnType>
82 dependent_variables_output.clear();
84 ready_for_value_extraction =
false;
85 has_been_serialized =
false;
90 template <
typename ReturnType>
99 "Cannot call set_optimization_method() once the optimizer is finalized."));
101# ifndef HAVE_SYMENGINE_LLVM
107 method = optimization_method;
108 flags = optimization_flags;
113 template <
typename ReturnType>
122 template <
typename ReturnType>
131 template <
typename ReturnType>
140 template <
typename ReturnType>
144 if (dependent_variables_output.size() > 0)
146 Assert(dependent_variables_output.size() ==
147 dependent_variables_functions.size(),
157 template <
typename ReturnType>
161 return ready_for_value_extraction;
166 template <
typename ReturnType>
171 Assert(optimized() ==
false,
173 "Cannot register symbols once the optimizer is finalized."));
179 for (
const auto &entry : substitution_map)
182 Assert(SymEngine::is_a<SymEngine::Symbol>(*(symbol.
get_RCP())),
183 ExcMessage(
"Key entry in map is not a symbol."));
188 independent_variables_symbols.insert(substitution_map.begin(),
189 substitution_map.end());
194 template <
typename ReturnType>
197 const SymEngine::map_basic_basic &substitution_map)
205 template <
typename ReturnType>
210 Assert(optimized() ==
false,
212 "Cannot register symbols once the optimizer is finalized."));
214 for (
const auto &symbol : symbols)
216 Assert(independent_variables_symbols.find(symbol) ==
217 independent_variables_symbols.end(),
219 independent_variables_symbols.insert(
226 template <
typename ReturnType>
229 const SymEngine::vec_basic &symbols)
237 template <
typename ReturnType>
246 template <
typename ReturnType>
250 return independent_variables_symbols.size();
255 template <
typename ReturnType>
259 Assert(optimized() ==
false,
261 "Cannot register functions once the optimizer is finalized."));
263 register_scalar_function(function);
268 template <
typename ReturnType>
273 Assert(optimized() ==
false,
275 "Cannot register functions once the optimizer is finalized."));
277 register_vector_functions(functions);
282 template <
typename ReturnType>
285 const SymEngine::vec_basic &functions)
293 template <
typename ReturnType>
297 return dependent_variables_functions;
302 template <
typename ReturnType>
306 if (has_been_serialized ==
false)
310 Assert(map_dep_expr_vec_entry.size() ==
311 dependent_variables_functions.size(),
314 return dependent_variables_functions.size();
319 template <
typename ReturnType>
323 Assert(optimized() ==
false,
324 ExcMessage(
"Cannot call optimize() more than once."));
327 create_optimizer(optimizer);
344 dependent_variables_functions),
345 optimization_flags());
359 dependent_variables_functions),
360 optimization_flags());
362# ifdef HAVE_SYMENGINE_LLVM
363 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
364 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
370 internal::LLVMOptimizer<ReturnType>>::
375 dependent_variables_functions),
376 optimization_flags());
391 dependent_variables_output.resize(n_dependent_variables());
396 template <
typename ReturnType>
404 "The optimizer is not configured to perform substitution. "
405 "This action can only performed after optimize() has been called."));
416 Assert(symbol_sub_vec.size() == symbol_vec.size(),
419 for (
unsigned int i = 0; i < symbol_sub_vec.size(); ++i)
424 "The input substitution map is either incomplete, or does "
425 "not match that used in the register_symbols() call."));
431 const std::vector<ReturnType> values =
432 Utilities::extract_values<ReturnType>(substitution_map);
438 template <
typename ReturnType>
441 const SymEngine::map_basic_basic &substitution_map)
const
449 template <
typename ReturnType>
453 const std::vector<ReturnType> &values)
const
463 template <
typename ReturnType>
466 const SymEngine::vec_basic &symbols,
467 const std::vector<ReturnType> &values)
const
476 template <
typename ReturnType>
479 const std::vector<ReturnType> &substitution_values)
const
484 "The optimizer is not configured to perform substitution. "
485 "This action can only performed after optimize() has been called."));
487 Assert(substitution_values.size() == independent_variables_symbols.size(),
489 independent_variables_symbols.size()));
499 substitute(opt, dependent_variables_output, substitution_values);
509 substitute(opt, dependent_variables_output, substitution_values);
511# ifdef HAVE_SYMENGINE_LLVM
512 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
513 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
519 internal::LLVMOptimizer<ReturnType>>
::
520 substitute(opt, dependent_variables_output, substitution_values);
528 ready_for_value_extraction =
true;
533 template <
typename ReturnType>
534 const std::vector<ReturnType> &
538 values_substituted() ==
true,
540 "The optimizer is not configured to perform evaluation. "
541 "This action can only performed after substitute() has been called."));
543 return dependent_variables_output;
548 template <
typename ReturnType>
552 const std::vector<ReturnType> &cached_evaluation)
const
561 const typename map_dependent_expression_to_vector_entry_t::const_iterator
562 it = map_dep_expr_vec_entry.find(func);
566 if (has_been_serialized && it == map_dep_expr_vec_entry.end())
580 auto serialize_and_deserialize_expression =
582 std::ostringstream oss;
584 boost::archive::text_oarchive oa(oss,
585 boost::archive::no_header);
591 std::istringstream iss(oss.str());
592 boost::archive::text_iarchive ia(iss,
593 boost::archive::no_header);
602 serialize_and_deserialize_expression(func);
609 for (
const auto &e : map_dep_expr_vec_entry)
612 serialize_and_deserialize_expression(e.first);
621 map_dep_expr_vec_entry[func] = e.second;
622 return extract(func, cached_evaluation);
629 "Still cannot find map entry, and there's no hope to recover from this situation."));
632 Assert(it != map_dep_expr_vec_entry.end(),
633 ExcMessage(
"Function has not been registered."));
636 return cached_evaluation[it->second];
641 template <
typename ReturnType>
646 values_substituted() ==
true,
648 "The optimizer is not configured to perform evaluation. "
649 "This action can only performed after substitute() has been called."));
651 return extract(func, dependent_variables_output);
656 template <
typename ReturnType>
657 std::vector<ReturnType>
659 const std::vector<Expression> &funcs,
660 const std::vector<ReturnType> &cached_evaluation)
const
662 std::vector<ReturnType> out;
663 out.reserve(funcs.size());
665 for (
const auto &func : funcs)
666 out.emplace_back(extract(func, cached_evaluation));
673 template <
typename ReturnType>
674 std::vector<ReturnType>
676 const std::vector<Expression> &funcs)
const
679 values_substituted() ==
true,
681 "The optimizer is not configured to perform evaluation. "
682 "This action can only performed after substitute() has been called."));
683 return extract(funcs, dependent_variables_output);
688 template <
typename ReturnType>
693 return is_valid_nonunique_dependent_variable(func.
get_RCP());
698 template <
typename ReturnType>
701 const SymEngine::RCP<const SymEngine::Basic> &func)
const
707 if (SymEngine::is_a<SymEngine::Constant>(*func))
709 if (&*func == &*SymEngine::zero)
711 if (&*func == &*SymEngine::one)
713 if (&*func == &*SymEngine::minus_one)
715 if (&*func == &*SymEngine::I)
717 if (&*func == &*SymEngine::Inf)
719 if (&*func == &*SymEngine::NegInf)
721 if (&*func == &*SymEngine::ComplexInf)
723 if (&*func == &*SymEngine::Nan)
731 template <
typename ReturnType>
737 dependent_variables_output.empty(),
739 "Cannot register function as the optimizer has already been finalized."));
740 dependent_variables_output.reserve(n_dependent_variables() + 1);
741 const bool entry_registered =
742 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
745 if (entry_registered ==
true &&
746 is_valid_nonunique_dependent_variable(func) ==
false)
748 ExcMessage(
"Function has already been registered."));
750 if (entry_registered ==
false)
752 dependent_variables_functions.push_back(func);
753 map_dep_expr_vec_entry[func] =
754 dependent_variables_functions.size() - 1;
760 template <
typename ReturnType>
766 dependent_variables_output.empty(),
768 "Cannot register function as the optimizer has already been finalized."));
769 const std::size_t n_dependents_old = n_dependent_variables();
770 dependent_variables_output.reserve(n_dependents_old + funcs.size());
771 dependent_variables_functions.reserve(n_dependents_old + funcs.size());
773 for (
const auto &func : funcs)
775 const bool entry_registered =
776 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
779 if (entry_registered ==
true &&
780 is_valid_nonunique_dependent_variable(func) ==
false)
782 ExcMessage(
"Function has already been registered."));
784 if (entry_registered ==
false)
786 dependent_variables_functions.push_back(func);
787 map_dep_expr_vec_entry[func] =
788 dependent_variables_functions.size() - 1;
795 template <
typename ReturnType>
798 std::unique_ptr<SymEngine::Visitor> &optimizer)
807 optimizer.reset(
new Optimizer_t());
813 optimizer.reset(
new Optimizer_t());
817# ifdef HAVE_SYMENGINE_LLVM
818 if (internal::LLVMOptimizer<ReturnType>::supported_by_LLVM)
821 typename internal::LLVMOptimizer<ReturnType>::OptimizerType;
822 optimizer.reset(
new Optimizer_t());
843# include "differentiation/sd/symengine_optimizer.inst"
bool use_symbolic_CSE() const
types::substitution_map independent_variables_symbols
types::symbol_vector dependent_variables_functions
void substitute(const types::substitution_map &substitution_map) const
enum OptimizationFlags flags
void register_scalar_function(const SD::Expression &function)
const types::symbol_vector & get_dependent_functions() const
void create_optimizer(std::unique_ptr< SymEngine::Visitor > &optimizer)
enum OptimizerType optimization_method() const
void copy_from(const BatchOptimizer &other)
void set_optimization_method(const enum OptimizerType &optimization_method, const enum OptimizationFlags &optimization_flags=OptimizationFlags::optimize_all)
enum OptimizationFlags optimization_flags() const
void register_functions(const types::symbol_vector &functions)
enum OptimizerType method
std::size_t n_dependent_variables() const
void register_symbols(const types::substitution_map &substitution_map)
const std::vector< ReturnType > & evaluate() const
void register_function(const Expression &function)
ReturnType extract(const Expression &func, const std::vector< ReturnType > &cached_evaluation) const
bool is_valid_nonunique_dependent_variable(const SD::Expression &function) const
void register_vector_functions(const types::symbol_vector &functions)
std::size_t n_independent_variables() const
types::symbol_vector get_independent_symbols() const
map_dependent_expression_to_vector_entry_t map_dep_expr_vec_entry
bool values_substituted() const
const SymEngine::RCP< const SymEngine::Basic > & get_RCP() const
const SymEngine::Basic & get_value() const
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcSymEngineLLVMReturnTypeNotSupported()
static ::ExceptionBase & ExcSymEngineLLVMNotAvailable()
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
SD::types::symbol_vector extract_symbols(const SD::types::substitution_map &substitution_values)
SD::types::substitution_map convert_basic_map_to_expression_map(const SymEngine::map_basic_basic &substitution_map)
SD::types::symbol_vector convert_basic_vector_to_expression_vector(const SymEngine::vec_basic &symbol_vector)
SymEngine::vec_basic convert_expression_vector_to_basic_vector(const SD::types::symbol_vector &symbol_vector)
bool use_symbolic_CSE(const enum OptimizationFlags &flags)
std::vector< SD::Expression > symbol_vector
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
Expression substitute(const Expression &expression, const types::substitution_map &substitution_map)
types::substitution_map make_substitution_map(const Expression &symbol, const Expression &value)
constexpr bool values_are_equal(const Number1 &value_1, const Number2 &value_2)