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