You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common_test.cc 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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. #include "common/common_test.h"
  17. #ifdef __cplusplus
  18. #if __cplusplus
  19. extern "C" {
  20. #endif
  21. #endif
  22. namespace ST {
  23. static std::string GetEnv(const std::string &envvar) {
  24. const char *value = std::getenv(envvar.c_str());
  25. if (value == nullptr) {
  26. return "";
  27. }
  28. return std::string(value);
  29. }
  30. void Common::SetUpTestCase() {}
  31. void Common::TearDownTestCase() {}
  32. void Common::SetUp() {}
  33. void Common::TearDown() {}
  34. void Common::ReadFile(const char *file, size_t *size, char **buf) {
  35. ASSERT_NE(nullptr, file);
  36. ASSERT_NE(nullptr, size);
  37. ASSERT_NE(nullptr, buf);
  38. std::string path = std::string(file);
  39. std::ifstream ifs(path);
  40. ASSERT_EQ(true, ifs.good());
  41. ASSERT_EQ(true, ifs.is_open());
  42. ifs.seekg(0, std::ios::end);
  43. *size = ifs.tellg();
  44. *buf = new char[*size];
  45. ifs.seekg(0, std::ios::beg);
  46. ifs.read(*buf, *size);
  47. ifs.close();
  48. }
  49. std::shared_ptr<mindspore::Context> Common::ContextAutoSet() {
  50. auto device_target_str = GetEnv("DEVICE_TARGET");
  51. if (device_target_str.empty()) {
  52. device_target_str = "Ascend310"; // default is 310
  53. }
  54. auto device_id_str = GetEnv("DEVICE_ID");
  55. if (device_id_str.empty()) {
  56. device_id_str = "0"; // default is 0
  57. }
  58. uint32_t device_id = std::strtoul(device_id_str.c_str(), nullptr, 10);
  59. auto context = std::make_shared<mindspore::Context>();
  60. if (device_target_str == "Ascend310") {
  61. auto ascend310_info = std::make_shared<mindspore::Ascend310DeviceInfo>();
  62. ascend310_info->SetDeviceID(device_id);
  63. context->MutableDeviceInfo().emplace_back(ascend310_info);
  64. } else if (device_target_str == "Ascend910") {
  65. auto ascend310_info = std::make_shared<mindspore::Ascend310DeviceInfo>();
  66. ascend310_info->SetDeviceID(device_id);
  67. context->MutableDeviceInfo().emplace_back(ascend310_info);
  68. } else {
  69. return context;
  70. }
  71. return context;
  72. }
  73. } // namespace ST
  74. #ifdef __cplusplus
  75. #if __cplusplus
  76. }
  77. #endif
  78. #endif