Skip to content

Commit

Permalink
Fix test_c_api memory leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Aug 16, 2018
1 parent 41d1132 commit 7adffe0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/c_api/c_api.cc
Expand Up @@ -442,14 +442,14 @@ void PrefixSum(size_t *x, size_t N) {
delete[] suma;
}

XGB_DLL int XGDMatrixCreateFromMat_omp(const bst_float* data, // NOLINT
XGB_DLL int XGDMatrixCreateFromMat_omp(const bst_float* _data, // NOLINT
xgboost::bst_ulong nrow,
xgboost::bst_ulong ncol,
bst_float missing, DMatrixHandle* out,
int nthread) {
// avoid openmp unless enough data to be worth it to avoid overhead costs
if (nrow*ncol <= 10000*50) {
return(XGDMatrixCreateFromMat(data, nrow, ncol, missing, out));
return(XGDMatrixCreateFromMat(_data, nrow, ncol, missing, out));
}

API_BEGIN();
Expand Down Expand Up @@ -480,10 +480,10 @@ XGB_DLL int XGDMatrixCreateFromMat_omp(const bst_float* data, // NOLINT
for (omp_ulong i = 0; i < nrow; ++i) {
xgboost::bst_ulong nelem = 0;
for (xgboost::bst_ulong j = 0; j < ncol; ++j) {
if (common::CheckNAN(data[ncol*i + j]) && !nan_missing) {
if (common::CheckNAN(_data[ncol*i + j]) && !nan_missing) {
badnan[ithread] = 1;
} else if (common::CheckNAN(data[ncol * i + j])) {
} else if (nan_missing || data[ncol * i + j] != missing) {
} else if (common::CheckNAN(_data[ncol * i + j])) {
} else if (nan_missing || _data[ncol * i + j] != missing) {
++nelem;
}
}
Expand All @@ -506,10 +506,10 @@ XGB_DLL int XGDMatrixCreateFromMat_omp(const bst_float* data, // NOLINT
for (omp_ulong i = 0; i < nrow; ++i) {
xgboost::bst_ulong matj = 0;
for (xgboost::bst_ulong j = 0; j < ncol; ++j) {
if (common::CheckNAN(data[ncol * i + j])) {
} else if (nan_missing || data[ncol * i + j] != missing) {
if (common::CheckNAN(_data[ncol * i + j])) {
} else if (nan_missing || _data[ncol * i + j] != missing) {
mat.page_.data[mat.page_.offset[i] + matj] =
Entry(j, data[ncol * i + j]);
Entry(j, _data[ncol * i + j]);
++matj;
}
}
Expand Down
19 changes: 11 additions & 8 deletions tests/cpp/c_api/test_c_api.cc
Expand Up @@ -13,14 +13,14 @@ TEST(c_api, XGDMatrixCreateFromMatDT) {
DMatrixHandle handle;
XGDMatrixCreateFromDT(data.data(), types.data(), 3, 2, &handle,
0);
std::shared_ptr<xgboost::DMatrix> dmat =
*static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
xgboost::MetaInfo &info = dmat->Info();
std::shared_ptr<xgboost::DMatrix> *dmat =
static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
xgboost::MetaInfo &info = (*dmat)->Info();
ASSERT_EQ(info.num_col_, 2);
ASSERT_EQ(info.num_row_, 3);
ASSERT_EQ(info.num_nonzero_, 6);

auto iter = dmat->RowIterator();
auto iter = (*dmat)->RowIterator();
iter->BeforeFirst();
while (iter->Next()) {
auto batch = iter->Value();
Expand All @@ -29,6 +29,8 @@ TEST(c_api, XGDMatrixCreateFromMatDT) {
ASSERT_EQ(batch[2][0].fvalue, 3.0f);
ASSERT_EQ(batch[2][1].fvalue, 0.0f);
}

delete dmat;
}

TEST(c_api, XGDMatrixCreateFromMat_omp) {
Expand All @@ -46,14 +48,14 @@ TEST(c_api, XGDMatrixCreateFromMat_omp) {
std::numeric_limits<float>::quiet_NaN(), &handle,
0);

std::shared_ptr<xgboost::DMatrix> dmat =
*static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
xgboost::MetaInfo &info = dmat->Info();
std::shared_ptr<xgboost::DMatrix> *dmat =
static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
xgboost::MetaInfo &info = (*dmat)->Info();
ASSERT_EQ(info.num_col_, num_cols);
ASSERT_EQ(info.num_row_, row);
ASSERT_EQ(info.num_nonzero_, num_cols * row - num_missing);

auto iter = dmat->RowIterator();
auto iter = (*dmat)->RowIterator();
iter->BeforeFirst();
while (iter->Next()) {
auto batch = iter->Value();
Expand All @@ -64,5 +66,6 @@ TEST(c_api, XGDMatrixCreateFromMat_omp) {
}
}
}
delete dmat;
}
}

0 comments on commit 7adffe0

Please sign in to comment.