1 /* Copyright 2019 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/threadpool_device.h"
17
18 #include "tensorflow/core/lib/core/status_test_util.h"
19 #include "tensorflow/core/platform/test.h"
20 #include "tensorflow/core/public/session_options.h"
21
22 namespace tensorflow {
23 namespace {
24
25 const int kDimSize = 2;
26
InitTensor(Tensor * tensor,float value)27 void InitTensor(Tensor* tensor, float value) {
28 auto eigen_tensor = tensor->tensor<float, kDimSize>();
29 for (int i = 0; i < kDimSize; ++i) {
30 for (int j = 0; j < kDimSize; ++j) {
31 eigen_tensor(i, j) = value;
32 }
33 }
34 }
35
Equal(const Tensor & tensor1,const Tensor & tensor2)36 bool Equal(const Tensor& tensor1, const Tensor& tensor2) {
37 auto eigen_tensor1 = tensor1.tensor<float, kDimSize>();
38 auto eigen_tensor2 = tensor2.tensor<float, kDimSize>();
39 for (int i = 0; i < kDimSize; ++i) {
40 for (int j = 0; j < kDimSize; ++j) {
41 if (eigen_tensor1(i, j) != eigen_tensor2(i, j)) {
42 return false;
43 }
44 }
45 }
46 return true;
47 }
48
TEST(ThreadPoolDeviceTest,CopyTensor)49 TEST(ThreadPoolDeviceTest, CopyTensor) {
50 Tensor input(DT_FLOAT, TensorShape({kDimSize, kDimSize}));
51 Tensor output(DT_FLOAT, TensorShape({kDimSize, kDimSize}));
52 InitTensor(&input, 1);
53 InitTensor(&output, 0);
54 ASSERT_FALSE(Equal(input, output));
55
56 ThreadPoolDevice device(SessionOptions(), "/device:CPU:0", Bytes(256),
57 DeviceLocality(), cpu_allocator());
58 DeviceContext* device_context = new DeviceContext;
59 Notification note;
60 device.CopyTensorInSameDevice(&input, &output, device_context,
61 [¬e](const Status& s) {
62 TF_ASSERT_OK(s);
63 note.Notify();
64 });
65 note.WaitForNotification();
66 ASSERT_TRUE(Equal(input, output));
67
68 device_context->Unref();
69 }
70
71 } // namespace
72 } // namespace tensorflow
73