Skip to content

Commit

Permalink
Fixes #566: Can now get and set kernel properties using an independen…
Browse files Browse the repository at this point in the history
…t function

* both for `kernel_t` and `void *` pointers,
* underlying functionality also for `kernel::handle_t`
* Typo fix
  • Loading branch information
eyalroz committed Jan 14, 2024
1 parent d752042 commit 41a206e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
36 changes: 34 additions & 2 deletions src/cuda/api/apriori_compiled_kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ inline handle_t get_handle(const void *, const char* = nullptr)
#else
inline handle_t get_handle(const void *kernel_function_ptr, const char* name = nullptr)
{

handle_t handle;
auto status = cudaGetFuncBySymbol(&handle, kernel_function_ptr);
throw_if_error_lazy(status, "Failed obtaining a CUDA function handle for "
Expand Down Expand Up @@ -390,7 +389,7 @@ class apriori_compiled_kernel_t final : public kernel_t {
dynamic_shared_memory_per_block,
disable_caching_override);
}
#endif // CAN_GET_APRIORI_KERNEL_HANDLE
#endif // ! CAN_GET_APRIORI_KERNEL_HANDLE

protected: // ctors & dtor
apriori_compiled_kernel_t(device::id_t device_id, context::handle_t primary_context_handle,
Expand Down Expand Up @@ -448,6 +447,39 @@ inline ::std::string identify(const apriori_compiled_kernel_t& kernel)

} // namespace detail

#if CAN_GET_APRIORI_KERNEL_HANDLE
inline attribute_value_t get_attribute(const void* function_ptr, attribute_t attribute)
{
auto handle = detail_::get_handle(function_ptr);
return kernel::detail_::get_attribute_in_current_context(handle, attribute);
}

inline void set_attribute(const void* function_ptr, attribute_t attribute, attribute_value_t value)
{
auto handle = detail_::get_handle(function_ptr);
return detail_::set_attribute_in_current_context(handle, attribute, value);
}

inline attribute_value_t get_attribute(
const context_t& context,
const void* function_ptr,
attribute_t attribute)
{
CAW_SET_SCOPE_CONTEXT(context.handle());
return get_attribute(function_ptr, attribute);
}

inline void set_attribute(
const context_t& context,
const void* function_ptr,
attribute_t attribute,
attribute_value_t value)
{
CAW_SET_SCOPE_CONTEXT(context.handle());
return set_attribute(function_ptr, attribute, value);
}
#endif // CAN_GET_APRIORI_KERNEL_HANDLE

/**
* @note The returned kernel proxy object will keep the device's primary
* context active while the kernel exists.
Expand Down
36 changes: 34 additions & 2 deletions src/cuda/api/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,23 @@ inline attribute_value_t get_attribute_in_current_context(handle_t handle, attri
return attribute_value;
}

inline void set_attribute_in_current_context(handle_t handle, attribute_t attribute, attribute_value_t value)
{
#if CUDA_VERSION >= 9000
auto result = cuFuncSetAttribute(handle, static_cast<CUfunction_attribute>(attribute), value);
throw_if_error_lazy(result,
"Setting CUDA device function attribute " +
::std::string(kernel::detail_::attribute_name(attribute)) + " of function at "
+ cuda::kernel::detail_::identify(handle) + " to value " + ::std::to_string(value));
#else
throw(cuda::runtime_error {cuda::status::not_yet_implemented});
#endif
}

} // namespace detail_

inline attribute_value_t get_attribute(const kernel_t& kernel, attribute_t attribute);

} // namespace kernel

/**
Expand Down Expand Up @@ -149,8 +164,7 @@ class kernel_t {
VIRTUAL_UNLESS_CAN_GET_APRIORI_KERNEL_HANDLE
kernel::attribute_value_t get_attribute(kernel::attribute_t attribute) const
{
context::current::detail_::scoped_override_t set_context_for_this_context(context_handle_);
return kernel::detail_::get_attribute_in_current_context(handle(), attribute);
return kernel::get_attribute(*this, attribute);
}

VIRTUAL_UNLESS_CAN_GET_APRIORI_KERNEL_HANDLE
Expand Down Expand Up @@ -381,6 +395,24 @@ inline kernel_t wrap(
return kernel_t{ device_id, context_id, f, hold_primary_context_refcount_unit };
}

inline attribute_value_t get_attribute(const kernel_t& kernel, attribute_t attribute)
{
CAW_SET_SCOPE_CONTEXT(kernel.context_handle());
return detail_::get_attribute_in_current_context(kernel.handle(), attribute);
}

inline void set_attribute(const kernel_t& kernel, attribute_t attribute, attribute_value_t value)
{
CAW_SET_SCOPE_CONTEXT(kernel.context_handle());
return detail_::set_attribute_in_current_context(kernel.handle(), attribute, value);
}

inline attribute_value_t set_attribute(const kernel_t& kernel, attribute_t attribute)
{
CAW_SET_SCOPE_CONTEXT(kernel.context_handle());
return kernel::detail_::get_attribute_in_current_context(kernel.handle(), attribute);
}

namespace occupancy {

namespace detail_ {
Expand Down
11 changes: 1 addition & 10 deletions src/cuda/api/multi_wrapper_impls/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,7 @@ return device::get(device_id_);

inline void kernel_t::set_attribute(kernel::attribute_t attribute, kernel::attribute_value_t value) const
{
#if CUDA_VERSION >= 9000
context::current::detail_::scoped_override_t set_context_for_this_context(context_handle_);
auto result = cuFuncSetAttribute(handle_, static_cast<CUfunction_attribute>(attribute), value);
throw_if_error_lazy(result,
"Setting CUDA device function attribute " +
::std::string(kernel::detail_::attribute_name(attribute)) +
" to value " + ::std::to_string(value));
#else
throw(cuda::runtime_error {cuda::status::not_yet_implemented});
#endif
kernel::detail_::set_attribute_in_current_context(handle_, attribute, value);
}

namespace detail_ {
Expand Down

0 comments on commit 41a206e

Please sign in to comment.