Skip to content

[RF] Speed up getting weights from RooDataHist#7859

Merged
guitargeek merged 5 commits intoroot-project:masterfrom
guitargeek:RooDataHist_1
Apr 23, 2021
Merged

[RF] Speed up getting weights from RooDataHist#7859
guitargeek merged 5 commits intoroot-project:masterfrom
guitargeek:RooDataHist_1

Conversation

@guitargeek
Copy link
Copy Markdown
Contributor

@guitargeek guitargeek commented Apr 13, 2021

Retreiving weights from a RooDataHist can be sped up signifcantly if it
is assumed that parameters passed to RooDataHist::weight are aligned
with the histogram variables.

This commit introduces a RooDataHist::weightFast function that makes
this assumption. It is used by RooHistFunc and RooHistPdf.

This was benchmarked with the same hist factory example as in #7838:

The effect is a 15 % speedup of the HistFactory example.

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@guitargeek guitargeek self-assigned this Apr 13, 2021
@phsft-bot
Copy link
Copy Markdown

Copy link
Copy Markdown
Member

@hageboeck hageboeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see that you find some opportunities for speed improvements!

// Asserts commented out because the code is performance critical.

// With fast, caller promises that layout of "coords" is identical to our internal "vars".
// assert(!fast || _vars.size() == coords.size());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this out: assert
It's the magic weapon for things that "should", but you cannot be sure that they "are", unless you get c++ contracts.
Note that there is no run time overhead in release builds - they don't generate any code.

Now, imagine the following:
A user comes to the forum and tells you that xy yields wrong results when they do A, but with B it works. It doesn't crash, the results are just wrong, and nobody knows where the error happens.
Those are the bugs that take long to track down.
You ask for the code, run it in your debug build that you should always have around, the assert fails and you tell the user what's wrong or fix the bug.

Long story short:
The asserts should stay in, and please use them to assert all things where you go "hmm, this should be OK if people (or you) used it correctly".
For things that are not performance critical, it's of course even better to validate inputs before you start a fit etc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For things that are not performance critical, it's of course even better to validate inputs before you start a fit etc.

I mean run-time checks that are active also in release builds, but this is mostly for the initialisation phase, while the asserts are better in the execution phase when you want speed.

Comment on lines +1012 to +1013
// If fast is on, users promise that the sets have the same layout.
// assert(!fast || strcmp(internalVar->GetName(), theVar->GetName()) == 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here and below.

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@guitargeek guitargeek changed the title [RF] Speedup RooDataHist in case of no interpolation [RF] Speed up RooDataHist Apr 14, 2021
@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@guitargeek guitargeek changed the title [RF] Speed up RooDataHist [RF] Speed up getting weights from RooDataHist Apr 14, 2021
@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-debian10-i386/cxx14.
Running on pcepsft10.dyndns.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-ubuntu16/nortcxxmod.
Running on sft-ubuntu-1604-4.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-04-20T16:25:22.845Z] FAILED: /usr/bin/ccache /usr/bin/c++ -DVECCORE_ENABLE_VC -I/mnt/build/workspace/root-pullrequests-build/root/roofit/roofitcore/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/unix/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/clingutils/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/textinput/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/thread/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/zip/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/rint/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/clib/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/meta/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/gui/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/cont/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/foundation/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/base/inc -Iginclude -I/mnt/build/workspace/root-pullrequests-build/root/hist/hist/inc -I/mnt/build/workspace/root-pullrequests-build/root/math/mathcore/inc -I/mnt/build/workspace/root-pullrequests-build/root/math/mathcore/v7/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/imt/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/multiproc/inc -Iexternals/mnt/build/workspace/root-pullrequests-build/install/include -I/mnt/build/workspace/root-pullrequests-build/root/math/matrix/inc -I/mnt/build/workspace/root-pullrequests-build/root/graf2d/graf/inc -I/mnt/build/workspace/root-pullrequests-build/root/io/io/inc -I/mnt/build/workspace/root-pullrequests-build/root/core/clib/res -I/mnt/build/workspace/root-pullrequests-build/root/builtins -I/usr/include/freetype2 -I/usr/include/x86_64-linux-gnu/freetype2 -I/mnt/build/workspace/root-pullrequests-build/root/tree/tree/inc -I/mnt/build/workspace/root-pullrequests-build/root/math/minuit/inc -I/mnt/build/workspace/root-pullrequests-build/root/math/foam/inc -I/mnt/build/workspace/root-pullrequests-build/root/math/smatrix/inc -I/mnt/build/workspace/root-pullrequests-build/root/roofit/batchcompute/inc -Iroofit/roofitcore/test -I/mnt/build/workspace/root-pullrequests-build/root/net/net/inc -I/mnt/build/workspace/root-pullrequests-build/root/test/unit_testing_support -isystem googletest-prefix/src/googletest/googletest/include -isystem googletest-prefix/src/googletest/googlemock/include -fdiagnostics-color=always -std=c++11 -pipe -Wshadow -Wall -W -Woverloaded-virtual -fsigned-char -pthread -O3 -fno-math-errno -std=c++11 -MD -MT roofit/roofitcore/test/CMakeFiles/testRooDataHist.dir/testRooDataHist.cxx.o -MF roofit/roofitcore/test/CMakeFiles/testRooDataHist.dir/testRooDataHist.cxx.o.d -o roofit/roofitcore/test/CMakeFiles/testRooDataHist.dir/testRooDataHist.cxx.o -c /mnt/build/workspace/root-pullrequests-build/root/roofit/roofitcore/test/testRooDataHist.cxx
  • [2021-04-20T16:25:22.845Z] /mnt/build/workspace/root-pullrequests-build/root/roofit/roofitcore/test/testRooDataHist.cxx:675:22: error: ‘clamp’ is not a member of ‘std’
  • [2021-04-20T16:25:22.845Z] /mnt/build/workspace/root-pullrequests-build/root/roofit/roofitcore/test/testRooDataHist.cxx:676:22: error: ‘clamp’ is not a member of ‘std’
  • [2021-04-20T16:25:22.845Z] /mnt/build/workspace/root-pullrequests-build/root/roofit/roofitcore/test/testRooDataHist.cxx:677:15: error: ‘clamp’ is not a member of ‘std’
  • [2021-04-20T16:25:22.845Z] /mnt/build/workspace/root-pullrequests-build/root/roofit/roofitcore/test/testRooDataHist.cxx:685:9: error: invalid use of void expression

@guitargeek
Copy link
Copy Markdown
Contributor Author

Okay, I can update the code with less auto! And thanks for the heads up with the streamer.

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-ubuntu16/nortcxxmod.
Running on sft-ubuntu-1604-3.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@guitargeek
Copy link
Copy Markdown
Contributor Author

Hi! Some updates on this PR:

  1. I have made some of the auto explicit as suggested by @hageboeck
  2. @pcanal confirmed in private chat that filling a temporary RooArgSet is the right solution for the legacy streamer
  3. I tested that RooDataHists written with ROOT 6.24 can still be read with this PR, as suggested by @lmoneta

Copy link
Copy Markdown
Member

@hageboeck hageboeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's looking good, but I have some suggestions to take it all the way.

Sorry that I insist on documentation, but you will never be so close to getting the docs done, because you should still remember what the functions and parameters mean.
In 3 months, this documentation won't get touched again.

Where is the schema evolution read test that you mentioned? Is it one of the tests that I implemented?

double* _binv {nullptr}; //[_arrSize] Bin volume array

RooArgSet _realVars ; // Real dimensions of the dataset
RooArgSet _realVars ; // Real dimensions of the dataset
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to keep formatting changes and code changes separate. (No need to change anything in the commit, though. 👍 )

////////////////////////////////////////////////////////////////////////////////
/// A faster version of RooDataHist::weight that assumes the passed arguments
/// are aligned with the histogram variables.
///
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever you touch a comment, try to write full doxygen style with documentation of the parameters.
Random example:

////////////////////////////////////////////////////////////////////////////////
/// Copy a RooBinSamplingPdf.
/// \param[in] other PDF to copy.
/// \param[in] name Optionally rename the copy.
RooBinSamplingPdf::RooBinSamplingPdf(const RooBinSamplingPdf& other, const char* name) :

I know it's more work, but if you don't do it right now, it won't get done in a long time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can do that.

Comment on lines 221 to 223
// cout << "RooHistPdf::evaluate(" << GetName() << ") ret = " << ret << " ";
// cout << _histObsList[0] << " ";
// _histObsList[0]->Print("");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kill useless code when you get close to it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

VarInfo const& varInfo = getVarInfo();

Double_t wInt(0) ;
auto centralIdx = calcTreeIndex(bin, true);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's very optional, but you could make temporary stuff const. You don't inadvertently overwrite it and make sure that it gets optimised out. (It will of course also be optimised out when it's not const, unless you write to it by accident.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's a good thing to do!

hist.Fill(1.5, 1.5);
}

hist.Print();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to be reasonably silent in unit tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll delete that print statement!


RooDataHist dataHist("data", "dataset with (x,y)", RooArgList(x, y), &hist);

dataHist.Print();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also silence this if possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll delete that print statement!

Retreiving weights from a RooDataHist can be sped up signifcantly if it
is assumed that parameters passed to `RooDataHist::weight` are aligned
with the histogram variables.

This commit introduces a `RooDataHist::weightFast` function that makes
this assumption. It is used by RooHistFunc and RooHistPdf.
Getting the weights from a RooDataHist is now also sped up for the
interpolation case with the assumption that the arguemtns passed to
`RooDataHist::weightFast` are aligned with the histogram variables.

To achieve this, the interpolation routines were changed such that the
value of the parameters is never set.
@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@guitargeek
Copy link
Copy Markdown
Contributor Author

Hi @hageboeck! For the schema evolution from the previous version, I just made a test myself where I wrote a RooDataHist to a file with ROOT 6.24 and read it back with ROOT master + this PR. Do you think I should implement a unit test for that? I didn't think this was necessary, after all the change is rather trivial (removal of _realVars class member).

Copy link
Copy Markdown
Member

@hageboeck hageboeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! 👍

@guitargeek guitargeek merged commit a6d4653 into root-project:master Apr 23, 2021
@guitargeek guitargeek deleted the RooDataHist_1 branch April 23, 2021 09:36
@guitargeek guitargeek mentioned this pull request Jun 3, 2021
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants