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 all 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 @@ -26,6 +26,7 @@
#include <cuda/std/__type_traits/is_swappable.h>
#include <cuda/std/__utility/exchange.h>
#include <cuda/std/__utility/forward.h>
#include <cuda/std/__utility/in_place.h>
#include <cuda/std/__utility/move.h>
#include <cuda/std/atomic>

Expand Down Expand Up @@ -56,7 +57,7 @@ struct shared_resource
//! 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)
explicit shared_resource(_CUDA_VSTD::in_place_type_t<_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>.


Expand Down Expand Up @@ -252,7 +253,7 @@ 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>{_CUDA_VSTD::in_place_type<_Resource>, _CUDA_VSTD::forward<_Args>(__args)...};
}

} // 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};
cudax::shared_resource mr{cuda::std::in_place_type<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};
cudax::shared_resource mr{cuda::std::in_place_type<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 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 mr5{cuda::std::in_place_type<TestResource>, TestResource{42, this}};
++expected.object_count;
++expected.move_count;
CHECK(mr3 == mr4); // pointers are not equal, calls TestResource::operator==
CHECK(mr4 == 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};
cudax::shared_resource mr{cuda::std::in_place_type<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};
cudax::shared_resource mr{cuda::std::in_place_type<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::shared_resource<TestResource>(cuda::std::in_place_type<TestResource>, 42, this), 42};
++expected.object_count;
++expected.allocate_count;
CHECK(this->counts == expected);
Expand Down
Loading