Skip to content

Commit

Permalink
Track USM allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
fknorr committed Dec 18, 2023
1 parent 2bea651 commit fc4d42a
Show file tree
Hide file tree
Showing 18 changed files with 369 additions and 193 deletions.
8 changes: 8 additions & 0 deletions include/simsycl/detail/allocation.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#pragma once

#include "../sycl/enums.hh"
#include "../sycl/forward.hh"

#include <cstdlib>
#include <cstring>
#include <optional>
#include <utility>


Expand All @@ -25,6 +29,10 @@ inline void aligned_free(void *ptr) {
#endif
}

void *usm_alloc(const sycl::context &context, sycl::usm::alloc kind, std::optional<sycl::device> opt_device,
size_t size_bytes, size_t alignment_bytes);
void usm_free(void *ptr, const sycl::context &context);


// floats and doubles filled with this pattern show up as "-nan"
inline constexpr std::byte uninitialized_memory_pattern = std::byte(0xff);
Expand Down
5 changes: 2 additions & 3 deletions include/simsycl/detail/coordinate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

#include "check.hh"

#include <concepts>
#include <cstdlib>
#include <functional>
#include <type_traits>


namespace simsycl::detail {
Expand Down Expand Up @@ -159,7 +158,7 @@ class coordinate {
#undef SIMSYCL_DETAIL_DEFINE_COORDINATE_UNARY_POSTFIX_OPERATOR

private:
size_t m_values[Dimensions];
size_t m_values[Dimensions]{};

// interface type construction helper to use in friend operators
// (because friendship is not transitive)
Expand Down
47 changes: 28 additions & 19 deletions include/simsycl/detail/reference_type.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "check.hh"

#include <memory>
#include <optional>

namespace simsycl::detail {

Expand All @@ -13,23 +14,15 @@ class reference_type;

template<typename Derived, typename State>
struct std::hash<simsycl::detail::reference_type<Derived, State>> {
size_t operator()(const Derived &rt) { return static_cast<size_t>(reinterpret_cast<uintptr_t>(rt.m_state.get())); }
size_t operator()(const Derived &rt) const {
return static_cast<size_t>(reinterpret_cast<uintptr_t>(rt.m_state.get()));
}
};

namespace simsycl::detail {

template<typename Derived, typename State>
class weak_ref {
public:
weak_ref() = default;

weak_ref(std::weak_ptr<State> &&state) : m_state(std::move(state)) {}

Derived lock() const { return Derived(m_state.lock()); }

private:
std::weak_ptr<State> m_state;
};
template<typename Derived>
class weak_ref;

template<typename Derived, typename State>
class reference_type {
Expand Down Expand Up @@ -62,18 +55,34 @@ class reference_type {
return *m_state;
}

detail::weak_ref<Derived, State> weak_ref() {
SIMSYCL_CHECK(m_state != nullptr);
return detail::weak_ref<Derived, State>(std::weak_ptr<state_type>(m_state));
}

private:
friend struct std::hash<reference_type<Derived, State>>;

template<typename, typename>
template<typename>
friend class weak_ref;

std::shared_ptr<state_type> m_state;
};

template<typename Derived>
class weak_ref {
private:
using state_type = typename Derived::state_type;

public:
weak_ref() = default;

explicit weak_ref(const Derived &ref) : m_state(ref.m_state) {
static_assert(std::is_base_of_v<reference_type<Derived, typename Derived::state_type>, Derived>);
}

std::optional<Derived> lock() const {
if(auto state = m_state.lock(); state != nullptr) { return Derived(std::move(state)); }
return std::nullopt;
}

private:
std::weak_ptr<state_type> m_state;
};

} // namespace simsycl::detail
2 changes: 0 additions & 2 deletions include/simsycl/sycl/allocator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <cstdlib>
#include <memory>

namespace simsycl::detail {} // namespace simsycl::detail

namespace simsycl::sycl {

template<class T>
Expand Down
4 changes: 2 additions & 2 deletions include/simsycl/sycl/async_handler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class exception_list : private std::vector<std::exception_ptr> {

using async_handler = std::function<void(sycl::exception_list)>;

}
} // namespace simsycl::sycl

namespace simsycl::detail {

[[noreturn]] void default_async_handler(sycl::exception_list exceptions);

void call_async_handler(const sycl::async_handler &handler_opt, sycl::exception_list exceptions);

}
} // namespace simsycl::detail
4 changes: 2 additions & 2 deletions include/simsycl/sycl/binary_ops.hh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct minimum {
template<>
struct minimum<void> {
template<typename T, typename U>
decltype(auto) operator()(T &&x, U &&y) const {
decltype(auto) operator()(T && x, U && y) const {
return x < y ? std::forward<T>(x) : std::forward<U>(y);
}
};
Expand All @@ -97,7 +97,7 @@ struct maximum {
template<>
struct maximum<void> {
template<typename T, typename U>
decltype(auto) operator()(T &&x, U &&y) const {
decltype(auto) operator()(T && x, U && y) const {
return x > y ? std::forward<T>(x) : std::forward<U>(y);
}
};
Expand Down
5 changes: 5 additions & 0 deletions include/simsycl/sycl/buffer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,15 @@ class buffer final
reinterpret() const;

private:
template<typename>
friend class detail::weak_ref;

template<typename U, int D, typename A>
friend U *simsycl::detail::get_buffer_data(sycl::buffer<U, D, A> &buf);

using reference_type::state;

buffer(std::shared_ptr<state_type> &&state) : reference_type(std::move(state)) {}
};

// Deduction guides
Expand Down
11 changes: 10 additions & 1 deletion include/simsycl/sycl/context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,19 @@ class context final : public detail::reference_type<context, detail::context_sta
typename Param::return_type get_backend_info() const;

private:
template<typename>
friend class detail::weak_ref;

struct internal_t {
} inline static constexpr internal{};

explicit context(internal_t, const std::vector<device> &devices, const async_handler &async_handler, const property_list &prop_list);
explicit context(internal_t, const std::vector<device> &devices, const async_handler &async_handler,
const property_list &prop_list);
context(std::shared_ptr<detail::context_state> &&state) : reference_type(std::move(state)) {}
};

} // namespace simsycl::sycl

template<>
struct std::hash<simsycl::sycl::context>
: public std::hash<simsycl::detail::reference_type<simsycl::sycl::context, simsycl::detail::context_state>> {};
11 changes: 9 additions & 2 deletions include/simsycl/sycl/device.hh
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ class device final : public detail::reference_type<device, detail::device_state>
static std::vector<device> get_devices(info::device_type device_type = info::device_type::all);

private:
template<typename>
friend class detail::weak_ref;

friend device simsycl::create_device(sycl::platform &platform, const device_config &config);

device(detail::device_state state);
device(const detail::device_selector &selector);
device(std::shared_ptr<detail::device_state> &&state) : reference_type(std::move(state)) {}
};

template<aspect Aspect>
Expand All @@ -123,4 +126,8 @@ inline constexpr bool any_device_has_v = any_device_has<A>::value;
template<aspect A>
inline constexpr bool all_devices_have_v = all_devices_have<A>::value;

} // namespace simsycl::sycl
} // namespace simsycl::sycl

template<>
struct std::hash<simsycl::sycl::device>
: public std::hash<simsycl::detail::reference_type<simsycl::sycl::device, simsycl::detail::device_state>> {};
31 changes: 20 additions & 11 deletions include/simsycl/sycl/event.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@

namespace simsycl::detail {

struct execution_status {
struct event_state {
std::chrono::steady_clock::time_point t_submit{};
std::chrono::steady_clock::time_point t_start{};
std::chrono::steady_clock::time_point t_end{};

void start() { t_start = std::chrono::steady_clock::now(); }

[[nodiscard]] static execution_status submit() {
execution_status status;
[[nodiscard]] static event_state submit() {
event_state status;
status.t_submit = std::chrono::steady_clock::now();
return status;
}

[[nodiscard]] static execution_status submit_and_start() {
[[nodiscard]] static event_state submit_and_start() {
auto status = submit();
status.start();
return status;
Expand All @@ -45,7 +45,7 @@ uint64_t nanoseconds_since_epoch(std::chrono::time_point<Clock, Dur> time_point)

namespace simsycl::sycl {

class event : detail::reference_type<event, detail::execution_status> {
class event : detail::reference_type<event, detail::event_state> {
public:
event() = default;

Expand Down Expand Up @@ -89,23 +89,32 @@ class event : detail::reference_type<event, detail::execution_status> {
}

private:
friend event detail::make_event(const detail::execution_status &status);
template<typename>
friend class detail::weak_ref;

explicit event(const detail::execution_status &status)
: detail::reference_type<event, detail::execution_status>(std::in_place, status) {}
friend event detail::make_event(std::shared_ptr<detail::event_state> &&state);

explicit event(std::shared_ptr<detail::event_state> &&state)
: detail::reference_type<event, detail::event_state>(std::move(state)) {}
};

} // namespace simsycl::sycl


template<>
struct std::hash<simsycl::sycl::event>
: public std::hash<simsycl::detail::reference_type<simsycl::sycl::event, simsycl::detail::event_state>> {};

namespace simsycl::detail {

inline sycl::event make_event(const execution_status &status) { return sycl::event(status); }
inline sycl::event make_event(std::shared_ptr<event_state> &&state) { return sycl::event(std::move(state)); }
inline sycl::event make_event(const event_state &state) { return make_event(std::make_shared<event_state>(state)); }

inline sycl::event execution_status::end() {
inline sycl::event event_state::end() {
t_end = std::chrono::steady_clock::now();
return make_event(*this);
}

inline sycl::event execution_status::instant() { return submit_and_start().end(); }
inline sycl::event event_state::instant() { return submit_and_start().end(); }

} // namespace simsycl::detail
4 changes: 2 additions & 2 deletions include/simsycl/sycl/forward.hh
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ sycl::handler make_handler();

void **require_local_memory(sycl::handler &cgh, size_t size, size_t align);

struct execution_status;
struct event_state;

sycl::event make_event(const execution_status &status);
sycl::event make_event(std::shared_ptr<event_state> &&state);

} // namespace simsycl::detail
9 changes: 6 additions & 3 deletions include/simsycl/sycl/platform.hh
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ class platform final : public detail::reference_type<platform, detail::platform_
static std::vector<platform> get_platforms();

private:
template<typename, typename>
template<typename>
friend class detail::weak_ref;

friend sycl::platform simsycl::create_platform(const platform_config &config);
friend device simsycl::create_device(platform &platform, const device_config &config);

platform(detail::platform_state state);
platform(const detail::device_selector &selector);
platform(std::shared_ptr<detail::platform_state> &&state);
platform(std::shared_ptr<detail::platform_state> &&state) : reference_type(std::move(state)) {}

void add_device(const device &dev);
};

} // namespace simsycl::sycl

template<>
struct std::hash<simsycl::sycl::platform>
: public std::hash<simsycl::detail::reference_type<simsycl::sycl::platform, simsycl::detail::platform_state>> {};
Loading

0 comments on commit fc4d42a

Please sign in to comment.