Skip to content

Instantly share code, notes, and snippets.

@eguiraud
Created May 18, 2017 22:20
Show Gist options
  • Save eguiraud/73339f2d4e0a4f317dc6eced6037eba1 to your computer and use it in GitHub Desktop.
Save eguiraud/73339f2d4e0a4f317dc6eced6037eba1 to your computer and use it in GitHub Desktop.
TDataSource proposal
// TDataSource
//
// The problem it tries to solve:
// - accessing specific columns of any tabular data-set through a uniform interface
// - allow thread-safe parallel processing of the data (by slicing the data-set in nThreads pieces)
// - offer the simplest interface possible, ideally iterator-based
// 1. -- build the data source
TDataSource s("file.root");
// 2. -- grab a range to iterate over + handles to the desired columns
/* c++17 */ auto [r, a, b] = s.GetReader<int, double>("a", "b");
/* or also */ auto [r, a, b] = s.GetReader({int(), "a"}, {double(), "b"});
/* c++11 */ TDS::Reader<int, double> r;
int a;
double b;
std::tie<r, a, b> = s.GetReader<int,double>("a", "b");
/* w/ macro */ TDS_GET_READER(s, r, int, a, double, b);
// 3. -- loop over the dataset (n will contain the row number)
for(auto n : range)
std::cout << n << " " << a << " " << b << "\n";
// parallel reading
// grab two iterators over separate halves of the data
// two threads can safely use one iterator each
auto [r1, x1, y1] = s.GetReader<int, double>("a", "b", 2, 0); // split in two, give me first half
auto [r2, x2, y2] = s.GetReader<int, double>("a", "b", 2, 1); // split in two, give me second half
auto loop = [](auto &r, auto &a, auto &b) {
for (auto n : r) std::cout << n << " " << a << " " << b << "\n";
};
std::thread t1(loop, r1, a1, b1);
std::thread t2(loop, r2, a2, b2);
// parallel reading 2
// this time each thread is responsible of grabbing its own iterator
auto loop = [](unsigned slot) {
auto[r, x, y] = s.GetReader<int, double>("a", "b", nThreads, slot);
for (auto n : r) std::cout << n << " " << a << " " << b << "\n";
};
for(auto i = 0u; i < nThreads; ++i)
std::thread(loop, i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment