Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2017 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/common_runtime/kernel_benchmark_testlib.h"
     17 #include "tensorflow/core/framework/tensor.h"
     18 #include "tensorflow/core/platform/test.h"
     19 #include "tensorflow/core/platform/test_benchmark.h"
     20 
     21 namespace tensorflow {
     22 
     23 namespace {
     24 
     25 // Implement a trivial version of the Rendezvous interface, to avoid
     26 // clouding the benchmark results with the time spent in the various
     27 // implementations, and to avoid the duplicate-send or duplicate-recv
     28 // errors that would arise from running either benchmark in a loop.
     29 class DummyRendezvous : public Rendezvous {
     30   Status Send(const ParsedKey& key, const Args& args, const Tensor& val,
     31               const bool is_dead) override {
     32     return Status::OK();
     33   }
     34   void RecvAsync(const ParsedKey& key, const Args& args,
     35                  DoneCallback done) override {
     36     static Tensor* t = new Tensor(DT_FLOAT, TensorShape({0}));
     37     done(Status::OK(), args, args, *t, false);
     38   }
     39   void StartAbort(const Status& status) override {}
     40 };
     41 
     42 static Graph* Send() {
     43   Graph* g = new Graph(OpRegistry::Global());
     44   Tensor in0(DT_FLOAT, TensorShape({0}));
     45   test::graph::Send(g, test::graph::Constant(g, in0), "T", "/cpu:0", 1,
     46                     "/cpu:0");
     47   test::graph::Recv(g, "T", "float", "/cpu:0", 1, "/cpu:0");
     48   return g;
     49 }
     50 
     51 static Graph* Recv() {
     52   Graph* g = new Graph(OpRegistry::Global());
     53   test::graph::Recv(g, "T", "float", "/cpu:0", 1, "/cpu:0");
     54   return g;
     55 }
     56 
     57 static void BM_Send(int iters) {
     58   testing::UseRealTime();
     59   testing::ItemsProcessed(static_cast<int64>(iters));
     60   test::Benchmark("cpu", Send(), nullptr, nullptr, new DummyRendezvous)
     61       .Run(iters);
     62 }
     63 BENCHMARK(BM_Send);
     64 
     65 static void BM_Recv(int iters) {
     66   testing::UseRealTime();
     67   testing::ItemsProcessed(static_cast<int64>(iters));
     68   test::Benchmark("cpu", Recv(), nullptr, nullptr, new DummyRendezvous)
     69       .Run(iters);
     70 }
     71 BENCHMARK(BM_Recv);
     72 
     73 }  // namespace
     74 }  // namespace tensorflow
     75