|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <cuda_runtime.h> |
| 12 | +#include <executorch/backends/aoti/common_shims.h> |
| 13 | +#include <executorch/backends/cuda/runtime/guard.h> |
| 14 | +#include <cstdint> |
| 15 | + |
| 16 | +namespace executorch { |
| 17 | +namespace backends { |
| 18 | +namespace cuda { |
| 19 | + |
| 20 | +using executorch::backends::aoti::AOTITorchError; |
| 21 | + |
| 22 | +extern "C" { |
| 23 | + |
| 24 | +// Handle types for CUDA guards |
| 25 | +using CUDAGuardHandle = CUDAGuard*; |
| 26 | +using CUDAStreamGuardHandle = CUDAStreamGuard*; |
| 27 | + |
| 28 | +/** |
| 29 | + * Creates a CUDA device guard that sets the current device and restores it |
| 30 | + * upon destruction. |
| 31 | + * |
| 32 | + * @param device_index The device index to set as current |
| 33 | + * @param ret_guard Output parameter for the created guard handle (must not be |
| 34 | + * null) |
| 35 | + * @return AOTITorchError error code (Error::Ok on success, or an error code on |
| 36 | + * failure) |
| 37 | + */ |
| 38 | +AOTITorchError aoti_torch_create_cuda_guard( |
| 39 | + int32_t device_index, |
| 40 | + CUDAGuardHandle* ret_guard); |
| 41 | + |
| 42 | +/** |
| 43 | + * Deletes a CUDA device guard and frees its associated resources. |
| 44 | + * |
| 45 | + * @param guard Handle to the guard to be deleted |
| 46 | + * @return AOTITorchError error code (Error::Ok on success, or an error code on |
| 47 | + * failure) |
| 48 | + */ |
| 49 | +AOTITorchError aoti_torch_delete_cuda_guard(CUDAGuardHandle guard); |
| 50 | + |
| 51 | +/** |
| 52 | + * Sets the CUDA device to a new index for an existing guard. |
| 53 | + * |
| 54 | + * @param guard Handle to the guard |
| 55 | + * @param device_index The device index to set as current |
| 56 | + * @return AOTITorchError error code (Error::Ok on success, or an error code on |
| 57 | + * failure) |
| 58 | + */ |
| 59 | +AOTITorchError aoti_torch_cuda_guard_set_index( |
| 60 | + CUDAGuardHandle guard, |
| 61 | + int32_t device_index); |
| 62 | + |
| 63 | +/** |
| 64 | + * Creates a CUDA stream guard that sets the current device and stream, |
| 65 | + * restoring both upon destruction. |
| 66 | + * |
| 67 | + * @param stream The CUDA stream to set as current |
| 68 | + * @param device_index The device index for the stream |
| 69 | + * @param ret_guard Output parameter for the created guard handle (must not be |
| 70 | + * null) |
| 71 | + * @return AOTITorchError error code (Error::Ok on success, or an error code on |
| 72 | + * failure) |
| 73 | + */ |
| 74 | +AOTITorchError aoti_torch_create_cuda_stream_guard( |
| 75 | + void* stream, |
| 76 | + int32_t device_index, |
| 77 | + CUDAStreamGuardHandle* ret_guard); |
| 78 | + |
| 79 | +/** |
| 80 | + * Deletes a CUDA stream guard and frees its associated resources. |
| 81 | + * |
| 82 | + * @param guard Handle to the stream guard to be deleted |
| 83 | + * @return AOTITorchError error code (Error::Ok on success, or an error code on |
| 84 | + * failure) |
| 85 | + */ |
| 86 | +AOTITorchError aoti_torch_delete_cuda_stream_guard(CUDAStreamGuardHandle guard); |
| 87 | + |
| 88 | +/** |
| 89 | + * Gets the current CUDA stream for a specified device. |
| 90 | + * |
| 91 | + * @param device_index The device index (-1 to use current device) |
| 92 | + * @param ret_stream Output parameter for the current stream (must not be null) |
| 93 | + * @return AOTITorchError error code (Error::Ok on success, or an error code on |
| 94 | + * failure) |
| 95 | + */ |
| 96 | +AOTITorchError aoti_torch_get_current_cuda_stream( |
| 97 | + int32_t device_index, |
| 98 | + void** ret_stream); |
| 99 | + |
| 100 | +} // extern "C" |
| 101 | + |
| 102 | +} // namespace cuda |
| 103 | +} // namespace backends |
| 104 | +} // namespace executorch |
0 commit comments