|
8 | 8 | //
|
9 | 9 | //===----------------------------------------------------------------------===//
|
10 | 10 |
|
| 11 | +#include "ur/ur.hpp" |
11 | 12 | #include "ur_api.h"
|
12 | 13 |
|
13 | 14 | #include "common.hpp"
|
| 15 | +#include "context.hpp" |
| 16 | +#include <cstdlib> |
14 | 17 |
|
15 |
| -UR_APIEXPORT ur_result_t UR_APICALL |
16 |
| -urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc, |
17 |
| - ur_usm_pool_handle_t pool, size_t size, void **ppMem) { |
18 |
| - std::ignore = hContext; |
19 |
| - std::ignore = pUSMDesc; |
20 |
| - std::ignore = pool; |
| 18 | +namespace native_cpu { |
21 | 19 |
|
| 20 | +static ur_result_t alloc_helper(ur_context_handle_t hContext, |
| 21 | + const ur_usm_desc_t *pUSMDesc, size_t size, |
| 22 | + void **ppMem, ur_usm_type_t type) { |
| 23 | + auto alignment = pUSMDesc ? pUSMDesc->align : 1u; |
| 24 | + UR_ASSERT((alignment & (alignment - 1)) == 0, UR_RESULT_ERROR_INVALID_VALUE); |
22 | 25 | UR_ASSERT(ppMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
|
23 | 26 | // TODO: Check Max size when UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE is implemented
|
24 | 27 | UR_ASSERT(size > 0, UR_RESULT_ERROR_INVALID_USM_SIZE);
|
25 | 28 |
|
26 |
| - *ppMem = malloc(size); |
| 29 | + auto *ptr = hContext->add_alloc(alignment, type, size, nullptr); |
| 30 | + UR_ASSERT(ptr != nullptr, UR_RESULT_ERROR_OUT_OF_RESOURCES); |
| 31 | + *ppMem = ptr; |
27 | 32 |
|
28 | 33 | return UR_RESULT_SUCCESS;
|
29 | 34 | }
|
30 | 35 |
|
| 36 | +} // namespace native_cpu |
| 37 | + |
| 38 | +UR_APIEXPORT ur_result_t UR_APICALL |
| 39 | +urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc, |
| 40 | + ur_usm_pool_handle_t pool, size_t size, void **ppMem) { |
| 41 | + std::ignore = pool; |
| 42 | + |
| 43 | + return native_cpu::alloc_helper(hContext, pUSMDesc, size, ppMem, |
| 44 | + UR_USM_TYPE_HOST); |
| 45 | +} |
| 46 | + |
31 | 47 | UR_APIEXPORT ur_result_t UR_APICALL
|
32 | 48 | urUSMDeviceAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
|
33 | 49 | const ur_usm_desc_t *pUSMDesc, ur_usm_pool_handle_t pool,
|
34 | 50 | size_t size, void **ppMem) {
|
35 |
| - std::ignore = hContext; |
36 | 51 | std::ignore = hDevice;
|
37 |
| - std::ignore = pUSMDesc; |
38 | 52 | std::ignore = pool;
|
39 | 53 |
|
40 |
| - UR_ASSERT(ppMem, UR_RESULT_ERROR_INVALID_NULL_POINTER); |
41 |
| - // TODO: Check Max size when UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE is implemented |
42 |
| - UR_ASSERT(size > 0, UR_RESULT_ERROR_INVALID_USM_SIZE); |
43 |
| - |
44 |
| - *ppMem = malloc(size); |
45 |
| - |
46 |
| - return UR_RESULT_SUCCESS; |
| 54 | + return native_cpu::alloc_helper(hContext, pUSMDesc, size, ppMem, |
| 55 | + UR_USM_TYPE_DEVICE); |
47 | 56 | }
|
48 | 57 |
|
49 | 58 | UR_APIEXPORT ur_result_t UR_APICALL
|
50 | 59 | urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
|
51 | 60 | const ur_usm_desc_t *pUSMDesc, ur_usm_pool_handle_t pool,
|
52 | 61 | size_t size, void **ppMem) {
|
53 |
| - std::ignore = hContext; |
54 | 62 | std::ignore = hDevice;
|
55 |
| - std::ignore = pUSMDesc; |
56 | 63 | std::ignore = pool;
|
57 | 64 |
|
58 |
| - UR_ASSERT(ppMem, UR_RESULT_ERROR_INVALID_NULL_POINTER); |
59 |
| - // TODO: Check Max size when UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE is implemented |
60 |
| - UR_ASSERT(size > 0, UR_RESULT_ERROR_INVALID_USM_SIZE); |
61 |
| - |
62 |
| - *ppMem = malloc(size); |
63 |
| - |
64 |
| - return UR_RESULT_SUCCESS; |
| 65 | + return native_cpu::alloc_helper(hContext, pUSMDesc, size, ppMem, |
| 66 | + UR_USM_TYPE_SHARED); |
65 | 67 | }
|
66 | 68 |
|
67 | 69 | UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,
|
68 | 70 | void *pMem) {
|
69 |
| - std::ignore = hContext; |
70 | 71 |
|
71 | 72 | UR_ASSERT(pMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
|
| 73 | + UR_ASSERT(hContext, UR_RESULT_ERROR_INVALID_NULL_POINTER); |
72 | 74 |
|
73 |
| - free(pMem); |
| 75 | + auto res = hContext->remove_alloc(pMem); |
74 | 76 |
|
75 |
| - return UR_RESULT_SUCCESS; |
| 77 | + return res; |
76 | 78 | }
|
77 | 79 |
|
78 | 80 | UR_APIEXPORT ur_result_t UR_APICALL
|
79 | 81 | urUSMGetMemAllocInfo(ur_context_handle_t hContext, const void *pMem,
|
80 | 82 | ur_usm_alloc_info_t propName, size_t propSize,
|
81 | 83 | void *pPropValue, size_t *pPropSizeRet) {
|
82 |
| - std::ignore = hContext; |
83 |
| - std::ignore = pMem; |
84 |
| - std::ignore = propName; |
85 |
| - std::ignore = propSize; |
86 |
| - std::ignore = pPropValue; |
87 |
| - std::ignore = pPropSizeRet; |
88 | 84 |
|
| 85 | + UR_ASSERT(pMem != nullptr, UR_RESULT_ERROR_INVALID_NULL_POINTER); |
89 | 86 | UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
|
| 87 | + if (propName == UR_USM_ALLOC_INFO_BASE_PTR) { |
| 88 | + // TODO: logic to compute base ptr given ptr |
| 89 | + DIE_NO_IMPLEMENTATION; |
| 90 | + } |
90 | 91 |
|
| 92 | + const native_cpu::usm_alloc_info &alloc_info = |
| 93 | + hContext->get_alloc_info_entry(pMem); |
91 | 94 | switch (propName) {
|
92 | 95 | case UR_USM_ALLOC_INFO_TYPE:
|
93 |
| - // Todo implement this in context |
94 |
| - return ReturnValue(UR_USM_TYPE_DEVICE); |
| 96 | + return ReturnValue(alloc_info.type); |
| 97 | + case UR_USM_ALLOC_INFO_SIZE: |
| 98 | + return ReturnValue(alloc_info.size); |
| 99 | + case UR_USM_ALLOC_INFO_DEVICE: |
| 100 | + return ReturnValue(alloc_info.device); |
| 101 | + case UR_USM_ALLOC_INFO_POOL: |
| 102 | + return ReturnValue(alloc_info.pool); |
95 | 103 | default:
|
96 | 104 | DIE_NO_IMPLEMENTATION;
|
97 | 105 | }
|
|
0 commit comments