Skip to content

Commit ce153b2

Browse files
Merge pull request #44310 from firejq:patch-4
PiperOrigin-RevId: 340451515 Change-Id: Icccac83aa7f09644ed34492ee06391d8016fc6e6
2 parents d2cad1e + 299062c commit ce153b2

1 file changed

Lines changed: 72 additions & 49 deletions

File tree

‎tensorflow/core/platform/file_system_helper.cc‎

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ limitations under the License.
1919
#include <string>
2020
#include <vector>
2121

22+
#include "tensorflow/core/platform/cpu_info.h"
2223
#include "tensorflow/core/platform/env.h"
2324
#include "tensorflow/core/platform/file_system.h"
25+
#include "tensorflow/core/platform/mutex.h"
2426
#include "tensorflow/core/platform/path.h"
2527
#include "tensorflow/core/platform/platform.h"
2628
#include "tensorflow/core/platform/status.h"
@@ -32,7 +34,7 @@ namespace internal {
3234

3335
namespace {
3436

35-
constexpr int kNumThreads = 8;
37+
const int kNumThreads = port::NumSchedulableCPUs();
3638

3739
// Run a function in parallel using a ThreadPool, but skip the ThreadPool
3840
// on the iOS platform due to its problems with more than a few threads.
@@ -75,67 +77,88 @@ Status GetMatchingPaths(FileSystem* fs, Env* env, const string& pattern,
7577
#endif
7678
std::vector<string> dirs;
7779
if (!is_directory) {
78-
dirs.push_back(eval_pattern);
80+
dirs.emplace_back(eval_pattern);
7981
}
8082
StringPiece tmp_dir(io::Dirname(eval_pattern));
8183
while (tmp_dir.size() > dir.size()) {
82-
dirs.push_back(string(tmp_dir));
84+
dirs.emplace_back(string(tmp_dir));
8385
tmp_dir = io::Dirname(tmp_dir);
8486
}
85-
dirs.push_back(dir);
87+
dirs.emplace_back(dir);
8688
std::reverse(dirs.begin(), dirs.end());
87-
// Setup a BFS to explore everything under dir.
89+
// Setup a parallel BFS to explore everything under dir.
8890
std::deque<std::pair<string, int>> dir_q;
89-
dir_q.push_back({dirs[0], 0});
91+
std::deque<std::pair<string, int>> next_dir_q;
92+
dir_q.emplace_back(std::make_pair(dirs[0], 0));
9093
Status ret; // Status to return.
91-
// children_dir_status holds is_dir status for children. It can have three
92-
// possible values: OK for true; FAILED_PRECONDITION for false; CANCELLED
93-
// if we don't calculate IsDirectory (we might do that because there isn't
94-
// any point in exploring that child path).
95-
std::vector<Status> children_dir_status;
94+
mutex results_mutex;
95+
condition_variable results_cond;
96+
mutex next_que_mutex;
97+
condition_variable next_que_cond;
9698
while (!dir_q.empty()) {
97-
string current_dir = dir_q.front().first;
98-
int dir_index = dir_q.front().second;
99-
dir_index++;
100-
dir_q.pop_front();
101-
std::vector<string> children;
102-
Status s = fs->GetChildren(current_dir, &children);
103-
// In case PERMISSION_DENIED is encountered, we bail here.
104-
if (s.code() == tensorflow::error::PERMISSION_DENIED) {
105-
continue;
106-
}
107-
ret.Update(s);
108-
if (children.empty()) continue;
109-
// This IsDirectory call can be expensive for some FS. Parallelizing it.
110-
children_dir_status.resize(children.size());
111-
ForEach(0, children.size(),
112-
[fs, &current_dir, &children, &dirs, dir_index, is_directory,
113-
&children_dir_status](int i) {
114-
const string child_path = io::JoinPath(current_dir, children[i]);
115-
if (!fs->Match(child_path, dirs[dir_index])) {
116-
children_dir_status[i] = Status(tensorflow::error::CANCELLED,
117-
"Operation not needed");
118-
} else if (dir_index != dirs.size() - 1) {
119-
children_dir_status[i] = fs->IsDirectory(child_path);
120-
} else {
121-
children_dir_status[i] =
122-
is_directory ? fs->IsDirectory(child_path) : Status::OK();
123-
}
124-
});
125-
for (size_t i = 0; i < children.size(); ++i) {
126-
const string child_path = io::JoinPath(current_dir, children[i]);
127-
// If the IsDirectory call was cancelled we bail.
128-
if (children_dir_status[i].code() == tensorflow::error::CANCELLED) {
129-
continue;
99+
next_dir_q.clear();
100+
std::vector<Status> new_rets(dir_q.size());
101+
auto handle_level = [fs, &results, &dir_q, &next_dir_q, &new_rets,
102+
&is_directory, &dirs, &results_mutex, &results_cond,
103+
&next_que_mutex, &next_que_cond](int i) {
104+
string current_dir = dir_q.at(i).first;
105+
int dir_index = dir_q.at(i).second;
106+
dir_index++;
107+
std::vector<string> children;
108+
Status s = fs->GetChildren(current_dir, &children);
109+
// In case PERMISSION_DENIED is encountered, we bail here.
110+
if (s.code() == tensorflow::error::PERMISSION_DENIED) {
111+
return;
130112
}
131-
if (children_dir_status[i].ok()) {
132-
if (dir_index != dirs.size() - 1) {
133-
dir_q.push_back({child_path, dir_index});
113+
new_rets[i] = s;
114+
if (children.empty()) return;
115+
116+
// children_dir_status holds is_dir status for children. It can have three
117+
// possible values: OK for true; FAILED_PRECONDITION for false; CANCELLED
118+
// if we don't calculate IsDirectory (we might do that because there isn't
119+
// any point in exploring that child path).
120+
std::vector<Status> children_dir_status;
121+
122+
// This IsDirectory call can be expensive for some FS. Parallelizing it.
123+
children_dir_status.resize(children.size());
124+
auto handle_children = [fs, &current_dir, &children, &dirs, dir_index,
125+
is_directory, &children_dir_status](int j) {
126+
const string child_path = io::JoinPath(current_dir, children[j]);
127+
if (!fs->Match(child_path, dirs[dir_index])) {
128+
children_dir_status[j] =
129+
Status(tensorflow::error::CANCELLED, "Operation not needed");
130+
} else if (dir_index != dirs.size() - 1) {
131+
children_dir_status[j] = fs->IsDirectory(child_path);
134132
} else {
135-
results->push_back(child_path);
133+
children_dir_status[j] =
134+
is_directory ? fs->IsDirectory(child_path) : Status::OK();
135+
}
136+
};
137+
ForEach(0, children.size(), handle_children);
138+
139+
for (size_t j = 0; j < children.size(); ++j) {
140+
const string child_path = io::JoinPath(current_dir, children[j]);
141+
// If the IsDirectory call was cancelled we bail.
142+
if (children_dir_status[j].code() == tensorflow::error::CANCELLED) {
143+
continue;
144+
}
145+
if (children_dir_status[j].ok()) {
146+
if (dir_index != dirs.size() - 1) {
147+
mutex_lock lk(next_que_mutex);
148+
next_dir_q.emplace_back(std::make_pair(child_path, dir_index));
149+
next_que_cond.notify_one();
150+
} else {
151+
mutex_lock lk(results_mutex);
152+
results->emplace_back(child_path);
153+
results_cond.notify_one();
154+
}
136155
}
137156
}
138-
}
157+
};
158+
ForEach(0, dir_q.size(), handle_level);
159+
160+
ret.Update(new_rets[dir_q.size() - 1]);
161+
std::swap(dir_q, next_dir_q);
139162
}
140163
return ret;
141164
}

0 commit comments

Comments
 (0)