tests/cpp/test_learner.cc
// Tests for automatic GPU configuration.
Tests for automatic GPU configuration.
Tests for automatic GPU configuration .
TEST(Learner, GPUConfiguration) {
  using Arg = std::pair<std::string, std::string>;
  size_t constexpr kRows = 10;
  auto pp_dmat = CreateDMatrix(kRows, 10, 0);
  auto p_dmat = *pp_dmat;
  std::vector<std::shared_ptr<DMatrix>> mat {p_dmat};
  std::vector<bst_float> labels(kRows);
  for (size_t i = 0; i < labels.size(); ++i) {
    labels[i] = i;
  }
  p_dmat->Info().labels_.HostVector() = labels;
  {
    std::unique_ptr<Learner> learner {Learner::Create(mat)};
    learner->SetParams({Arg{"booster", "gblinear"},
                        Arg{"updater", "gpu_coord_descent"}});
    learner->UpdateOneIter(0, p_dmat.get());
    ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
  }
  {
    std::unique_ptr<Learner> learner {Learner::Create(mat)};
    learner->SetParams({Arg{"tree_method", "gpu_hist"}});
    learner->UpdateOneIter(0, p_dmat.get());
    ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
  }
  {
    std::unique_ptr<Learner> learner {Learner::Create(mat)};
    learner->SetParams({Arg{"tree_method", "hist"}});
    learner->UpdateOneIter(0, p_dmat.get());
    ASSERT_EQ(learner->GetGenericParameter().gpu_id, -1);
  }
  {
    , but `gpu_id` takes priority
    std::unique_ptr<Learner> learner {Learner::Create(mat)};
    learner->SetParams({Arg{"tree_method", "hist"},
                        Arg{"gpu_id", "0"}});
    learner->UpdateOneIter(0, p_dmat.get());
    ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
  }
  {
    std::unique_ptr<Learner> learner {Learner::Create(mat)};
    learner->SetParams({Arg{"tree_method", "hist"},
                        Arg{"predictor", "gpu_predictor"}});
    learner->UpdateOneIter(0, p_dmat.get());
    ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
  }
  delete pp_dmat;
}
TEST ( Learner , GPUConfiguration ) { using Arg = std :: pair < std :: string , std :: string > ; size_t constexpr kRows = 10 ; auto pp_dmat = CreateDMatrix ( kRows , 10 , 0 ) ; auto p_dmat = * pp_dmat ; std :: vector < std :: shared_ptr < DMatrix > > mat { p_dmat } ; std :: vector < bst_float > labels ( kRows ) ; for ( size_t i = 0 ; i < labels . size ( ) ; ++ i ) { labels [ i ] = i ; } p_dmat -> Info ( ) . labels_ . HostVector ( ) = labels ; { std :: unique_ptr < Learner > learner { Learner :: Create ( mat ) } ; learner -> SetParams ( { Arg { " " , " " } , Arg { " " , " " } } ) ; learner -> UpdateOneIter ( 0 , p_dmat . get ( ) ) ; ASSERT_EQ ( learner -> GetGenericParameter ( ) . gpu_id , 0 ) ; } { std :: unique_ptr < Learner > learner { Learner :: Create ( mat ) } ; learner -> SetParams ( { Arg { " " , " " } } ) ; learner -> UpdateOneIter ( 0 , p_dmat . get ( ) ) ; ASSERT_EQ ( learner -> GetGenericParameter ( ) . gpu_id , 0 ) ; } { std :: unique_ptr < Learner > learner { Learner :: Create ( mat ) } ; learner -> SetParams ( { Arg { " " , " " } } ) ; learner -> UpdateOneIter ( 0 , p_dmat . get ( ) ) ; ASSERT_EQ ( learner -> GetGenericParameter ( ) . gpu_id , -1 ) ; } { std :: unique_ptr < Learner > learner { Learner :: Create ( mat ) } ; learner -> SetParams ( { Arg { " " , " " } , Arg { " " , " " } } ) ; learner -> UpdateOneIter ( 0 , p_dmat . get ( ) ) ; ASSERT_EQ ( learner -> GetGenericParameter ( ) . gpu_id , 0 ) ; } { std :: unique_ptr < Learner > learner { Learner :: Create ( mat ) } ; learner -> SetParams ( { Arg { " " , " " } , Arg { " " , " " } } ) ; learner -> UpdateOneIter ( 0 , p_dmat . get ( ) ) ; ASSERT_EQ ( learner -> GetGenericParameter ( ) . gpu_id , 0 ) ; } delete pp_dmat ; }
Tests for automatic GPU configuration.
Tests for automatic GPU configuration .
// with CPU algorithm // with CPU algorithm, but `gpu_id` takes priority // With CPU algorithm but GPU Predictor, this is to simulate when // XGBoost is only used for prediction, so tree method is not // specified.