Skip to content

Commit fa1c1f9

Browse files
committed
(optionally) return unassigned groups in a different vector greatly simplifying the grouping
1 parent dac185f commit fa1c1f9

3 files changed

Lines changed: 29 additions & 58 deletions

File tree

Framework/Core/include/Framework/AnalysisTask.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -262,23 +262,6 @@ struct AnalysisDataProcessorBuilder {
262262
}
263263
}
264264

265-
template <typename Z>
266-
auto changeShifts()
267-
{
268-
constexpr auto index = framework::has_type_at_v<Z>(associated_pack_t{});
269-
if (unassignedGroups[index] > 0) {
270-
uint64_t pos;
271-
if constexpr (soa::is_soa_filtered_t<std::decay_t<G>>::value) {
272-
pos = (*groupSelection)[position];
273-
} else {
274-
pos = position;
275-
}
276-
if ((idValues[index])[pos] < 0) {
277-
++shifts[index];
278-
}
279-
}
280-
}
281-
282265
GroupSlicerIterator(G& gt, std::tuple<A...>& at)
283266
: mAt{&at},
284267
mGroupingElement{gt.begin()},
@@ -299,14 +282,12 @@ struct AnalysisDataProcessorBuilder {
299282
x.asArrowTable(),
300283
static_cast<int32_t>(gt.tableSize()),
301284
&groups[index],
302-
&idValues[index],
303285
&offsets[index]);
304286
if (result.ok() == false) {
305287
throw runtime_error("Cannot split collection");
306288
}
307-
unassignedGroups[index] = std::count_if(idValues[index].begin(), idValues[index].end(), [](auto&& x) { return x < 0; });
308-
if ((groups[index].size() - unassignedGroups[index]) > gt.tableSize()) {
309-
throw runtime_error_f("Splitting collection resulted in a larger group number (%d, %d of them unassigned) than there is rows in the grouping table (%d).", groups[index].size(), unassignedGroups[index], gt.tableSize());
289+
if (groups[index].size() > gt.tableSize()) {
290+
throw runtime_error_f("Splitting collection resulted in a larger group number (%d) than there is rows in the grouping table (%d).", groups[index].size(), gt.tableSize());
310291
};
311292
}
312293
};
@@ -331,8 +312,6 @@ struct AnalysisDataProcessorBuilder {
331312
(extractor(x), ...);
332313
},
333314
at);
334-
335-
(changeShifts<A>(), ...);
336315
}
337316

338317
template <typename B, typename... C>
@@ -410,12 +389,6 @@ struct AnalysisDataProcessorBuilder {
410389
} else {
411390
pos = position;
412391
}
413-
if (unassignedGroups[index] > 0) {
414-
if ((idValues[index])[pos + shifts[index]] < 0) {
415-
++shifts[index];
416-
}
417-
pos += shifts[index];
418-
}
419392
if constexpr (soa::is_soa_filtered_t<std::decay_t<A1>>::value) {
420393
auto groupedElementsTable = arrow::util::get<std::shared_ptr<arrow::Table>>(((groups[index])[pos]).value);
421394

@@ -446,14 +419,10 @@ struct AnalysisDataProcessorBuilder {
446419
typename grouping_t::iterator mGroupingElement;
447420
uint64_t position = 0;
448421
soa::SelectionVector const* groupSelection = nullptr;
449-
450422
std::array<std::vector<arrow::Datum>, sizeof...(A)> groups;
451-
std::array<std::vector<int32_t>, sizeof...(A)> idValues;
452423
std::array<std::vector<uint64_t>, sizeof...(A)> offsets;
453424
std::array<soa::SelectionVector const*, sizeof...(A)> selections;
454425
std::array<soa::SelectionVector::const_iterator, sizeof...(A)> starts;
455-
std::array<int, sizeof...(A)> unassignedGroups{0};
456-
std::array<int, sizeof...(A)> shifts{0};
457426
};
458427

459428
GroupSlicerIterator& begin()

Framework/Core/include/Framework/Kernels.h

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ auto sliceByColumn(
3535
std::shared_ptr<arrow::Table> const& input,
3636
T fullSize,
3737
std::vector<arrow::Datum>* slices,
38-
std::vector<int32_t>* vals = nullptr,
39-
std::vector<uint64_t>* offsets = nullptr)
38+
std::vector<uint64_t>* offsets = nullptr,
39+
std::vector<arrow::Datum>* unassignedSlices = nullptr,
40+
std::vector<uint64_t>* unassignedOffsets = nullptr)
4041
{
4142
arrow::Datum value_counts;
4243
auto options = arrow::compute::CountOptions::Defaults();
@@ -49,47 +50,49 @@ auto sliceByColumn(
4950

5051
// create slices and offsets
5152
uint64_t offset = 0;
53+
uint64_t unassignedOffset = 0;
5254
auto count = 0;
53-
5455
auto size = values.length();
55-
if (vals != nullptr) {
56-
for (auto i = 0; i < size; ++i) {
57-
vals->push_back(values.Value(i));
58-
}
59-
}
6056

61-
auto makeSlice = [&](uint64_t offset_, T count) {
62-
slices->emplace_back(arrow::Datum{input->Slice(offset_, count)});
57+
auto makeSlice = [&](uint64_t offset_, T count_) {
58+
slices->emplace_back(arrow::Datum{input->Slice(offset_, count_)});
6359
if (offsets) {
6460
offsets->emplace_back(offset_);
6561
}
6662
};
6763

68-
auto v = values.Value(0);
64+
auto makeUnassignedSlice = [&](uint64_t offset_, T count_) {
65+
if (unassignedSlices) {
66+
unassignedSlices->emplace_back(arrow::Datum{input->Slice(offset_, count_)});
67+
}
68+
if (unassignedOffsets) {
69+
unassignedOffsets->emplace_back(offset_);
70+
}
71+
};
72+
73+
auto v = 0;
6974
auto vprev = v;
70-
auto vnext = v;
7175
auto nzeros = 0;
72-
for (auto i = 0; i < size - 1; ++i) {
73-
nzeros = 0;
74-
vprev = v;
75-
v = values.Value(i);
76+
77+
for (auto i = 0; i < size; ++i) {
7678
count = counts.Value(i);
79+
if (v >= 0) {
80+
vprev = v;
81+
}
82+
v = values.Value(i);
7783
if (v < 0) {
78-
vnext = values.Value(i + 1);
79-
nzeros = vnext - vprev - 1;
80-
} else if (vprev >= 0 && (v - vprev) != 1) {
81-
nzeros = v - vprev - 1;
84+
makeUnassignedSlice(offset, count);
85+
offset += count;
86+
continue;
8287
}
88+
nzeros = v - vprev - ((i == 0) ? 0 : 1);
8389
for (auto z = 0; z < nzeros; ++z) {
8490
makeSlice(offset, 0);
8591
}
8692
makeSlice(offset, count);
8793
offset += count;
8894
}
8995

90-
makeSlice(offset, counts.Value(size - 1));
91-
offset += counts.Value(size - 1);
92-
9396
if (values.Value(size - 1) < fullSize - 1) {
9497
for (auto v = values.Value(size - 1) + 1; v < fullSize; ++v) {
9598
makeSlice(offset, 0);
@@ -98,7 +101,6 @@ auto sliceByColumn(
98101

99102
return arrow::Status::OK();
100103
}
101-
102104
} // namespace o2::framework
103105

104106
#endif // O2_FRAMEWORK_KERNELS_H_

Framework/Core/test/test_Kernels.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE(TestSlicingFramework)
7070

7171
std::vector<uint64_t> offsets;
7272
std::vector<arrow::Datum> slices;
73-
auto status = sliceByColumn<int32_t>("x", table, 12, &slices, nullptr, &offsets);
73+
auto status = sliceByColumn<int32_t>("x", table, 12, &slices, &offsets);
7474
BOOST_REQUIRE(status.ok());
7575
BOOST_REQUIRE_EQUAL(slices.size(), 12);
7676
std::array<int, 12> sizes{0, 4, 1, 0, 1, 2, 0, 0, 0, 0, 0, 0};

0 commit comments

Comments
 (0)