From 46d41a2b43fede00dea91fac4548bf36eb169ade Mon Sep 17 00:00:00 2001 From: "tqchen@graphlab.com" Date: Sun, 24 Aug 2014 09:32:06 -0700 Subject: [PATCH] fix compilation on mac --- Makefile | 9 ++++++--- wrapper/R-example/demo.R | 4 ++-- wrapper/xgboost.R | 8 ++++---- wrapper/xgboost_R.cpp | 8 +++++--- wrapper/xgboost_wrapper.cpp | 1 + 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 942e3239e..6dcd1efe4 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,6 @@ export CXX = g++ export LDFLAGS= -pthread -lm # note for R module # add include path to Rinternals.h here -export CPLUS_INCLUDE_PATH=/usr/share/R/include ifeq ($(no_omp),1) export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -DDISABLE_OPENMP @@ -14,7 +13,8 @@ endif # specify tensor path BIN = xgboost OBJ = -SLIB = wrapper/libxgboostwrapper.so wrapper/libxgboostR.so +SLIB = wrapper/libxgboostwrapper.so +RLIB = wrapper/libxgboostR.so .PHONY: clean all R all: $(BIN) wrapper/libxgboostwrapper.so @@ -31,6 +31,9 @@ $(BIN) : $(SLIB) : $(CXX) $(CFLAGS) -fPIC $(LDFLAGS) -shared -o $@ $(filter %.cpp %.o %.c, $^) +$(RLIB) : + R CMD SHLIB -c -o $@ $(filter %.cpp %.o %.c, $^) + $(OBJ) : $(CXX) -c $(CFLAGS) -o $@ $(firstword $(filter %.cpp %.c, $^) ) @@ -38,4 +41,4 @@ install: cp -f -r $(BIN) $(INSTALL_PATH) clean: - $(RM) $(OBJ) $(BIN) $(SLIB) *~ */*~ */*/*~ + $(RM) $(OBJ) $(BIN) $(SLIB) $(RLIB) *~ */*~ */*/*~ diff --git a/wrapper/R-example/demo.R b/wrapper/R-example/demo.R index 55d323d9e..448194d03 100644 --- a/wrapper/R-example/demo.R +++ b/wrapper/R-example/demo.R @@ -35,7 +35,7 @@ bst <- xgb.train(param, dtrain, nround=2, watchlist=watchlist) # make prediction preds <- xgb.predict(bst, dtest) labels <- xgb.getinfo(dtest, "label") -err <- as.real(sum(as.integer(preds > 0.5) != labels)) / length(labels) +err <- as.numeric(sum(as.integer(preds > 0.5) != labels)) / length(labels) # print error rate print(paste("error=",err)) @@ -100,7 +100,7 @@ logregobj <- function(preds, dtrain) { # Take this in mind when you use the customization, and maybe you need write customized evaluation function evalerror <- function(preds, dtrain) { labels <- xgb.getinfo(dtrain, "label") - err <- as.real(sum(labels != (preds > 0.0))) / length(labels) + err <- as.numeric(sum(labels != (preds > 0.0))) / length(labels) return(list(metric="error", value=err)) } diff --git a/wrapper/xgboost.R b/wrapper/xgboost.R index 962376027..ae42f66c3 100644 --- a/wrapper/xgboost.R +++ b/wrapper/xgboost.R @@ -48,15 +48,15 @@ xgb.setinfo <- function(dmat, name, info) { stop("xgb.setinfo: first argument dtrain must be xgb.DMatrix"); } if (name == "label") { - .Call("XGDMatrixSetInfo_R", dmat, name, as.real(info)) + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info)) return(TRUE) } if (name == "weight") { - .Call("XGDMatrixSetInfo_R", dmat, name, as.real(info)) + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info)) return(TRUE) } if (name == "base_margin") { - .Call("XGDMatrixSetInfo_R", dmat, name, as.real(info)) + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info)) return(TRUE) } if (name == "group") { @@ -214,7 +214,7 @@ xgb.iter.eval <- function(booster, watchlist, iter) { if (length(names(w)) == 0) { stop("xgb.eval: name tag must be presented for every elements in watchlist") } - evnames <- append(evnames, names(w)) + evnames <- append(evnames, names(w)) } } msg <- .Call("XGBoosterEvalOneIter_R", booster, as.integer(iter), watchlist, evnames) diff --git a/wrapper/xgboost_R.cpp b/wrapper/xgboost_R.cpp index 98f31d7c2..6f342a208 100644 --- a/wrapper/xgboost_R.cpp +++ b/wrapper/xgboost_R.cpp @@ -165,14 +165,16 @@ extern "C" { std::vector vec_dmats; std::vector vec_names; std::vector vec_sptr; - for (int i = 0; i < len; ++i){ + for (int i = 0; i < len; ++i) { vec_dmats.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i))); vec_names.push_back(std::string(CHAR(asChar(VECTOR_ELT(evnames, i))))); - vec_sptr.push_back(vec_names.back().c_str()); + } + for (int i = 0; i < len; ++i) { + vec_sptr.push_back(vec_names[i].c_str()); } return mkString(XGBoosterEvalOneIter(R_ExternalPtrAddr(handle), asInteger(iter), - &vec_dmats[0], &vec_sptr[0], len)); + &vec_dmats[0], &vec_sptr[0], len)); } SEXP XGBoosterPredict_R(SEXP handle, SEXP dmat, SEXP output_margin) { size_t olen; diff --git a/wrapper/xgboost_wrapper.cpp b/wrapper/xgboost_wrapper.cpp index 7bc25eb40..35e73a589 100644 --- a/wrapper/xgboost_wrapper.cpp +++ b/wrapper/xgboost_wrapper.cpp @@ -225,6 +225,7 @@ extern "C"{ for (size_t i = 0; i < len; ++i) { mats.push_back(static_cast(dmats[i])); names.push_back(std::string(evnames[i])); + printf("add names: %s\n", names.back().c_str()); } bst->CheckInitModel(); bst->eval_str = bst->EvalOneIter(iter, mats, names);