1#ifndef AMPGEN_COMPILEDEXPRESSION_H
2#define AMPGEN_COMPILEDEXPRESSION_H
28 static constexpr unsigned value =
sizeof(T);
31 static constexpr unsigned value = 0;
42 DynamicFCN<void(
const size_t &,
const size_t &,
const size_t &, ret_type *, arg_types...)> m_batchFcn;
44 std::vector<real_t> m_externals = {};
45 bool m_hasExternalsChanged = {
false};
51 template <
typename... namedArgs>
60 auto process_argument = [
this, &mps](
const auto &arg)
mutable {
62 if constexpr(std::is_convertible<
decltype(arg),
DebugSymbols>::value) {
64 }
else if constexpr(std::is_convertible<
decltype(arg), std::map<std::string, unsigned>>::value)
66 else if constexpr(std::is_convertible<
decltype(arg),
const MinuitParameterSet *>::value
73 else if constexpr(std::is_convertible<
decltype(arg), disableBatch>::value) {
74 DEBUG(
"Disabling bulk evaluation: did you do this on purpose?");
76 }
else if constexpr(std::is_convertible<
decltype(arg), includePythonBindings>::value) {
78 }
else if constexpr(std::is_convertible<
decltype(arg), includeParameters>::value) {
83 for_each(std::tuple<const namedArgs &...>(args...), process_argument);
84 if(mps ==
nullptr) {
DEBUG(
"No minuit parameterset linked."); }
86 if constexpr(std::is_same<ret_type, void>::value) {
87 typedef typename std::remove_pointer<
zeroType<arg_types...>>::type zt;
90 }
else if constexpr(isVector<ret_type>::value) {
102 std::vector<real_t>
externBuffer()
const override {
return m_externals; }
104 bool use_rto()
const override {
return std::is_same<ret_type, void>::value; }
105 std::vector<std::string>
types()
const override {
return typelist<arg_types...>(); }
107 void setExternals(
const std::vector<double> &external) { m_externals = external; }
109 unsigned int getNParams()
const {
return m_externals.size(); }
114 INFO(
"IsReady? = " <<
isReady() <<
" IsLinked? " << (m_fcn.isLinked()));
117 for(
auto &c : func) c->print();
120 void setExternal(
const double &value,
const unsigned int &address)
override {
121 if(m_externals[address] == value)
return;
122 DEBUG(
"Setting external " << address <<
" / " << m_externals.size() <<
" to value = " << value <<
" ; current = " << m_externals[address]);
123 m_externals[address] = value;
124 m_hasExternalsChanged =
true;
127 if(m_externals.size() < N) m_externals.resize(N);
133 bool isReady()
const override {
return m_fcn.isLinked(); }
138 template <
typename T> ret_type
operator()(
const T *event)
const {
return m_fcn(m_externals.data(), event); }
139 ret_type
operator()(
const arg_types &...args)
const {
return m_fcn(args...); }
140 template <
typename... batch_arg_types>
void batch(batch_arg_types... args)
const { m_batchFcn(args...); }
142 template <
typename T>
void debug(
const T *event)
const {
143 if(!m_fcn.isLinked()) {
FATAL(
"Function " <<
name() <<
" not linked"); }
144 if(!m_fdb.isLinked()) {
FATAL(
"Function" <<
name() <<
" debugging symbols not linked"); }
145 std::vector<std::pair<std::string, complex_v>> debug_results;
146 if constexpr(std::is_same<void, ret_type>::value)
147 debug_results = m_fdb(
nullptr, 0, &(m_externals[0]), event);
149 debug_results = m_fdb(&(m_externals[0]), event);
150 for(
auto &debug_result : debug_results) {
151 auto val = debug_result.second;
152 auto label = debug_result.first;
154 std::cout <<
bold_on << std::setw(50) << std::left << label <<
bold_off << std::endl;
156 std::cout <<
" " << std::setw(50) << std::left << label <<
" = " << val.real() << std::endl;
158 std::cout <<
" " << std::setw(50) << std::left << label <<
" = " << val << std::endl;
162 bool link(
void *handle)
override {
163 const std::string symbol =
progName();
165 status &= m_fcn.set(handle, symbol,
true);
166 status &=
m_db.size() == 0 || m_fdb.set(handle, symbol +
"_DB");
167 if(!
m_disableBatch) status &= m_batchFcn.set(handle, symbol +
"_batch");
170 bool link(
const std::string &handle)
override {
return link(dlopen(handle.c_str(), RTLD_NOW)); };
171 std::string
arg_type(
const unsigned &i)
const override {
return typelist<arg_types...>()[i]; }
174 template <
typename return_type>
176 CompiledExpression<void(return_type *,
const double *,
const double *)> rt(expression, name);
182 template <
typename return_type>
189 template <
typename return_type,
typename arg1 = double,
typename arg2 = double,
typename... arg_types>
191 CompiledExpression<return_type(
const arg1 *,
const arg2 *)> rt(expression, name, args...);
#define DECLARE_ARGUMENT(X, Y)
void print() const override
bool hasExternalsChanged()
bool link(void *handle) override
std::vector< std::string > types() const override
void batch(batch_arg_types... args) const
bool isReady() const override
unsigned int getNParams() const
ret_type operator()(const T *event) const
const Expression & expression() const
CompiledExpression(const std::string &name="")
std::string returnTypename() const override
bool use_rto() const override
void resolve(const MinuitParameterSet *mps=nullptr)
bool link(const std::string &handle) override
ret_type operator()(const arg_types &...args) const
void setExternals(const std::vector< double > &external)
void set(const Expression &expression, const std::string &name, const namedArgs &...args)
CompiledExpression(const Expression &expression, const std::string &name, const namedArgs &...args)
void setExternal(const double &value, const unsigned int &address) override
void resizeExternalCache(const size_t &N) override
std::vector< real_t > externBuffer() const override
unsigned returnTypeSize() const override
void setDebug(const DebugSymbols &db)
void debug(const T *event) const
std::string arg_type(const unsigned &i) const override
void resolve(const MinuitParameterSet *mps=nullptr)
std::string progName() const
std::map< std::string, unsigned > m_evtMap
std::vector< const CacheTransfer * > orderedCacheFunctors() const
bool m_includePythonBindings
unsigned int hash() const
Wrapper class for shared_ptrs to virtual expressions for use in conjunction with operators to build e...
#define ERROR(X)
Used for printing errors messages, and will always be printed.
#define INFO(X)
Used for printing information messages, and will always be printed.
#define DEBUG(X)
Used for printing verbose debugging messages, only if DEBUGLEVEL is defined.
#define FATAL(X)
Used for printing fatal errors messages, and will always be printed and will terminate the process af...
bool all_of(const simd_type &obj)
std::string vectorToString(iterator_type begin, iterator_type end, const std::string &delim, functor_type fcn)
typename detail::zeroType< args... >::type zeroType
CompiledExpression< void(return_type *, const double *, const double *)> make_rto_expression(const Expression &expression, const std::string &name)
std::ostream & bold_on(std::ostream &)
bool is(const Expression &expression)
std::ostream & bold_off(std::ostream &)
CompiledExpression< return_type(const double *, const double *)> make_expression(const Expression &expression, const std::string &name)
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
std::vector< DebugSymbol > DebugSymbols
T cast(const Expression &expression)
std::enable_if_t< I==sizeof...(Tp), void > for_each(std::tuple< Tp... > &, FuncT)
Wrapper to give templated interface to a function contained in a dynamically linked library.
static constexpr unsigned value
static constexpr unsigned value