Skip to content

[CUDAX] Add in_place_type argument to pass-through constructor of shared resource #4714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ struct shared_resource
{
static_assert(_CUDA_VMR::resource<_Resource>, "");

//! @brief Constructs a \c shared_resource referring to an object of type \c _Resource
//! that has been constructed with arguments \c __args. The \c _Resource object is
//! dynamically allocated with \c new.
//! @param __args The arguments to be passed to the \c _Resource constructor.
template <class... _Args>
explicit shared_resource(_Args&&... __args)
: __control_block(new _Control_block{_Resource{_CUDA_VSTD::forward<_Args>(__args)...}, 1})
{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have a weak preference for keeping this constructor but making its first argument cuda::std::in_place_type_t<_Resource>.


//! @brief Copy-constructs a \c shared_resource object resulting in an copy that shares
//! ownership of the wrapped resource with \c __other.
//! @param __other The \c shared_resource object to copy from.
Expand Down Expand Up @@ -223,6 +214,9 @@ struct shared_resource
return get_property(__self.__control_block->__resource, _Property{});
}

template <class _Res, class... _Args>
friend auto make_shared_resource(_Args&&... __args) -> shared_resource<_Res>;

private:
// Use a custom shared_ptr implementation because (a) we don't need to support weak_ptr so we only
// need one pointer, not two, and (b) this implementation can work on device also.
Expand All @@ -233,6 +227,14 @@ private:
};

_Control_block* __control_block;

//! @brief Constructs a \c shared_resource referring to an object of type \c _Resource
//! that has been constructed with arguments \c __args. The \c _Resource object is
//! dynamically allocated with \c new.
//! @param __args The arguments to be passed to the \c _Resource constructor.
explicit shared_resource(_Control_block* __control_block)
: __control_block(__control_block)
{}
};

//! @rst
Expand All @@ -252,7 +254,8 @@ template <class _Resource, class... _Args>
auto make_shared_resource(_Args&&... __args) -> shared_resource<_Resource>
{
static_assert(_CUDA_VMR::resource<_Resource>, "_Resource does not satisfy the cuda::mr::resource concept");
return shared_resource<_Resource>{_CUDA_VSTD::forward<_Args>(__args)...};
return shared_resource<_Resource>{
new typename shared_resource<_Resource>::_Control_block{_Resource{_CUDA_VSTD::forward<_Args>(__args)...}, 1}};
}

} // namespace cuda::experimental
Expand Down
23 changes: 14 additions & 9 deletions cudax/test/memory_resource/shared_resource.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEMPLATE_TEST_CASE_METHOD(test_fixture, "shared_resource", "[container][resource
Counts expected{};
CHECK(this->counts == expected);
{
cudax::shared_resource<TestResource> mr{42, this};
auto mr = cudax::make_shared_resource<TestResource>(42, this);
++expected.object_count;
CHECK(this->counts == expected);
}
Expand All @@ -42,7 +42,7 @@ TEMPLATE_TEST_CASE_METHOD(test_fixture, "shared_resource", "[container][resource
Counts expected{};
CHECK(this->counts == expected);
{
cudax::shared_resource<TestResource> mr{42, this};
auto mr = cudax::make_shared_resource<TestResource>(42, this);
++expected.object_count;
CHECK(this->counts == expected);

Expand All @@ -51,15 +51,20 @@ TEMPLATE_TEST_CASE_METHOD(test_fixture, "shared_resource", "[container][resource
CHECK(mr == mr2); // pointers compare equal, no call to TestResource::operator==
CHECK(this->counts == expected);

auto mr3 = std::move(mr);
cudax::shared_resource<TestResource> mr3(mr);
CHECK(this->counts == expected);
CHECK(mr2 == mr3); // pointers compare equal, no call to TestResource::operator==
CHECK(mr == mr3); // pointers compare equal, no call to TestResource::operator==
CHECK(this->counts == expected);

cudax::shared_resource<TestResource> mr4{TestResource{42, this}};
auto mr4 = std::move(mr);
CHECK(this->counts == expected);
CHECK(mr2 == mr4); // pointers compare equal, no call to TestResource::operator==
CHECK(this->counts == expected);

cudax::shared_resource<TestResource> mr5{cudax::make_shared_resource<TestResource>(TestResource{42, this})};
++expected.object_count;
++expected.move_count;
CHECK(mr3 == mr4); // pointers are not equal, calls TestResource::operator==
CHECK(mr3 == mr5); // pointers are not equal, calls TestResource::operator==
++expected.equal_to_count;
CHECK(this->counts == expected);
}
Expand All @@ -76,7 +81,7 @@ TEMPLATE_TEST_CASE_METHOD(test_fixture, "shared_resource", "[container][resource
Counts expected{};
CHECK(this->counts == expected);
{
cudax::shared_resource<TestResource> mr{42, this};
auto mr = cudax::make_shared_resource<TestResource>(42, this);
++expected.object_count;
CHECK(this->counts == expected);

Expand All @@ -101,7 +106,7 @@ TEMPLATE_TEST_CASE_METHOD(test_fixture, "shared_resource", "[container][resource
{
Counts expected{};
{
cudax::shared_resource<TestResource> mr{42, this};
auto mr = cudax::make_shared_resource<TestResource>(42, this);
++expected.object_count;
CHECK(this->counts == expected);

Expand Down Expand Up @@ -130,7 +135,7 @@ TEMPLATE_TEST_CASE_METHOD(test_fixture, "shared_resource", "[container][resource
{
bytes(42 * sizeof(int));
cudax::uninitialized_buffer<int, cudax::host_accessible> buffer{
cudax::shared_resource<TestResource>(42, this), 42};
cudax::make_shared_resource<TestResource>(42, this), 42};
++expected.object_count;
++expected.allocate_count;
CHECK(this->counts == expected);
Expand Down
Loading