2020-12-30 09:40:31 +08:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
|
|
|
*
|
|
|
|
* 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 "common/common_test.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#if __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace ST {
|
|
|
|
static std::string GetEnv(const std::string &envvar) {
|
|
|
|
const char *value = std::getenv(envvar.c_str());
|
|
|
|
if (value == nullptr) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Common::SetUpTestCase() {}
|
|
|
|
|
|
|
|
void Common::TearDownTestCase() {}
|
|
|
|
|
|
|
|
void Common::SetUp() {}
|
|
|
|
|
|
|
|
void Common::TearDown() {}
|
|
|
|
|
|
|
|
void Common::ReadFile(const char *file, size_t *size, char **buf) {
|
|
|
|
ASSERT_NE(nullptr, file);
|
|
|
|
ASSERT_NE(nullptr, size);
|
|
|
|
ASSERT_NE(nullptr, buf);
|
|
|
|
std::string path = std::string(file);
|
|
|
|
std::ifstream ifs(path);
|
|
|
|
ASSERT_EQ(true, ifs.good());
|
|
|
|
ASSERT_EQ(true, ifs.is_open());
|
|
|
|
|
|
|
|
ifs.seekg(0, std::ios::end);
|
|
|
|
*size = ifs.tellg();
|
|
|
|
*buf = new char[*size];
|
|
|
|
|
|
|
|
ifs.seekg(0, std::ios::beg);
|
|
|
|
ifs.read(*buf, *size);
|
|
|
|
ifs.close();
|
|
|
|
}
|
|
|
|
|
2021-03-18 21:01:55 +08:00
|
|
|
std::shared_ptr<mindspore::Context> Common::ContextAutoSet() {
|
|
|
|
auto device_target_str = GetEnv("DEVICE_TARGET");
|
|
|
|
if (device_target_str.empty()) {
|
|
|
|
device_target_str = "Ascend310"; // default is 310
|
2020-12-30 09:40:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto device_id_str = GetEnv("DEVICE_ID");
|
|
|
|
if (device_id_str.empty()) {
|
|
|
|
device_id_str = "0"; // default is 0
|
|
|
|
}
|
|
|
|
uint32_t device_id = std::strtoul(device_id_str.c_str(), nullptr, 10);
|
2021-03-18 21:01:55 +08:00
|
|
|
auto context = std::make_shared<mindspore::Context>();
|
2020-12-30 09:40:31 +08:00
|
|
|
|
2021-03-18 21:01:55 +08:00
|
|
|
if (device_target_str == "Ascend310") {
|
2021-12-09 15:20:31 +08:00
|
|
|
auto ascend310_info = std::make_shared<mindspore::AscendDeviceInfo>();
|
2021-03-18 21:01:55 +08:00
|
|
|
ascend310_info->SetDeviceID(device_id);
|
|
|
|
context->MutableDeviceInfo().emplace_back(ascend310_info);
|
|
|
|
} else if (device_target_str == "Ascend910") {
|
2021-12-09 15:20:31 +08:00
|
|
|
auto ascend310_info = std::make_shared<mindspore::AscendDeviceInfo>();
|
2021-03-18 21:01:55 +08:00
|
|
|
ascend310_info->SetDeviceID(device_id);
|
|
|
|
context->MutableDeviceInfo().emplace_back(ascend310_info);
|
|
|
|
} else {
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
2020-12-30 09:40:31 +08:00
|
|
|
}
|
|
|
|
} // namespace ST
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#if __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|