Skip to content

Commit 18bc9e6

Browse files
authored
Merge pull request #2254 from PietroGhg/pietro/events_rr
[NATIVECPU] Implement events on Native CPU
2 parents 41aa088 + 5872ff0 commit 18bc9e6

File tree

13 files changed

+512
-325
lines changed

13 files changed

+512
-325
lines changed

source/adapters/native_cpu/common.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "logger/ur_logger.hpp"
1414
#include "ur/ur.hpp"
15+
#include <chrono>
1516

1617
constexpr size_t MaxMessageSize = 256;
1718

@@ -70,3 +71,31 @@ template <typename T> inline void decrementOrDelete(T *refC) {
7071
if (refC->decrementReferenceCount() == 0)
7172
delete refC;
7273
}
74+
75+
inline uint64_t get_timestamp() {
76+
return std::chrono::duration_cast<std::chrono::nanoseconds>(
77+
std::chrono::high_resolution_clock::now().time_since_epoch())
78+
.count();
79+
}
80+
81+
namespace native_cpu {
82+
83+
inline void *aligned_malloc(size_t alignment, size_t size) {
84+
void *ptr = nullptr;
85+
#ifdef _MSC_VER
86+
ptr = _aligned_malloc(size, alignment);
87+
#else
88+
ptr = std::aligned_alloc(alignment, size);
89+
#endif
90+
return ptr;
91+
}
92+
93+
inline void aligned_free(void *ptr) {
94+
#ifdef _MSC_VER
95+
_aligned_free(ptr);
96+
#else
97+
free(ptr);
98+
#endif
99+
}
100+
101+
} // namespace native_cpu

source/adapters/native_cpu/context.hpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,10 @@ static size_t get_padding(uint32_t alignment) {
6464
// allocation so that the pointer returned to the user
6565
// always satisfies (ptr % align) == 0.
6666
static inline void *malloc_impl(uint32_t alignment, size_t size) {
67-
void *ptr = nullptr;
6867
assert(alignment >= alignof(usm_alloc_info) &&
6968
"memory not aligned to usm_alloc_info");
70-
#ifdef _MSC_VER
71-
ptr = _aligned_malloc(alloc_header_size + get_padding(alignment) + size,
72-
alignment);
73-
74-
#else
75-
ptr = std::aligned_alloc(alignment,
76-
alloc_header_size + get_padding(alignment) + size);
77-
#endif
69+
void *ptr = native_cpu::aligned_malloc(
70+
alignment, alloc_header_size + get_padding(alignment) + size);
7871
return ptr;
7972
}
8073

@@ -100,11 +93,8 @@ struct ur_context_handle_t_ : RefCounted {
10093
const native_cpu::usm_alloc_info &info = native_cpu::get_alloc_info(ptr);
10194
UR_ASSERT(info.type != UR_USM_TYPE_UNKNOWN,
10295
UR_RESULT_ERROR_INVALID_MEM_OBJECT);
103-
#ifdef _MSC_VER
104-
_aligned_free(info.base_alloc_ptr);
105-
#else
106-
free(info.base_alloc_ptr);
107-
#endif
96+
97+
native_cpu::aligned_free(info.base_alloc_ptr);
10898
allocations.erase(ptr);
10999
return UR_RESULT_SUCCESS;
110100
}

source/adapters/native_cpu/device.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <ur_api.h>
1212

13+
#include "common.hpp"
1314
#include "platform.hpp"
1415

1516
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
@@ -247,7 +248,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
247248
return ReturnValue(uint32_t{4});
248249
case UR_DEVICE_INFO_NATIVE_VECTOR_WIDTH_HALF:
249250
return ReturnValue(uint32_t{16});
250-
// Imported from level_zero
251251
case UR_DEVICE_INFO_USM_HOST_SUPPORT:
252252
case UR_DEVICE_INFO_USM_DEVICE_SUPPORT:
253253
case UR_DEVICE_INFO_USM_SINGLE_SHARED_SUPPORT:
@@ -472,19 +472,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
472472
UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetGlobalTimestamps(
473473
ur_device_handle_t hDevice, uint64_t *pDeviceTimestamp,
474474
uint64_t *pHostTimestamp) {
475-
std::ignore = hDevice; // todo
475+
std::ignore = hDevice;
476476
if (pHostTimestamp) {
477-
using namespace std::chrono;
478-
*pHostTimestamp =
479-
duration_cast<nanoseconds>(steady_clock::now().time_since_epoch())
480-
.count();
477+
*pHostTimestamp = get_timestamp();
481478
}
482479
if (pDeviceTimestamp) {
483-
// todo: calculate elapsed time properly
484-
using namespace std::chrono;
485-
*pDeviceTimestamp =
486-
duration_cast<nanoseconds>(steady_clock::now().time_since_epoch())
487-
.count();
480+
*pDeviceTimestamp = get_timestamp();
488481
}
489482
return UR_RESULT_SUCCESS;
490483
}

0 commit comments

Comments
 (0)