Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::SortedRunMergerGlobalState Class Reference
Inheritance diagram for duckdb::SortedRunMergerGlobalState:
Collaboration diagram for duckdb::SortedRunMergerGlobalState:

Public Member Functions

 SortedRunMergerGlobalState (ClientContext &context_p, const SortedRunMerger &merger_p)
bool AssignTask (SortedRunMergerLocalState &lstate)
idx_t MaxThreads () override
void DestroyScannedData ()
Public Member Functions inherited from duckdb::GlobalSourceState
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const
Public Member Functions inherited from duckdb::StateWithBlockableTasks
unique_lock< mutex > Lock ()
void PreventBlocking (const unique_lock< mutex > &guard)
bool BlockTask (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
 Add a task to 'blocked_tasks' before returning SourceResultType::BLOCKED (must hold the lock).
bool CanBlock (const unique_lock< mutex > &guard) const
bool UnblockTasks (const unique_lock< mutex > &guard)
 Unblock all tasks (must hold the lock).
SinkResultType BlockSink (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
SourceResultType BlockSource (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
void VerifyLock (const unique_lock< mutex > &guard) const

Public Attributes

ClientContext & context
const idx_t num_threads
const SortedRunMerger & merger
const idx_t num_runs
const idx_t num_partitions
const BlockIteratorStateType iterator_state_type
const SortKeyType sort_key_type
idx_t next_partition_idx
vector< unique_ptr< SortedRunMergePartition > > partitions
atomic< idx_t > total_scanned
mutex destroy_lock
idx_t destroy_partition_idx
mutex materialized_partition_lock
vector< unique_ptr< SortedRun > > materialized_partitions

Static Private Member Functions

static BlockIteratorStateType GetBlockIteratorStateType (const bool &external)

Constructor & Destructor Documentation

◆ SortedRunMergerGlobalState()

duckdb::SortedRunMergerGlobalState::SortedRunMergerGlobalState ( ClientContext & context_p,
const SortedRunMerger & merger_p )
inlineexplicit
107263 : context(context_p), num_threads(NumericCast<idx_t>(TaskScheduler::GetScheduler(context).NumberOfThreads())),
107264 merger(merger_p), num_runs(merger.sorted_runs.size()),
107265 num_partitions((merger.total_count + (merger.partition_size - 1)) / merger.partition_size),
107266 iterator_state_type(GetBlockIteratorStateType(merger.external)),
107267 sort_key_type(merger.sort.key_layout->GetSortKeyType()), next_partition_idx(0), total_scanned(0),
107268 destroy_partition_idx(0) {
107269 // Initialize partitions
107270 partitions.resize(num_partitions);
107271 for (idx_t partition_idx = 0; partition_idx < num_partitions; partition_idx++) {
107272 partitions[partition_idx] = make_uniq<SortedRunMergePartition>(merger, partition_idx);
107273 }
107274 }
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Function Documentation

◆ AssignTask()

bool duckdb::SortedRunMergerGlobalState::AssignTask ( SortedRunMergerLocalState & lstate)
inline
107277 {
107278 D_ASSERT(!lstate.partition_idx.IsValid());
107279 D_ASSERT(lstate.task == SortedRunMergerTask::FINISHED);
107280 auto guard = Lock();
107281 if (next_partition_idx == num_partitions) {
107282 return false; // Nothing left to do
107283 }
107284 lstate.partition_idx = next_partition_idx++;
107286 return true;
107287 }
@ FINISHED
No task.
Definition duckdb.cpp:107179
@ COMPUTE_BOUNDARIES
Compute boundaries of the assigned partition.
Definition duckdb.cpp:107171

◆ MaxThreads()

idx_t duckdb::SortedRunMergerGlobalState::MaxThreads ( )
inlineoverridevirtual

Reimplemented from duckdb::GlobalSourceState.

107289 {
107290 return MaxValue<idx_t>(num_partitions, 1);
107291 }

◆ DestroyScannedData()

void duckdb::SortedRunMergerGlobalState::DestroyScannedData ( )
inline
107293 {
107294 if (!merger.external) {
107295 return; // Only need to destroy when doing an external sort
107296 }
107297
107298 // Have to do this under lock, but other threads don't have to wait
107299 unique_lock<mutex> guard(destroy_lock, std::try_to_lock);
107300 if (!guard.owns_lock()) {
107301 return;
107302 }
107303
107304 // Check how many partitions we can destroy
107305 idx_t end_partition_idx;
107306 for (end_partition_idx = destroy_partition_idx; end_partition_idx < num_partitions; end_partition_idx++) {
107307 const auto &partition = *partitions[end_partition_idx];
107308 if (!partition.scanned) {
107309 break;
107310 }
107311 }
107312
107313 // Destroying must lag "num_threads" partitions behind to avoid destroying actively used data
107314 if (end_partition_idx < num_threads) {
107315 return;
107316 }
107317 end_partition_idx -= num_threads;
107318 if (end_partition_idx <= destroy_partition_idx) {
107319 return;
107320 }
107321
107322 // Compute average number of tuples per partition to destroy
107323 const auto total_tuples_to_destroy = (end_partition_idx - destroy_partition_idx) * merger.partition_size;
107324 const auto tuples_to_destroy_per_partition = total_tuples_to_destroy / num_partitions;
107325
107326 // Compute max tuples per block
107327 const auto &run = *merger.sorted_runs[0];
107328 const auto tuples_per_block =
107329 run.payload_data ? run.payload_data->TuplesPerBlock() : run.key_data->TuplesPerBlock();
107330
107331 // Compute number of blocks we can destroy per partition
107332 const auto blocks_to_destroy_per_partition = tuples_to_destroy_per_partition / tuples_per_block;
107333 if (blocks_to_destroy_per_partition == 0) {
107334 return;
107335 }
107336
107337 for (idx_t run_idx = 0; run_idx < num_runs; run_idx++) {
107338 idx_t begin_idx = 0;
107339 if (destroy_partition_idx != 0) {
107340 auto &begin_partition = *partitions[destroy_partition_idx];
107341 auto partition_guard = begin_partition.Lock();
107342 begin_idx = begin_partition.GetRunBoundaries(partition_guard)[run_idx].end;
107343 }
107344
107345 idx_t end_idx = merger.sorted_runs[run_idx]->Count();
107346 if (end_partition_idx != num_partitions) {
107347 auto &end_partition = *partitions[end_partition_idx];
107348 auto partition_guard = end_partition.Lock();
107349 end_idx = end_partition.GetRunBoundaries(partition_guard)[run_idx].end;
107350 }
107351
107352 merger.sorted_runs[run_idx]->DestroyData(begin_idx, end_idx);
107353 }
107354
107355 destroy_partition_idx = end_partition_idx;
107356 }
int partition(const std::vector< _Tp > &_vec, std::vector< int > &labels, _EqPredicate predicate=_EqPredicate())

◆ GetBlockIteratorStateType()

BlockIteratorStateType duckdb::SortedRunMergerGlobalState::GetBlockIteratorStateType ( const bool & external)
inlinestaticprivate
107359 {
107361 }
@ EXTERNAL
For ExternalBlockIteratorState.
Definition duckdb.cpp:57030
@ IN_MEMORY
For InMemoryBlockIteratorState.
Definition duckdb.cpp:57028

The documentation for this class was generated from the following file: