1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/util/work_sharder.h"
17
18 #include "tensorflow/core/lib/core/blocking_counter.h"
19 #include "tensorflow/core/platform/logging.h"
20
21 namespace tensorflow {
22
23 /* ABSL_CONST_INIT */ thread_local int per_thread_max_parallism = 1000000;
24
SetPerThreadMaxParallelism(int max_parallelism)25 void SetPerThreadMaxParallelism(int max_parallelism) {
26 CHECK_LE(0, max_parallelism);
27 per_thread_max_parallism = max_parallelism;
28 }
29
GetPerThreadMaxParallelism()30 int GetPerThreadMaxParallelism() { return per_thread_max_parallism; }
31
Shard(int max_parallelism,thread::ThreadPool * workers,int64 total,int64 cost_per_unit,std::function<void (int64,int64)> work)32 void Shard(int max_parallelism, thread::ThreadPool* workers, int64 total,
33 int64 cost_per_unit, std::function<void(int64, int64)> work) {
34 CHECK_GE(total, 0);
35 if (total == 0) {
36 return;
37 }
38 max_parallelism = std::min(max_parallelism, GetPerThreadMaxParallelism());
39 if (max_parallelism <= 1) {
40 // Just inline the whole work since we only have 1 thread (core).
41 work(0, total);
42 return;
43 }
44 if (max_parallelism >= workers->NumThreads()) {
45 workers->ParallelFor(total, cost_per_unit, work);
46 return;
47 }
48 Sharder::Do(total, cost_per_unit, work,
49 [&workers](Sharder::Closure c) { workers->Schedule(c); },
50 max_parallelism);
51 }
52
53 // DEPRECATED: Prefer threadpool->TransformRangeConcurrently, which allows you
54 // to directly specify the shard size.
Do(int64 total,int64 cost_per_unit,const Work & work,const Runner & runner,int max_parallelism)55 void Sharder::Do(int64 total, int64 cost_per_unit, const Work& work,
56 const Runner& runner, int max_parallelism) {
57 cost_per_unit = std::max(int64{1}, cost_per_unit);
58 // We shard [0, total) into "num_shards" shards.
59 // 1 <= num_shards <= num worker threads
60 //
61 // If total * cost_per_unit is small, it is not worth shard too
62 // much. Let us assume each cost unit is 1ns, kMinCostPerShard=10000
63 // is 10us.
64 static const int64 kMinCostPerShard = 10000;
65 const int num_shards =
66 std::max<int>(1, std::min(static_cast<int64>(max_parallelism),
67 total * cost_per_unit / kMinCostPerShard));
68
69 // Each shard contains up to "block_size" units. [0, total) is sharded
70 // into:
71 // [0, block_size), [block_size, 2*block_size), ...
72 // The 1st shard is done by the caller thread and the other shards
73 // are dispatched to the worker threads. The last shard may be smaller than
74 // block_size.
75 const int64 block_size = (total + num_shards - 1) / num_shards;
76 CHECK_GT(block_size, 0); // total > 0 guarantees this.
77 if (block_size >= total) {
78 work(0, total);
79 return;
80 }
81 const int num_shards_used = (total + block_size - 1) / block_size;
82 BlockingCounter counter(num_shards_used - 1);
83 for (int64 start = block_size; start < total; start += block_size) {
84 auto limit = std::min(start + block_size, total);
85 runner([&work, &counter, start, limit]() {
86 work(start, limit); // Compute the shard.
87 counter.DecrementCount(); // The shard is done.
88 });
89 }
90
91 // Inline execute the 1st shard.
92 work(0, std::min(block_size, total));
93 counter.Wait();
94 }
95
96 } // end namespace tensorflow
97