• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RESOURCEMANAGER_H
17 #define RESOURCEMANAGER_H
18 
19 #include <climits>
20 #include <cstring>
21 #include <climits>
22 #include <mutex>
23 #include <set>
24 #include <sys/stat.h>
25 #include <unordered_map>
26 #include <vector>
27 #include "mindspore/core/utils/log_adapter.h"
28 #include "mindspore/ccsrc/cxx_api/graph/acl/acl_env_guard.h"
29 #include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
30 #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
31 
32 enum ModelLoadMethod {
33   LOAD_FROM_FILE = 0,       // Loading from file, memory of model and weights are managed by ACL
34   LOAD_FROM_MEM,            // Loading from memory, memory of model and weights are managed by ACL
35   LOAD_FROM_FILE_WITH_MEM,  // Loading from file, memory of model and weight are managed by user
36   LOAD_FROM_MEM_WITH_MEM    // Loading from memory, memory of model and weight are managed by user
37 };
38 
39 struct ModelInfo {
40   std::string modelName;
41   std::string modelPath;               // Path of om model file
42   size_t modelFileSize;                // Size of om model file
43   std::shared_ptr<void> modelFilePtr;  // Smart pointer of model file data
44   uint32_t modelWidth;                 // Input width of model
45   uint32_t modelHeight;                // Input height of model
46   ModelLoadMethod method;              // Loading method of model
47 };
48 
49 // Device resource info, such as model infos, etc
50 struct DeviceResInfo {
51   std::vector<ModelInfo> modelInfos;
52 };
53 
54 struct ResourceInfo {
55   std::set<int> deviceIds;
56   std::string singleOpFolderPath;
57   std::unordered_map<int, DeviceResInfo> deviceResInfos;  // map <deviceId, deviceResourceInfo>
58 };
59 
60 APP_ERROR ExistFile(const std::string &filePath);
61 
62 class ResourceManager {
63   friend APP_ERROR ExistFile(const std::string &filePath);
64 
65  public:
ResourceManager()66   ResourceManager(){};
67 
~ResourceManager()68   ~ResourceManager(){};
69 
70   // Get the Instance of resource manager
71   static std::shared_ptr<ResourceManager> GetInstance();
72 
73   // Init the resource of resource manager
74   APP_ERROR InitResource(ResourceInfo &resourceInfo);
75 
76   aclrtContext GetContext(int deviceId);
77 
78   void Release();
79 
GetInitStatus()80   static bool GetInitStatus() { return initFlag_; }
81 
82  private:
83   static std::shared_ptr<ResourceManager> ptr_;
84   static bool initFlag_;
85   std::vector<int> deviceIds_;
86   std::vector<aclrtContext> contexts_;
87   std::unordered_map<int, int> deviceIdMap_;  // Map of device to index
88   std::shared_ptr<mindspore::AclEnvGuard> acl_env_;
89 };
90 
91 #endif