aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/verbs/verbs_util.cc
blob: 76e44d34a9f5e8e391f1e35f0610cd79e9be9c5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/contrib/verbs/verbs_util.h"

#include "tensorflow/core/common_runtime/gpu/gpu_util.h"
#include "tensorflow/core/lib/core/notification.h"
#include "tensorflow/core/lib/strings/str_util.h"
namespace tensorflow {

// static sync wrapper:
Status VerbsUtil::CopyGPUTensorToCPUSync(Device* gpu_device,
                              const DeviceContext* device_context,
                              const Tensor* gpu_tensor,
                              Tensor* cpu_tensor) {
  Notification n;
  Status status;
  GPUUtil::CopyGPUTensorToCPU(gpu_device, device_context,
                              gpu_tensor, cpu_tensor,
                              [&n, &status](const Status& s) {
                                status = s;
                                n.Notify();
                              });
  n.WaitForNotification();
  return status;
}

// static sync wrapper:
Status VerbsUtil::CopyCPUTensorToGPUSync(const Tensor* cpu_tensor,
                                         const DeviceContext* device_context,
                                         Device* gpu_device,
                                         Tensor* gpu_tensor) {
  Notification n;
  Status status;
  GPUUtil::CopyCPUTensorToGPU(cpu_tensor, device_context,
                              gpu_device, gpu_tensor,
                              [&n, &status](const Status& s) {
                                status = s;
                                n.Notify();
                              });
  n.WaitForNotification();
  return status;
}

// static sync wrapper:
Status VerbsUtil::SetProtoFromGPUSync(const Tensor& tensor, Device* dev,
                                      const DeviceContext* device_context,
                                      TensorProto* proto, bool is_dead) {
  Notification n;
  Status status;
  GPUUtil::SetProtoFromGPU(tensor, dev, device_context, proto, is_dead,
                           [&n, &status](const Status& s) {
                             status = s;
                             n.Notify();
                           });
  n.WaitForNotification();
  return status;
}

// static
string VerbsUtil::AppendStepidToKey(const string& key, int64 step_id) {
  return strings::StrCat(key, ";", step_id);
}

// static
void VerbsUtil::GetKeyAndStepId(const string& key_with_step_id, string& key,
                                int64& step_id) {
  StringPiece s(key_with_step_id);
  // a key (with step_id) has exact 6 parts if split by ";"
  // part 1: src_device;
  // part 2: src_incarnation;
  // part 3: dst_device;
  // part 4: name;
  // part 5: frame_iter.frame_id:frame_iter.iter_id
  // part 6: step_id
  std::vector<string> parts = str_util::Split(s, ';');
  CHECK(parts.size() == 6) << "Key with step_id must have 6 parts";
  strings::safe_strto64(parts[5], &step_id);
  parts.pop_back();                        // remove step_id
  key.assign(str_util::Join(parts, ";"));  // stitch them together
}

}  // namespace tensorflow