search nvcc in entire PATH

This commit is contained in:
lizhenyu 2020-09-04 11:41:26 +08:00
parent bc4c5afc1a
commit aeb833f212
2 changed files with 15 additions and 57 deletions

View File

@ -28,9 +28,9 @@ bool CudaEnvChecker::CheckNvccInPath() {
return find_nvcc_;
}
auto checker = [](const std::string &cuda_path) {
auto checker = [](const std::string &path) {
bool find_nvcc = false;
DIR *dir = opendir(cuda_path.c_str());
DIR *dir = opendir(path.c_str());
if (dir == nullptr) {
return find_nvcc;
}
@ -46,23 +46,23 @@ bool CudaEnvChecker::CheckNvccInPath() {
return find_nvcc;
};
auto cuda_paths = GetCudaRealPaths();
find_nvcc_ = std::any_of(cuda_paths.begin(), cuda_paths.end(), checker);
std::set<std::string> paths;
GetRealPaths(&paths);
find_nvcc_ = std::any_of(paths.begin(), paths.end(), checker);
already_check_nvcc_ = true;
return find_nvcc_;
}
std::vector<std::string> CudaEnvChecker::GetCudaRealPaths() const {
std::vector<std::string> res;
void CudaEnvChecker::GetRealPaths(std::set<std::string> *paths) const {
auto env_paths_ptr = std::getenv(kPathEnv);
if (env_paths_ptr == nullptr) {
MS_LOG(ERROR) << "Please export env: PATH";
return res;
MS_LOG(ERROR) << "Please export environment variable PATH";
return;
}
std::string env_paths = env_paths_ptr;
if (env_paths.empty()) {
MS_LOG(ERROR) << "env PATH is empty";
return res;
MS_LOG(ERROR) << "Empty environment variable PATH";
return;
}
std::string cur_path;
@ -71,52 +71,14 @@ std::vector<std::string> CudaEnvChecker::GetCudaRealPaths() const {
cur_path += ch;
continue;
}
auto real_path_pair = IsCudaRealPath(cur_path);
if (real_path_pair.second) {
res.push_back(real_path_pair.first);
if (!cur_path.empty()) {
(void)paths->insert(cur_path);
}
cur_path.clear();
}
if (!cur_path.empty()) {
auto last_real_path_pair = IsCudaRealPath(cur_path);
if (last_real_path_pair.second) {
res.push_back(last_real_path_pair.first);
}
(void)paths->insert(cur_path);
}
return res;
}
std::pair<std::string, bool> CudaEnvChecker::IsCudaRealPath(const std::string &path) const {
std::string real_path = path;
bool valid_path = false;
// 8: string length of kCudaSoftLinkPath
if (real_path.size() < 8) {
return {"", false};
}
// remove redundance space in path
auto front_space_pos = real_path.find_first_not_of(' ');
if (front_space_pos != 0) {
real_path.erase(0, front_space_pos);
}
auto back_space_pos = real_path.find_last_not_of(' ');
if (back_space_pos != real_path.size() - 1) {
real_path.erase(back_space_pos + 1);
}
auto cuda_softlink_path_pos = real_path.rfind(kCudaSoftLinkPath);
auto cuda_real_path_pos = real_path.rfind(kCudaRealPath);
auto start = (cuda_softlink_path_pos == std::string::npos || cuda_real_path_pos == std::string::npos)
? std::min(cuda_softlink_path_pos, cuda_real_path_pos)
: std::max(cuda_softlink_path_pos, cuda_real_path_pos);
if (start == std::string::npos) {
return {"", false};
}
auto end = real_path.find('n', start);
valid_path = (end == real_path.size() - 1) ? true : ((end == real_path.size() - 2) && (real_path.back() == '/'));
return {real_path.substr(0, end + 1), valid_path};
}
} // namespace gpu
} // namespace device

View File

@ -16,9 +16,8 @@
#ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_CUDA_ENV_CHECKER_H_
#define MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_CUDA_ENV_CHECKER_H_
#include <vector>
#include <set>
#include <string>
#include <utility>
namespace mindspore {
namespace device {
@ -38,15 +37,12 @@ class CudaEnvChecker {
CudaEnvChecker(const CudaEnvChecker &);
CudaEnvChecker &operator=(const CudaEnvChecker &);
std::vector<std::string> GetCudaRealPaths() const;
std::pair<std::string, bool> IsCudaRealPath(const std::string &path) const;
void GetRealPaths(std::set<std::string> *paths) const;
bool already_check_nvcc_ = false;
bool find_nvcc_ = false;
static constexpr auto kPathEnv = "PATH";
static constexpr auto kNvcc = "nvcc";
static constexpr auto kCudaSoftLinkPath = "cuda/bin";
static constexpr auto kCudaRealPath = "cuda-10.1/bin";
};
} // namespace gpu
} // namespace device