|
| 1 | +//===--------- adapter.cpp - CUDA Adapter ----------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===-----------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <ur_api.h> |
| 10 | + |
| 11 | +#include "common.hpp" |
| 12 | + |
| 13 | +void enableCUDATracing(); |
| 14 | +void disableCUDATracing(); |
| 15 | + |
| 16 | +struct ur_adapter_handle_t_ { |
| 17 | + std::atomic<uint32_t> RefCount = 0; |
| 18 | + std::mutex Mutex; |
| 19 | +}; |
| 20 | + |
| 21 | +ur_adapter_handle_t_ adapter{}; |
| 22 | + |
| 23 | +UR_APIEXPORT ur_result_t UR_APICALL urInit(ur_device_init_flags_t, |
| 24 | + ur_loader_config_handle_t) { |
| 25 | + return UR_RESULT_SUCCESS; |
| 26 | +} |
| 27 | + |
| 28 | +UR_APIEXPORT ur_result_t UR_APICALL urTearDown(void *) { |
| 29 | + return UR_RESULT_SUCCESS; |
| 30 | +} |
| 31 | + |
| 32 | +UR_APIEXPORT ur_result_t UR_APICALL |
| 33 | +urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, |
| 34 | + uint32_t *pNumAdapters) { |
| 35 | + if (NumEntries > 0 && phAdapters) { |
| 36 | + std::lock_guard<std::mutex> Lock{adapter.Mutex}; |
| 37 | + if (adapter.RefCount++ == 0) { |
| 38 | + enableCUDATracing(); |
| 39 | + } |
| 40 | + |
| 41 | + *phAdapters = &adapter; |
| 42 | + } |
| 43 | + |
| 44 | + if (pNumAdapters) { |
| 45 | + *pNumAdapters = 1; |
| 46 | + } |
| 47 | + |
| 48 | + return UR_RESULT_SUCCESS; |
| 49 | +} |
| 50 | + |
| 51 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) { |
| 52 | + adapter.RefCount++; |
| 53 | + |
| 54 | + return UR_RESULT_SUCCESS; |
| 55 | +} |
| 56 | + |
| 57 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) { |
| 58 | + std::lock_guard<std::mutex> Lock{adapter.Mutex}; |
| 59 | + if (--adapter.RefCount == 0) { |
| 60 | + disableCUDATracing(); |
| 61 | + } |
| 62 | + return UR_RESULT_SUCCESS; |
| 63 | +} |
| 64 | + |
| 65 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetLastError( |
| 66 | + ur_adapter_handle_t, const char **ppMessage, int32_t *pError) { |
| 67 | + *ppMessage = ErrorMessage; |
| 68 | + *pError = ErrorMessageCode; |
| 69 | + return UR_RESULT_SUCCESS; |
| 70 | +} |
| 71 | + |
| 72 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t, |
| 73 | + ur_adapter_info_t propName, |
| 74 | + size_t propSize, |
| 75 | + void *pPropValue, |
| 76 | + size_t *pPropSizeRet) { |
| 77 | + UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); |
| 78 | + |
| 79 | + switch (propName) { |
| 80 | + case UR_ADAPTER_INFO_BACKEND: |
| 81 | + return ReturnValue(UR_ADAPTER_BACKEND_CUDA); |
| 82 | + case UR_ADAPTER_INFO_REFERENCE_COUNT: |
| 83 | + return ReturnValue(adapter.RefCount.load()); |
| 84 | + default: |
| 85 | + return UR_RESULT_ERROR_INVALID_ENUMERATION; |
| 86 | + } |
| 87 | + |
| 88 | + return UR_RESULT_SUCCESS; |
| 89 | +} |
0 commit comments