Skip to content
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

[REVIEW] impl and refactor device_scalar::set_value and value, respectively. #167

Merged
merged 19 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 16 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## New Features

- PR #106 Added multi-GPU initialization
- PR #167 Added value setter to `device_scalar`

## Improvements

Expand Down
58 changes: 33 additions & 25 deletions include/rmm/device_scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,49 @@ class device_scalar {
* @brief Construct a new `device_scalar`
*
* @param initial_value The initial value of the object in device memory
* @param stream_ Optional, stream on which to perform allocation and copy
* @param mr_ Optional, resource with which to allocate
* @param stream Optional, stream on which to perform allocation and copy
* @param mr Optional, resource with which to allocate
*---------------------------------------------------------------------------**/
explicit device_scalar(
T const &initial_value, cudaStream_t stream_ = 0,
rmm::mr::device_memory_resource *mr_ = rmm::mr::get_default_resource())
: buff{sizeof(T), stream_, mr_} {
auto status = cudaMemcpyAsync(buff.data(), &initial_value, sizeof(T),
cudaMemcpyDefault, buff.stream());

if (cudaSuccess != status) {
throw std::runtime_error{"Device memcpy failed."};
}
T const &initial_value, cudaStream_t stream = 0,
rmm::mr::device_memory_resource *mr = rmm::mr::get_default_resource())
: buff{sizeof(T), stream, mr} {

_memcpy(buff.data(), &initial_value, stream);
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from device to host and returns the value.
* @brief Copies the value from device to host synchronously and returns the
* value.
*
* @return T The value of the scalar after synchronizing its stream
* @return T The value of the scalar
* @param stream CUDA stream on which to perform the copy
*---------------------------------------------------------------------------**/
T value() const {
T value(cudaStream_t stream = 0) const {
T host_value{};
auto status = cudaMemcpyAsync(&host_value, buff.data(), sizeof(T),
cudaMemcpyDefault, buff.stream());
if (cudaSuccess != status) {
throw std::runtime_error{"Device memcpy failed."};
}
status = cudaStreamSynchronize(buff.stream());
if (cudaSuccess != status) {
throw std::runtime_error{"Stream sync failed."};
}
_memcpy(&host_value, buff.data(), stream);
return host_value;
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from host to device synchronously.
*
* @param host_value The host value which will be copied to device
* @param stream CUDA stream on which to perform the copy
*---------------------------------------------------------------------------**/
void set_value(T host_value, cudaStream_t stream = 0) {
_memcpy(buff.data(), &host_value, stream);
}

/**---------------------------------------------------------------------------*
* @brief Returns pointer to object in device memory.
*---------------------------------------------------------------------------**/
T *get() noexcept { return static_cast<T *>(buff.data()); }
T *data() noexcept { return static_cast<T *>(buff.data()); }

/**---------------------------------------------------------------------------*
* @brief Returns pointer to object in device memory.
*---------------------------------------------------------------------------**/
T const *get() const noexcept { return static_cast<T const *>(buff.data()); }
T const *data() const noexcept { return static_cast<T const *>(buff.data()); }

device_scalar() = default;
~device_scalar() = default;
Expand All @@ -92,6 +92,14 @@ class device_scalar {

private:
rmm::device_buffer buff{sizeof(T)};

inline void _memcpy(void *dst, const void *src, cudaStream_t stream) const {
auto status = cudaMemcpyAsync(dst, src, sizeof(T), cudaMemcpyDefault,
Copy link
Member

Choose a reason for hiding this comment

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

Please either use cudaMemcpy or add a cudaStreamSynchronize() after this. Otherwise it appears to be asynchronous and is therefore ambiguous with the documentation.

I know I said that cudaMemcpyAsync is not async when either src or dst is un-pinned host memory, but even we should make the synchronization explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We cannot specify the stream with cudaMemcpy, but Jake mentioned specifying the stream may still be beneficial. I'm not sure how to proceed.

Copy link
Member

@harrism harrism Oct 30, 2019

Choose a reason for hiding this comment

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

Right, it's still better to use cudaMemcpyAsync because it let's us sync a stream that's NOT the null stream (synchronizing the NULL stream is the same as cudaDeviceSynchronize(), which is overkill). To proceed, just add in cudaStreamSynchronize(stream) after the cudaMemcpyAsync like you had before.

stream);
if (cudaSuccess != status) {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
throw std::runtime_error{"Device memcpy failed."};
}
}
};

} // namespace rmm
30 changes: 20 additions & 10 deletions tests/device_scalar_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,47 @@ TYPED_TEST_CASE(DeviceScalarTest, Types);

TYPED_TEST(DeviceScalarTest, DefaultUninitialized) {
rmm::device_scalar<TypeParam> scalar{};
EXPECT_NE(nullptr, scalar.get());
EXPECT_NE(nullptr, scalar.data());
}

TYPED_TEST(DeviceScalarTest, InitialValue) {
rmm::device_scalar<TypeParam> scalar{this->value, this->stream, this->mr};
EXPECT_NE(nullptr, scalar.get());
EXPECT_NE(nullptr, scalar.data());
EXPECT_EQ(this->value, scalar.value());
}

TYPED_TEST(DeviceScalarTest, CopyCtor) {
rmm::device_scalar<TypeParam> scalar{this->value, this->stream, this->mr};
EXPECT_NE(nullptr, scalar.get());
EXPECT_NE(nullptr, scalar.data());
EXPECT_EQ(this->value, scalar.value());

rmm::device_scalar<TypeParam> copy{scalar};
EXPECT_NE(nullptr, copy.get());
EXPECT_NE(copy.get(), scalar.get());
EXPECT_NE(nullptr, copy.data());
EXPECT_NE(copy.data(), scalar.data());
EXPECT_EQ(copy.value(), scalar.value());
}

TYPED_TEST(DeviceScalarTest, MoveCtor) {
rmm::device_scalar<TypeParam> scalar{this->value, this->stream, this->mr};
EXPECT_NE(nullptr, scalar.get());
EXPECT_NE(nullptr, scalar.data());
EXPECT_EQ(this->value, scalar.value());

auto original_pointer = scalar.get();
auto original_pointer = scalar.data();
auto original_value = scalar.value();

rmm::device_scalar<TypeParam> moved_to{std::move(scalar)};
EXPECT_NE(nullptr, moved_to.get());
EXPECT_EQ(moved_to.get(), original_pointer);
EXPECT_NE(nullptr, moved_to.data());
EXPECT_EQ(moved_to.data(), original_pointer);
EXPECT_EQ(moved_to.value(), original_value);
EXPECT_EQ(nullptr, scalar.get());
EXPECT_EQ(nullptr, scalar.data());
}

TYPED_TEST(DeviceScalarTest, SetValue) {
rmm::device_scalar<TypeParam> scalar{this->value, this->stream, this->mr};
EXPECT_NE(nullptr, scalar.data());

auto expected = this->distribution(this->generator);

scalar.set_value(expected);
EXPECT_EQ(expected, scalar.value());
}