1 /**
2 * Copyright 2022-2024 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 #ifndef MINDSPORE_CCSRC_RUNTIME_HARDWARE_ASCEND_ASCEND_UTILS_H_
18 #define MINDSPORE_CCSRC_RUNTIME_HARDWARE_ASCEND_ASCEND_UTILS_H_
19
20 #include <pthread.h>
21
22 #include <atomic>
23 #include <memory>
24 #include <string>
25 #include <set>
26 #include <vector>
27
28 #include "include/backend/kernel_graph.h"
29 #include "mindspore/core/utils/ms_context.h"
30
31 namespace mindspore {
32 namespace device {
33 namespace ascend {
34 template <typename Map, typename K = typename Map::key_type, typename V = typename Map::mapped_type>
MapToString(const Map & value)35 std::string MapToString(const Map &value) {
36 std::stringstream buffer;
37 buffer << "{";
38 for (auto it = value.begin(); it != value.end(); it++) {
39 if (it != value.begin()) {
40 buffer << ", ";
41 }
42 buffer << it->first << ": " << it->second;
43 }
44 buffer << "}";
45 return buffer.str();
46 }
47
48 class ErrorManagerAdapter {
49 public:
50 ErrorManagerAdapter() = default;
51 ~ErrorManagerAdapter() = default;
52 static bool Init();
53 static std::string GetErrorMessage(bool add_title = false);
54
55 private:
56 static void MessageHandler(std::ostringstream *oss);
57
58 private:
59 static std::mutex initialized_mutex_;
60 static bool initialized_;
61 };
62
63 std::string GetErrorMsg(uint32_t rt_error_code);
64
65 void *callback_thread_func(void *data);
66
67 // Callback thread for ascend streams.
68 struct CallbackThread {
~CallbackThreadCallbackThread69 ~CallbackThread() { cancel(); }
70
71 // pthread_cancel may cause bug now, so just set flag to false.
cancelCallbackThread72 void cancel() {
73 if (flag_.load()) {
74 flag_.store(false);
75 }
76 }
77
createCallbackThread78 int create() {
79 flag_.store(true);
80 return pthread_create(&thread_, nullptr, &callback_thread_func, this);
81 }
82
83 pthread_t thread_;
84 std::atomic_bool flag_{true};
85 int32_t default_timeout_{500};
86 };
87 using CallbackThreadPtr = std::shared_ptr<CallbackThread>;
88
89 void InitializeAcl();
90
91 std::string GetFormatMode();
92 } // namespace ascend
93 } // namespace device
94 } // namespace mindspore
95
96 #endif // MINDSPORE_CCSRC_RUNTIME_HARDWARE_ASCEND_ASCEND_UTILS_H_
97