7 #ifndef XGBOOST_DATA_H_ 8 #define XGBOOST_DATA_H_ 10 #include <dmlc/base.h> 11 #include <dmlc/data.h> 12 #include <dmlc/serializer.h> 13 #include <rabit/rabit.h> 43 static constexpr uint64_t kNumField = 9;
50 uint64_t num_nonzero_{0};
106 void Validate(int32_t device)
const;
119 if (label_order_cache_.size() == labels_.
Size()) {
120 return label_order_cache_;
122 label_order_cache_.resize(labels_.
Size());
123 std::iota(label_order_cache_.begin(), label_order_cache_.end(), 0);
126 [&l](
size_t i1,
size_t i2) {
return std::abs(l[i1]) < std::abs(l[i2]);});
128 return label_order_cache_;
136 void LoadBinary(dmlc::Stream* fi);
141 void SaveBinary(dmlc::Stream* fo)
const;
149 void SetInfo(
const char* key,
const void* dptr,
DataType dtype,
size_t num);
159 void SetInfo(
const char* key, std::string
const& interface_str);
163 mutable std::vector<size_t> label_order_cache_;
185 return (this->index == other.
index && this->fvalue == other.
fvalue);
200 BatchParam(int32_t device, int32_t max_bin,
size_t gpu_page_size = 0)
201 : gpu_id{device}, max_bin{max_bin}, gpu_page_size{gpu_page_size} {}
230 if (rabit::IsDistributed() && i + 1 >= offset_vec.size()) {
233 size = offset_vec[i + 1] - offset_vec[i];
235 return {data_vec.data() + offset_vec[i],
246 return offset.
Size() == 0 ? 0 : offset.
Size() - 1;
251 return offset.
Size() *
sizeof(size_t) + data.
Size() *
sizeof(
Entry);
259 offset_vec.push_back(0);
268 SparsePage GetTranspose(
int num_columns)
const;
272 #pragma omp parallel for default(none) shared(ncol) schedule(dynamic, 1) 287 void Push(
const dmlc::RowBlock<uint32_t>& batch);
299 template <
typename AdapterBatchT>
300 uint64_t Push(
const AdapterBatchT& batch,
float missing,
int nthread);
326 class EllpackPageImpl;
358 void SetBaseRowId(
size_t row_id);
360 const EllpackPageImpl*
Impl()
const {
return impl_.get(); }
361 EllpackPageImpl*
Impl() {
return impl_.get(); }
364 std::unique_ptr<EllpackPageImpl> impl_;
371 virtual T& operator*() = 0;
372 virtual const T& operator*()
const = 0;
373 virtual void operator++() = 0;
374 virtual bool AtEnd()
const = 0;
384 CHECK(impl_ !=
nullptr);
389 CHECK(impl_ !=
nullptr);
394 CHECK(impl_ !=
nullptr);
399 CHECK(impl_ !=
nullptr);
400 return !impl_->AtEnd();
404 CHECK(impl_ !=
nullptr);
405 return impl_->AtEnd();
409 std::shared_ptr<BatchIteratorImpl<T>> impl_;
458 virtual const MetaInfo& Info()
const = 0;
464 template <
typename T>
465 bool PageExists()
const;
469 virtual bool SingleColBlock()
const = 0;
475 return Info().num_nonzero_ == Info().num_row_ * Info().num_col_;
488 static DMatrix* Load(
const std::string& uri,
491 const std::string& file_format =
"auto",
492 size_t page_size = kPageSize);
506 template <
typename AdapterT>
507 static DMatrix* Create(AdapterT* adapter,
float missing,
int nthread,
508 const std::string& cache_prefix =
"",
509 size_t page_size = kPageSize);
513 static const size_t kPageSize = 32UL << 20UL;
521 virtual bool EllpackExists()
const = 0;
522 virtual bool SparsePageExists()
const = 0;
527 return GetRowBatches();
531 inline bool DMatrix::PageExists<EllpackPage>()
const {
532 return this->EllpackExists();
536 inline bool DMatrix::PageExists<SparsePage>()
const {
537 return this->SparsePageExists();
542 return GetColumnBatches();
547 return GetSortedColumnBatches();
552 return GetEllpackBatches(param);
559 namespace serializer {
564 strm->Write(data.
index);
569 return strm->Read(&data->
index) && strm->Read(&data->
fvalue);
575 #endif // XGBOOST_DATA_H_
void operator++()
Definition: data.h:383
std::forward_iterator_tag iterator_category
Definition: data.h:380
float bst_float
float type, used for storing statistics
Definition: base.h:111
bool IsDense() const
Whether the matrix is dense.
Definition: data.h:474
void Copy(const HostDeviceVector< T > &other)
BatchIterator(BatchIteratorImpl< T > *impl)
Definition: data.h:381
#define XGBOOST_PARALLEL_SORT(X, Y, Z)
Definition: base.h:68
A page stored in ELLPACK format.
Definition: data.h:333
const T & operator*() const
Definition: data.h:393
T & operator*()
Definition: data.h:388
SortedCSCPage()
Definition: data.h:322
std::size_t index_type
Definition: span.h:405
bool operator!=(const BatchParam &other) const
Definition: data.h:202
bool AtEnd() const
Definition: data.h:403
void SetBaseRowId(size_t row_id)
Set the base row id for this page.
Definition: data.h:264
void SortRows()
Definition: data.h:270
size_t gpu_page_size
Page size for external memory mode.
Definition: data.h:198
BatchSet(BatchIterator< T > begin_iter)
Definition: data.h:415
uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:114
dmlc::omp_uint bst_omp_uint
define unsigned int for openmp loop
Definition: base.h:253
Internal data structured used by XGBoost during training. There are two ways to create a customized D...
Definition: data.h:451
In-memory storage unit of sparse batch, stored in CSR format.
Definition: data.h:211
A device-and-host vector abstraction layer.
static bool Read(Stream *strm, xgboost::Entry *data)
Definition: data.h:568
Parameters for constructing batches.
Definition: data.h:192
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:126
BatchSet< T > GetBatches(const BatchParam ¶m={})
Gets batches. Use range based for loop over BatchSet to access individual batches.
int gpu_id
The GPU device to use.
Definition: data.h:194
HostDeviceVector< bst_row_t > offset
Definition: data.h:214
XGBOOST_DEVICE Entry(bst_feature_t index, bst_float fvalue)
constructor with index and value
Definition: data.h:179
SparsePage()
constructor
Definition: data.h:240
BatchIterator< T > begin()
Definition: data.h:416
const EllpackPageImpl * Impl() const
Definition: data.h:360
SortedCSCPage(SparsePage page)
Definition: data.h:323
CSCPage()
Definition: data.h:316
std::vector< T > & HostVector()
bool operator==(const Entry &other) const
Definition: data.h:184
Inst operator[](size_t i) const
get i-th row from the batch
Definition: data.h:224
This is data structure that user can pass to DMatrix::Create to create a DMatrix for training...
Definition: data.h:431
bool operator!=(const BatchIterator &rhs) const
Definition: data.h:398
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
size_t MemCostBytes() const
Definition: data.h:250
namespace of xgboost
Definition: base.h:102
CSCPage(SparsePage page)
Definition: data.h:317
defines configuration macros of xgboost.
size_t Size() const
Definition: data.h:245
DataType
data type accepted by xgboost interface
Definition: data.h:30
HostDeviceVector< Entry > data
the data of the segments
Definition: data.h:216
Element from a sparse vector.
Definition: data.h:167
DMLC_DECLARE_TRAITS(is_pod, xgboost::Entry, true)
BatchParam(int32_t device, int32_t max_bin, size_t gpu_page_size=0)
Definition: data.h:200
BatchIterator< T > end()
Definition: data.h:417
static void Write(Stream *strm, const xgboost::Entry &data)
Definition: data.h:563
bst_feature_t index
feature index
Definition: data.h:169
bst_float fvalue
feature value
Definition: data.h:171
static bool CmpValue(const Entry &a, const Entry &b)
reversely compare feature values
Definition: data.h:181
void Resize(size_t new_size, T v=T())
void Clear()
clear the page
Definition: data.h:255
int max_bin
Maximum number of bins per feature for histograms.
Definition: data.h:196
MetaInfo info
Meta information about the dataset The subclass need to be able to load this correctly from data...
Definition: data.h:437
EllpackPageImpl * Impl()
Definition: data.h:361