Skip to content

Commit ec745a4

Browse files
author
Montana Low
committed
update for 2021 edition
1 parent d851631 commit ec745a4

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ homepage = "https://github.com/davechallis/rust-xgboost"
88
description = "Machine learning using XGBoost"
99
documentation = "https://docs.rs/xgboost"
1010
readme = "README.md"
11+
edition = "2021"
1112

1213
[dependencies]
1314
xgboost-sys = { path = "xgboost-sys" }
1415
libc = "0.2"
15-
derive_builder = "0.12"
16+
derive_builder = "0.20"
1617
log = "0.4"
17-
tempfile = "3.9"
18-
indexmap = "2.1"
18+
tempfile = "3.15"
19+
indexmap = "2.7"
1920

2021
[features]
2122
cuda = ["xgboost-sys/cuda"]

src/booster.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use dmatrix::DMatrix;
2-
use error::XGBError;
1+
use crate::dmatrix::DMatrix;
2+
use crate::error::XGBError;
33
use libc;
44
use std::collections::{BTreeMap, HashMap};
55
use std::io::{self, BufRead, BufReader, Write};
@@ -13,7 +13,7 @@ use tempfile;
1313
use xgboost_sys;
1414

1515
use super::XGBResult;
16-
use parameters::{BoosterParameters, TrainingParameters};
16+
use crate::parameters::{BoosterParameters, TrainingParameters};
1717

1818
pub type CustomObjective = fn(&[f32], &DMatrix) -> (Vec<f32>, Vec<f32>);
1919

@@ -365,13 +365,16 @@ impl Booster {
365365
let mut out_len = 0;
366366
let mut out = ptr::null_mut();
367367
xgb_call!(xgboost_sys::XGBoosterGetAttrNames(self.handle, &mut out_len, &mut out))?;
368-
369-
let out_ptr_slice = unsafe { slice::from_raw_parts(out, out_len as usize) };
370-
let out_vec = out_ptr_slice
371-
.iter()
372-
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
373-
.collect();
374-
Ok(out_vec)
368+
if out_len > 0 {
369+
let out_ptr_slice = unsafe { slice::from_raw_parts(out, out_len as usize) };
370+
let out_vec = out_ptr_slice
371+
.iter()
372+
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
373+
.collect();
374+
Ok(out_vec)
375+
} else {
376+
Ok(Vec::new())
377+
}
375378
}
376379

377380
/// Predict results for given data.
@@ -517,7 +520,7 @@ impl Booster {
517520
Err(err) => return Err(XGBError::new(err.to_string())),
518521
};
519522

520-
let file_path = tmp_dir.path().join("fmap.txt");
523+
let file_path = tmp_dir.path().join("fmap.json");
521524
let mut file: File = match File::create(&file_path) {
522525
Ok(f) => f,
523526
Err(err) => return Err(XGBError::new(err.to_string())),
@@ -551,14 +554,18 @@ impl Booster {
551554
&mut out_dump_array
552555
))?;
553556

554-
let out_ptr_slice = unsafe { slice::from_raw_parts(out_dump_array, out_len as usize) };
555-
let out_vec: Vec<String> = out_ptr_slice
556-
.iter()
557-
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
558-
.collect();
557+
if out_len > 0 {
558+
let out_ptr_slice = unsafe { slice::from_raw_parts(out_dump_array, out_len as usize) };
559+
let out_vec: Vec<String> = out_ptr_slice
560+
.iter()
561+
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
562+
.collect();
559563

560-
assert_eq!(out_len as usize, out_vec.len());
561-
Ok(out_vec.join("\n"))
564+
assert_eq!(out_len as usize, out_vec.len());
565+
Ok(out_vec.join("\n"))
566+
} else {
567+
Ok(String::new())
568+
}
562569
}
563570

564571
pub(crate) fn load_rabit_checkpoint(&self) -> XGBResult<i32> {
@@ -721,7 +728,7 @@ impl fmt::Display for FeatureType {
721728
#[cfg(test)]
722729
mod tests {
723730
use super::*;
724-
use parameters::{self, learning, tree};
731+
use crate::parameters::{self, learning, tree};
725732

726733
fn read_train_matrix() -> XGBResult<DMatrix> {
727734
DMatrix::load(r#"{"uri": "xgboost-sys/xgboost/demo/data/agaricus.txt.train?format=libsvm"}"#)

src/dmatrix.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ impl DMatrix {
314314
&mut out_dptr
315315
))?;
316316

317-
Ok(unsafe { slice::from_raw_parts(out_dptr as *mut c_float, out_len as usize) })
317+
if out_len > 0 {
318+
Ok(unsafe { slice::from_raw_parts(out_dptr as *mut c_float, out_len as usize) })
319+
} else {
320+
Err(XGBError::new( "error"))
321+
}
318322
}
319323

320324
fn set_float_info(&mut self, field: &str, array: &[f32]) -> XGBResult<()> {
File renamed without changes.

xgboost-sys/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ license = "MIT"
88
repository = "https://github.com/davechallis/rust-xgboost"
99
description = "Native bindings to the xgboost library"
1010
readme = "README.md"
11+
edition = "2021"
1112

1213
[dependencies]
1314
libc = "0.2"
1415

1516
[build-dependencies]
16-
bindgen = "0.69"
17+
bindgen = "0.71"
1718
cmake = "0.1"
1819

1920
[features]

xgboost-sys/build.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ fn main() {
2525
dst.define("BUILD_STATIC_LIB", "ON").define("CMAKE_CXX_STANDARD", "17");
2626

2727
// CMake
28+
let mut dst = Config::new(&xgb_root);
29+
let mut dst = dst.define("BUILD_STATIC_LIB", "ON");
30+
2831
#[cfg(feature = "cuda")]
29-
dst.define("USE_CUDA", "ON")
32+
let mut dst = dst
33+
.define("USE_CUDA", "ON")
3034
.define("BUILD_WITH_CUDA", "ON")
3135
.define("BUILD_WITH_CUDA_CUB", "ON");
3236

3337
#[cfg(target_os = "macos")]
3438
{
3539
let path = PathBuf::from("/opt/homebrew/"); // check for m1 vs intel config
3640
if let Ok(_dir) = std::fs::read_dir(&path) {
37-
dst.define("CMAKE_C_COMPILER", "/opt/homebrew/opt/llvm/bin/clang")
41+
dst = dst
42+
.define("CMAKE_C_COMPILER", "/opt/homebrew/opt/llvm/bin/clang")
3843
.define("CMAKE_CXX_COMPILER", "/opt/homebrew/opt/llvm/bin/clang++")
3944
.define("OPENMP_LIBRARIES", "/opt/homebrew/opt/llvm/lib")
4045
.define("OPENMP_INCLUDES", "/opt/homebrew/opt/llvm/include");
@@ -54,9 +59,11 @@ fn main() {
5459

5560
#[cfg(feature = "cuda")]
5661
let bindings = bindings.clang_arg("-I/usr/local/cuda/include");
57-
let bindings = bindings.generate().expect("Unable to generate bindings.");
62+
let bindings = bindings
63+
.generate()
64+
.expect("Unable to generate bindings.");
5865

59-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
66+
let out_path = PathBuf::from(out_dir);
6067
bindings
6168
.write_to_file(out_path.join("bindings.rs"))
6269
.expect("Couldn't write bindings.");

xgboost-sys/xgboost

Submodule xgboost updated 797 files

0 commit comments

Comments
 (0)