Consistent use of context to specify number of threads. (#8733)

- Use context in all tests.
- Use context in R.
- Use context in C API DMatrix initialization. (0 threads is used as dft).
This commit is contained in:
Jiaming Yuan
2023-01-30 15:25:31 +08:00
committed by GitHub
parent 21a28f2cc5
commit 3760cede0f
24 changed files with 212 additions and 152 deletions

View File

@@ -1,16 +1,21 @@
/*!
* Copyright 2019-2022 XGBoost contributors
/**
* Copyright 2019-2023 XGBoost contributors
*/
#include <gtest/gtest.h>
#include <xgboost/version_config.h>
#include <xgboost/c_api.h>
#include <xgboost/data.h>
#include <xgboost/json.h> // Json
#include <xgboost/learner.h>
#include <xgboost/version_config.h>
#include "../helpers.h"
#include "../../../src/common/io.h"
#include <cstddef> // std::size_t
#include <limits> // std::numeric_limits
#include <string> // std::string
#include <vector>
#include "../../../src/c_api/c_api_error.h"
#include "../../../src/common/io.h"
#include "../helpers.h"
TEST(CAPI, XGDMatrixCreateFromMatDT) {
std::vector<int> col0 = {0, -1, 3};
@@ -83,6 +88,39 @@ TEST(CAPI, Version) {
ASSERT_EQ(patch, XGBOOST_VER_PATCH);
}
TEST(CAPI, XGDMatrixCreateFromCSR) {
HostDeviceVector<std::size_t> indptr{0, 3};
HostDeviceVector<double> data{0.0, 1.0, 2.0};
HostDeviceVector<std::size_t> indices{0, 1, 2};
auto indptr_arr = GetArrayInterface(&indptr, 2, 1);
auto indices_arr = GetArrayInterface(&indices, 3, 1);
auto data_arr = GetArrayInterface(&data, 3, 1);
std::string sindptr, sindices, sdata, sconfig;
Json::Dump(indptr_arr, &sindptr);
Json::Dump(indices_arr, &sindices);
Json::Dump(data_arr, &sdata);
Json config{Object{}};
config["missing"] = Number{std::numeric_limits<float>::quiet_NaN()};
Json::Dump(config, &sconfig);
DMatrixHandle handle;
XGDMatrixCreateFromCSR(sindptr.c_str(), sindices.c_str(), sdata.c_str(), 3, sconfig.c_str(),
&handle);
bst_ulong n;
ASSERT_EQ(XGDMatrixNumRow(handle, &n), 0);
ASSERT_EQ(n, 1);
ASSERT_EQ(XGDMatrixNumCol(handle, &n), 0);
ASSERT_EQ(n, 3);
ASSERT_EQ(XGDMatrixNumNonMissing(handle, &n), 0);
ASSERT_EQ(n, 3);
std::shared_ptr<xgboost::DMatrix> *pp_fmat =
static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
ASSERT_EQ((*pp_fmat)->Ctx()->Threads(), AllThreadsForTest());
XGDMatrixFree(handle);
}
TEST(CAPI, ConfigIO) {
size_t constexpr kRows = 10;
auto p_dmat = RandomDataGenerator(kRows, 10, 0).GenerateDMatrix();