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."));
175 for (
const auto &entry : substitution_map)
178 Assert(SymEngine::is_a<SymEngine::Symbol>(*(symbol.
get_RCP())),
179 ExcMessage(
"Key entry in map is not a symbol."));
184 independent_variables_symbols.insert(substitution_map.begin(),
185 substitution_map.end());
190 template <
typename ReturnType>
193 const SymEngine::map_basic_basic &substitution_map)
201 template <
typename ReturnType>
206 Assert(optimized() ==
false,
208 "Cannot register symbols once the optimizer is finalized."));
210 for (
const auto &symbol : symbols)
212 Assert(independent_variables_symbols.find(symbol) ==
213 independent_variables_symbols.end(),
215 independent_variables_symbols.insert(
222 template <
typename ReturnType>
225 const SymEngine::vec_basic &symbols)
233 template <
typename ReturnType>
242 template <
typename ReturnType>
246 return independent_variables_symbols.size();
251 template <
typename ReturnType>
255 Assert(optimized() ==
false,
257 "Cannot register functions once the optimizer is finalized."));
259 register_scalar_function(function);
264 template <
typename ReturnType>
269 Assert(optimized() ==
false,
271 "Cannot register functions once the optimizer is finalized."));
273 register_vector_functions(functions);
278 template <
typename ReturnType>
281 const SymEngine::vec_basic &functions)
289 template <
typename ReturnType>
293 return dependent_variables_functions;
298 template <
typename ReturnType>
302 if (has_been_serialized ==
false)
306 Assert(map_dep_expr_vec_entry.size() ==
307 dependent_variables_functions.size(),
310 return dependent_variables_functions.size();
315 template <
typename ReturnType>
319 Assert(optimized() ==
false,
320 ExcMessage(
"Cannot call optimize() more than once."));
323 create_optimizer(optimizer);
340 dependent_variables_functions),
341 optimization_flags());
355 dependent_variables_functions),
356 optimization_flags());
358# ifdef HAVE_SYMENGINE_LLVM
359 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
360 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
366 internal::LLVMOptimizer<ReturnType>>::
371 dependent_variables_functions),
372 optimization_flags());
387 dependent_variables_output.resize(n_dependent_variables());
392 template <
typename ReturnType>
400 "The optimizer is not configured to perform substitution. "
401 "This action can only performed after optimize() has been called."));
411 Assert(symbol_sub_vec.size() == symbol_vec.size(),
413 for (
unsigned int i = 0; i < symbol_sub_vec.size(); ++i)
417 "The input substitution map is either incomplete, or does "
418 "not match that used in the register_symbols() call."));
424 const std::vector<ReturnType> values =
425 Utilities::extract_values<ReturnType>(substitution_map);
431 template <
typename ReturnType>
434 const SymEngine::map_basic_basic &substitution_map)
const
442 template <
typename ReturnType>
446 const std::vector<ReturnType> &values)
const
456 template <
typename ReturnType>
459 const SymEngine::vec_basic &symbols,
460 const std::vector<ReturnType> &values)
const
469 template <
typename ReturnType>
472 const std::vector<ReturnType> &substitution_values)
const
477 "The optimizer is not configured to perform substitution. "
478 "This action can only performed after optimize() has been called."));
480 Assert(substitution_values.size() == independent_variables_symbols.size(),
482 independent_variables_symbols.size()));
492 substitute(opt, dependent_variables_output, substitution_values);
502 substitute(opt, dependent_variables_output, substitution_values);
504# ifdef HAVE_SYMENGINE_LLVM
505 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
506 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
512 internal::LLVMOptimizer<ReturnType>>
::
513 substitute(opt, dependent_variables_output, substitution_values);
521 ready_for_value_extraction =
true;
526 template <
typename ReturnType>
527 const std::vector<ReturnType> &
531 values_substituted() ==
true,
533 "The optimizer is not configured to perform evaluation. "
534 "This action can only performed after substitute() has been called."));
536 return dependent_variables_output;
541 template <
typename ReturnType>
545 const std::vector<ReturnType> &cached_evaluation)
const
554 const typename map_dependent_expression_to_vector_entry_t::const_iterator
555 it = map_dep_expr_vec_entry.find(func);
559 if (has_been_serialized && it == map_dep_expr_vec_entry.end())
573 auto serialize_and_deserialize_expression =
575 std::ostringstream oss;
577 boost::archive::text_oarchive oa(oss,
578 boost::archive::no_header);
584 std::istringstream iss(oss.str());
585 boost::archive::text_iarchive ia(iss,
586 boost::archive::no_header);
595 serialize_and_deserialize_expression(func);
602 for (
const auto &e : map_dep_expr_vec_entry)
605 serialize_and_deserialize_expression(e.first);
614 map_dep_expr_vec_entry[func] = e.second;
615 return extract(func, cached_evaluation);
622 "Still cannot find map entry, and there's no hope to recover from this situation."));
625 Assert(it != map_dep_expr_vec_entry.end(),
626 ExcMessage(
"Function has not been registered."));
629 return cached_evaluation[it->second];
634 template <
typename ReturnType>
639 values_substituted() ==
true,
641 "The optimizer is not configured to perform evaluation. "
642 "This action can only performed after substitute() has been called."));
644 return extract(func, dependent_variables_output);
649 template <
typename ReturnType>
650 std::vector<ReturnType>
652 const std::vector<Expression> &funcs,
653 const std::vector<ReturnType> &cached_evaluation)
const
655 std::vector<ReturnType> out;
656 out.reserve(funcs.size());
658 for (
const auto &func : funcs)
659 out.emplace_back(extract(func, cached_evaluation));
666 template <
typename ReturnType>
667 std::vector<ReturnType>
669 const std::vector<Expression> &funcs)
const
672 values_substituted() ==
true,
674 "The optimizer is not configured to perform evaluation. "
675 "This action can only performed after substitute() has been called."));
676 return extract(funcs, dependent_variables_output);
681 template <
typename ReturnType>
686 return is_valid_nonunique_dependent_variable(func.
get_RCP());
691 template <
typename ReturnType>
694 const SymEngine::RCP<const SymEngine::Basic> &func)
const
700 if (SymEngine::is_a<SymEngine::Constant>(*func))
702 if (&*func == &*SymEngine::zero)
704 if (&*func == &*SymEngine::one)
706 if (&*func == &*SymEngine::minus_one)
708 if (&*func == &*SymEngine::I)
710 if (&*func == &*SymEngine::Inf)
712 if (&*func == &*SymEngine::NegInf)
714 if (&*func == &*SymEngine::ComplexInf)
716 if (&*func == &*SymEngine::Nan)
724 template <
typename ReturnType>
730 dependent_variables_output.empty(),
732 "Cannot register function as the optimizer has already been finalized."));
733 dependent_variables_output.reserve(n_dependent_variables() + 1);
734 const bool entry_registered =
735 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
737 if (entry_registered ==
true &&
738 is_valid_nonunique_dependent_variable(func) ==
false)
740 ExcMessage(
"Function has already been registered."));
742 if (entry_registered ==
false)
744 dependent_variables_functions.push_back(func);
745 map_dep_expr_vec_entry[func] =
746 dependent_variables_functions.size() - 1;
752 template <
typename ReturnType>
758 dependent_variables_output.empty(),
760 "Cannot register function as the optimizer has already been finalized."));
761 const std::size_t n_dependents_old = n_dependent_variables();
762 dependent_variables_output.reserve(n_dependents_old + funcs.size());
763 dependent_variables_functions.reserve(n_dependents_old + funcs.size());
765 for (
const auto &func : funcs)
767 const bool entry_registered =
768 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
770 if (entry_registered ==
true &&
771 is_valid_nonunique_dependent_variable(func) ==
false)
773 ExcMessage(
"Function has already been registered."));
775 if (entry_registered ==
false)
777 dependent_variables_functions.push_back(func);
778 map_dep_expr_vec_entry[func] =
779 dependent_variables_functions.size() - 1;
786 template <
typename ReturnType>
789 std::unique_ptr<SymEngine::Visitor> &optimizer)
798 optimizer.reset(
new Optimizer_t());
804 optimizer.reset(
new Optimizer_t());
808# ifdef HAVE_SYMENGINE_LLVM
809 if (internal::LLVMOptimizer<ReturnType>::supported_by_LLVM)
812 typename internal::LLVMOptimizer<ReturnType>::OptimizerType;
813 optimizer.reset(
new Optimizer_t());
834# include "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
#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)