Replaced std::vector with HostDeviceVector in MetaInfo and SparsePage. (#3446)
* Replaced std::vector with HostDeviceVector in MetaInfo and SparsePage. - added distributions to HostDeviceVector - using HostDeviceVector for labels, weights and base margings in MetaInfo - using HostDeviceVector for offset and data in SparsePage - other necessary refactoring * Added const version of HostDeviceVector API calls. - const versions added to calls that can trigger data transfers, e.g. DevicePointer() - updated the code that uses HostDeviceVector - objective functions now accept const HostDeviceVector<bst_float>& for predictions * Updated src/linear/updater_gpu_coordinate.cu. * Added read-only state for HostDeviceVector sync. - this means no copies are performed if both host and devices access the HostDeviceVector read-only * Fixed linter and test errors. - updated the lz4 plugin - added ConstDeviceSpan to HostDeviceVector - using device % dh::NVisibleDevices() for the physical device number, e.g. in calls to cudaSetDevice() * Fixed explicit template instantiation errors for HostDeviceVector. - replaced HostDeviceVector<unsigned int> with HostDeviceVector<int> * Fixed HostDeviceVector tests that require multiple GPUs. - added a mock set device handler; when set, it is called instead of cudaSetDevice()
This commit is contained in:
committed by
Rory Mitchell
parent
58d783df16
commit
72cd1517d6
@@ -25,12 +25,12 @@ namespace xgboost {
|
||||
// implementation of inline functions
|
||||
void MetaInfo::Clear() {
|
||||
num_row_ = num_col_ = num_nonzero_ = 0;
|
||||
labels_.clear();
|
||||
labels_.HostVector().clear();
|
||||
root_index_.clear();
|
||||
group_ptr_.clear();
|
||||
qids_.clear();
|
||||
weights_.clear();
|
||||
base_margin_.clear();
|
||||
weights_.HostVector().clear();
|
||||
base_margin_.HostVector().clear();
|
||||
}
|
||||
|
||||
void MetaInfo::SaveBinary(dmlc::Stream *fo) const {
|
||||
@@ -39,12 +39,12 @@ void MetaInfo::SaveBinary(dmlc::Stream *fo) const {
|
||||
fo->Write(&num_row_, sizeof(num_row_));
|
||||
fo->Write(&num_col_, sizeof(num_col_));
|
||||
fo->Write(&num_nonzero_, sizeof(num_nonzero_));
|
||||
fo->Write(labels_);
|
||||
fo->Write(labels_.HostVector());
|
||||
fo->Write(group_ptr_);
|
||||
fo->Write(qids_);
|
||||
fo->Write(weights_);
|
||||
fo->Write(weights_.HostVector());
|
||||
fo->Write(root_index_);
|
||||
fo->Write(base_margin_);
|
||||
fo->Write(base_margin_.HostVector());
|
||||
}
|
||||
|
||||
void MetaInfo::LoadBinary(dmlc::Stream *fi) {
|
||||
@@ -55,16 +55,16 @@ void MetaInfo::LoadBinary(dmlc::Stream *fi) {
|
||||
CHECK(fi->Read(&num_col_, sizeof(num_col_)) == sizeof(num_col_)) << "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&num_nonzero_, sizeof(num_nonzero_)) == sizeof(num_nonzero_))
|
||||
<< "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&labels_)) << "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&labels_.HostVector())) << "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&group_ptr_)) << "MetaInfo: invalid format";
|
||||
if (version >= kVersionQidAdded) {
|
||||
CHECK(fi->Read(&qids_)) << "MetaInfo: invalid format";
|
||||
} else { // old format doesn't contain qid field
|
||||
qids_.clear();
|
||||
}
|
||||
CHECK(fi->Read(&weights_)) << "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&weights_.HostVector())) << "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&root_index_)) << "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&base_margin_)) << "MetaInfo: invalid format";
|
||||
CHECK(fi->Read(&base_margin_.HostVector())) << "MetaInfo: invalid format";
|
||||
}
|
||||
|
||||
// try to load group information from file, if exists
|
||||
@@ -121,17 +121,20 @@ void MetaInfo::SetInfo(const char* key, const void* dptr, DataType dtype, size_t
|
||||
DISPATCH_CONST_PTR(dtype, dptr, cast_dptr,
|
||||
std::copy(cast_dptr, cast_dptr + num, root_index_.begin()));
|
||||
} else if (!std::strcmp(key, "label")) {
|
||||
labels_.resize(num);
|
||||
auto& labels = labels_.HostVector();
|
||||
labels.resize(num);
|
||||
DISPATCH_CONST_PTR(dtype, dptr, cast_dptr,
|
||||
std::copy(cast_dptr, cast_dptr + num, labels_.begin()));
|
||||
std::copy(cast_dptr, cast_dptr + num, labels.begin()));
|
||||
} else if (!std::strcmp(key, "weight")) {
|
||||
weights_.resize(num);
|
||||
auto& weights = weights_.HostVector();
|
||||
weights.resize(num);
|
||||
DISPATCH_CONST_PTR(dtype, dptr, cast_dptr,
|
||||
std::copy(cast_dptr, cast_dptr + num, weights_.begin()));
|
||||
std::copy(cast_dptr, cast_dptr + num, weights.begin()));
|
||||
} else if (!std::strcmp(key, "base_margin")) {
|
||||
base_margin_.resize(num);
|
||||
auto& base_margin = base_margin_.HostVector();
|
||||
base_margin.resize(num);
|
||||
DISPATCH_CONST_PTR(dtype, dptr, cast_dptr,
|
||||
std::copy(cast_dptr, cast_dptr + num, base_margin_.begin()));
|
||||
std::copy(cast_dptr, cast_dptr + num, base_margin.begin()));
|
||||
} else if (!std::strcmp(key, "group")) {
|
||||
group_ptr_.resize(num + 1);
|
||||
DISPATCH_CONST_PTR(dtype, dptr, cast_dptr,
|
||||
@@ -230,12 +233,14 @@ DMatrix* DMatrix::Load(const std::string& uri,
|
||||
LOG(CONSOLE) << info.group_ptr_.size() - 1
|
||||
<< " groups are loaded from " << fname << ".group";
|
||||
}
|
||||
if (MetaTryLoadFloatInfo(fname + ".base_margin", &info.base_margin_) && !silent) {
|
||||
LOG(CONSOLE) << info.base_margin_.size()
|
||||
if (MetaTryLoadFloatInfo
|
||||
(fname + ".base_margin", &info.base_margin_.HostVector()) && !silent) {
|
||||
LOG(CONSOLE) << info.base_margin_.Size()
|
||||
<< " base_margin are loaded from " << fname << ".base_margin";
|
||||
}
|
||||
if (MetaTryLoadFloatInfo(fname + ".weight", &info.weights_) && !silent) {
|
||||
LOG(CONSOLE) << info.weights_.size()
|
||||
if (MetaTryLoadFloatInfo
|
||||
(fname + ".weight", &info.weights_.HostVector()) && !silent) {
|
||||
LOG(CONSOLE) << info.weights_.Size()
|
||||
<< " weights are loaded from " << fname << ".weight";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,10 +35,12 @@ void SimpleCSRSource::CopyFrom(dmlc::Parser<uint32_t>* parser) {
|
||||
while (parser->Next()) {
|
||||
const dmlc::RowBlock<uint32_t>& batch = parser->Value();
|
||||
if (batch.label != nullptr) {
|
||||
info.labels_.insert(info.labels_.end(), batch.label, batch.label + batch.size);
|
||||
auto& labels = info.labels_.HostVector();
|
||||
labels.insert(labels.end(), batch.label, batch.label + batch.size);
|
||||
}
|
||||
if (batch.weight != nullptr) {
|
||||
info.weights_.insert(info.weights_.end(), batch.weight, batch.weight + batch.size);
|
||||
auto& weights = info.weights_.HostVector();
|
||||
weights.insert(weights.end(), batch.weight, batch.weight + batch.size);
|
||||
}
|
||||
if (batch.qid != nullptr) {
|
||||
info.qids_.insert(info.qids_.end(), batch.qid, batch.qid + batch.size);
|
||||
@@ -62,16 +64,18 @@ void SimpleCSRSource::CopyFrom(dmlc::Parser<uint32_t>* parser) {
|
||||
// update information
|
||||
this->info.num_row_ += batch.size;
|
||||
// copy the data over
|
||||
auto& data_vec = page_.data.HostVector();
|
||||
auto& offset_vec = page_.offset.HostVector();
|
||||
for (size_t i = batch.offset[0]; i < batch.offset[batch.size]; ++i) {
|
||||
uint32_t index = batch.index[i];
|
||||
bst_float fvalue = batch.value == nullptr ? 1.0f : batch.value[i];
|
||||
page_.data.emplace_back(index, fvalue);
|
||||
data_vec.emplace_back(index, fvalue);
|
||||
this->info.num_col_ = std::max(this->info.num_col_,
|
||||
static_cast<uint64_t>(index + 1));
|
||||
}
|
||||
size_t top = page_.offset.size();
|
||||
size_t top = page_.offset.Size();
|
||||
for (size_t i = 0; i < batch.size; ++i) {
|
||||
page_.offset.push_back(page_.offset[top - 1] + batch.offset[i + 1] - batch.offset[0]);
|
||||
offset_vec.push_back(offset_vec[top - 1] + batch.offset[i + 1] - batch.offset[0]);
|
||||
}
|
||||
}
|
||||
if (last_group_id != default_max) {
|
||||
@@ -79,7 +83,7 @@ void SimpleCSRSource::CopyFrom(dmlc::Parser<uint32_t>* parser) {
|
||||
info.group_ptr_.push_back(group_size);
|
||||
}
|
||||
}
|
||||
this->info.num_nonzero_ = static_cast<uint64_t>(page_.data.size());
|
||||
this->info.num_nonzero_ = static_cast<uint64_t>(page_.data.Size());
|
||||
// Either every row has query ID or none at all
|
||||
CHECK(info.qids_.empty() || info.qids_.size() == info.num_row_);
|
||||
}
|
||||
@@ -89,16 +93,16 @@ void SimpleCSRSource::LoadBinary(dmlc::Stream* fi) {
|
||||
CHECK(fi->Read(&tmagic, sizeof(tmagic)) == sizeof(tmagic)) << "invalid input file format";
|
||||
CHECK_EQ(tmagic, kMagic) << "invalid format, magic number mismatch";
|
||||
info.LoadBinary(fi);
|
||||
fi->Read(&page_.offset);
|
||||
fi->Read(&page_.data);
|
||||
fi->Read(&page_.offset.HostVector());
|
||||
fi->Read(&page_.data.HostVector());
|
||||
}
|
||||
|
||||
void SimpleCSRSource::SaveBinary(dmlc::Stream* fo) const {
|
||||
int tmagic = kMagic;
|
||||
fo->Write(&tmagic, sizeof(tmagic));
|
||||
info.SaveBinary(fo);
|
||||
fo->Write(page_.offset);
|
||||
fo->Write(page_.data);
|
||||
fo->Write(page_.offset.HostVector());
|
||||
fo->Write(page_.data.HostVector());
|
||||
}
|
||||
|
||||
void SimpleCSRSource::BeforeFirst() {
|
||||
|
||||
@@ -41,8 +41,10 @@ void SimpleDMatrix::MakeOneBatch(SparsePage* pcol, bool sorted) {
|
||||
// bit map
|
||||
const int nthread = omp_get_max_threads();
|
||||
pcol->Clear();
|
||||
auto& pcol_offset_vec = pcol->offset.HostVector();
|
||||
auto& pcol_data_vec = pcol->data.HostVector();
|
||||
common::ParallelGroupBuilder<Entry>
|
||||
builder(&pcol->offset, &pcol->data);
|
||||
builder(&pcol_offset_vec, &pcol_data_vec);
|
||||
builder.InitBudget(Info().num_col_, nthread);
|
||||
// start working
|
||||
auto iter = this->RowIterator();
|
||||
@@ -88,9 +90,9 @@ void SimpleDMatrix::MakeOneBatch(SparsePage* pcol, bool sorted) {
|
||||
auto ncol = static_cast<bst_omp_uint>(pcol->Size());
|
||||
#pragma omp parallel for schedule(dynamic, 1) num_threads(nthread)
|
||||
for (bst_omp_uint i = 0; i < ncol; ++i) {
|
||||
if (pcol->offset[i] < pcol->offset[i + 1]) {
|
||||
std::sort(dmlc::BeginPtr(pcol->data) + pcol->offset[i],
|
||||
dmlc::BeginPtr(pcol->data) + pcol->offset[i + 1],
|
||||
if (pcol_offset_vec[i] < pcol_offset_vec[i + 1]) {
|
||||
std::sort(dmlc::BeginPtr(pcol_data_vec) + pcol_offset_vec[i],
|
||||
dmlc::BeginPtr(pcol_data_vec) + pcol_offset_vec[i + 1],
|
||||
Entry::CmpValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,15 +141,19 @@ void SparsePageDMatrix::InitColAccess(
|
||||
pcol->Clear();
|
||||
pcol->base_rowid = buffered_rowset_[begin];
|
||||
const int nthread = std::max(omp_get_max_threads(), std::max(omp_get_num_procs() / 2 - 1, 1));
|
||||
auto& offset_vec = pcol->offset.HostVector();
|
||||
auto& data_vec = pcol->data.HostVector();
|
||||
common::ParallelGroupBuilder<Entry>
|
||||
builder(&pcol->offset, &pcol->data);
|
||||
builder(&offset_vec, &data_vec);
|
||||
builder.InitBudget(info.num_col_, nthread);
|
||||
bst_omp_uint ndata = static_cast<bst_uint>(prow.Size());
|
||||
const auto& prow_offset_vec = prow.offset.HostVector();
|
||||
const auto& prow_data_vec = prow.data.HostVector();
|
||||
#pragma omp parallel for schedule(static) num_threads(nthread)
|
||||
for (bst_omp_uint i = 0; i < ndata; ++i) {
|
||||
int tid = omp_get_thread_num();
|
||||
for (size_t j = prow.offset[i]; j < prow.offset[i+1]; ++j) {
|
||||
const auto e = prow.data[j];
|
||||
for (size_t j = prow_offset_vec[i]; j < prow_offset_vec[i+1]; ++j) {
|
||||
const auto e = prow_data_vec[j];
|
||||
builder.AddBudget(e.index, tid);
|
||||
}
|
||||
}
|
||||
@@ -157,8 +161,8 @@ void SparsePageDMatrix::InitColAccess(
|
||||
#pragma omp parallel for schedule(static) num_threads(nthread)
|
||||
for (bst_omp_uint i = 0; i < ndata; ++i) {
|
||||
int tid = omp_get_thread_num();
|
||||
for (size_t j = prow.offset[i]; j < prow.offset[i+1]; ++j) {
|
||||
const Entry &e = prow.data[j];
|
||||
for (size_t j = prow_offset_vec[i]; j < prow_offset_vec[i+1]; ++j) {
|
||||
const Entry &e = prow_data_vec[j];
|
||||
builder.Push(e.index,
|
||||
Entry(buffered_rowset_[i + begin], e.fvalue),
|
||||
tid);
|
||||
@@ -170,9 +174,9 @@ void SparsePageDMatrix::InitColAccess(
|
||||
auto ncol = static_cast<bst_omp_uint>(pcol->Size());
|
||||
#pragma omp parallel for schedule(dynamic, 1) num_threads(nthread)
|
||||
for (bst_omp_uint i = 0; i < ncol; ++i) {
|
||||
if (pcol->offset[i] < pcol->offset[i + 1]) {
|
||||
std::sort(dmlc::BeginPtr(pcol->data) + pcol->offset[i],
|
||||
dmlc::BeginPtr(pcol->data) + pcol->offset[i + 1],
|
||||
if (offset_vec[i] < offset_vec[i + 1]) {
|
||||
std::sort(dmlc::BeginPtr(data_vec) + offset_vec[i],
|
||||
dmlc::BeginPtr(data_vec) + offset_vec[i + 1],
|
||||
Entry::CmpValue);
|
||||
}
|
||||
}
|
||||
@@ -233,8 +237,9 @@ void SparsePageDMatrix::InitColAccess(
|
||||
size_t tick_expected = kStep;
|
||||
|
||||
while (make_next_col(page.get())) {
|
||||
const auto& page_offset_vec = page->offset.ConstHostVector();
|
||||
for (size_t i = 0; i < page->Size(); ++i) {
|
||||
col_size_[i] += page->offset[i + 1] - page->offset[i];
|
||||
col_size_[i] += page_offset_vec[i + 1] - page_offset_vec[i];
|
||||
}
|
||||
|
||||
bytes_write += page->MemCostBytes();
|
||||
|
||||
@@ -15,13 +15,15 @@ DMLC_REGISTRY_FILE_TAG(sparse_page_raw_format);
|
||||
class SparsePageRawFormat : public SparsePageFormat {
|
||||
public:
|
||||
bool Read(SparsePage* page, dmlc::SeekStream* fi) override {
|
||||
if (!fi->Read(&(page->offset))) return false;
|
||||
CHECK_NE(page->offset.size(), 0U) << "Invalid SparsePage file";
|
||||
page->data.resize(page->offset.back());
|
||||
if (page->data.size() != 0) {
|
||||
CHECK_EQ(fi->Read(dmlc::BeginPtr(page->data),
|
||||
(page->data).size() * sizeof(Entry)),
|
||||
(page->data).size() * sizeof(Entry))
|
||||
auto& offset_vec = page->offset.HostVector();
|
||||
if (!fi->Read(&offset_vec)) return false;
|
||||
auto& data_vec = page->data.HostVector();
|
||||
CHECK_NE(page->offset.Size(), 0U) << "Invalid SparsePage file";
|
||||
data_vec.resize(offset_vec.back());
|
||||
if (page->data.Size() != 0) {
|
||||
CHECK_EQ(fi->Read(dmlc::BeginPtr(data_vec),
|
||||
(page->data).Size() * sizeof(Entry)),
|
||||
(page->data).Size() * sizeof(Entry))
|
||||
<< "Invalid SparsePage file";
|
||||
}
|
||||
return true;
|
||||
@@ -31,15 +33,17 @@ class SparsePageRawFormat : public SparsePageFormat {
|
||||
dmlc::SeekStream* fi,
|
||||
const std::vector<bst_uint>& sorted_index_set) override {
|
||||
if (!fi->Read(&disk_offset_)) return false;
|
||||
auto& offset_vec = page->offset.HostVector();
|
||||
auto& data_vec = page->data.HostVector();
|
||||
// setup the offset
|
||||
page->offset.clear();
|
||||
page->offset.push_back(0);
|
||||
offset_vec.clear();
|
||||
offset_vec.push_back(0);
|
||||
for (unsigned int fid : sorted_index_set) {
|
||||
CHECK_LT(fid + 1, disk_offset_.size());
|
||||
size_t size = disk_offset_[fid + 1] - disk_offset_[fid];
|
||||
page->offset.push_back(page->offset.back() + size);
|
||||
offset_vec.push_back(offset_vec.back() + size);
|
||||
}
|
||||
page->data.resize(page->offset.back());
|
||||
data_vec.resize(offset_vec.back());
|
||||
// read in the data
|
||||
size_t begin = fi->Tell();
|
||||
size_t curr_offset = 0;
|
||||
@@ -53,14 +57,14 @@ class SparsePageRawFormat : public SparsePageFormat {
|
||||
size_t j, size_to_read = 0;
|
||||
for (j = i; j < sorted_index_set.size(); ++j) {
|
||||
if (disk_offset_[sorted_index_set[j]] == disk_offset_[fid] + size_to_read) {
|
||||
size_to_read += page->offset[j + 1] - page->offset[j];
|
||||
size_to_read += offset_vec[j + 1] - offset_vec[j];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (size_to_read != 0) {
|
||||
CHECK_EQ(fi->Read(dmlc::BeginPtr(page->data) + page->offset[i],
|
||||
CHECK_EQ(fi->Read(dmlc::BeginPtr(data_vec) + offset_vec[i],
|
||||
size_to_read * sizeof(Entry)),
|
||||
size_to_read * sizeof(Entry))
|
||||
<< "Invalid SparsePage file";
|
||||
@@ -76,11 +80,13 @@ class SparsePageRawFormat : public SparsePageFormat {
|
||||
}
|
||||
|
||||
void Write(const SparsePage& page, dmlc::Stream* fo) override {
|
||||
CHECK(page.offset.size() != 0 && page.offset[0] == 0);
|
||||
CHECK_EQ(page.offset.back(), page.data.size());
|
||||
fo->Write(page.offset);
|
||||
if (page.data.size() != 0) {
|
||||
fo->Write(dmlc::BeginPtr(page.data), page.data.size() * sizeof(Entry));
|
||||
const auto& offset_vec = page.offset.HostVector();
|
||||
const auto& data_vec = page.data.HostVector();
|
||||
CHECK(page.offset.Size() != 0 && offset_vec[0] == 0);
|
||||
CHECK_EQ(offset_vec.back(), page.data.Size());
|
||||
fo->Write(offset_vec);
|
||||
if (page.data.Size() != 0) {
|
||||
fo->Write(dmlc::BeginPtr(data_vec), page.data.Size() * sizeof(Entry));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -129,10 +129,12 @@ void SparsePageSource::Create(dmlc::Parser<uint32_t>* src,
|
||||
while (src->Next()) {
|
||||
const dmlc::RowBlock<uint32_t>& batch = src->Value();
|
||||
if (batch.label != nullptr) {
|
||||
info.labels_.insert(info.labels_.end(), batch.label, batch.label + batch.size);
|
||||
auto& labels = info.labels_.HostVector();
|
||||
labels.insert(labels.end(), batch.label, batch.label + batch.size);
|
||||
}
|
||||
if (batch.weight != nullptr) {
|
||||
info.weights_.insert(info.weights_.end(), batch.weight, batch.weight + batch.size);
|
||||
auto& weights = info.weights_.HostVector();
|
||||
weights.insert(weights.end(), batch.weight, batch.weight + batch.size);
|
||||
}
|
||||
if (batch.qid != nullptr) {
|
||||
info.qids_.insert(info.qids_.end(), batch.qid, batch.qid + batch.size);
|
||||
@@ -175,7 +177,7 @@ void SparsePageSource::Create(dmlc::Parser<uint32_t>* src,
|
||||
}
|
||||
}
|
||||
|
||||
if (page->data.size() != 0) {
|
||||
if (page->data.Size() != 0) {
|
||||
writer.PushWrite(std::move(page));
|
||||
}
|
||||
|
||||
@@ -224,7 +226,7 @@ void SparsePageSource::Create(DMatrix* src,
|
||||
<< (bytes_write >> 20UL) << " written";
|
||||
}
|
||||
}
|
||||
if (page->data.size() != 0) {
|
||||
if (page->data.Size() != 0) {
|
||||
writer.PushWrite(std::move(page));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user