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/contrib/verbs/verbs_util.h"
17
18 #include <vector>
19
20 #include "tensorflow/core/lib/core/stringpiece.h"
21 #include "tensorflow/core/lib/strings/numbers.h"
22 #include "tensorflow/core/lib/strings/str_util.h"
23 #include "tensorflow/core/lib/strings/strcat.h"
24
25 namespace tensorflow {
26
27 // static
AppendStepidToKey(const string & key,int64 step_id)28 string VerbsUtil::AppendStepidToKey(const string& key, int64 step_id) {
29 return strings::StrCat(key, ";", step_id);
30 }
31
32 // static
GetKeyAndStepId(const string & key_with_step_id,string & key,int64 & step_id)33 void VerbsUtil::GetKeyAndStepId(const string& key_with_step_id, string& key,
34 int64& step_id) {
35 StringPiece s(key_with_step_id);
36 // a key (with step_id) has exact 6 parts if split by ";"
37 // part 1: src_device;
38 // part 2: src_incarnation;
39 // part 3: dst_device;
40 // part 4: name;
41 // part 5: frame_iter.frame_id:frame_iter.iter_id
42 // part 6: step_id
43 std::vector<string> parts = str_util::Split(s, ';');
44 CHECK(parts.size() == 6) << "Key with step_id must have 6 parts";
45 strings::safe_strto64(parts[5], &step_id);
46 parts.pop_back(); // remove step_id
47 key.assign(str_util::Join(parts, ";")); // stitch them together
48 }
49
50 } // namespace tensorflow
51