• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TDT_TDT_HANDLE_H_
17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TDT_TDT_HANDLE_H_
18 
19 #include <iostream>
20 #include <map>
21 #include <thread>
22 #include "utils/log_adapter.h"
23 #include "acl/acl_tdt.h"
24 
25 namespace mindspore {
26 namespace dataset {
27 class TdtHandle {
28  public:
29   static void AddHandle(acltdtChannelHandle **handle, std::thread *use_thread);
30 
31   static bool DestroyHandle();
32 
33   static void DelHandle(acltdtChannelHandle **handle);
34 
35  private:
TdtHandle()36   TdtHandle() {}
37   ~TdtHandle() = default;
38 };
39 
AddHandle(acltdtChannelHandle ** handle,std::thread * use_thread)40 inline void TdtHandle::AddHandle(acltdtChannelHandle **handle, std::thread *use_thread) {
41   if (*handle != nullptr) {
42     acl_handle_map.insert({reinterpret_cast<void **>(handle), use_thread});
43   }
44 }
45 
DelHandle(acltdtChannelHandle ** handle)46 inline void TdtHandle::DelHandle(acltdtChannelHandle **handle) {
47   void **void_handle = reinterpret_cast<void **>(handle);
48   acl_handle_map.erase(void_handle);
49 }
50 
DestroyHandle()51 inline bool TdtHandle::DestroyHandle() {
52   bool destroy_all = true;
53   for (auto &item : acl_handle_map) {
54     acltdtChannelHandle **handle = reinterpret_cast<acltdtChannelHandle **>(item.first);
55     if (*handle != nullptr) {
56       acltdtStopChannel(*handle);
57       if (item.second != nullptr && item.second->joinable()) {
58         item.second->join();
59       }
60       if (acltdtDestroyChannel(*handle) != ACL_SUCCESS) {
61         destroy_all = false;
62       } else {
63         *handle = nullptr;
64       }
65     }
66   }
67   // clear the map container when all the handles have been destroyed
68   if (destroy_all) {
69     acl_handle_map.clear();
70   }
71   return destroy_all;
72 }
73 
74 }  // namespace dataset
75 }  // namespace mindspore
76 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TDT_TDT_HANDLE_H_
77