# Snapshot shuffles data invisibly with ImplicitMT

**URL:** <https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473>\
**Category:** ROOT\
**Tags:** bug, rdataframe\
**Created:** [February 9, 2021, 4:56pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473 "2021-02-09T16:56:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [February 9, 2021, 4:56pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/1 "2021-02-09T16:56:42Z")

</div>

If I have enabled ImplicitMT, `RDataFrame::Snapshot` does not preserve the row order. Moreover, this happens completely invisibly with no errors or warnings, so the user is left with shuffled rows and no indication this happened without manually checking. This strikes me as a feature-breaking bug.

Reproducer below.

* * *

_ROOT Version:_ 6.22/06  
_Platform:_ macOS  
_Compiler:_ conda-forge

* * *

```python
import ROOT as r
r.ROOT.EnableImplicitMT(4)
rdf = r.RDataFrame(10).Define("e", "rdfentry_")
rdf.Snapshot("test", "test.root")
f = r.TFile.Open("test.root")
f.test.Scan()

```

Output:

```auto
************************
* Row * e.e *
************************
* 0 * 7 *
* 1 * 0 *
* 2 * 1 *
* 3 * 8 *
* 4 * 6 *
* 5 * 2 *
* 6 * 3 *
* 7 * 9 *
* 8 * 4 *
* 9 * 5 *
************************

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 9, 2021, 7:08pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/2 "2021-02-09T19:08:50Z")

</div>

Hi,  
it’s not a bug, if that TTree is used as a friend of another tree we warn users that it is a TTree that has been written by a “shuffling” operation.

What’s your usecase that’s broken by this behavior?

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [February 9, 2021, 7:26pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/3 "2021-02-09T19:26:04Z")

</div>

Well, anything that depends on the order of the rows, but mainly friend trees. If friends are considered the only use-case for row-ordering, then indeed, this is not a bug.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 10, 2021, 8:38am UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/4 "2021-02-10T08:38:55Z")

</div>

In many cases rows are independent (as in, independent physical events) and order does not matter. Order relative to other TTrees of course matters, hence the problem with friend trees.

In 6.22 you can’t use a “shuffled tree” as a friend unless you manually unset a certain flag in that TTree.

In 6.24 (being released in O(few weeks)) we filled a feature gap and added support for _indexed_ friend trees in RDataFrame, so you can use one of the TTree columns as an index to recover ordered access into the shuffled TTree if needed.

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [February 10, 2021, 12:05pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/5 "2021-02-10T12:05:36Z")

</div>

This seems like strange behavior for an (otherwise) ordered data structure, but friend trees are my only use-case, so this doesn’t currently break anything for me.

There are, however, workflows where this could be a problem. For example:  
`Tree1` created in ROOT  
 → `Tree2` created from `Tree1` using ROOT with ImplicitMT  
 → `Tree3` created from `Tree2` using pandas dataframe  
 → `Tree1.AddFriend(Tree3)`  
This involves leaving the ROOT package, and one could see it as a case of outside programs not providing feature support. But since ROOT is designed to work with other storage formats and be somewhat interoperable, I think it would be appropriate for Snapshot to throw a warning when writing out with ImplicitMT turned on.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 10, 2021, 12:23pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/6 "2021-02-10T12:23:06Z")

</div>

I think a runtime warning is too much (it would affect all programs that currently use RDF+multi-thread+Snapshot, and that’s a lot of programs) – but we can certainly add a big-letter warning to Snapshot’s docs.

It would be also nice to have an option to force Snapshot to maintain entry ordering (at the cost of performance and RAM): that’s a stretch goal for this year’s plan of work.

---

<div class="post-metadata">

**Author:** ![nmangane](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nmangane/32/23112_2.png) [@nmangane](https://root-forum.cern.ch/u/nmangane)\
**Post date:** [February 11, 2021, 1:04pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/7 "2021-02-11T13:04:56Z")

</div>

Would this operate via a cache for ‘forward’ results that wait until the thread handling earlier chunks finishes and writes out?

By the way, is simultaneous snapshots supported right now? I think I tried this in version 6.20 or 6.18 and trying to snapshot two different nodes at the same time resulted in seg faults (i.e. I split the data into two or four distinct datasets and want to dump them to separate root files with one processing loop). I ended up predicting the largest of them (pretty asymmetric), writing that one out immediately and putting the remaining sets into Cache, then snapshotting those one at a time. Of course, this is shifting performance bottlenecks around and had some severe limitations with the memory available.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 11, 2021, 1:16pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/8 "2021-02-11T13:16:04Z")

</div>

> [@nmangane](#):
>
> Would this operate via a cache for ‘forward’ results that wait until the thread handling earlier chunks finishes and writes out?

That’s the only idea I have for now, because it works well with the design of TBufferMerger, which Snapshot uses for multi-thread writes to a ROOT file – there is already a thread-safe queue of buffered “clusters ready to be written out”, we would “just” need to switch from FIFO to an ordered processing (much easier said than done, but it’s what I have so far). The main problem is that memory usage, in this scenario, might have hard-to-predict, highly undesirable long tails.

> [@nmangane](#):
>
> is simultaneous snapshots supported right now?

Assuming each Snapshot writes to a different file, I think it has always been supported. In other words, please report the bug, ideally with a self-contained reproducer, at [Issues · root-project/root · GitHub](https://github.com/root-project/root/issues) .

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 19, 2021, 2:32pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/9 "2021-02-19T14:32:43Z")

</div>

[[DF][NFC][skip-ci] Warn about "shuffled" output TTrees in Snapshot docs by eguiraud · Pull Request #7259 · root-project/root · GitHub](https://github.com/root-project/root/pull/7259) adds a warning about this behavior to Snapshot’s docs.

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [February 19, 2021, 2:45pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/10 "2021-02-19T14:45:54Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 5, 2021, 2:46pm UTC](https://root-forum.cern.ch/t/snapshot-shuffles-data-invisibly-with-implicitmt/43473/11 "2021-03-05T14:46:51Z")

</div>

Closed on March 5, 2021, 2:46pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
