1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #include "LoaderUtils.h" |
18 | |
19 | using namespace glow; |
20 | |
21 | void glow::exitWithErr(llvm::StringRef errMsg) { |
22 | llvm::errs() << "ERROR: " << errMsg << "\n" ; |
23 | std::exit(1); |
24 | } |
25 | |
26 | void glow::checkCond(bool cond, llvm::StringRef errMsg) { |
27 | if (!cond) { |
28 | llvm::errs() << "ERROR: " << errMsg << "\n" ; |
29 | std::exit(1); |
30 | } |
31 | } |
32 | |
33 | UnlabeledDataSet |
34 | glow::readUnlabeledDataSetFromFile(llvm::StringRef dataSetFile, |
35 | llvm::StringRef dataSetDirPath) { |
36 | // Verify the dataset directory path is valid (if not empty). |
37 | checkCond(dataSetDirPath.empty() || |
38 | llvm::sys::fs::is_directory(dataSetDirPath), |
39 | strFormat("The dataset path '%s' is not a directory!" , |
40 | dataSetDirPath.data())); |
41 | // Parse the dataset file. |
42 | std::ifstream inpFile(dataSetFile.str()); |
43 | checkCond(inpFile.is_open(), strFormat("Cannot open the dataset file '%s'!" , |
44 | dataSetFile.data())); |
45 | std::string line; |
46 | UnlabeledDataSet dataset; |
47 | while (std::getline(inpFile, line)) { |
48 | std::string dataPath; |
49 | // Replace commas with spaces. |
50 | while (line.find("," ) != std::string::npos) { |
51 | line.replace(line.find("," ), 1, " " ); |
52 | } |
53 | // Read data path. Add one extra space to make sure the string stream |
54 | // can read strings from lines without extra separators. |
55 | std::istringstream lineStream(line + " " ); |
56 | checkCond((lineStream >> dataPath).good(), |
57 | strFormat("Failed parsing the unlabeled dataset file '%s'! Check " |
58 | "the file has the right format!" , |
59 | dataSetFile.data())); |
60 | // Concatenate (prepend) dataset directory path (if not empty). |
61 | if (!dataSetDirPath.empty()) { |
62 | llvm::StringRef sep = llvm::sys::path::get_separator(); |
63 | dataPath = std::string(dataSetDirPath) + |
64 | (dataSetDirPath.endswith(sep) ? "" : std::string(sep)) + |
65 | dataPath; |
66 | } |
67 | checkCond(llvm::sys::fs::exists(dataPath), |
68 | strFormat("Data path '%s' does not exist!" , dataPath.c_str())); |
69 | dataset.emplace_back(dataPath); |
70 | } |
71 | return dataset; |
72 | } |
73 | |
74 | UnlabeledDataSet |
75 | glow::readUnlabeledDataSetFromDir(llvm::StringRef dataSetDirPath) { |
76 | // Verify the dataset directory path is valid. |
77 | checkCond(llvm::sys::fs::is_directory(dataSetDirPath), |
78 | strFormat("The dataset path '%s' is not a directory!" , |
79 | dataSetDirPath.data())); |
80 | std::error_code code; |
81 | llvm::sys::fs::directory_iterator dirIt(dataSetDirPath, code); |
82 | UnlabeledDataSet dataset; |
83 | while (!code && dirIt != llvm::sys::fs::directory_iterator()) { |
84 | auto path = dirIt->path(); |
85 | if (llvm::sys::fs::is_regular_file(path)) { |
86 | dataset.emplace_back(path); |
87 | } |
88 | dirIt.increment(code); |
89 | } |
90 | // The paths retrieved by the directory iterator are not sorted. |
91 | // Sort the paths alphabetically in increasing order. |
92 | std::sort(dataset.begin(), dataset.end()); |
93 | return dataset; |
94 | } |
95 | |
96 | LabeledDataSet glow::readLabeledDataSet(llvm::StringRef dataSetFile, |
97 | llvm::StringRef dataSetDirPath) { |
98 | // Verify the dataset directory path is valid. |
99 | checkCond(llvm::sys::fs::is_directory(dataSetDirPath), |
100 | strFormat("The dataset path '%s' is not a directory!" , |
101 | dataSetDirPath.data())); |
102 | // Parse the dataset file. |
103 | std::ifstream inpFile(dataSetFile.str()); |
104 | checkCond(inpFile.is_open(), strFormat("Cannot open the dataset file '%s'!" , |
105 | dataSetFile.data())); |
106 | std::string line; |
107 | LabeledDataSet dataset; |
108 | while (std::getline(inpFile, line)) { |
109 | std::string dataPath; |
110 | unsigned label; |
111 | // Replace commas with spaces. |
112 | while (line.find("," ) != std::string::npos) { |
113 | line.replace(line.find("," ), 1, " " ); |
114 | } |
115 | // Read data path and label. |
116 | std::istringstream lineStream(line); |
117 | checkCond((lineStream >> dataPath >> label).good(), |
118 | strFormat("Failed parsing the labeled dataset file '%s'! Check " |
119 | "the file has the right format!" , |
120 | dataSetFile.data())); |
121 | // Prepend dataset directory path. |
122 | dataPath = std::string(dataSetDirPath) + |
123 | std::string(llvm::sys::path::get_separator()) + dataPath; |
124 | checkCond(llvm::sys::fs::exists(dataPath), |
125 | strFormat("Data path '%s' does not exist!" , dataPath.c_str())); |
126 | dataset.emplace_back(dataPath, label); |
127 | } |
128 | return dataset; |
129 | } |
130 | |
131 | TimeStamp glow::getTimeStamp() { |
132 | return std::chrono::high_resolution_clock::now(); |
133 | } |
134 | |
135 | unsigned glow::getDurationSec(TimeStamp &startTime) { |
136 | TimeStamp currTime = std::chrono::high_resolution_clock::now(); |
137 | return std::chrono::duration_cast<std::chrono::seconds>(currTime - startTime) |
138 | .count(); |
139 | } |
140 | |
141 | void glow::getDuration(TimeStamp &startTime, unsigned &sec, unsigned &min, |
142 | unsigned &hrs) { |
143 | unsigned totSec = glow::getDurationSec(startTime); |
144 | unsigned totMin = totSec / 60; |
145 | sec = totSec % 60; |
146 | min = totMin % 60; |
147 | hrs = totMin / 60; |
148 | } |
149 | |