Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleParallelWriter.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleParallelWriter.hxx
2/// \ingroup NTuple
3/// \author Jonas Hahnfeld <jonas.hahnfeld@cern.ch>
4/// \date 2024-02-01
5
6/*************************************************************************
7 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#ifndef ROOT_RNTupleParallelWriter
15#define ROOT_RNTupleParallelWriter
16
19
20#include <memory>
21#include <mutex>
22#include <string_view>
23#include <vector>
24
25class TDirectory;
26
27namespace ROOT {
28
29class RNTupleModel;
30
31namespace Internal {
32class RPageSink;
33} // namespace Internal
34
35class RNTupleFillContext;
36
37/**
38\class ROOT::RNTupleParallelWriter
39\ingroup NTuple
40\brief A writer to fill an RNTuple from multiple contexts
41
42Compared to the sequential RNTupleWriter, a parallel writer enables the creation of multiple RNTupleFillContext (see
43CreateFillContext()). Each fill context prepares independent clusters that are appended to the common RNTuple with
44internal synchronization. Before destruction, all fill contexts must have flushed their data and been destroyed (or
45data could be lost!).
46
47For user convenience, CreateFillContext() is thread-safe and may be called from multiple threads in parallel at any
48time, also after some data has already been written. Internally, the original model is cloned and ownership is passed
49to a newly created RNTupleFillContext. For that reason, it is recommended to use RNTupleModel::CreateBare when creating
50the model for parallel writing and avoid the allocation of a useless default REntry per context.
51
52Note that the sequence of independently prepared clusters is indeterminate and therefore entries are only partially
53ordered: Entries from one context are totally ordered as they were filled. However, there is no orderering with other
54contexts and the entries may be appended to the RNTuple either before or after other entries written in parallel into
55other contexts. In addition, two consecutive entries in one fill context can end up separated in the final RNTuple, if
56they happen to fall onto a cluster boundary and other contexts append more entries before the next cluster is full.
57
58At the moment, the parallel writer does not (yet) support incremental updates of the underlying model. Please refer to
59RNTupleWriter::CreateModelUpdater if required for your use case.
60*/
62private:
63 /// A global mutex to protect the internal data structures of this object.
64 std::mutex fMutex;
65 /// A mutex to synchronize the final page sink.
66 std::mutex fSinkMutex;
67 /// The final RPageSink that represents the synchronization point.
68 std::unique_ptr<ROOT::Internal::RPageSink> fSink;
69 /// The original RNTupleModel connected to fSink; needs to be destructed before it.
70 std::unique_ptr<ROOT::RNTupleModel> fModel;
72 /// List of all created helpers. They must be destroyed before this RNTupleParallelWriter is destructed.
73 std::vector<std::weak_ptr<RNTupleFillContext>> fFillContexts;
74
75 RNTupleParallelWriter(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<ROOT::Internal::RPageSink> sink);
78
79public:
80 /// Recreate a new file and return a writer to write an RNTuple.
81 static std::unique_ptr<RNTupleParallelWriter>
82 Recreate(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName, std::string_view storage,
84 /// Append an RNTuple to the existing file.
85 ///
86 /// While the writer synchronizes between multiple fill contexts created from the same writer, there is no
87 /// synchronization with other writers or other clients that write into the same file. The caller must ensure that
88 /// the underlying file is not be accessed while data is filled into any created context. To improve performance, it
89 /// is allowed to use special methods that are guaranteed to not interact with the underlying file, such as
90 /// RNTupleFillContext::FillNoFlush().
91 static std::unique_ptr<RNTupleParallelWriter>
92 Append(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName, TDirectory &fileOrDirectory,
94
96
97 /// Create a new RNTupleFillContext that can be used to fill entries and prepare clusters in parallel. This method is
98 /// thread-safe and may be called from multiple threads in parallel at any time, also after some data has already
99 /// been written.
100 ///
101 /// Note that all fill contexts must be destroyed before CommitDataset() is called.
102 std::shared_ptr<RNTupleFillContext> CreateFillContext();
103
104 /// Automatically called by the destructor
105 void CommitDataset();
106
109};
110
111} // namespace ROOT
112
113#endif
A collection of Counter objects with a name, a unit, and a description.
A writer to fill an RNTuple from multiple contexts.
static std::unique_ptr< RNTupleParallelWriter > Recreate(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Recreate a new file and return a writer to write an RNTuple.
RNTupleParallelWriter(const RNTupleParallelWriter &)=delete
std::vector< std::weak_ptr< RNTupleFillContext > > fFillContexts
List of all created helpers. They must be destroyed before this RNTupleParallelWriter is destructed.
const Experimental::Detail::RNTupleMetrics & GetMetrics() const
static std::unique_ptr< RNTupleParallelWriter > Append(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, TDirectory &fileOrDirectory, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Append an RNTuple to the existing file.
Experimental::Detail::RNTupleMetrics fMetrics
void CommitDataset()
Automatically called by the destructor.
std::mutex fMutex
A global mutex to protect the internal data structures of this object.
std::unique_ptr< ROOT::Internal::RPageSink > fSink
The final RPageSink that represents the synchronization point.
RNTupleParallelWriter(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< ROOT::Internal::RPageSink > sink)
RNTupleParallelWriter & operator=(const RNTupleParallelWriter &)=delete
std::shared_ptr< RNTupleFillContext > CreateFillContext()
Create a new RNTupleFillContext that can be used to fill entries and prepare clusters in parallel.
std::unique_ptr< ROOT::RNTupleModel > fModel
The original RNTupleModel connected to fSink; needs to be destructed before it.
std::mutex fSinkMutex
A mutex to synchronize the final page sink.
Common user-tunable settings for storing RNTuples.
Describe directory structure in memory.
Definition TDirectory.h:45