|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * */ |
| 24 | +#include "ascend_stream.h" |
| 25 | + |
| 26 | +namespace UC::Trans { |
| 27 | + |
| 28 | +AscendStream::~AscendStream() |
| 29 | +{ |
| 30 | + if (cbThread_.joinable()) { |
| 31 | + auto tid = cbThread_.native_handle(); |
| 32 | + (void)aclrtUnSubscribeReport(tid, stream_); |
| 33 | + stop_ = true; |
| 34 | + cbThread_.join(); |
| 35 | + } |
| 36 | + if (stream_) { |
| 37 | + (void)aclrtDestroyStream(stream_); |
| 38 | + stream_ = nullptr; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +Status AscendStream::Setup() |
| 43 | +{ |
| 44 | + auto ret = aclrtCreateStream(&stream_); |
| 45 | + if (ret != ACL_SUCCESS) [[unlikely]] { return Status{ret, std::to_string(ret)}; } |
| 46 | + cbThread_ = std::thread([this] { |
| 47 | + while (!this->stop_) { (void)aclrtProcessReport(10); } |
| 48 | + }); |
| 49 | + auto tid = cbThread_.native_handle(); |
| 50 | + ret = aclrtSubscribeReport(tid, stream_); |
| 51 | + if (ret != ACL_SUCCESS) [[unlikely]] { return Status{ret, std::to_string(ret)}; } |
| 52 | + return Status::OK(); |
| 53 | +} |
| 54 | + |
| 55 | +Status AscendStream::DeviceToHost(void* device, void* host, size_t size) |
| 56 | +{ |
| 57 | + auto ret = aclrtMemcpy(host, size, device, size, ACL_MEMCPY_DEVICE_TO_HOST); |
| 58 | + if (ret == ACL_SUCCESS) { return Status::OK(); } |
| 59 | + return Status{ret, std::to_string(ret)}; |
| 60 | +} |
| 61 | + |
| 62 | +Status AscendStream::DeviceToHost(void* device[], void* host[], size_t size, size_t number) |
| 63 | +{ |
| 64 | + auto s = DeviceToHostAsync(device, host, size, number); |
| 65 | + if (s.Failure()) [[unlikely]] { return s; } |
| 66 | + return Synchronized(); |
| 67 | +} |
| 68 | + |
| 69 | +Status AscendStream::DeviceToHost(void* device[], void* host, size_t size, size_t number) |
| 70 | +{ |
| 71 | + auto s = DeviceToHostAsync(device, host, size, number); |
| 72 | + if (s.Failure()) [[unlikely]] { return s; } |
| 73 | + return Synchronized(); |
| 74 | +} |
| 75 | + |
| 76 | +Status AscendStream::DeviceToHostAsync(void* device, void* host, size_t size) |
| 77 | +{ |
| 78 | + auto ret = aclrtMemcpyAsync(host, size, device, size, ACL_MEMCPY_DEVICE_TO_HOST, stream_); |
| 79 | + if (ret == ACL_SUCCESS) { return Status::OK(); } |
| 80 | + return Status{ret, std::to_string(ret)}; |
| 81 | +} |
| 82 | + |
| 83 | +Status AscendStream::DeviceToHostAsync(void* device[], void* host[], size_t size, size_t number) |
| 84 | +{ |
| 85 | + for (size_t i = 0; i < number; i++) { |
| 86 | + auto s = DeviceToHostAsync(device[i], host[i], size); |
| 87 | + if (s.Failure()) [[unlikely]] { return s; } |
| 88 | + } |
| 89 | + return Status::OK(); |
| 90 | +} |
| 91 | + |
| 92 | +Status AscendStream::DeviceToHostAsync(void* device[], void* host, size_t size, size_t number) |
| 93 | +{ |
| 94 | + for (size_t i = 0; i < number; i++) { |
| 95 | + auto pHost = (void*)(((int8_t*)host) + size * i); |
| 96 | + auto s = DeviceToHostAsync(device[i], pHost, size); |
| 97 | + if (s.Failure()) [[unlikely]] { return s; } |
| 98 | + } |
| 99 | + return Status::OK(); |
| 100 | +} |
| 101 | + |
| 102 | +Status AscendStream::HostToDevice(void* host, void* device, size_t size) |
| 103 | +{ |
| 104 | + auto ret = aclrtMemcpy(device, size, host, size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 105 | + if (ret == ACL_SUCCESS) { return Status::OK(); } |
| 106 | + return Status{ret, std::to_string(ret)}; |
| 107 | +} |
| 108 | + |
| 109 | +Status AscendStream::HostToDevice(void* host[], void* device[], size_t size, size_t number) |
| 110 | +{ |
| 111 | + auto s = HostToDeviceAsync(host, device, size, number); |
| 112 | + if (s.Failure()) [[unlikely]] { return s; } |
| 113 | + return Synchronized(); |
| 114 | +} |
| 115 | + |
| 116 | +Status AscendStream::HostToDevice(void* host, void* device[], size_t size, size_t number) |
| 117 | +{ |
| 118 | + auto s = HostToDeviceAsync(host, device, size, number); |
| 119 | + if (s.Failure()) [[unlikely]] { return s; } |
| 120 | + return Synchronized(); |
| 121 | +} |
| 122 | + |
| 123 | +Status AscendStream::HostToDeviceAsync(void* host, void* device, size_t size) |
| 124 | +{ |
| 125 | + auto ret = aclrtMemcpyAsync(device, size, host, size, ACL_MEMCPY_HOST_TO_DEVICE, stream_); |
| 126 | + if (ret == ACL_SUCCESS) { return Status::OK(); } |
| 127 | + return Status{ret, std::to_string(ret)}; |
| 128 | +} |
| 129 | + |
| 130 | +Status AscendStream::HostToDeviceAsync(void* host[], void* device[], size_t size, size_t number) |
| 131 | +{ |
| 132 | + for (size_t i = 0; i < number; i++) { |
| 133 | + auto s = HostToDeviceAsync(host[i], device[i], size); |
| 134 | + if (s.Failure()) [[unlikely]] { return s; } |
| 135 | + } |
| 136 | + return Status::OK(); |
| 137 | +} |
| 138 | + |
| 139 | +Status AscendStream::HostToDeviceAsync(void* host, void* device[], size_t size, size_t number) |
| 140 | +{ |
| 141 | + for (size_t i = 0; i < number; i++) { |
| 142 | + auto pHost = (void*)(((int8_t*)host) + size * i); |
| 143 | + auto s = HostToDeviceAsync(pHost, device[i], size); |
| 144 | + if (s.Failure()) [[unlikely]] { return s; } |
| 145 | + } |
| 146 | + return Status::OK(); |
| 147 | +} |
| 148 | + |
| 149 | +using Closure = std::function<void(bool)>; |
| 150 | + |
| 151 | +static void Trampoline(void* data) |
| 152 | +{ |
| 153 | + auto c = static_cast<Closure*>(data); |
| 154 | + (*c)(true); |
| 155 | + delete c; |
| 156 | +} |
| 157 | + |
| 158 | +Status Trans::AscendStream::AppendCallback(std::function<void(bool)> cb) |
| 159 | +{ |
| 160 | + auto c = new (std::nothrow) Closure{std::move(cb)}; |
| 161 | + if (!c) [[unlikely]] { return Status::Error("out of memory for appending callback"); } |
| 162 | + auto ret = aclrtLaunchCallback(Trampoline, (void*)c, ACL_CALLBACK_NO_BLOCK, stream_); |
| 163 | + if (ret != ACL_SUCCESS) [[unlikely]] { |
| 164 | + delete c; |
| 165 | + return Status{ret, std::to_string(ret)}; |
| 166 | + } |
| 167 | + return Status::OK(); |
| 168 | +} |
| 169 | + |
| 170 | +Status AscendStream::Synchronized() |
| 171 | +{ |
| 172 | + auto ret = aclrtSynchronizeStream(stream_); |
| 173 | + if (ret == ACL_SUCCESS) { return Status::OK(); } |
| 174 | + return Status{ret, std::to_string(ret)}; |
| 175 | +} |
| 176 | + |
| 177 | +} // namespace UC::Trans |
0 commit comments