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 <atomic>
     19 #include <vector>
     20 #include "tensorflow/core/lib/core/threadpool.h"
     21 #include "tensorflow/core/platform/logging.h"
     22 #include "tensorflow/core/platform/mutex.h"
     23 #include "tensorflow/core/platform/test.h"
     24 #include "tensorflow/core/platform/test_benchmark.h"
     25 #include "tensorflow/core/platform/types.h"
     26 
     27 namespace tensorflow {
     28 namespace {
     29 
     30 void RunSharding(int64 num_workers, int64 total, int64 cost_per_unit,
     31                  thread::ThreadPool* threads) {
     32   mutex mu;
     33   int64 num_shards = 0;
     34   int64 num_done_work = 0;
     35   std::vector<bool> work(total, false);
     36   Shard(num_workers, threads, total, cost_per_unit,
     37         [=, &mu, &num_shards, &num_done_work, &work](int64 start, int64 limit) {
     38           VLOG(1) << "Shard [" << start << "," << limit << ")";
     39           EXPECT_GE(start, 0);
     40           EXPECT_LE(limit, total);
     41           mutex_lock l(mu);
     42           ++num_shards;
     43           for (; start < limit; ++start) {
     44             EXPECT_FALSE(work[start]);  // No duplicate
     45             ++num_done_work;
     46             work[start] = true;
     47           }
     48         });
     49   EXPECT_EQ(num_done_work, total);
     50   LOG(INFO) << num_workers << " " << total << " " << cost_per_unit << " "
     51             << num_shards;
     52 }
     53 
     54 TEST(Shard, Basic) {
     55   thread::ThreadPool threads(Env::Default(), "test", 16);
     56   for (auto workers : {0, 1, 2, 3, 5, 7, 10, 11, 15, 100, 1000}) {
     57     for (auto total : {0, 1, 7, 10, 64, 100, 256, 1000, 9999}) {
     58       for (auto cost_per_unit : {0, 1, 11, 102, 1003, 10005, 1000007}) {
     59         RunSharding(workers, total, cost_per_unit, &threads);
     60       }
     61     }
     62   }
     63 }
     64 
     65 TEST(Shard, OverflowTest) {
     66   thread::ThreadPool threads(Env::Default(), "test", 3);
     67   for (auto workers : {1, 2, 3}) {
     68     const int64 total_elements = 1LL << 32;
     69     const int64 cost_per_unit = 10;
     70     std::atomic<int64> num_elements(0);
     71     Shard(workers, &threads, total_elements, cost_per_unit,
     72           [&num_elements](int64 start, int64 limit) {
     73             num_elements += limit - start;
     74           });
     75     EXPECT_EQ(num_elements.load(), total_elements);
     76   }
     77 }
     78 
     79 void BM_Sharding(int iters, int arg) {
     80   thread::ThreadPool threads(Env::Default(), "test", 16);
     81   const int64 total = 1LL << 30;
     82   auto lambda = [](int64 start, int64 limit) {};
     83   auto work = std::cref(lambda);
     84   for (; iters > 0; iters -= arg) {
     85     Shard(arg - 1, &threads, total, 1, work);
     86   }
     87 }
     88 BENCHMARK(BM_Sharding)->Range(1, 128);
     89 
     90 }  // namespace
     91 }  // namespace tensorflow
     92