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 8 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## New Features

- PR #167 Added value setter to `device_scalar`

## Improvements

- PR #161 Use `std::atexit` to finalize RMM after Python interpreter shutdown
Expand Down
123 changes: 104 additions & 19 deletions include/rmm/device_scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ class device_scalar {
T const &initial_value, cudaStream_t stream_ = 0,
harrism marked this conversation as resolved.
Show resolved Hide resolved
rmm::mr::device_memory_resource *mr_ = rmm::mr::get_default_resource())
: buff{sizeof(T), stream_, mr_} {
harrism marked this conversation as resolved.
Show resolved Hide resolved
auto status = cudaMemcpyAsync(buff.data(), &initial_value, sizeof(T),
cudaMemcpyDefault, buff.stream());

if (cudaSuccess != status) {
throw std::runtime_error{"Device memcpy failed."};
}

_set_value<false>(initial_value, buff.stream());
cwharris marked this conversation as resolved.
Show resolved Hide resolved
}

/**---------------------------------------------------------------------------*
Expand All @@ -60,28 +56,86 @@ class device_scalar {
* @return T The value of the scalar after synchronizing its stream
*---------------------------------------------------------------------------**/
T value() const {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
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."};
}
return host_value;
return _value<true>(buff.stream());
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from device to host and returns the value.
*
* @return T The value of the scalar after synchronizing its stream
cwharris marked this conversation as resolved.
Show resolved Hide resolved
* @param stream CUDA stream on which to perform the copy
*---------------------------------------------------------------------------**/
T value(cudaStream_t stream) const {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
return _value<true>(stream);
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from device to host and returns the value.
*
* @return T The value of the scalar
*---------------------------------------------------------------------------**/
T value_async() const {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
return _value<false>(buff.stream());
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from device to host and returns the value.
*
* @return T The value of the scalar
* @param stream CUDA stream on which to perform the copy
*---------------------------------------------------------------------------**/
T value_async(cudaStream_t stream) const {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
return _value<false>(stream);
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from host to device and synchronizes.
*
* @param host_value The host value which will be copied to device
*---------------------------------------------------------------------------**/
void set_value(T host_value) {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
_set_value<true>(host_value, buff.stream());
}


/**---------------------------------------------------------------------------*
* @brief Copies the value from host to device and synchronizes.
cwharris marked this conversation as resolved.
Show resolved Hide resolved
*
* @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) {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
_set_value<true>(host_value, stream);
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from host to device.
*
* @param host_value The host value which will be copied to device
*---------------------------------------------------------------------------**/
void set_value_async(T host_value) {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
_set_value<false>(host_value, buff.stream());
}

/**---------------------------------------------------------------------------*
* @brief Copies the value from host to device.
*
* @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_async(T host_value, cudaStream_t stream) {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
_set_value<false>(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 +146,37 @@ class device_scalar {

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

template<bool synchronize>
inline T _value(cudaStream_t stream) const {
T host_value{};
_memcpy<synchronize>(&host_value, buff.data(), sizeof(T), stream);
cwharris marked this conversation as resolved.
Show resolved Hide resolved
return host_value;
}

template<bool synchronize>
inline void _set_value(T host_value, cudaStream_t stream) {
_memcpy<synchronize>(buff.data(), &host_value, sizeof(T), stream);
}

template<bool synchronize>
inline void _memcpy(void *dst, const void *src, size_t count, cudaStream_t stream) const {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
auto status = cudaMemcpyAsync(dst, src, count, cudaMemcpyDefault, stream);
cwharris marked this conversation as resolved.
Show resolved Hide resolved

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

if (false == synchronize) {
cwharris marked this conversation as resolved.
Show resolved Hide resolved
return;
cwharris marked this conversation as resolved.
Show resolved Hide resolved
}

status = cudaStreamSynchronize(stream);

if (cudaSuccess != status) {
throw std::runtime_error{"Stream sync 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());
}