• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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 
17 #include "include/c_api/ms/context.h"
18 #include "c_api/src/common.h"
19 #include "c_api/src/utils.h"
20 #include "c_api/src/resource_manager.h"
21 #include "utils/ms_context.h"
22 
MSResourceManagerCreate()23 ResMgrHandle MSResourceManagerCreate() {
24   auto res_mgr_ptr = new (std::nothrow) ResourceManager();
25   if (res_mgr_ptr == nullptr) {
26     MS_LOG(ERROR) << "Failed to allocate Resource Manager!";
27     return nullptr;
28   }
29   return res_mgr_ptr;
30 }
31 
MSResourceManagerDestroy(ResMgrHandle res_mgr)32 void MSResourceManagerDestroy(ResMgrHandle res_mgr) {
33   auto res_mgr_ptr = reinterpret_cast<ResourceManager *>(res_mgr);
34   delete res_mgr_ptr;
35   res_mgr_ptr = nullptr;
36 }
37 
MSSetEagerMode(bool eager_mode)38 void MSSetEagerMode(bool eager_mode) {
39   int mode = eager_mode ? mindspore::kPynativeMode : mindspore::kGraphMode;
40   MS_LOG(WARNING) << "Set Execution mode: " << mode;
41   auto context = mindspore::MsContext::GetInstance();
42   context->set_param<int>(mindspore::MS_CTX_EXECUTION_MODE, mode);
43 }
44 
MSSetBackendPolicy(const char * policy)45 STATUS MSSetBackendPolicy(const char *policy) {
46   MS_LOG(WARNING) << "Set Backend Policy: " << policy;
47   auto context = mindspore::MsContext::GetInstance();
48   return context->set_backend_policy(policy) ? RET_OK : RET_ERROR;
49 }
50 
MSGetBackendPolicy(char str_buf[],size_t str_len)51 STATUS MSGetBackendPolicy(char str_buf[], size_t str_len) {
52   if (str_buf == nullptr) {
53     MS_LOG(ERROR) << "Input char array [str_buf] is nullptr.";
54     return RET_NULL_PTR;
55   }
56   auto context = mindspore::MsContext::GetInstance();
57   auto policy = context->backend_policy();
58   size_t valid_size = policy.size() < str_len - 1 ? policy.size() : str_len - 1;
59   for (size_t i = 0; i < valid_size; i++) {
60     str_buf[i] = policy.c_str()[i];
61   }
62   str_buf[valid_size] = '\0';
63   return RET_OK;
64 }
65 
MSSetDeviceTarget(const char * device)66 void MSSetDeviceTarget(const char *device) {
67   MS_LOG(WARNING) << "Set Device Target: " << device;
68   auto context = mindspore::MsContext::GetInstance();
69   context->set_param<std::string>(mindspore::MS_CTX_DEVICE_TARGET, device);
70 }
71 
MSGetDeviceTarget(char str_buf[],size_t str_len)72 STATUS MSGetDeviceTarget(char str_buf[], size_t str_len) {
73   if (str_buf == nullptr) {
74     MS_LOG(ERROR) << "Input char array [str_buf] is nullptr.";
75     return RET_NULL_PTR;
76   }
77   auto context = mindspore::MsContext::GetInstance();
78   auto device = context->get_param<std::string>(mindspore::MS_CTX_DEVICE_TARGET);
79   size_t valid_size = device.size() < str_len - 1 ? device.size() : str_len - 1;
80   for (size_t i = 0; i < valid_size; i++) {
81     str_buf[i] = device.c_str()[i];
82   }
83   str_buf[valid_size] = '\0';
84   return RET_OK;
85 }
86 
MSSetDeviceId(uint32_t deviceId)87 void MSSetDeviceId(uint32_t deviceId) {
88   MS_LOG(WARNING) << "Set Device ID: " << deviceId;
89   auto context = mindspore::MsContext::GetInstance();
90   context->set_param<std::uint32_t>(mindspore::MS_CTX_DEVICE_ID, deviceId);
91 }
92 
MSSetIRGraphsSaveMode(int save_mode)93 void MSSetIRGraphsSaveMode(int save_mode) {
94   MS_LOG(DEBUG) << "Set Graphs Save Mode: " << save_mode;
95   auto context = mindspore::MsContext::GetInstance();
96   context->set_param<int>(mindspore::MS_CTX_SAVE_GRAPHS_FLAG, save_mode);
97 }
98 
MSSetIRGraphsSavePath(const char * save_path)99 void MSSetIRGraphsSavePath(const char *save_path) {
100   MS_LOG(DEBUG) << "Set Graphs Save Path: " << save_path;
101   auto context = mindspore::MsContext::GetInstance();
102   context->set_param<std::string>(mindspore::MS_CTX_SAVE_GRAPHS_PATH, save_path);
103 }
104 
MSSetInfer(ResMgrHandle res_mgr,bool infer)105 void MSSetInfer(ResMgrHandle res_mgr, bool infer) {
106   MS_LOG(DEBUG) << "Set Infer Graph: " << infer;
107   auto res_mgr_ptr = reinterpret_cast<ResourceManager *>(res_mgr);
108   res_mgr_ptr->SetInfer(infer);
109 }
110 
MSGetInfer(ResMgrHandle res_mgr)111 bool MSGetInfer(ResMgrHandle res_mgr) {
112   MS_ERROR_IF_TRUE_W_RET_N_LOG(res_mgr == nullptr, false, "Input Handle [res_mgr] is nullptr!");
113   auto res_mgr_ptr = reinterpret_cast<ResourceManager *>(res_mgr);
114   return res_mgr_ptr->GetInfer();
115 }
116