File: zer_libapi.cpp

package info (click to toggle)
level-zero 1.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,468 kB
  • sloc: cpp: 130,327; ansic: 16,197; python: 9,824; makefile: 4
file content (245 lines) | stat: -rw-r--r-- 8,909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 *
 * Copyright (C) 2019-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 * @file zer_libapi.cpp
 *
 * @brief C++ static library for zer
 *
 */
#include "ze_lib.h"
#include "error_state.h"

extern "C" {

///////////////////////////////////////////////////////////////////////////////
/// @brief Retrieves a string describing the last error code returned by the
///        default driver in the current thread.
/// 
/// @details
///     - String returned is thread local.
///     - String is only updated on calls returning an error, i.e., not on calls
///       returning ::ZE_RESULT_SUCCESS.
///     - String may be empty if driver considers error code is already explicit
///       enough to describe cause.
///     - Memory pointed to by ppString is owned by the default driver.
///     - String returned is null-terminated.
/// 
/// @returns
///     - ::ZE_RESULT_SUCCESS
///     - ::ZE_RESULT_ERROR_UNINITIALIZED
///     - ::ZE_RESULT_ERROR_DEVICE_LOST
///     - ::ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY
///     - ::ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY
///     - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER
///         + `nullptr == ppString`
ze_result_t ZE_APICALL
zerGetLastErrorDescription(
    const char** ppString                           ///< [in,out] pointer to a null-terminated array of characters describing
                                                    ///< cause of error.
    )
{
    #ifdef L0_STATIC_LOADER_BUILD
    ze_result_t result = ZE_RESULT_SUCCESS;
    if(ze_lib::destruction) {
        return ZE_RESULT_ERROR_UNINITIALIZED;
    }
    error_state::getErrorDesc(ppString);
    if (ppString && *ppString && strlen(*ppString) > 0)
    {
        return ZE_RESULT_SUCCESS;
    }
    static const zer_pfnGetLastErrorDescription_t pfnGetLastErrorDescription = [&result] {
        auto pfnGetLastErrorDescription = ze_lib::context->zerDdiTable.load()->Global.pfnGetLastErrorDescription;
        if( nullptr == pfnGetLastErrorDescription ) {
            result = ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
        }
        return pfnGetLastErrorDescription;
    }();
    if (result != ZE_RESULT_SUCCESS) {
        return result;
    }
    return pfnGetLastErrorDescription( ppString );
    #else
    if(ze_lib::destruction) {
        return ZE_RESULT_ERROR_UNINITIALIZED;
    }

    error_state::getErrorDesc(ppString);
    if (ppString && *ppString && strlen(*ppString) > 0)
    {
        return ZE_RESULT_SUCCESS;
    }
    auto pfnGetLastErrorDescription = ze_lib::context->zerDdiTable.load()->Global.pfnGetLastErrorDescription;
    if( nullptr == pfnGetLastErrorDescription ) {
        if(!ze_lib::context->isInitialized)
            return ZE_RESULT_ERROR_UNINITIALIZED;
        else
            return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
    }

    return pfnGetLastErrorDescription( ppString );
    #endif
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Translates device handle to integer identifier.
/// 
/// @details
///     - The implementation of this function should be lock-free.
///     - This function does not return error code, to get info about failure
///       user may use ::zerGetLastErrorDescription function.
///     - In case of failure, this function returns UINT32_MAX.
/// 
/// @returns
///     - integer identifier for the device
///     - UINT32_MAX
uint32_t ZE_APICALL
zerTranslateDeviceHandleToIdentifier(
    ze_device_handle_t hDevice                      ///< [in] handle of the device
    )
{
    #ifdef L0_STATIC_LOADER_BUILD
    if(ze_lib::destruction) {
        error_state::setErrorDesc("ERROR UNINITIALIZED");
        return UINT32_MAX;
    }
    static const zer_pfnTranslateDeviceHandleToIdentifier_t pfnTranslateDeviceHandleToIdentifier = [] {
        auto pfnTranslateDeviceHandleToIdentifier = ze_lib::context->zerDdiTable.load()->Global.pfnTranslateDeviceHandleToIdentifier;
        return pfnTranslateDeviceHandleToIdentifier;
    }();
    if (nullptr == pfnTranslateDeviceHandleToIdentifier) {
        error_state::setErrorDesc("ERROR UNSUPPORTED FEATURE");
        return UINT32_MAX;
    }    
    return pfnTranslateDeviceHandleToIdentifier( hDevice );
    #else
    if(ze_lib::destruction) {
        error_state::setErrorDesc("ERROR UNINITIALIZED");
        return UINT32_MAX;
    }

    auto pfnTranslateDeviceHandleToIdentifier = ze_lib::context->zerDdiTable.load()->Global.pfnTranslateDeviceHandleToIdentifier;
    if( nullptr == pfnTranslateDeviceHandleToIdentifier ) {
        if(!ze_lib::context->isInitialized)
            error_state::setErrorDesc("ERROR UNINITIALIZED");
        else
            error_state::setErrorDesc("ERROR UNSUPPORTED FEATURE");
        return UINT32_MAX;
    }

    return pfnTranslateDeviceHandleToIdentifier( hDevice );
    #endif
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Translates to integer identifier to a device handle.
/// 
/// @details
///     - The driver must be initialized before calling this function.
///     - The implementation of this function should be lock-free.
///     - This function does not return error code, to get info about failure
///       user may use ::zerGetLastErrorDescription function.
///     - In case of failure, this function returns null.
///     - Details on the error can be retrieved using
///       ::zerGetLastErrorDescription function.
/// 
/// @returns
///     - handle of the device with the given identifier
///     - nullptr
ze_device_handle_t ZE_APICALL
zerTranslateIdentifierToDeviceHandle(
    uint32_t identifier                             ///< [in] integer identifier of the device
    )
{
    #ifdef L0_STATIC_LOADER_BUILD
    if(ze_lib::destruction) {
        error_state::setErrorDesc("ERROR UNINITIALIZED");
        return nullptr;
    }
    static const zer_pfnTranslateIdentifierToDeviceHandle_t pfnTranslateIdentifierToDeviceHandle = [] {
        auto pfnTranslateIdentifierToDeviceHandle = ze_lib::context->zerDdiTable.load()->Global.pfnTranslateIdentifierToDeviceHandle;
        return pfnTranslateIdentifierToDeviceHandle;
    }();
    if (nullptr == pfnTranslateIdentifierToDeviceHandle) {
        error_state::setErrorDesc("ERROR UNSUPPORTED FEATURE");
        return nullptr;
    }    
    return pfnTranslateIdentifierToDeviceHandle( identifier );
    #else
    if(ze_lib::destruction) {
        error_state::setErrorDesc("ERROR UNINITIALIZED");
        return nullptr;
    }

    auto pfnTranslateIdentifierToDeviceHandle = ze_lib::context->zerDdiTable.load()->Global.pfnTranslateIdentifierToDeviceHandle;
    if( nullptr == pfnTranslateIdentifierToDeviceHandle ) {
        if(!ze_lib::context->isInitialized)
            error_state::setErrorDesc("ERROR UNINITIALIZED");
        else
            error_state::setErrorDesc("ERROR UNSUPPORTED FEATURE");
        return nullptr;
    }

    return pfnTranslateIdentifierToDeviceHandle( identifier );
    #endif
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Retrieves handle to default context from the default driver.
/// 
/// @details
///     - The driver must be initialized before calling this function.
///     - The implementation of this function should be lock-free.
///     - This returned context contains all the devices available in the
///       default driver.
///     - This function does not return error code, to get info about failure
///       user may use ::zerGetLastErrorDescription function.
///     - In case of failure, this function returns null.
///     - Details on the error can be retrieved using
///       ::zerGetLastErrorDescription function.
/// 
/// @returns
///     - handle of the default context
///     - nullptr
ze_context_handle_t ZE_APICALL
zerGetDefaultContext(
    void
    )
{
    #ifdef L0_STATIC_LOADER_BUILD
    if(ze_lib::destruction) {
        error_state::setErrorDesc("ERROR UNINITIALIZED");
        return nullptr;
    }
    static const zer_pfnGetDefaultContext_t pfnGetDefaultContext = [] {
        auto pfnGetDefaultContext = ze_lib::context->zerDdiTable.load()->Global.pfnGetDefaultContext;
        return pfnGetDefaultContext;
    }();
    if (nullptr == pfnGetDefaultContext) {
        error_state::setErrorDesc("ERROR UNSUPPORTED FEATURE");
        return nullptr;
    }    
    return pfnGetDefaultContext(  );
    #else
    if(ze_lib::destruction) {
        error_state::setErrorDesc("ERROR UNINITIALIZED");
        return nullptr;
    }

    auto pfnGetDefaultContext = ze_lib::context->zerDdiTable.load()->Global.pfnGetDefaultContext;
    if( nullptr == pfnGetDefaultContext ) {
        if(!ze_lib::context->isInitialized)
            error_state::setErrorDesc("ERROR UNINITIALIZED");
        else
            error_state::setErrorDesc("ERROR UNSUPPORTED FEATURE");
        return nullptr;
    }

    return pfnGetDefaultContext(  );
    #endif
}

} // extern "C"