|
| 1 | +//===----------- adapter.cpp - LLVM Offload Adapter ----------------------===// |
| 2 | +// |
| 3 | +// Copyright (C) 2024 Intel Corporation |
| 4 | +// |
| 5 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM |
| 6 | +// Exceptions. See LICENSE.TXT |
| 7 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#include <OffloadAPI.h> |
| 12 | +#include <atomic> |
| 13 | +#include <cstdint> |
| 14 | +#include <unordered_set> |
| 15 | + |
| 16 | +#include "adapter.hpp" |
| 17 | +#include "device.hpp" |
| 18 | +#include "platform.hpp" |
| 19 | +#include "ur/ur.hpp" |
| 20 | +#include "ur_api.h" |
| 21 | + |
| 22 | +ur_adapter_handle_t_ Adapter{}; |
| 23 | + |
| 24 | +// Initialize liboffload and perform the initial platform and device discovery |
| 25 | +ur_result_t ur_adapter_handle_t_::init() { |
| 26 | + auto Res = olInit(); |
| 27 | + (void)Res; |
| 28 | + |
| 29 | + // Discover every platform and device |
| 30 | + Res = olIterateDevices( |
| 31 | + [](ol_device_handle_t D, void *UserData) { |
| 32 | + auto *Platforms = |
| 33 | + reinterpret_cast<decltype(Adapter.Platforms) *>(UserData); |
| 34 | + |
| 35 | + ol_platform_handle_t Platform; |
| 36 | + olGetDeviceInfo(D, OL_DEVICE_INFO_PLATFORM, sizeof(Platform), |
| 37 | + &Platform); |
| 38 | + ol_platform_backend_t Backend; |
| 39 | + olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND, sizeof(Backend), |
| 40 | + &Backend); |
| 41 | + if (Backend == OL_PLATFORM_BACKEND_HOST) { |
| 42 | + Adapter.HostDevice = D; |
| 43 | + } else if (Backend != OL_PLATFORM_BACKEND_UNKNOWN) { |
| 44 | + auto URPlatform = |
| 45 | + std::find_if(Platforms->begin(), Platforms->end(), [&](auto &P) { |
| 46 | + return P.OffloadPlatform == Platform; |
| 47 | + }); |
| 48 | + |
| 49 | + if (URPlatform == Platforms->end()) { |
| 50 | + URPlatform = |
| 51 | + Platforms->insert(URPlatform, ur_platform_handle_t_(Platform)); |
| 52 | + } |
| 53 | + |
| 54 | + URPlatform->Devices.push_back(ur_device_handle_t_{&*URPlatform, D}); |
| 55 | + } |
| 56 | + return false; |
| 57 | + }, |
| 58 | + &Adapter.Platforms); |
| 59 | + |
| 60 | + (void)Res; |
| 61 | + |
| 62 | + return UR_RESULT_SUCCESS; |
| 63 | +} |
| 64 | + |
| 65 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet( |
| 66 | + uint32_t, ur_adapter_handle_t *phAdapters, uint32_t *pNumAdapters) { |
| 67 | + if (phAdapters) { |
| 68 | + if (++Adapter.RefCount == 1) { |
| 69 | + Adapter.init(); |
| 70 | + } |
| 71 | + *phAdapters = &Adapter; |
| 72 | + } |
| 73 | + if (pNumAdapters) { |
| 74 | + *pNumAdapters = 1; |
| 75 | + } |
| 76 | + return UR_RESULT_SUCCESS; |
| 77 | +} |
| 78 | + |
| 79 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) { |
| 80 | + if (--Adapter.RefCount == 0) { |
| 81 | + // This can crash when tracing is enabled. |
| 82 | + // olShutDown(); |
| 83 | + }; |
| 84 | + return UR_RESULT_SUCCESS; |
| 85 | +} |
| 86 | + |
| 87 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) { |
| 88 | + Adapter.RefCount++; |
| 89 | + return UR_RESULT_SUCCESS; |
| 90 | +} |
| 91 | + |
| 92 | +UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t, |
| 93 | + ur_adapter_info_t propName, |
| 94 | + size_t propSize, |
| 95 | + void *pPropValue, |
| 96 | + size_t *pPropSizeRet) { |
| 97 | + UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); |
| 98 | + |
| 99 | + switch (propName) { |
| 100 | + case UR_ADAPTER_INFO_BACKEND: |
| 101 | + return ReturnValue(UR_BACKEND_OFFLOAD); |
| 102 | + case UR_ADAPTER_INFO_REFERENCE_COUNT: |
| 103 | + return ReturnValue(Adapter.RefCount.load()); |
| 104 | + default: |
| 105 | + return UR_RESULT_ERROR_INVALID_ENUMERATION; |
| 106 | + } |
| 107 | + |
| 108 | + return UR_RESULT_SUCCESS; |
| 109 | +} |
0 commit comments