Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:23:04

0001 #ifndef TMVA_SOFIE_SOFIE_HELPERS
0002 #define TMVA_SOFIE_SOFIE_HELPERS
0003 
0004 
0005 #include <type_traits>
0006 #include <utility>
0007 #include <vector>
0008 #include <string>
0009 
0010 
0011 namespace TMVA{
0012 namespace Experimental{
0013 
0014 ///Helper class used by SOFIEFunctor to wrap the
0015 ///infer signature interface to RDataFrame
0016 template <typename I, typename F, typename T>
0017 class SofieFunctorHelper;
0018 
0019 template <std::size_t... N,  typename Session_t, typename T>
0020 class SofieFunctorHelper<std::index_sequence<N...>, Session_t, T> {
0021    /// this is the magic to define the operator() with N fixed parameter arguments
0022    template <std::size_t Idx>
0023    using AlwaysT = T;
0024 
0025    std::vector<std::vector<T>> fInput;
0026    std::vector<Session_t> fSessions;
0027 
0028 public:
0029 
0030    SofieFunctorHelper(unsigned int nslots = 0, const std::string & filename = "") :
0031       fInput(1)
0032    {
0033       // create Sessions according to given number of slots.
0034       // if number of slots is zero create a single session
0035       if (nslots < 1) nslots = 1;
0036       fInput.resize(nslots);
0037       fSessions.reserve(nslots);
0038       for (unsigned int i = 0; i < nslots; i++) {
0039          if (filename.empty())
0040             fSessions.emplace_back();
0041          else
0042             fSessions.emplace_back(filename);
0043       }
0044    }
0045 
0046    double operator()(unsigned slot, AlwaysT<N>... args) {
0047       fInput[slot] = {args...};
0048       auto y =  fSessions[slot].infer(fInput[slot].data());
0049       return y[0];
0050    }
0051 };
0052 
0053 /// SofieFunctor : used to wrap the infer function of the
0054 /// generated model by SOFIE in a RDF compatible signature.
0055 /// The number of slots is an optional parameter used to
0056 /// create multiple SOFIE Sessions, which can be run in a parallel
0057 /// model evaluation. One shouild use as number of slots the number of slots used by
0058 /// RDataFrame. By default, in case of `nslots=0`, only a single Session will be created
0059 /// and the Functor cannot be run in parallel.
0060 /// Examples of using the SofieFunctor are the C++ tutorial TMVA_SOFIE_RDataFrame.C
0061 /// and the Python tutorial TMVA_SOFIE_RDataFrame.py which makes use of the ROOT JIT
0062 /// to compile on the fly the generated SOFIE model.
0063 template <std::size_t N, typename Session_t>
0064 auto SofieFunctor(unsigned int nslots = 0, const std::string & weightsFile = "") -> SofieFunctorHelper<std::make_index_sequence<N>, Session_t, float>
0065 {
0066    return SofieFunctorHelper<std::make_index_sequence<N>, Session_t, float>(nslots, weightsFile);
0067 }
0068 
0069 }//Experimental
0070 }//TMVA
0071 
0072 #endif //TMVA_SOFIE_SOFIE_HELPERS