Skip to content

Commit

Permalink
Apply event raii wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcrimsontianyu committed Sep 30, 2024
1 parent e2bcbb8 commit 9cd7e1a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 33 deletions.
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ add_library(
src/unary/nan_ops.cu
src/unary/null_ops.cu
src/utilities/cuda.cpp
src/utilities/cuda_event.cpp
src/utilities/cuda_memcpy.cu
src/utilities/default_stream.cpp
src/utilities/host_memory.cpp
Expand Down
31 changes: 31 additions & 0 deletions cpp/include/cudf/utilities/cuda_event.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <cudf/utilities/export.hpp>

#include <cuda_runtime_api.h>

namespace CUDF_EXPORT cudf {

namespace detail {
/**
* @brief RAII struct to wrap a cuda event and ensure its proper destruction.
*/
struct cuda_event {
cuda_event();
virtual ~cuda_event();

// Moveable but not copyable.
cuda_event(const cuda_event&) = delete;
cuda_event& operator=(const cuda_event&) = delete;

cuda_event(cuda_event&&) = default;
cuda_event& operator=(cuda_event&&) = default;

operator cudaEvent_t() const;

private:
cudaEvent_t e_;
};
} // namespace detail

} // namespace CUDF_EXPORT cudf
5 changes: 2 additions & 3 deletions cpp/src/interop/to_arrow_device.cu
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <cudf/strings/strings_column_view.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/cuda_event.hpp>
#include <cudf/utilities/memory_resource.hpp>
#include <cudf/utilities/traits.hpp>
#include <cudf/utilities/type_dispatcher.hpp>
Expand Down Expand Up @@ -559,13 +560,12 @@ int dispatch_to_arrow_device_view::operator()<cudf::dictionary32>(ArrowArray* ou

struct ArrowDeviceArrayPrivateData {
ArrowArray parent;
cudaEvent_t sync_event;
cudf::detail::cuda_event sync_event;
};

void ArrowDeviceArrayRelease(ArrowArray* array)
{
auto private_data = reinterpret_cast<ArrowDeviceArrayPrivateData*>(array->private_data);
RMM_ASSERT_CUDA_SUCCESS(cudaEventDestroy(private_data->sync_event));
ArrowArrayRelease(&private_data->parent);
delete private_data;
array->release = nullptr;
Expand All @@ -578,7 +578,6 @@ unique_device_array_t create_device_array(nanoarrow::UniqueArray&& out,
ArrowArrayFinishBuilding(out.get(), NANOARROW_VALIDATION_LEVEL_MINIMAL, nullptr));

auto private_data = std::make_unique<detail::ArrowDeviceArrayPrivateData>();
CUDF_CUDA_TRY(cudaEventCreate(&private_data->sync_event));
CUDF_CUDA_TRY(cudaEventRecord(private_data->sync_event, stream.value()));

ArrowArrayMove(out.get(), &private_data->parent);
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/io/text/bgzip_data_chunk_source.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cudf/io/config_utils.hpp>
#include <cudf/io/text/data_chunk_source_factories.hpp>
#include <cudf/io/text/detail/bgzip_utils.hpp>
#include <cudf/utilities/cuda_event.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/error.hpp>

Expand Down Expand Up @@ -84,7 +85,7 @@ class bgzip_data_chunk_reader : public data_chunk_reader {
static constexpr std::size_t default_offset_alloc =
1 << 16; // 64k offset allocation, resized on demand

cudaEvent_t event;
cudf::detail::cuda_event event;
cudf::detail::host_vector<char> h_compressed_blocks;
cudf::detail::host_vector<std::size_t> h_compressed_offsets;
cudf::detail::host_vector<std::size_t> h_decompressed_offsets;
Expand Down Expand Up @@ -115,7 +116,6 @@ class bgzip_data_chunk_reader : public data_chunk_reader {
d_decompressed_spans(0, init_stream),
d_decompression_results(0, init_stream)
{
CUDF_CUDA_TRY(cudaEventCreate(&event));
h_compressed_blocks.reserve(default_buffer_alloc);
h_compressed_offsets.reserve(default_offset_alloc);
h_compressed_offsets.push_back(0);
Expand Down
6 changes: 2 additions & 4 deletions cpp/src/io/text/data_chunk_source_factories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cudf/detail/utilities/host_vector.hpp>
#include <cudf/detail/utilities/vector_factories.hpp>
#include <cudf/io/text/data_chunk_source_factories.hpp>
#include <cudf/utilities/cuda_event.hpp>

#include <rmm/device_buffer.hpp>

Expand All @@ -33,15 +34,12 @@ namespace cudf::io::text {
namespace {

struct host_ticket {
cudaEvent_t event{}; // tracks the completion of the last device-to-host copy.
cudf::detail::cuda_event event; // tracks the completion of the last device-to-host copy.
cudf::detail::host_vector<char> buffer;

host_ticket() : buffer{cudf::detail::make_pinned_vector_sync<char>(0, cudf::get_default_stream())}
{
cudaEventCreate(&event);
}

~host_ticket() { cudaEventDestroy(event); }
};

/**
Expand Down
6 changes: 2 additions & 4 deletions cpp/src/io/text/multibyte_split.cu
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cudf/scalar/scalar.hpp>
#include <cudf/strings/detail/strings_column_factories.cuh>
#include <cudf/strings/detail/utilities.hpp>
#include <cudf/utilities/cuda_event.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/memory_resource.hpp>
#include <cudf/utilities/span.hpp>
Expand Down Expand Up @@ -383,8 +384,7 @@ std::unique_ptr<cudf::column> multibyte_split(cudf::io::text::data_chunk_source

auto streams = cudf::detail::fork_streams(stream, concurrency);

cudaEvent_t last_launch_event;
CUDF_CUDA_TRY(cudaEventCreate(&last_launch_event));
cudf::detail::cuda_event last_launch_event;

auto& read_stream = streams[0];
auto& scan_stream = streams[1];
Expand Down Expand Up @@ -499,8 +499,6 @@ std::unique_ptr<cudf::column> multibyte_split(cudf::io::text::data_chunk_source
chunk = std::move(next_chunk);
}

CUDF_CUDA_TRY(cudaEventDestroy(last_launch_event));

cudf::detail::join_streams(streams, stream);

auto chars = char_storage.gather(stream, mr);
Expand Down
14 changes: 14 additions & 0 deletions cpp/src/utilities/cuda_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <cudf/utilities/cuda_event.hpp>
#include <cudf/utilities/error.hpp>

#include <rmm/detail/error.hpp>

namespace cudf::detail {

cuda_event::cuda_event() { CUDF_CUDA_TRY(cudaEventCreateWithFlags(&e_, cudaEventDisableTiming)); }

cuda_event::~cuda_event() { RMM_ASSERT_CUDA_SUCCESS(cudaEventDestroy(e_)); }

cuda_event::operator cudaEvent_t() const { return e_; }

} // namespace cudf::detail
21 changes: 1 addition & 20 deletions cpp/src/utilities/stream_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cudf/detail/utilities/logger.hpp>
#include <cudf/detail/utilities/stream_pool.hpp>
#include <cudf/utilities/cuda_event.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/error.hpp>

Expand Down Expand Up @@ -125,26 +126,6 @@ rmm::cuda_device_id get_current_cuda_device()
return rmm::cuda_device_id{device_id};
}

/**
* @brief RAII struct to wrap a cuda event and ensure its proper destruction.
*/
struct cuda_event {
cuda_event() { CUDF_CUDA_TRY(cudaEventCreateWithFlags(&e_, cudaEventDisableTiming)); }
virtual ~cuda_event() { CUDF_ASSERT_CUDA_SUCCESS(cudaEventDestroy(e_)); }

// Moveable but not copyable.
cuda_event(const cuda_event&) = delete;
cuda_event& operator=(const cuda_event&) = delete;

cuda_event(cuda_event&&) = default;
cuda_event& operator=(cuda_event&&) = default;

operator cudaEvent_t() { return e_; }

private:
cudaEvent_t e_;
};

/**
* @brief Returns a cudaEvent_t for the current thread.
*
Expand Down

0 comments on commit 9cd7e1a

Please sign in to comment.