aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/matching/docid_range_scheduler.cpp
blob: 49d2ac04ae2c2ba35484d302c53dbf766f779464 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "docid_range_scheduler.h"
#include <cassert>

namespace proton::matching {

namespace {

size_t clamped_sub(size_t a, size_t b) { return (b > a) ? 0 : (a - b); }

} // namespace proton::matching::<unnamed>

const std::atomic<size_t> IdleObserver::_always_zero(0);

//-----------------------------------------------------------------------------

PartitionDocidRangeScheduler::PartitionDocidRangeScheduler(size_t num_threads, uint32_t docid_limit)
    : _ranges()
{
    DocidRangeSplitter splitter(DocidRange(1, docid_limit), num_threads);
    for (size_t i = 0; i < num_threads; ++i) {
        _ranges.push_back(splitter.get(i));
    }
}

PartitionDocidRangeScheduler::~PartitionDocidRangeScheduler() = default;

//-----------------------------------------------------------------------------

DocidRange
TaskDocidRangeScheduler::next_task(size_t thread_id)
{
    std::lock_guard<std::mutex> guard(_lock);
    DocidRange work = _splitter.get(_next_task);
    if (_next_task < _num_tasks) {
        ++_next_task;
    }
    _assigned[thread_id] += work.size();
    size_t todo = _unassigned.load(std::memory_order_relaxed);
    _unassigned.store(clamped_sub(todo, work.size()), std::memory_order_relaxed);
    return work;
}

TaskDocidRangeScheduler::TaskDocidRangeScheduler(size_t num_threads, size_t num_tasks, uint32_t docid_limit)
    : _lock(),
      _splitter(DocidRange(1, docid_limit), num_tasks),
      _next_task(0),
      _num_tasks(num_tasks),
      _assigned(num_threads, 0),
      _unassigned(_splitter.full_range().size())
{
}

TaskDocidRangeScheduler::~TaskDocidRangeScheduler() = default;

//-----------------------------------------------------------------------------

size_t
AdaptiveDocidRangeScheduler::take_idle(const Guard &)
{
    size_t thread_id = _idle.back();
    _idle.pop_back();
    _num_idle.store(_idle.size(), std::memory_order_relaxed);
    assert(_workers[thread_id].is_idle);
    return thread_id;
}

void
AdaptiveDocidRangeScheduler::make_idle(const Guard &, size_t thread_id)
{
    assert(!_workers[thread_id].is_idle);
    _workers[thread_id].is_idle = true;
    _idle.push_back(thread_id);
    _num_idle.store(_idle.size(), std::memory_order_relaxed);
}

void
AdaptiveDocidRangeScheduler::donate(const Guard &guard, size_t src_thread, DocidRange range)
{
    size_t dst_thread = take_idle(guard);
    _workers[dst_thread].next_range = range;
    _workers[dst_thread].is_idle = false;
    _workers[dst_thread].condition.notify_one();
    _assigned[src_thread] -= range.size();
    _assigned[dst_thread] += range.size();
}

bool
AdaptiveDocidRangeScheduler::all_work_done(const Guard &) const
{
    // when all threads are idle at the same time there is no more work
    return ((_idle.size() + 1) == _workers.size());
}

DocidRange
AdaptiveDocidRangeScheduler::finalize(const Guard &guard, size_t thread_id)
{
    while (!_idle.empty()) {
        donate(guard, thread_id, DocidRange());
    }
    return DocidRange();
}

AdaptiveDocidRangeScheduler::AdaptiveDocidRangeScheduler(size_t num_threads, uint32_t min_task, uint32_t docid_limit)
    : _splitter(DocidRange(1, docid_limit), num_threads),
      _min_task(std::max(1u, min_task)),
      _lock(),
      _assigned(num_threads, 0),
      _workers(num_threads),
      _idle(),
      _num_idle(_idle.size())
{
    _idle.reserve(num_threads);
    for (size_t i = 0; i < num_threads; ++i) {
        _assigned[i] = _splitter.get(i).size();
    }
}

AdaptiveDocidRangeScheduler::~AdaptiveDocidRangeScheduler() = default;

DocidRange
AdaptiveDocidRangeScheduler::first_range(size_t thread_id)
{
    DocidRange range = _splitter.get(thread_id);
    if (range.empty()) {
        // block and be counted as idle
        return next_range(thread_id);
    }
    return range;
}

DocidRange
AdaptiveDocidRangeScheduler::next_range(size_t thread_id)
{
    Guard guard(_lock);
    if (all_work_done(guard)) {
        return finalize(guard, thread_id);
    }
    make_idle(guard, thread_id);
    while (_workers[thread_id].is_idle) {
        _workers[thread_id].condition.wait(guard);
    }
    return _workers[thread_id].next_range;
}

DocidRange
AdaptiveDocidRangeScheduler::share_range(size_t thread_id, DocidRange todo)
{
    size_t max_parts = (todo.size() / _min_task);
    if (max_parts > 1) {
        Guard guard(_lock);
        size_t parts = std::min(_idle.size() + 1, max_parts);
        if (parts > 1) {
            DocidRangeSplitter splitter(todo, parts);
            for (size_t i = 1; i < parts; ++i) {
                donate(guard, thread_id, splitter.get(i));
            }
            return splitter.get(0);
        }
    }
    return todo;
}

//-----------------------------------------------------------------------------

}