Home | History | Annotate | Download | only in distributed_runtime
      1 /* Copyright 2016 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/distributed_runtime/remote_device.h"
     17 
     18 #include "tensorflow/core/common_runtime/device.h"
     19 #include "tensorflow/core/distributed_runtime/rpc/grpc_channel.h"
     20 #include "tensorflow/core/distributed_runtime/rpc/grpc_testlib.h"
     21 #include "tensorflow/core/distributed_runtime/rpc/grpc_worker_cache.h"
     22 #include "tensorflow/core/distributed_runtime/worker_interface.h"
     23 #include "tensorflow/core/framework/tensor.h"
     24 #include "tensorflow/core/framework/tensor_shape.h"
     25 #include "tensorflow/core/framework/tensor_testutil.h"
     26 #include "tensorflow/core/lib/core/notification.h"
     27 #include "tensorflow/core/lib/strings/strcat.h"
     28 #include "tensorflow/core/platform/logging.h"
     29 #include "tensorflow/core/platform/test.h"
     30 
     31 namespace tensorflow {
     32 
     33 const char* const kSession = "remote_session";
     34 
     35 class RemoteDeviceTest : public ::testing::Test {
     36  protected:
     37   string remote_name_;
     38   std::unique_ptr<WorkerCacheInterface> worker_cache_;
     39   WorkerInterface* wi_;
     40   std::vector<Device*> devices_;
     41   std::unique_ptr<test::TestCluster> cluster_;
     42 
     43   RemoteDeviceTest() {
     44     SessionOptions options;
     45     (*options.config.mutable_device_count())["CPU"] = 2;
     46     TF_CHECK_OK(test::TestCluster::MakeTestCluster(options, 1, &cluster_));
     47     const string& hostport = cluster_->targets()[0];
     48     GrpcChannelSpec spec;
     49     TF_CHECK_OK(spec.AddHostPortsJob("localhost", {hostport}));
     50     ChannelCreationFunction channel_func =
     51         ConvertToChannelCreationFunction(NewHostPortGrpcChannel);
     52     worker_cache_.reset(
     53         NewGrpcWorkerCache(NewGrpcChannelCache(spec, channel_func)));
     54     remote_name_ = "/job:localhost/replica:0/task:0";
     55     wi_ = worker_cache_->CreateWorker(remote_name_);
     56   }
     57 
     58   ~RemoteDeviceTest() override {
     59     worker_cache_->ReleaseWorker(remote_name_, wi_);
     60   }
     61 
     62   void SetUp() override {
     63     Notification n;
     64     NewRemoteDevices(Env::Default(), worker_cache_.get(), remote_name_,
     65                      [&n, this](const Status& s, std::vector<Device*>* found) {
     66                        TF_CHECK_OK(s);
     67                        devices_ = *found;
     68                        n.Notify();
     69                      });
     70     n.WaitForNotification();
     71     EXPECT_EQ(devices_.size(), 2);
     72     std::sort(devices_.begin(), devices_.end(), [](Device* a, Device* b) {
     73       return a->name().compare(b->name()) < 0;
     74     });
     75   }
     76 
     77   void TearDown() override {
     78     for (auto d : devices_) delete d;
     79   }
     80 };
     81 
     82 TEST_F(RemoteDeviceTest, GetStatus) {
     83   // We know what the testlib's fake server does.
     84   EXPECT_EQ(devices_[0]->name(), strings::StrCat(remote_name_, "/cpu:0"));
     85   EXPECT_EQ(devices_[0]->attributes().device_type(),
     86             DeviceType(DEVICE_CPU).type());
     87   EXPECT_EQ(devices_[0]->attributes().memory_limit(), 256 << 20);
     88   EXPECT_EQ(devices_[1]->name(), strings::StrCat(remote_name_, "/cpu:1"));
     89   EXPECT_EQ(devices_[1]->attributes().memory_limit(), 256 << 20);
     90 }
     91 
     92 }  // namespace tensorflow
     93