88//
99// ===----------------------------------------------------------------------===//
1010
11+ #include " ur/ur.hpp"
1112#include " ur_api.h"
1213
1314#include " common.hpp"
15+ #include " context.hpp"
16+ #include < cstdlib>
17+
18+ namespace native_cpu {
19+
20+ static void *malloc_impl (uint32_t alignment, size_t size) {
21+ void *ptr = nullptr ;
22+ if (alignment) {
23+ ptr = std::aligned_alloc (alignment, size);
24+ } else {
25+ ptr = malloc (size);
26+ }
27+ return ptr;
28+ }
29+
30+ } // namespace native_cpu
1431
1532UR_APIEXPORT ur_result_t UR_APICALL
1633urUSMHostAlloc (ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,
1734 ur_usm_pool_handle_t pool, size_t size, void **ppMem) {
18- std::ignore = hContext;
19- std::ignore = pUSMDesc;
2035 std::ignore = pool;
2136
37+ auto alignment = pUSMDesc ? pUSMDesc->align : 0u ;
38+ UR_ASSERT ((alignment & (alignment - 1 )) == 0 , UR_RESULT_ERROR_INVALID_VALUE);
2239 UR_ASSERT (ppMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
2340 // TODO: Check Max size when UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE is implemented
2441 UR_ASSERT (size > 0 , UR_RESULT_ERROR_INVALID_USM_SIZE);
2542
26- *ppMem = malloc (size);
43+ void *ptr = native_cpu::malloc_impl (alignment, size);
44+ UR_ASSERT (ptr != nullptr , UR_RESULT_ERROR_OUT_OF_HOST_MEMORY);
45+ hContext->add_alloc_info_entry (ptr, UR_USM_TYPE_HOST, size, nullptr );
46+ *ppMem = ptr;
2747
2848 return UR_RESULT_SUCCESS;
2949}
@@ -32,16 +52,19 @@ UR_APIEXPORT ur_result_t UR_APICALL
3252urUSMDeviceAlloc (ur_context_handle_t hContext, ur_device_handle_t hDevice,
3353 const ur_usm_desc_t *pUSMDesc, ur_usm_pool_handle_t pool,
3454 size_t size, void **ppMem) {
35- std::ignore = hContext;
3655 std::ignore = hDevice;
37- std::ignore = pUSMDesc;
3856 std::ignore = pool;
3957
58+ auto alignment = pUSMDesc ? pUSMDesc->align : 0u ;
59+ UR_ASSERT ((alignment & (alignment - 1 )) == 0 , UR_RESULT_ERROR_INVALID_VALUE);
4060 UR_ASSERT (ppMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
4161 // TODO: Check Max size when UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE is implemented
4262 UR_ASSERT (size > 0 , UR_RESULT_ERROR_INVALID_USM_SIZE);
4363
44- *ppMem = malloc (size);
64+ void *ptr = native_cpu::malloc_impl (alignment, size);
65+ UR_ASSERT (ptr != nullptr , UR_RESULT_ERROR_OUT_OF_RESOURCES);
66+ *ppMem = ptr;
67+ hContext->add_alloc_info_entry (ptr, UR_USM_TYPE_DEVICE, size, nullptr );
4568
4669 return UR_RESULT_SUCCESS;
4770}
@@ -50,26 +73,29 @@ UR_APIEXPORT ur_result_t UR_APICALL
5073urUSMSharedAlloc (ur_context_handle_t hContext, ur_device_handle_t hDevice,
5174 const ur_usm_desc_t *pUSMDesc, ur_usm_pool_handle_t pool,
5275 size_t size, void **ppMem) {
53- std::ignore = hContext;
5476 std::ignore = hDevice;
55- std::ignore = pUSMDesc;
5677 std::ignore = pool;
5778
79+ auto alignment = pUSMDesc ? pUSMDesc->align : 0u ;
80+ UR_ASSERT ((alignment & (alignment - 1 )) == 0 , UR_RESULT_ERROR_INVALID_VALUE);
5881 UR_ASSERT (ppMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
5982 // TODO: Check Max size when UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE is implemented
6083 UR_ASSERT (size > 0 , UR_RESULT_ERROR_INVALID_USM_SIZE);
6184
62- *ppMem = malloc (size);
85+ void *ptr = native_cpu::malloc_impl (alignment, size);
86+ UR_ASSERT (ptr != nullptr , UR_RESULT_ERROR_OUT_OF_HOST_MEMORY);
87+ *ppMem = ptr;
88+ hContext->add_alloc_info_entry (ptr, UR_USM_TYPE_SHARED, size, nullptr );
6389
6490 return UR_RESULT_SUCCESS;
6591}
6692
6793UR_APIEXPORT ur_result_t UR_APICALL urUSMFree (ur_context_handle_t hContext,
6894 void *pMem) {
69- std::ignore = hContext;
7095
7196 UR_ASSERT (pMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
7297
98+ hContext->remove_alloc_info_entry (pMem);
7399 free (pMem);
74100
75101 return UR_RESULT_SUCCESS;
@@ -79,19 +105,25 @@ UR_APIEXPORT ur_result_t UR_APICALL
79105urUSMGetMemAllocInfo (ur_context_handle_t hContext, const void *pMem,
80106 ur_usm_alloc_info_t propName, size_t propSize,
81107 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;
88108
109+ UR_ASSERT (pMem != nullptr , UR_RESULT_ERROR_INVALID_NULL_POINTER);
89110 UrReturnHelper ReturnValue (propSize, pPropValue, pPropSizeRet);
111+ if (propName == UR_USM_ALLOC_INFO_BASE_PTR) {
112+ // TODO: logic to compute base ptr given ptr
113+ DIE_NO_IMPLEMENTATION;
114+ }
90115
116+ native_cpu::usm_alloc_info alloc_info = hContext->get_alloc_info_entry (pMem);
91117 switch (propName) {
92118 case UR_USM_ALLOC_INFO_TYPE:
93- // Todo implement this in context
94- return ReturnValue (UR_USM_TYPE_DEVICE);
119+ return ReturnValue (alloc_info.type );
120+ case UR_USM_ALLOC_INFO_SIZE:
121+ return ReturnValue (alloc_info.size );
122+ case UR_USM_ALLOC_INFO_DEVICE:
123+ return ReturnValue (alloc_info.device );
124+ case UR_USM_ALLOC_INFO_POOL:
125+ return ReturnValue (alloc_info.pool );
126+ ;
95127 default :
96128 DIE_NO_IMPLEMENTATION;
97129 }
0 commit comments