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.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include "include/api/context.h"
  18. #ifdef __cplusplus
  19. #if __cplusplus
  20. extern "C" {
  21. #endif
  22. #endif
  23. namespace ST {
  24. static std::string GetEnv(const std::string &envvar) {
  25. const char *value = std::getenv(envvar.c_str());
  26. if (value == nullptr) {
  27. return "";
  28. }
  29. return std::string(value);
  30. }
  31. void Common::SetUpTestCase() {}
  32. void Common::TearDownTestCase() {}
  33. void Common::SetUp() {}
  34. void Common::TearDown() {}
  35. void Common::ReadFile(const char *file, size_t *size, char **buf) {
  36. ASSERT_NE(nullptr, file);
  37. ASSERT_NE(nullptr, size);
  38. ASSERT_NE(nullptr, buf);
  39. std::string path = std::string(file);
  40. std::ifstream ifs(path);
  41. ASSERT_EQ(true, ifs.good());
  42. ASSERT_EQ(true, ifs.is_open());
  43. ifs.seekg(0, std::ios::end);
  44. *size = ifs.tellg();
  45. *buf = new char[*size];
  46. ifs.seekg(0, std::ios::beg);
  47. ifs.read(*buf, *size);
  48. ifs.close();
  49. }
  50. void Common::ContextAutoSet() {
  51. auto device_target = GetEnv("DEVICE_TARGET");
  52. if (device_target.empty()) {
  53. device_target = mindspore::kDeviceTypeAscend310; // default is 310
  54. }
  55. auto device_id_str = GetEnv("DEVICE_ID");
  56. if (device_id_str.empty()) {
  57. device_id_str = "0"; // default is 0
  58. }
  59. uint32_t device_id = std::strtoul(device_id_str.c_str(), nullptr, 10);
  60. mindspore::GlobalContext::SetGlobalDeviceTarget(device_target);
  61. mindspore::GlobalContext::SetGlobalDeviceID(device_id);
  62. }
  63. } // namespace ST
  64. #ifdef __cplusplus
  65. #if __cplusplus
  66. }
  67. #endif
  68. #endif