|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +/* |
| 20 | + * \file src/ffi/extra/stream_context.cc |
| 21 | + * |
| 22 | + * \brief A minimalistic stream context based on ffi values. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <tvm/ffi/extra/c_env_api.h> |
| 26 | +#include <tvm/ffi/function.h> |
| 27 | + |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +namespace tvm { |
| 31 | +namespace ffi { |
| 32 | + |
| 33 | +class StreamContext { |
| 34 | + public: |
| 35 | + void SetStream(int32_t device_type, int32_t device_id, TVMFFIStreamHandle stream, |
| 36 | + TVMFFIStreamHandle* out_original_stream) { |
| 37 | + if (static_cast<size_t>(device_type) >= stream_table_.size()) { |
| 38 | + stream_table_.resize(device_type + 1); |
| 39 | + } |
| 40 | + if (static_cast<size_t>(device_id) >= stream_table_[device_type].size()) { |
| 41 | + stream_table_[device_type].resize(device_id + 1, nullptr); |
| 42 | + } |
| 43 | + if (out_original_stream != nullptr) { |
| 44 | + *out_original_stream = stream_table_[device_type][device_id]; |
| 45 | + } |
| 46 | + stream_table_[device_type][device_id] = stream; |
| 47 | + } |
| 48 | + |
| 49 | + TVMFFIStreamHandle GetStream(int32_t device_type, int32_t device_id) { |
| 50 | + if (static_cast<size_t>(device_type) < stream_table_.size() && |
| 51 | + static_cast<size_t>(device_id) < stream_table_[device_type].size()) { |
| 52 | + return stream_table_[device_type][device_id]; |
| 53 | + } |
| 54 | + return nullptr; |
| 55 | + } |
| 56 | + |
| 57 | + static StreamContext* ThreadLocal() { |
| 58 | + static thread_local StreamContext inst; |
| 59 | + return &inst; |
| 60 | + } |
| 61 | + |
| 62 | + private: |
| 63 | + std::vector<std::vector<TVMFFIStreamHandle>> stream_table_; |
| 64 | +}; |
| 65 | + |
| 66 | +} // namespace ffi |
| 67 | +} // namespace tvm |
| 68 | + |
| 69 | +int TVMFFIEnvSetStream(int32_t device_type, int32_t device_id, TVMFFIStreamHandle stream, |
| 70 | + TVMFFIStreamHandle* out_original_stream) { |
| 71 | + TVM_FFI_SAFE_CALL_BEGIN(); |
| 72 | + tvm::ffi::StreamContext::ThreadLocal()->SetStream(device_type, device_id, stream, |
| 73 | + out_original_stream); |
| 74 | + TVM_FFI_SAFE_CALL_END(); |
| 75 | +} |
| 76 | + |
| 77 | +TVMFFIStreamHandle TVMFFIEnvGetCurrentStream(int32_t device_type, int32_t device_id) { |
| 78 | + TVM_FFI_LOG_EXCEPTION_CALL_BEGIN(); |
| 79 | + return tvm::ffi::StreamContext::ThreadLocal()->GetStream(device_type, device_id); |
| 80 | + TVM_FFI_LOG_EXCEPTION_CALL_END(TVMFFIEnvGetCurrentStream); |
| 81 | +} |
0 commit comments