1#ifndef AMPGEN_COMPILEDEXPRESSION_H
2#define AMPGEN_COMPILEDEXPRESSION_H
28 template <
typename T>
struct size_of {
static constexpr unsigned value =
sizeof(T); };
29 template <>
struct size_of<void> {
static constexpr unsigned value = 0; } ;
36 template <
typename ret_type,
typename... arg_types>
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};
61 auto process_argument = [
this, &mps](
const auto& arg )
mutable
64 if constexpr( std::is_convertible<
decltype(arg),
DebugSymbols>::value ){
67 else if constexpr( std::is_convertible<
decltype(arg), std::map<std::string, unsigned>>::value ) this->
m_evtMap = arg;
68 else if constexpr( std::is_convertible<
decltype(arg),
const MinuitParameterSet*>::value or
73 else if constexpr( std::is_convertible<
decltype(arg),
MinuitParameterSet>::value ) mps = &arg;
75 else if constexpr( std::is_convertible<
decltype(arg), disableBatch>::value ) {
76 DEBUG(
"Disabling bulk evaluation: did you do this on purpose?");
79 else if constexpr( std::is_convertible<
decltype(arg), includePythonBindings>::value ){
82 else if constexpr( std::is_convertible<
decltype(arg), includeParameters>::value ) {
87 for_each( std::tuple<const namedArgs&...>(args...), process_argument);
88 if( mps ==
nullptr ){
DEBUG(
"No minuit parameterset linked."); }
90 if constexpr(std::is_same<ret_type,void>::value )
92 typedef typename std::remove_pointer<
zeroType<arg_types...>>::type zt;
96 else if constexpr( isVector<ret_type>::value ){
111 std::vector<real_t>
externBuffer()
const override {
return m_externals ; }
113 bool use_rto()
const override {
return std::is_same<ret_type, void>::value; }
114 std::vector<std::string>
types()
const override {
return typelist<arg_types...>();}
116 void setExternals(
const std::vector<double>& external ) { m_externals = external; }
118 unsigned int getNParams()
const {
return m_externals.size(); }
124 INFO(
"IsReady? = " <<
isReady() <<
" IsLinked? " << (m_fcn.isLinked() ) );
127 for(
auto& c : func ) c->print() ;
130 void setExternal(
const double& value,
const unsigned int& address )
override
132 if ( m_externals[address] == value )
return;
133 DEBUG(
"Setting external " << address <<
" / " << m_externals.size() <<
" to value = " << value <<
" ; current = " << m_externals[address] );
134 m_externals[address] = value;
135 m_hasExternalsChanged =
true;
139 if( m_externals.size() < N ) m_externals.resize(N);
145 bool isReady()
const override {
return m_fcn.isLinked(); }
146 bool isLinked()
const {
return m_fcn.isLinked() ; }
150 template <
typename T > ret_type
operator()(
const T* event )
const
152 return m_fcn( m_externals.data(), event );
156 return m_fcn( args... );
158 template <
typename... batch_arg_types>
void batch( batch_arg_types... args )
const {
162 template <
typename T>
void debug(
const T* event )
const
164 if ( !m_fcn.isLinked() ) {
165 FATAL(
"Function " <<
name() <<
" not linked" );
167 if ( !m_fdb.isLinked() ) {
168 FATAL(
"Function" <<
name() <<
" debugging symbols not linked" );
170 std::vector<std::pair<std::string, complex_v>> debug_results;
171 if constexpr(std::is_same<void, ret_type>::value) debug_results = m_fdb(
nullptr, 0, &( m_externals[0] ), event );
172 else debug_results = m_fdb( &(m_externals[0]), event);
173 for(
auto& debug_result : debug_results ){
174 auto val = debug_result.second;
175 auto label = debug_result.first;
177 else if(
utils::all_of(val.imag(), 0.) ) std::cout <<
" " << std::setw(50) << std::left << label <<
" = " << val.real() << std::endl;
179 std::cout <<
" " << std::setw(50) << std::left << label <<
" = " << val << std::endl;
183 bool link(
void* handle )
override
185 const std::string symbol =
progName() ;
187 status &= m_fcn.set(handle, symbol,
true);
188 status &=
m_db.size() == 0 || m_fdb.set(handle, symbol +
"_DB");
189 if( !
m_disableBatch ) status &= m_batchFcn.set(handle, symbol +
"_batch");
192 bool link(
const std::string& handle )
override
194 return link( dlopen( handle.c_str(), RTLD_NOW ) );
196 std::string
arg_type(
const unsigned& i )
const override
198 return typelist<arg_types...>()[i];
202 template <
typename return_type>
203 CompiledExpression<void(return_type*,
const double*,
const double*)>
206 CompiledExpression<void(return_type*,
const double*,
const double*)> rt(expression,name);
212 template <
typename return_type> CompiledExpression<return_type(
const double*,
const double*)>
220 template <
typename return_type,
typename arg1 = double,
typename arg2 =double,
typename... arg_types>
221 CompiledExpression<return_type(
const arg1*,
const arg2*)>
223 const std::string& name,
224 const arg_types&... args)
226 CompiledExpression<return_type(
const arg1*,
const arg2*)> rt(expression,name,args...);
#define DECLARE_ARGUMENT(X, Y)
void print() const override
bool hasExternalsChanged()
ret_type operator()(const arg_types &... args) const
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
void setExternals(const std::vector< double > &external)
void setExternal(const double &value, const unsigned int &address) override
void resizeExternalCache(const size_t &N) override
std::vector< real_t > externBuffer() const override
CompiledExpression(const Expression &expression, const std::string &name, const namedArgs &... args)
void set(const Expression &expression, const std::string &name, const namedArgs &... args)
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