Home | History | Annotate | Download | only in util
      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 void Shard(int max_parallelism, thread::ThreadPool* workers, int64 total,
     24            int64 cost_per_unit, std::function<void(int64, int64)> work) {
     25   CHECK_GE(total, 0);
     26   if (total == 0) {
     27     return;
     28   }
     29   if (max_parallelism <= 1) {
     30     // Just inline the whole work since we only have 1 thread (core).
     31     work(0, total);
     32     return;
     33   }
     34   if (max_parallelism >= workers->NumThreads()) {
     35     workers->ParallelFor(total, cost_per_unit, work);
     36     return;
     37   }
     38   cost_per_unit = std::max(1LL, cost_per_unit);
     39   // We shard [0, total) into "num_shards" shards.
     40   //   1 <= num_shards <= num worker threads
     41   //
     42   // If total * cost_per_unit is small, it is not worth shard too
     43   // much. Let us assume each cost unit is 1ns, kMinCostPerShard=10000
     44   // is 10us.
     45   static const int64 kMinCostPerShard = 10000;
     46   const int num_shards =
     47       std::max<int>(1, std::min(static_cast<int64>(max_parallelism),
     48                                 total * cost_per_unit / kMinCostPerShard));
     49 
     50   // Each shard contains up to "block_size" units. [0, total) is sharded
     51   // into:
     52   //   [0, block_size), [block_size, 2*block_size), ...
     53   // The 1st shard is done by the caller thread and the other shards
     54   // are dispatched to the worker threads. The last shard may be smaller than
     55   // block_size.
     56   const int64 block_size = (total + num_shards - 1) / num_shards;
     57   CHECK_GT(block_size, 0);  // total > 0 guarantees this.
     58   if (block_size >= total) {
     59     work(0, total);
     60     return;
     61   }
     62   const int num_shards_used = (total + block_size - 1) / block_size;
     63   BlockingCounter counter(num_shards_used - 1);
     64   for (int64 start = block_size; start < total; start += block_size) {
     65     auto limit = std::min(start + block_size, total);
     66     workers->Schedule([&work, &counter, start, limit]() {
     67       work(start, limit);        // Compute the shard.
     68       counter.DecrementCount();  // The shard is done.
     69     });
     70   }
     71 
     72   // Inline execute the 1st shard.
     73   work(0, std::min(block_size, total));
     74   counter.Wait();
     75 }
     76 
     77 }  // end namespace tensorflow
     78