17#ifdef DEAL_II_WITH_SYMENGINE
22# include <boost/archive/text_iarchive.hpp>
23# include <boost/archive/text_oarchive.hpp>
34 template <
typename ReturnType>
38 , ready_for_value_extraction(false)
39 , has_been_serialized(false)
44 template <
typename ReturnType>
55 template <
typename ReturnType>
58 : method(other.method)
60 , independent_variables_symbols(other.independent_variables_symbols)
61 , dependent_variables_functions(other.dependent_variables_functions)
62 , dependent_variables_output(0)
63 , map_dep_expr_vec_entry(other.map_dep_expr_vec_entry)
64 , ready_for_value_extraction(false)
65 , has_been_serialized(false)
70 template <
typename ReturnType>
79 dependent_variables_output.clear();
81 ready_for_value_extraction =
false;
82 has_been_serialized =
false;
87 template <
typename ReturnType>
96 "Cannot call set_optimization_method() once the optimizer is finalized."));
98# ifndef HAVE_SYMENGINE_LLVM
104 method = optimization_method;
105 flags = optimization_flags;
110 template <
typename ReturnType>
119 template <
typename ReturnType>
128 template <
typename ReturnType>
137 template <
typename ReturnType>
141 if (dependent_variables_output.size() > 0)
143 Assert(dependent_variables_output.size() ==
144 dependent_variables_functions.size(),
154 template <
typename ReturnType>
158 return ready_for_value_extraction;
163 template <
typename ReturnType>
168 Assert(optimized() ==
false,
170 "Cannot register symbols once the optimizer is finalized."));
176 for (
const auto &entry : substitution_map)
179 Assert(SymEngine::is_a<SymEngine::Symbol>(*(symbol.
get_RCP())),
180 ExcMessage(
"Key entry in map is not a symbol."));
185 independent_variables_symbols.insert(substitution_map.begin(),
186 substitution_map.end());
191 template <
typename ReturnType>
194 const SymEngine::map_basic_basic &substitution_map)
202 template <
typename ReturnType>
207 Assert(optimized() ==
false,
209 "Cannot register symbols once the optimizer is finalized."));
211 for (
const auto &symbol : symbols)
213 Assert(independent_variables_symbols.find(symbol) ==
214 independent_variables_symbols.end(),
216 independent_variables_symbols.insert(
223 template <
typename ReturnType>
226 const SymEngine::vec_basic &symbols)
234 template <
typename ReturnType>
243 template <
typename ReturnType>
247 return independent_variables_symbols.size();
252 template <
typename ReturnType>
256 Assert(optimized() ==
false,
258 "Cannot register functions once the optimizer is finalized."));
260 register_scalar_function(function);
265 template <
typename ReturnType>
270 Assert(optimized() ==
false,
272 "Cannot register functions once the optimizer is finalized."));
274 register_vector_functions(functions);
279 template <
typename ReturnType>
282 const SymEngine::vec_basic &functions)
290 template <
typename ReturnType>
294 return dependent_variables_functions;
299 template <
typename ReturnType>
303 if (has_been_serialized ==
false)
307 Assert(map_dep_expr_vec_entry.size() ==
308 dependent_variables_functions.size(),
311 return dependent_variables_functions.size();
316 template <
typename ReturnType>
320 Assert(optimized() ==
false,
321 ExcMessage(
"Cannot call optimize() more than once."));
324 create_optimizer(optimizer);
341 dependent_variables_functions),
342 optimization_flags());
356 dependent_variables_functions),
357 optimization_flags());
359# ifdef HAVE_SYMENGINE_LLVM
360 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
361 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
367 internal::LLVMOptimizer<ReturnType>>::
372 dependent_variables_functions),
373 optimization_flags());
388 dependent_variables_output.resize(n_dependent_variables());
393 template <
typename ReturnType>
401 "The optimizer is not configured to perform substitution. "
402 "This action can only performed after optimize() has been called."));
413 Assert(symbol_sub_vec.size() == symbol_vec.size(),
416 for (
unsigned int i = 0; i < symbol_sub_vec.size(); ++i)
421 "The input substitution map is either incomplete, or does "
422 "not match that used in the register_symbols() call."));
428 const std::vector<ReturnType> values =
429 Utilities::extract_values<ReturnType>(substitution_map);
435 template <
typename ReturnType>
438 const SymEngine::map_basic_basic &substitution_map)
const
446 template <
typename ReturnType>
450 const std::vector<ReturnType> &values)
const
460 template <
typename ReturnType>
463 const SymEngine::vec_basic &symbols,
464 const std::vector<ReturnType> &values)
const
473 template <
typename ReturnType>
476 const std::vector<ReturnType> &substitution_values)
const
481 "The optimizer is not configured to perform substitution. "
482 "This action can only performed after optimize() has been called."));
484 Assert(substitution_values.size() == independent_variables_symbols.size(),
486 independent_variables_symbols.size()));
496 substitute(opt, dependent_variables_output, substitution_values);
506 substitute(opt, dependent_variables_output, substitution_values);
508# ifdef HAVE_SYMENGINE_LLVM
509 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
510 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
516 internal::LLVMOptimizer<ReturnType>>
::
517 substitute(opt, dependent_variables_output, substitution_values);
525 ready_for_value_extraction =
true;
530 template <
typename ReturnType>
531 const std::vector<ReturnType> &
535 values_substituted() ==
true,
537 "The optimizer is not configured to perform evaluation. "
538 "This action can only performed after substitute() has been called."));
540 return dependent_variables_output;
545 template <
typename ReturnType>
549 const std::vector<ReturnType> &cached_evaluation)
const
558 const typename map_dependent_expression_to_vector_entry_t::const_iterator
559 it = map_dep_expr_vec_entry.find(func);
563 if (has_been_serialized && it == map_dep_expr_vec_entry.end())
577 auto serialize_and_deserialize_expression =
579 std::ostringstream oss;
581 boost::archive::text_oarchive oa(oss,
582 boost::archive::no_header);
588 std::istringstream iss(oss.str());
589 boost::archive::text_iarchive ia(iss,
590 boost::archive::no_header);
599 serialize_and_deserialize_expression(func);
606 for (
const auto &e : map_dep_expr_vec_entry)
609 serialize_and_deserialize_expression(e.first);
618 map_dep_expr_vec_entry[func] = e.second;
619 return extract(func, cached_evaluation);
626 "Still cannot find map entry, and there's no hope to recover from this situation."));
629 Assert(it != map_dep_expr_vec_entry.end(),
630 ExcMessage(
"Function has not been registered."));
633 return cached_evaluation[it->second];
638 template <
typename ReturnType>
643 values_substituted() ==
true,
645 "The optimizer is not configured to perform evaluation. "
646 "This action can only performed after substitute() has been called."));
648 return extract(func, dependent_variables_output);
653 template <
typename ReturnType>
654 std::vector<ReturnType>
656 const std::vector<Expression> &funcs,
657 const std::vector<ReturnType> &cached_evaluation)
const
659 std::vector<ReturnType> out;
660 out.reserve(funcs.size());
662 for (
const auto &func : funcs)
663 out.emplace_back(extract(func, cached_evaluation));
670 template <
typename ReturnType>
671 std::vector<ReturnType>
673 const std::vector<Expression> &funcs)
const
676 values_substituted() ==
true,
678 "The optimizer is not configured to perform evaluation. "
679 "This action can only performed after substitute() has been called."));
680 return extract(funcs, dependent_variables_output);
685 template <
typename ReturnType>
690 return is_valid_nonunique_dependent_variable(func.
get_RCP());
695 template <
typename ReturnType>
698 const SymEngine::RCP<const SymEngine::Basic> &func)
const
704 if (SymEngine::is_a<SymEngine::Constant>(*func))
706 if (&*func == &*SymEngine::zero)
708 if (&*func == &*SymEngine::one)
710 if (&*func == &*SymEngine::minus_one)
712 if (&*func == &*SymEngine::I)
714 if (&*func == &*SymEngine::Inf)
716 if (&*func == &*SymEngine::NegInf)
718 if (&*func == &*SymEngine::ComplexInf)
720 if (&*func == &*SymEngine::Nan)
728 template <
typename ReturnType>
734 dependent_variables_output.empty(),
736 "Cannot register function as the optimizer has already been finalized."));
737 dependent_variables_output.reserve(n_dependent_variables() + 1);
738 const bool entry_registered =
739 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
742 if (entry_registered ==
true &&
743 is_valid_nonunique_dependent_variable(func) ==
false)
745 ExcMessage(
"Function has already been registered."));
747 if (entry_registered ==
false)
749 dependent_variables_functions.push_back(func);
750 map_dep_expr_vec_entry[func] =
751 dependent_variables_functions.size() - 1;
757 template <
typename ReturnType>
763 dependent_variables_output.empty(),
765 "Cannot register function as the optimizer has already been finalized."));
766 const std::size_t n_dependents_old = n_dependent_variables();
767 dependent_variables_output.reserve(n_dependents_old + funcs.size());
768 dependent_variables_functions.reserve(n_dependents_old + funcs.size());
770 for (
const auto &func : funcs)
772 const bool entry_registered =
773 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
776 if (entry_registered ==
true &&
777 is_valid_nonunique_dependent_variable(func) ==
false)
779 ExcMessage(
"Function has already been registered."));
781 if (entry_registered ==
false)
783 dependent_variables_functions.push_back(func);
784 map_dep_expr_vec_entry[func] =
785 dependent_variables_functions.size() - 1;
792 template <
typename ReturnType>
795 std::unique_ptr<SymEngine::Visitor> &optimizer)
804 optimizer.reset(
new Optimizer_t());
810 optimizer.reset(
new Optimizer_t());
814# ifdef HAVE_SYMENGINE_LLVM
815 if (internal::LLVMOptimizer<ReturnType>::supported_by_LLVM)
818 typename internal::LLVMOptimizer<ReturnType>::OptimizerType;
819 optimizer.reset(
new Optimizer_t());
840# 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)