Skip to content

Commit 6cd56e7

Browse files
author
Montana Low
committed
update for 2021 edition
1 parent 1da1c03 commit 6cd56e7

File tree

7 files changed

+48
-41
lines changed

7 files changed

+48
-41
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

@@ -340,13 +340,16 @@ impl Booster {
340340
let mut out_len = 0;
341341
let mut out = ptr::null_mut();
342342
xgb_call!(xgboost_sys::XGBoosterGetAttrNames(self.handle, &mut out_len, &mut out))?;
343-
344-
let out_ptr_slice = unsafe { slice::from_raw_parts(out, out_len as usize) };
345-
let out_vec = out_ptr_slice
346-
.iter()
347-
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
348-
.collect();
349-
Ok(out_vec)
343+
if out_len > 0 {
344+
let out_ptr_slice = unsafe { slice::from_raw_parts(out, out_len as usize) };
345+
let out_vec = out_ptr_slice
346+
.iter()
347+
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
348+
.collect();
349+
Ok(out_vec)
350+
} else {
351+
Ok(Vec::new())
352+
}
350353
}
351354

352355
/// Predict results for given data.
@@ -492,7 +495,7 @@ impl Booster {
492495
Err(err) => return Err(XGBError::new(err.to_string())),
493496
};
494497

495-
let file_path = tmp_dir.path().join("fmap.txt");
498+
let file_path = tmp_dir.path().join("fmap.json");
496499
let mut file: File = match File::create(&file_path) {
497500
Ok(f) => f,
498501
Err(err) => return Err(XGBError::new(err.to_string())),
@@ -526,14 +529,18 @@ impl Booster {
526529
&mut out_dump_array
527530
))?;
528531

529-
let out_ptr_slice = unsafe { slice::from_raw_parts(out_dump_array, out_len as usize) };
530-
let out_vec: Vec<String> = out_ptr_slice
531-
.iter()
532-
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
533-
.collect();
532+
if out_len > 0 {
533+
let out_ptr_slice = unsafe { slice::from_raw_parts(out_dump_array, out_len as usize) };
534+
let out_vec: Vec<String> = out_ptr_slice
535+
.iter()
536+
.map(|str_ptr| unsafe { ffi::CStr::from_ptr(*str_ptr).to_str().unwrap().to_owned() })
537+
.collect();
534538

535-
assert_eq!(out_len as usize, out_vec.len());
536-
Ok(out_vec.join("\n"))
539+
assert_eq!(out_len as usize, out_vec.len());
540+
Ok(out_vec.join("\n"))
541+
} else {
542+
Ok(String::new())
543+
}
537544
}
538545

539546
pub fn set_param(&mut self, name: &str, value: &str) -> XGBResult<()> {
@@ -686,7 +693,7 @@ impl fmt::Display for FeatureType {
686693
#[cfg(test)]
687694
mod tests {
688695
use super::*;
689-
use parameters::{self, learning, tree};
696+
use crate::parameters::{self, learning, tree};
690697

691698
fn read_train_matrix() -> XGBResult<DMatrix> {
692699
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 & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,24 @@ fn main() {
2222
}
2323

2424
// CMake
25+
let mut dst = Config::new(&xgb_root);
26+
let mut dst = dst.define("BUILD_STATIC_LIB", "ON");
27+
2528
#[cfg(feature = "cuda")]
26-
let dst = Config::new(&xgb_root)
27-
.uses_cxx11()
28-
.define("BUILD_STATIC_LIB", "ON")
29+
let mut dst = dst
2930
.define("USE_CUDA", "ON")
3031
.define("BUILD_WITH_CUDA", "ON")
3132
.define("BUILD_WITH_CUDA_CUB", "ON");
3233

33-
#[cfg(not(feature = "cuda"))]
34-
let mut dst = Config::new(&xgb_root);
35-
36-
let mut dst = dst.uses_cxx11()
37-
.define("BUILD_STATIC_LIB", "ON");
38-
3934
#[cfg(target_os = "macos")]
4035
{
4136
let path = PathBuf::from("/opt/homebrew/"); // check for m1 vs intel config
4237
if let Ok(_dir) = std::fs::read_dir(&path) {
43-
dst =
44-
dst
45-
.define("CMAKE_C_COMPILER", "/opt/homebrew/opt/llvm/bin/clang")
46-
.define("CMAKE_CXX_COMPILER", "/opt/homebrew/opt/llvm/bin/clang++")
47-
.define("OPENMP_LIBRARIES", "/opt/homebrew/opt/llvm/lib")
48-
.define("OPENMP_INCLUDES", "/opt/homebrew/opt/llvm/include");
38+
dst = dst
39+
.define("CMAKE_C_COMPILER", "/opt/homebrew/opt/llvm/bin/clang")
40+
.define("CMAKE_CXX_COMPILER", "/opt/homebrew/opt/llvm/bin/clang++")
41+
.define("OPENMP_LIBRARIES", "/opt/homebrew/opt/llvm/lib")
42+
.define("OPENMP_INCLUDES", "/opt/homebrew/opt/llvm/include");
4943
};
5044
}
5145
let dst = dst.build();
@@ -67,11 +61,12 @@ fn main() {
6761

6862
#[cfg(feature = "cuda")]
6963
let bindings = bindings.clang_arg("-I/usr/local/cuda/include");
64+
7065
let bindings = bindings
7166
.generate()
7267
.expect("Unable to generate bindings.");
7368

74-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
69+
let out_path = PathBuf::from(out_dir);
7570
bindings
7671
.write_to_file(out_path.join("bindings.rs"))
7772
.expect("Couldn't write bindings.");

xgboost-sys/wrapper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
#include <xgboost/c_api.h>
2-
#include <rabit/c_api.h>

0 commit comments

Comments
 (0)