From c78a2164c237cbac0d48b0f9e0deb49e5e8bb7ec Mon Sep 17 00:00:00 2001 From: "tqchen@graphlab.com" Date: Mon, 25 Aug 2014 11:34:49 -0700 Subject: [PATCH 1/3] fix line from auto spacing by msvc --- src/gbm/gblinear-inl.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gbm/gblinear-inl.hpp b/src/gbm/gblinear-inl.hpp index e77edf884..4f9bd0707 100644 --- a/src/gbm/gblinear-inl.hpp +++ b/src/gbm/gblinear-inl.hpp @@ -60,7 +60,8 @@ class GBLinear : public IGradBooster { } } // remove bias effect - bst_float dw = static_cast(param.learning_rate * param.CalcDeltaBias(sum_grad, sum_hess, model.bias()[gid])); + bst_float dw = static_cast( + param.learning_rate * param.CalcDeltaBias(sum_grad, sum_hess, model.bias()[gid])); model.bias()[gid] += dw; // update grad value #pragma omp parallel for schedule(static) From 6da62159d08009d7bf6def314e4e3317d889d4b5 Mon Sep 17 00:00:00 2001 From: "tqchen@graphlab.com" Date: Mon, 25 Aug 2014 12:10:45 -0700 Subject: [PATCH 2/3] fix by giulio --- src/data.h | 4 ++-- src/tree/model.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/data.h b/src/data.h index f85b69034..1c9d9a290 100644 --- a/src/data.h +++ b/src/data.h @@ -373,8 +373,8 @@ class FMatrixS : public FMatrixInterface{ unsigned ncol = static_cast(this->NumCol()); #pragma omp parallel for schedule(static) for (unsigned i = 0; i < ncol; ++i) { - std::sort(&col_data_[col_ptr_[i]], - &col_data_[col_ptr_[i + 1]], Entry::CmpValue); + std::sort(&col_data_[0] + col_ptr_[i], + &col_data_[0] + col_ptr_[i + 1], Entry::CmpValue); } } diff --git a/src/tree/model.h b/src/tree/model.h index af99a5145..650e6b305 100644 --- a/src/tree/model.h +++ b/src/tree/model.h @@ -259,10 +259,12 @@ class TreeModel { } /*! \brief get leaf vector given nid */ inline bst_float* leafvec(int nid) { + if (leaf_vector.size() == 0) return NULL; return &leaf_vector[nid * param.size_leaf_vector]; } /*! \brief get leaf vector given nid */ inline const bst_float* leafvec(int nid) const{ + if (leaf_vector.size() == 0) return NULL; return &leaf_vector[nid * param.size_leaf_vector]; } /*! \brief initialize the model */ From c2484f3134f006ce39d6858e4169e3ad99ebb923 Mon Sep 17 00:00:00 2001 From: "tqchen@graphlab.com" Date: Mon, 25 Aug 2014 15:58:52 -0700 Subject: [PATCH 3/3] better error handling --- Makefile | 2 +- src/utils/utils.h | 56 +++++++++++++++++++++++++++++-------------- wrapper/xgboost_R.cpp | 13 +++++++++- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 80848649d..a7c7912ce 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ else endif # expose these flags to R CMD SHLIB -export PKG_CPPFLAGS = $(CFLAGS) +export PKG_CPPFLAGS = $(CFLAGS) -DXGBOOST_CUSTOMIZE_ERROR_ # specify tensor path BIN = xgboost diff --git a/src/utils/utils.h b/src/utils/utils.h index baaac5de2..085725486 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -11,14 +11,10 @@ #include #ifdef _MSC_VER #define fopen64 fopen -// temporal solution for MSVC -inline int snprintf(char *ptr, size_t sz, const char *fmt, ...) { - va_list args; - va_start(args, fmt); - int ret = vsprintf(ptr, fmt, args); - va_end(args); - return ret; -} +// NOTE: sprintf_s is not equivalent to snprintf, +// they are equivalent when success, which is sufficient for our case +#define snprintf sprintf_s +#define vsnprintf vsprintf_s #else #ifdef _FILE_OFFSET_BITS #if _FILE_OFFSET_BITS == 32 @@ -50,41 +46,65 @@ typedef long int64_t; namespace xgboost { /*! \brief namespace for helper utils of the project */ namespace utils { +/*! \brief error message buffer length */ +const int kErrorBuffer = 1 << 12; + +#ifndef XGBOOST_CUSTOMIZE_ERROR_ +/*! + * \brief handling of Assert error, caused by in-apropriate input + * \param msg error message + */ +inline void HandleAssertError(const char *msg) { + fprintf(stderr, "AssertError:%s\n", msg); + exit(-1); +} +/*! + * \brief handling of Check error, caused by in-apropriate input + * \param msg error message + */ +inline void HandleCheckError(const char *msg) { + fprintf(stderr, "%s\n", msg); + exit(-1); +} +#else +// include declarations, some one must implement this +void HandleAssertError(const char *msg); +void HandleCheckError(const char *msg); +#endif /*! \brief assert an condition is true, use this to handle debug information */ inline void Assert(bool exp, const char *fmt, ...) { if (!exp) { + std::string msg(kErrorBuffer, '\0'); va_list args; va_start(args, fmt); - fprintf(stderr, "AssertError:"); - vfprintf(stderr, fmt, args); + vsnprintf(&msg[0], kErrorBuffer, fmt, args); va_end(args); - fprintf(stderr, "\n"); - exit(-1); + HandleAssertError(msg.c_str()); } } /*!\brief same as assert, but this is intended to be used as message for user*/ inline void Check(bool exp, const char *fmt, ...) { if (!exp) { + std::string msg(kErrorBuffer, '\0'); va_list args; va_start(args, fmt); - vfprintf(stderr, fmt, args); + vsnprintf(&msg[0], kErrorBuffer, fmt, args); va_end(args); - fprintf(stderr, "\n"); - exit(-1); + HandleCheckError(msg.c_str()); } } /*! \brief report error message, same as check */ inline void Error(const char *fmt, ...) { { + std::string msg(kErrorBuffer, '\0'); va_list args; va_start(args, fmt); - vfprintf(stderr, fmt, args); + vsnprintf(&msg[0], kErrorBuffer, fmt, args); va_end(args); - fprintf(stderr, "\n"); - exit(-1); + HandleCheckError(msg.c_str()); } } diff --git a/wrapper/xgboost_R.cpp b/wrapper/xgboost_R.cpp index 6f342a208..65085c885 100644 --- a/wrapper/xgboost_R.cpp +++ b/wrapper/xgboost_R.cpp @@ -2,13 +2,24 @@ #include #include #include -#include "xgboost_wrapper.h" #include "xgboost_R.h" +#include "xgboost_wrapper.h" #include "../src/utils/utils.h" #include "../src/utils/omp.h" #include "../src/utils/matrix_csr.h" using namespace xgboost; +// implements error handling +namespace xgboost { +namespace utils { +void HandleAssertError(const char *msg) { + error("%s", msg); +} +void HandleCheckError(const char *msg) { + error("%s", msg); +} +} // namespace utils +} // namespace xgboost extern "C" { void _DMatrixFinalizer(SEXP ext) {