Skip to content

Commit

Permalink
Fixes #629: texture_view_t now has a device_id_ field + now obtai…
Browse files Browse the repository at this point in the history
…ning contexts for `array_t`'s and `texture_view_t`'s without having to make an API call for the device ID
  • Loading branch information
eyalroz committed Apr 19, 2024
1 parent 5017a9d commit 305ca90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/cuda/api/multi_wrapper_impls/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ array_t<T,NumDimensions> create(

} // namespace array

inline device_t texture_view::device() const
inline context_t texture_view::context() const
{
return device::get(context::detail_::get_device_id(context_handle_));
return context::wrap(device_id_, context_handle_);
}

inline context_t texture_view::context() const
inline device_t texture_view::device() const
{
return context::detail_::from_handle(context_handle_);
return device::get(device_id_);
}

template <typename T, dimensionality_t NumDimensions>
Expand All @@ -66,7 +66,7 @@ template <typename T, dimensionality_t NumDimensions>
context_t array_t<T, NumDimensions>::context() const
{
// TODO: Save the device id in the array_t as well.
return context::detail_::from_handle(context_handle_);
return context::wrap(device_id_, context_handle_);
}

} // namespace cuda
Expand Down
45 changes: 33 additions & 12 deletions src/cuda/api/texture_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct descriptor_t : public CUDA_TEXTURE_DESC {
* @return a proxy object associated with the specified texture view
*/
inline texture_view wrap(
device::id_t device_id,
context::handle_t context_handle,
texture::raw_handle_t handle,
bool take_ownership) noexcept;
Expand Down Expand Up @@ -89,25 +90,35 @@ class texture_view {
using scoped_context_setter = cuda::context::current::detail_::scoped_override_t;

public:
bool is_owning() const noexcept { return owning; }
/// Getters for this object's raw fields
///@{
device::id_t device_id() const noexcept { return device_id_; }
context::handle_t context_handle() const noexcept { return context_handle_; }
raw_handle_type raw_handle() const noexcept { return raw_view_handle; }
bool is_owning() const noexcept { return owning_; }
///@}

public: // constructors and destructors

texture_view(const texture_view& other) = delete;

texture_view(texture_view&& other) noexcept :
raw_view_handle(other.raw_view_handle), owning(other.raw_view_handle)
device_id_(other.device_id_),
context_handle_(other.context_handle_),
raw_view_handle(other.raw_view_handle),
owning_(other.raw_view_handle)
{
other.owning = false;
other.owning_ = false;
};


template <typename T, dimensionality_t NumDimensions>
texture_view(
const cuda::array_t<T, NumDimensions>& arr,
texture::descriptor_t descriptor = texture::descriptor_t()) :
context_handle_(arr.context_handle()), owning(true)
device_id_(arr.device_id()),
context_handle_(arr.context_handle()),
owning_(true)
{
scoped_context_setter set_context(context_handle_);
CUDA_RESOURCE_DESC resource_descriptor;
Expand All @@ -123,7 +134,7 @@ class texture_view {

~texture_view() noexcept(false)
{
if (owning) {
if (owning_) {
scoped_context_setter set_context(context_handle_);
auto status = cuTexObjectDestroy(raw_view_handle);
throw_if_error_lazy(status, "failed destroying texture object");
Expand All @@ -136,8 +147,16 @@ class texture_view {
protected: // constructor

// Usable by the wrap function
texture_view(context::handle_t context_handle, raw_handle_type handle , bool take_ownership) noexcept
: context_handle_(context_handle), raw_view_handle(handle), owning(take_ownership) { }
texture_view(
device::id_t device_id,
context::handle_t context_handle,
raw_handle_type handle ,
bool take_ownership) noexcept
:
device_id_(device_id),
context_handle_(context_handle),
raw_view_handle(handle),
owning_(take_ownership) { }

public: // non-mutating getters

Expand All @@ -146,12 +165,13 @@ class texture_view {

public: // friendship

friend texture_view texture::wrap(context::handle_t, raw_handle_type, bool) noexcept;
friend texture_view texture::wrap(device::id_t, context::handle_t, raw_handle_type, bool) noexcept;

protected:
context::handle_t context_handle_ { } ;
raw_handle_type raw_view_handle { } ;
bool owning;
device::id_t device_id_;
context::handle_t context_handle_;
raw_handle_type raw_view_handle;
bool owning_;
};


Expand All @@ -168,11 +188,12 @@ inline bool operator!=(const texture_view& lhs, const texture_view& rhs) noexcep
namespace texture {

inline texture_view wrap(
device::id_t device_id,
context::handle_t context_handle,
texture::raw_handle_t handle,
bool take_ownership) noexcept
{
return { context_handle, handle, take_ownership };
return { device_id, context_handle, handle, take_ownership };
}

} // namespace texture
Expand Down

0 comments on commit 305ca90

Please sign in to comment.