datasource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/io/types.hpp>
10 #include <cudf/utilities/export.hpp>
11 #include <cudf/utilities/span.hpp>
12 
13 #include <rmm/cuda_stream_view.hpp>
14 
15 #include <future>
16 #include <memory>
17 #include <optional>
18 
25 namespace CUDF_EXPORT cudf {
27 namespace io {
28 
37 class datasource {
38  public:
39  template <typename Container>
40  class owning_buffer; // forward declaration
46  class buffer {
47  public:
53  [[nodiscard]] virtual size_t size() const = 0;
54 
60  [[nodiscard]] virtual uint8_t const* data() const = 0;
61 
65  virtual ~buffer() = default;
66 
76  {
77  return cudf::host_span<uint8_t const>{data(), size()};
78  }
79 
87  template <typename Container>
88  static std::unique_ptr<buffer> create(Container&& data_owner)
89  {
90  return std::make_unique<owning_buffer<Container>>(std::forward<Container>(data_owner));
91  }
92  };
93 
111  static std::unique_ptr<datasource> create(std::string const& filepath,
112  size_t offset = 0,
113  size_t max_size_estimate = 0,
114  std::optional<std::size_t> known_size = std::nullopt);
115 
122  static std::unique_ptr<datasource> create(cudf::host_span<std::byte const> buffer);
123 
130  static std::unique_ptr<datasource> create(cudf::device_span<std::byte const> buffer);
131 
138  static std::unique_ptr<datasource> create(datasource* source);
139 
146  template <typename T>
147  static std::vector<std::unique_ptr<datasource>> create(std::vector<T> const& args)
148  {
149  std::vector<std::unique_ptr<datasource>> sources;
150  sources.reserve(args.size());
151  std::transform(args.cbegin(), args.cend(), std::back_inserter(sources), [](auto const& arg) {
152  return datasource::create(arg);
153  });
154  return sources;
155  }
156 
160  virtual ~datasource() = default;
161 
170  virtual std::unique_ptr<datasource::buffer> host_read(size_t offset, size_t size) = 0;
171 
184  virtual std::future<std::unique_ptr<datasource::buffer>> host_read_async(size_t offset,
185  size_t size);
186 
196  virtual size_t host_read(size_t offset, size_t size, uint8_t* dst) = 0;
197 
212  virtual std::future<size_t> host_read_async(size_t offset, size_t size, uint8_t* dst);
213 
226  [[nodiscard]] virtual bool supports_device_read() const { return false; }
227 
234  [[nodiscard]] virtual bool is_device_read_preferred(size_t size) const
235  {
236  return supports_device_read();
237  }
238 
255  virtual std::unique_ptr<datasource::buffer> device_read(size_t offset,
256  size_t size,
257  rmm::cuda_stream_view stream)
258  {
259  CUDF_FAIL("datasource classes that support device_read must override it.");
260  }
261 
279  virtual size_t device_read(size_t offset, size_t size, uint8_t* dst, rmm::cuda_stream_view stream)
280  {
281  CUDF_FAIL("datasource classes that support device_read must override it.");
282  }
283 
307  virtual std::future<size_t> device_read_async(size_t offset,
308  size_t size,
309  uint8_t* dst,
310  rmm::cuda_stream_view stream)
311  {
312  CUDF_FAIL("datasource classes that support device_read_async must override it.");
313  }
314 
320  [[nodiscard]] virtual size_t size() const = 0;
321 
327  [[nodiscard]] virtual bool is_empty() const { return size() == 0; }
328 
332  class non_owning_buffer : public buffer {
333  public:
334  non_owning_buffer() = default;
335 
342  non_owning_buffer(uint8_t const* data, size_t size) : _data(data), _size(size) {}
343 
349  [[nodiscard]] size_t size() const override { return _size; }
350 
356  [[nodiscard]] uint8_t const* data() const override { return _data; }
357 
358  private:
359  uint8_t const* _data{nullptr};
360  size_t _size{0};
361  };
362 
370  template <typename Container>
371  class owning_buffer : public buffer {
372  public:
373  // Require that the argument passed to the constructor be an rvalue (Container&& being an rvalue
374  // reference).
375  static_assert(std::is_rvalue_reference_v<Container&&>,
376  "The container argument passed to the constructor must be an rvalue.");
377 
384  owning_buffer(Container&& moved_data_owner)
385  : _data(std::move(moved_data_owner)), _data_ptr(_data.data()), _size(_data.size())
386  {
387  }
388 
398  owning_buffer(Container&& moved_data_owner, uint8_t const* data_ptr, size_t size)
399  : _data(std::move(moved_data_owner)), _data_ptr(data_ptr), _size(size)
400  {
401  }
402 
408  [[nodiscard]] size_t size() const override { return _size; }
409 
415  [[nodiscard]] uint8_t const* data() const override
416  {
417  return static_cast<uint8_t const*>(_data_ptr);
418  }
419 
420  private:
421  Container _data;
422  void const* _data_ptr;
423  size_t _size;
424  };
425 };
426 
438 std::vector<std::unique_ptr<cudf::io::datasource>> make_datasources(source_info const& info,
439  size_t offset = 0,
440  size_t max_size_estimate = 0);
441  // end of group
443 } // namespace io
444 } // namespace CUDF_EXPORT cudf
Interface class for buffers that the datasource returns to the caller.
Definition: datasource.hpp:46
virtual ~buffer()=default
Base class destructor.
static std::unique_ptr< buffer > create(Container &&data_owner)
Factory to construct a datasource buffer object from a container.
Definition: datasource.hpp:88
virtual size_t size() const =0
Returns the buffer size in bytes.
virtual uint8_t const * data() const =0
Returns the address of the data in the buffer.
Implementation for non owning buffer where datasource holds buffer until destruction.
Definition: datasource.hpp:332
size_t size() const override
Returns the size of the buffer.
Definition: datasource.hpp:349
uint8_t const * data() const override
Returns the pointer to the buffer.
Definition: datasource.hpp:356
non_owning_buffer(uint8_t const *data, size_t size)
Construct a new non owning buffer object.
Definition: datasource.hpp:342
Derived implementation of buffer that owns the data.
Definition: datasource.hpp:371
owning_buffer(Container &&moved_data_owner)
Moves the input container into the newly created object.
Definition: datasource.hpp:384
owning_buffer(Container &&moved_data_owner, uint8_t const *data_ptr, size_t size)
Moves the input container into the newly created object, and exposes a subspan of the buffer.
Definition: datasource.hpp:398
size_t size() const override
Returns the size of the buffer.
Definition: datasource.hpp:408
uint8_t const * data() const override
Returns the pointer to the data in the buffer.
Definition: datasource.hpp:415
Interface class for providing input data to the readers.
Definition: datasource.hpp:37
static std::unique_ptr< datasource > create(std::string const &filepath, size_t offset=0, size_t max_size_estimate=0, std::optional< std::size_t > known_size=std::nullopt)
Creates a source from a file path.
virtual ~datasource()=default
Base class destructor.
static std::vector< std::unique_ptr< datasource > > create(std::vector< T > const &args)
Creates a vector of datasources, one per element in the input vector.
Definition: datasource.hpp:147
virtual bool supports_device_read() const
Whether or not this source supports reading directly into device memory.
Definition: datasource.hpp:226
static std::unique_ptr< datasource > create(datasource *source)
Creates a source from an user implemented datasource object.
virtual std::future< std::unique_ptr< datasource::buffer > > host_read_async(size_t offset, size_t size)
Asynchronously reads a specified portion of data from the datasource.
virtual size_t device_read(size_t offset, size_t size, uint8_t *dst, rmm::cuda_stream_view stream)
Reads a selected range into a preallocated device buffer.
Definition: datasource.hpp:279
virtual bool is_device_read_preferred(size_t size) const
Estimates whether a direct device read would be more optimal for the given size.
Definition: datasource.hpp:234
static std::unique_ptr< datasource > create(cudf::device_span< std::byte const > buffer)
Creates a source from a device memory buffer.
virtual std::future< size_t > device_read_async(size_t offset, size_t size, uint8_t *dst, rmm::cuda_stream_view stream)
Asynchronously reads a selected range into a preallocated device buffer.
Definition: datasource.hpp:307
virtual bool is_empty() const
Returns whether the source contains any data.
Definition: datasource.hpp:327
virtual std::future< size_t > host_read_async(size_t offset, size_t size, uint8_t *dst)
Asynchronously reads data from the source into the provided host memory buffer.
virtual size_t host_read(size_t offset, size_t size, uint8_t *dst)=0
Reads a selected range into a preallocated buffer.
virtual std::unique_ptr< datasource::buffer > device_read(size_t offset, size_t size, rmm::cuda_stream_view stream)
Returns a device buffer with a subset of data from the source.
Definition: datasource.hpp:255
virtual size_t size() const =0
Returns the size of the data in the source.
virtual std::unique_ptr< datasource::buffer > host_read(size_t offset, size_t size)=0
Returns a buffer with a subset of data from the source.
static std::unique_ptr< datasource > create(cudf::host_span< std::byte const > buffer)
Creates a source from a host memory buffer.
Exception types and error-checking macros used throughout libcudf.
std::vector< std::unique_ptr< cudf::io::datasource > > make_datasources(source_info const &info, size_t offset=0, size_t max_size_estimate=0)
Constructs datasources from dataset source information.
std::unique_ptr< column > transform(std::vector< column_view > const &inputs, std::string const &transform_udf, data_type output_type, bool is_ptx, std::optional< void * > user_data=std::nullopt, null_aware is_null_aware=null_aware::NO, output_nullability null_policy=output_nullability::PRESERVE, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Creates a new column by applying a transform function against every element of the input columns.
#define CUDF_FAIL(...)
Indicates that an erroneous code path has been taken.
Definition: error.hpp:223
cuda::std::span< T, Extent > device_span
Device span is an alias of cuda::std::span.
Definition: span.hpp:296
Type definitions for the cuDF-IO API.
cuDF interfaces
Definition: host_udf.hpp:26
APIs for spans.
Host span, a non-owning view over a contiguous sequence of host-accessible elements.
Definition: span.hpp:65
Source information for read interfaces.
Definition: types.hpp:306