From 42fb7b4d9d218d00ad74f6b7ad79875a837e874f Mon Sep 17 00:00:00 2001 From: tqchen Date: Mon, 1 Sep 2014 22:06:10 -0700 Subject: [PATCH] some fix to make it more c++ --- Makefile | 2 +- R-package/src/xgboost_R.cpp | 11 +++++++---- src/learner/evaluation-inl.hpp | 2 +- src/learner/helper_utils.h | 1 + src/learner/objective-inl.hpp | 16 ++++++++-------- src/utils/random.h | 2 +- src/utils/utils.h | 13 +++++++------ 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 2fd881843..3705bc4a5 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ export CC = gcc export CXX = g++ export LDFLAGS= -pthread -lm -export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -pedantic -ansi +export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -pedantic -std=c++98 -Wpedantic -pedantic-errors ifeq ($(no_omp),1) CFLAGS += -DDISABLE_OPENMP diff --git a/R-package/src/xgboost_R.cpp b/R-package/src/xgboost_R.cpp index 2f1a8b772..ac5934d17 100644 --- a/R-package/src/xgboost_R.cpp +++ b/R-package/src/xgboost_R.cpp @@ -8,6 +8,7 @@ #include "src/utils/utils.h" #include "src/utils/omp.h" #include "src/utils/matrix_csr.h" +using namespace std; using namespace xgboost; extern "C" { @@ -18,10 +19,12 @@ extern "C" { // implements error handling namespace xgboost { namespace utils { -void (*Printf)(const char *fmt, ...) = Rprintf; -void (*Assert)(int exp, const char *fmt, ...) = XGBoostAssert_R; -void (*Check)(int exp, const char *fmt, ...) = XGBoostCheck_R; -void (*Error)(const char *fmt, ...) = error; +extern "C" { + void (*Printf)(const char *fmt, ...) = Rprintf; + void (*Assert)(int exp, const char *fmt, ...) = XGBoostAssert_R; + void (*Check)(int exp, const char *fmt, ...) = XGBoostCheck_R; + void (*Error)(const char *fmt, ...) = error; +} } // namespace utils namespace random { diff --git a/src/learner/evaluation-inl.hpp b/src/learner/evaluation-inl.hpp index 4f34a7ac4..52877e17b 100644 --- a/src/learner/evaluation-inl.hpp +++ b/src/learner/evaluation-inl.hpp @@ -8,8 +8,8 @@ #include #include #include -#include #include +#include #include #include "./evaluation.h" #include "./helper_utils.h" diff --git a/src/learner/helper_utils.h b/src/learner/helper_utils.h index e2f8a3574..aa1e66bbc 100644 --- a/src/learner/helper_utils.h +++ b/src/learner/helper_utils.h @@ -7,6 +7,7 @@ */ #include #include +#include #include namespace xgboost { namespace learner { diff --git a/src/learner/objective-inl.hpp b/src/learner/objective-inl.hpp index 9e338a6b2..576549eac 100644 --- a/src/learner/objective-inl.hpp +++ b/src/learner/objective-inl.hpp @@ -6,9 +6,9 @@ * \author Tianqi Chen, Kailong Chen */ #include -#include #include #include +#include #include #include "../data.h" #include "./objective.h" @@ -37,7 +37,7 @@ struct LossType { case kLogisticRaw: case kLinearSquare: return x; case kLogisticClassify: - case kLogisticNeglik: return 1.0f / (1.0f + expf(-x)); + case kLogisticNeglik: return 1.0f / (1.0f + std::exp(-x)); default: utils::Error("unknown loss_type"); return 0.0f; } } @@ -50,7 +50,7 @@ struct LossType { inline float FirstOrderGradient(float predt, float label) const { switch (loss_type) { case kLinearSquare: return predt - label; - case kLogisticRaw: predt = 1.0f / (1.0f + expf(-predt)); + case kLogisticRaw: predt = 1.0f / (1.0f + std::exp(-predt)); case kLogisticClassify: case kLogisticNeglik: return predt - label; default: utils::Error("unknown loss_type"); return 0.0f; @@ -65,7 +65,7 @@ struct LossType { inline float SecondOrderGradient(float predt, float label) const { switch (loss_type) { case kLinearSquare: return 1.0f; - case kLogisticRaw: predt = 1.0f / (1.0f + expf(-predt)); + case kLogisticRaw: predt = 1.0f / (1.0f + std::exp(-predt)); case kLogisticClassify: case kLogisticNeglik: return predt * (1 - predt); default: utils::Error("unknown loss_type"); return 0.0f; @@ -80,7 +80,7 @@ struct LossType { loss_type == kLogisticNeglik ) { utils::Check(base_score > 0.0f && base_score < 1.0f, "base_score must be in (0,1) for logistic loss"); - base_score = -logf(1.0f / base_score - 1.0f); + base_score = -std::log(1.0f / base_score - 1.0f); } return base_score; } @@ -419,8 +419,8 @@ class LambdaRankObjNDCG : public LambdaRankObj { for (size_t i = 0; i < pairs.size(); ++i) { unsigned pos_idx = pairs[i].pos_index; unsigned neg_idx = pairs[i].neg_index; - float pos_loginv = 1.0f / logf(pos_idx + 2.0f); - float neg_loginv = 1.0f / logf(neg_idx + 2.0f); + float pos_loginv = 1.0f / std::log(pos_idx + 2.0f); + float neg_loginv = 1.0f / std::log(neg_idx + 2.0f); int pos_label = static_cast(sorted_list[pos_idx].label); int neg_label = static_cast(sorted_list[neg_idx].label); float original = @@ -438,7 +438,7 @@ class LambdaRankObjNDCG : public LambdaRankObj { for (size_t i = 0; i < labels.size(); ++i) { const unsigned rel = static_cast(labels[i]); if (rel != 0) { - sumdcg += ((1 << rel) - 1) / logf(static_cast(i + 2)); + sumdcg += ((1 << rel) - 1) / std::log(static_cast(i + 2)); } } return static_cast(sumdcg); diff --git a/src/utils/random.h b/src/utils/random.h index 590baca2a..57e1f243d 100644 --- a/src/utils/random.h +++ b/src/utils/random.h @@ -91,7 +91,7 @@ struct Random{ // use rand instead of rand_r in windows, for MSVC it is fine since rand is threadsafe // For cygwin and mingw, this can slows down parallelism, but rand_r is only used in objective-inl.hpp, won't affect speed in general // todo, replace with another PRNG -#if defined(_MSC_VER)||defined(_WIN32) +#if defined(_MSC_VER)||defined(_WIN32)||defined(XGBOOST_STRICT_CXX98_) return Uniform(); #else return static_cast(rand_r(&rseed)) / (static_cast(RAND_MAX) + 1.0); diff --git a/src/utils/utils.h b/src/utils/utils.h index 56a957df9..d2de82fbe 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -15,7 +15,7 @@ #endif #if !defined(__GNUC__) -#define fopen64 fopen +#define fopen64 std::fopen #endif #ifdef _MSC_VER // NOTE: sprintf_s is not equivalent to snprintf, @@ -31,7 +31,7 @@ #ifdef __APPLE__ #define off64_t off_t -#define fopen64 fopen +#define fopen64 std::fopen #endif extern "C" { @@ -52,6 +52,7 @@ typedef long int64_t; namespace xgboost { /*! \brief namespace for helper utils of the project */ namespace utils { + /*! \brief error message buffer length */ const int kPrintBuffer = 1 << 12; @@ -86,10 +87,10 @@ void HandlePrint(const char *msg); #ifdef XGBOOST_STRICT_CXX98_ // these function pointers are to be assigned -extern void (*Printf)(const char *fmt, ...); -extern void (*Assert)(int exp, const char *fmt, ...); -extern void (*Check)(int exp, const char *fmt, ...); -extern void (*Error)(const char *fmt, ...); +extern "C" void (*Printf)(const char *fmt, ...); +extern "C" void (*Assert)(int exp, const char *fmt, ...); +extern "C" void (*Check)(int exp, const char *fmt, ...); +extern "C" void (*Error)(const char *fmt, ...); #else /*! \brief printf, print message to the console */ inline void Printf(const char *fmt, ...) {