Skip to content

Commit

Permalink
Merge pull request #2254 from PietroGhg/pietro/events_rr
Browse files Browse the repository at this point in the history
[NATIVECPU] Implement events on Native CPU
  • Loading branch information
callumfare authored Nov 13, 2024
2 parents 9a209aa + b1222f0 commit cd92e72
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 325 deletions.
29 changes: 29 additions & 0 deletions source/adapters/native_cpu/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "logger/ur_logger.hpp"
#include "ur/ur.hpp"
#include <chrono>

constexpr size_t MaxMessageSize = 256;

Expand Down Expand Up @@ -70,3 +71,31 @@ template <typename T> inline void decrementOrDelete(T *refC) {
if (refC->decrementReferenceCount() == 0)
delete refC;
}

inline uint64_t get_timestamp() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count();
}

namespace native_cpu {

inline void *aligned_malloc(size_t alignment, size_t size) {
void *ptr = nullptr;
#ifdef _MSC_VER
ptr = _aligned_malloc(size, alignment);
#else
ptr = std::aligned_alloc(alignment, size);
#endif
return ptr;
}

inline void aligned_free(void *ptr) {
#ifdef _MSC_VER
_aligned_free(ptr);
#else
free(ptr);
#endif
}

} // namespace native_cpu
18 changes: 4 additions & 14 deletions source/adapters/native_cpu/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,10 @@ static size_t get_padding(uint32_t alignment) {
// allocation so that the pointer returned to the user
// always satisfies (ptr % align) == 0.
static inline void *malloc_impl(uint32_t alignment, size_t size) {
void *ptr = nullptr;
assert(alignment >= alignof(usm_alloc_info) &&
"memory not aligned to usm_alloc_info");
#ifdef _MSC_VER
ptr = _aligned_malloc(alloc_header_size + get_padding(alignment) + size,
alignment);

#else
ptr = std::aligned_alloc(alignment,
alloc_header_size + get_padding(alignment) + size);
#endif
void *ptr = native_cpu::aligned_malloc(
alignment, alloc_header_size + get_padding(alignment) + size);
return ptr;
}

Expand All @@ -100,11 +93,8 @@ struct ur_context_handle_t_ : RefCounted {
const native_cpu::usm_alloc_info &info = native_cpu::get_alloc_info(ptr);
UR_ASSERT(info.type != UR_USM_TYPE_UNKNOWN,
UR_RESULT_ERROR_INVALID_MEM_OBJECT);
#ifdef _MSC_VER
_aligned_free(info.base_alloc_ptr);
#else
free(info.base_alloc_ptr);
#endif

native_cpu::aligned_free(info.base_alloc_ptr);
allocations.erase(ptr);
return UR_RESULT_SUCCESS;
}
Expand Down
15 changes: 4 additions & 11 deletions source/adapters/native_cpu/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <ur_api.h>

#include "common.hpp"
#include "platform.hpp"

#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
Expand Down Expand Up @@ -247,7 +248,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
return ReturnValue(uint32_t{4});
case UR_DEVICE_INFO_NATIVE_VECTOR_WIDTH_HALF:
return ReturnValue(uint32_t{16});
// Imported from level_zero
case UR_DEVICE_INFO_USM_HOST_SUPPORT:
case UR_DEVICE_INFO_USM_DEVICE_SUPPORT:
case UR_DEVICE_INFO_USM_SINGLE_SHARED_SUPPORT:
Expand Down Expand Up @@ -472,19 +472,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetGlobalTimestamps(
ur_device_handle_t hDevice, uint64_t *pDeviceTimestamp,
uint64_t *pHostTimestamp) {
std::ignore = hDevice; // todo
std::ignore = hDevice;
if (pHostTimestamp) {
using namespace std::chrono;
*pHostTimestamp =
duration_cast<nanoseconds>(steady_clock::now().time_since_epoch())
.count();
*pHostTimestamp = get_timestamp();
}
if (pDeviceTimestamp) {
// todo: calculate elapsed time properly
using namespace std::chrono;
*pDeviceTimestamp =
duration_cast<nanoseconds>(steady_clock::now().time_since_epoch())
.count();
*pDeviceTimestamp = get_timestamp();
}
return UR_RESULT_SUCCESS;
}
Expand Down
Loading

0 comments on commit cd92e72

Please sign in to comment.