Repository navigation
Expand file tree
/
Copy pathsharded_executor.h
More file actions
98 lines (81 loc) · 2.87 KB
/
Copy pathsharded_executor.h
File metadata and controls
98 lines (81 loc) · 2.87 KB
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CC_ASYNC_SHARDED_EXECUTOR_H_
#define CC_ASYNC_SHARDED_EXECUTOR_H_
#include <functional>
#include <memory>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/synchronization/blocking_counter.h"
#include "cc/async/thread.h"
#include "cc/async/thread_safe_queue.h"
namespace minigo {
// Helper class for running a function over an array in parallel shards.
// A simple example for setting all elements of an array to 1:
//
// constexpr int kSize = 10000;
// constexpr int kNumShards = 4;
//
// ShardedExecutor executor(kNumShards);
// std::array<int, kSize> a;
// executor.Execute([&a](int shard, int num_shards) {
// auto range = ShardedExecutor::GetShardRange(shard, num_shards, a.size());
// for (int i = range.begin; i < range.end; ++i) {
// a[i] = 1;
// }
// });
//
// `ShardedExecutor` is thread-safe.
class ShardedExecutor {
public:
struct Range {
int begin;
int end;
};
// Helper function for mapping from a shard to a sub-range of elements.
static Range GetShardRange(int shard_idx, int num_shards, int num_elements) {
auto begin = shard_idx * num_elements / num_shards;
auto end = (shard_idx + 1) * num_elements / num_shards;
return {begin, end};
}
explicit ShardedExecutor(int num_shards);
~ShardedExecutor();
// Invoke `fn` `num_shards` times.
// The first argument to `fn` is the shard ID in the range [0, `num_shards`).
// The second argument to `fn` is `num_shards`.
// One invocation of `fn` happens on the calling thread; if `num_shards > 0`,
// the remaining invocations happen in parallel on threads owned by the
// `ShardedExecutor`.
// Blocks until all shards of work are complete.
void Execute(const std::function<void(int, int)>& fn);
private:
struct WorkerThread : public Thread {
WorkerThread(int shard, int num_shards);
void Execute(const std::function<void(int, int)>* fn,
absl::BlockingCounter* counter);
void Join() override;
private:
struct Work {
const std::function<void(int, int)>* fn;
absl::BlockingCounter* counter;
};
void Run() override;
const int shard_;
const int num_shards_;
ThreadSafeQueue<Work> work_queue_;
};
std::vector<std::unique_ptr<WorkerThread>> threads_;
};
} // namespace minigo
#endif // CC_ASYNC_WORK_SHARDED_EXECUTOR_H_