Skip to content

Commit 03ced5f

Browse files
committed
[RF] JIT all generated code in one gInterpreter call
This speeds up RooFit codegen by about 20 %.
1 parent a62a650 commit 03ced5f

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

roofit/roofitcore/inc/RooFit/CodegenContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class CodegenContext {
117117
std::vector<double> const &xlArr() { return _xlArr; }
118118

119119
void collectFunction(std::string const &name);
120+
std::string const &collectedCode() { return _collectedCode; }
120121
std::vector<std::string> const &collectedFunctions() { return _collectedFunctions; }
121122

122123
std::string
@@ -207,6 +208,7 @@ class CodegenContext {
207208
std::unordered_map<RooFit::UniqueId<RooAbsCollection>::Value_t, std::string> _listNames;
208209
std::vector<double> _xlArr;
209210
std::vector<std::string> _collectedFunctions;
211+
std::string _collectedCode;
210212
};
211213

212214
template <>

roofit/roofitcore/src/RooEvaluatorWrapper.cxx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,28 @@ RooFuncWrapper::RooFuncWrapper(RooAbsReal &obj, const RooAbsData *data, RooSimul
258258
}
259259
}
260260

261-
gInterpreter->Declare("#pragma cling optimize(2)");
262-
263261
// Declare the function and create its derivative.
264262
auto print = [](std::string const &msg) { oocoutI(nullptr, Fitting) << msg << std::endl; };
265263
ROOT::Math::Util::TimingScope timingScope(print, "Function JIT time:");
266264
_funcName = ctx.buildFunction(obj, nodeOutputSizes);
265+
266+
// Make sure the codegen implementations are known to the interpreter
267+
gInterpreter->Declare("#include <RooFit/CodegenImpl.h>\n");
268+
269+
if (!gInterpreter->Declare(ctx.collectedCode().c_str())) {
270+
std::stringstream errorMsg;
271+
std::string debugFileName = "_codegen_" + _funcName + ".cxx";
272+
errorMsg << "Function " << _funcName << " could not be compiled. See above for details. Full code dumped to file "
273+
<< debugFileName << "for debugging";
274+
{
275+
std::ofstream outFile;
276+
outFile.open(debugFileName.c_str());
277+
outFile << ctx.collectedCode();
278+
}
279+
oocoutE(nullptr, InputArguments) << errorMsg.str() << std::endl;
280+
throw std::runtime_error(errorMsg.str().c_str());
281+
}
282+
267283
_func = reinterpret_cast<Func>(gInterpreter->ProcessLine((_funcName + ";").c_str()));
268284

269285
_xlArr = ctx.xlArr();
@@ -385,7 +401,6 @@ void RooFuncWrapper::writeDebugMacro(std::string const &filename) const
385401
#include <RooFit/Detail/MathFuncs.h>
386402
#include <Math/CladDerivator.h>
387403
388-
#pragma cling optimize(2)
389404
)" << allCode.str()
390405
<< R"(
391406
#pragma clad ON

roofit/roofitcore/src/RooFit/CodegenContext.cxx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,11 @@ CodegenContext::buildFunction(RooAbsArg const &arg, std::map<RooFit::Detail::Dat
341341
}
342342
ctx._xlArr = _xlArr;
343343
ctx._collectedFunctions = _collectedFunctions;
344+
ctx._collectedCode = _collectedCode;
344345

345346
static int iCodegen = 0;
346347
auto funcName = "roo_codegen_" + std::to_string(iCodegen++);
347348

348-
// Make sure the codegen implementations are known to the interpreter
349-
gInterpreter->Declare("#include <RooFit/CodegenImpl.h>\n");
350-
351349
ctx.pushScope();
352350
std::string funcBody = ctx.getResult(arg);
353351
ctx.popScope();
@@ -357,24 +355,13 @@ CodegenContext::buildFunction(RooAbsArg const &arg, std::map<RooFit::Detail::Dat
357355
std::stringstream bodyWithSigStrm;
358356
bodyWithSigStrm << "double " << funcName << "(double* params, double const* obs, double const* xlArr) {\n"
359357
<< "constexpr double inf = std::numeric_limits<double>::infinity();\n"
360-
<< funcBody << "\n}";
358+
<< funcBody << "\n}\n\n";
361359
ctx._collectedFunctions.emplace_back(funcName);
362-
if (!gInterpreter->Declare(bodyWithSigStrm.str().c_str())) {
363-
std::stringstream errorMsg;
364-
std::string debugFileName = "_codegen_" + funcName + ".cxx";
365-
errorMsg << "Function " << funcName << " could not be compiled. See above for details. Full code dumped to file "
366-
<< debugFileName << "for debugging";
367-
{
368-
std::ofstream outFile;
369-
outFile.open(debugFileName.c_str());
370-
outFile << bodyWithSigStrm.str();
371-
}
372-
oocoutE(nullptr, InputArguments) << errorMsg.str() << std::endl;
373-
throw std::runtime_error(errorMsg.str().c_str());
374-
}
360+
ctx._collectedCode += bodyWithSigStrm.str();
375361

376362
_xlArr = ctx._xlArr;
377363
_collectedFunctions = ctx._collectedFunctions;
364+
_collectedCode = ctx._collectedCode;
378365

379366
return funcName;
380367
}

0 commit comments

Comments
 (0)