some fix to make it more c++
This commit is contained in:
parent
50f1b5d903
commit
42fb7b4d9d
2
Makefile
2
Makefile
@ -2,7 +2,7 @@ export CC = gcc
|
|||||||
export CXX = g++
|
export CXX = g++
|
||||||
export LDFLAGS= -pthread -lm
|
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)
|
ifeq ($(no_omp),1)
|
||||||
CFLAGS += -DDISABLE_OPENMP
|
CFLAGS += -DDISABLE_OPENMP
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include "src/utils/utils.h"
|
#include "src/utils/utils.h"
|
||||||
#include "src/utils/omp.h"
|
#include "src/utils/omp.h"
|
||||||
#include "src/utils/matrix_csr.h"
|
#include "src/utils/matrix_csr.h"
|
||||||
|
using namespace std;
|
||||||
using namespace xgboost;
|
using namespace xgboost;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -18,10 +19,12 @@ extern "C" {
|
|||||||
// implements error handling
|
// implements error handling
|
||||||
namespace xgboost {
|
namespace xgboost {
|
||||||
namespace utils {
|
namespace utils {
|
||||||
void (*Printf)(const char *fmt, ...) = Rprintf;
|
extern "C" {
|
||||||
void (*Assert)(int exp, const char *fmt, ...) = XGBoostAssert_R;
|
void (*Printf)(const char *fmt, ...) = Rprintf;
|
||||||
void (*Check)(int exp, const char *fmt, ...) = XGBoostCheck_R;
|
void (*Assert)(int exp, const char *fmt, ...) = XGBoostAssert_R;
|
||||||
void (*Error)(const char *fmt, ...) = error;
|
void (*Check)(int exp, const char *fmt, ...) = XGBoostCheck_R;
|
||||||
|
void (*Error)(const char *fmt, ...) = error;
|
||||||
|
}
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
|
|
||||||
namespace random {
|
namespace random {
|
||||||
|
|||||||
@ -8,8 +8,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <climits>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <climits>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "./evaluation.h"
|
#include "./evaluation.h"
|
||||||
#include "./helper_utils.h"
|
#include "./helper_utils.h"
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cmath>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
namespace xgboost {
|
namespace xgboost {
|
||||||
namespace learner {
|
namespace learner {
|
||||||
|
|||||||
@ -6,9 +6,9 @@
|
|||||||
* \author Tianqi Chen, Kailong Chen
|
* \author Tianqi Chen, Kailong Chen
|
||||||
*/
|
*/
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cmath>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <cmath>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "../data.h"
|
#include "../data.h"
|
||||||
#include "./objective.h"
|
#include "./objective.h"
|
||||||
@ -37,7 +37,7 @@ struct LossType {
|
|||||||
case kLogisticRaw:
|
case kLogisticRaw:
|
||||||
case kLinearSquare: return x;
|
case kLinearSquare: return x;
|
||||||
case kLogisticClassify:
|
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;
|
default: utils::Error("unknown loss_type"); return 0.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ struct LossType {
|
|||||||
inline float FirstOrderGradient(float predt, float label) const {
|
inline float FirstOrderGradient(float predt, float label) const {
|
||||||
switch (loss_type) {
|
switch (loss_type) {
|
||||||
case kLinearSquare: return predt - label;
|
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 kLogisticClassify:
|
||||||
case kLogisticNeglik: return predt - label;
|
case kLogisticNeglik: return predt - label;
|
||||||
default: utils::Error("unknown loss_type"); return 0.0f;
|
default: utils::Error("unknown loss_type"); return 0.0f;
|
||||||
@ -65,7 +65,7 @@ struct LossType {
|
|||||||
inline float SecondOrderGradient(float predt, float label) const {
|
inline float SecondOrderGradient(float predt, float label) const {
|
||||||
switch (loss_type) {
|
switch (loss_type) {
|
||||||
case kLinearSquare: return 1.0f;
|
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 kLogisticClassify:
|
||||||
case kLogisticNeglik: return predt * (1 - predt);
|
case kLogisticNeglik: return predt * (1 - predt);
|
||||||
default: utils::Error("unknown loss_type"); return 0.0f;
|
default: utils::Error("unknown loss_type"); return 0.0f;
|
||||||
@ -80,7 +80,7 @@ struct LossType {
|
|||||||
loss_type == kLogisticNeglik ) {
|
loss_type == kLogisticNeglik ) {
|
||||||
utils::Check(base_score > 0.0f && base_score < 1.0f,
|
utils::Check(base_score > 0.0f && base_score < 1.0f,
|
||||||
"base_score must be in (0,1) for logistic loss");
|
"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;
|
return base_score;
|
||||||
}
|
}
|
||||||
@ -419,8 +419,8 @@ class LambdaRankObjNDCG : public LambdaRankObj {
|
|||||||
for (size_t i = 0; i < pairs.size(); ++i) {
|
for (size_t i = 0; i < pairs.size(); ++i) {
|
||||||
unsigned pos_idx = pairs[i].pos_index;
|
unsigned pos_idx = pairs[i].pos_index;
|
||||||
unsigned neg_idx = pairs[i].neg_index;
|
unsigned neg_idx = pairs[i].neg_index;
|
||||||
float pos_loginv = 1.0f / logf(pos_idx + 2.0f);
|
float pos_loginv = 1.0f / std::log(pos_idx + 2.0f);
|
||||||
float neg_loginv = 1.0f / logf(neg_idx + 2.0f);
|
float neg_loginv = 1.0f / std::log(neg_idx + 2.0f);
|
||||||
int pos_label = static_cast<int>(sorted_list[pos_idx].label);
|
int pos_label = static_cast<int>(sorted_list[pos_idx].label);
|
||||||
int neg_label = static_cast<int>(sorted_list[neg_idx].label);
|
int neg_label = static_cast<int>(sorted_list[neg_idx].label);
|
||||||
float original =
|
float original =
|
||||||
@ -438,7 +438,7 @@ class LambdaRankObjNDCG : public LambdaRankObj {
|
|||||||
for (size_t i = 0; i < labels.size(); ++i) {
|
for (size_t i = 0; i < labels.size(); ++i) {
|
||||||
const unsigned rel = static_cast<unsigned>(labels[i]);
|
const unsigned rel = static_cast<unsigned>(labels[i]);
|
||||||
if (rel != 0) {
|
if (rel != 0) {
|
||||||
sumdcg += ((1 << rel) - 1) / logf(static_cast<float>(i + 2));
|
sumdcg += ((1 << rel) - 1) / std::log(static_cast<float>(i + 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return static_cast<float>(sumdcg);
|
return static_cast<float>(sumdcg);
|
||||||
|
|||||||
@ -91,7 +91,7 @@ struct Random{
|
|||||||
// use rand instead of rand_r in windows, for MSVC it is fine since rand is threadsafe
|
// 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
|
// 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
|
// todo, replace with another PRNG
|
||||||
#if defined(_MSC_VER)||defined(_WIN32)
|
#if defined(_MSC_VER)||defined(_WIN32)||defined(XGBOOST_STRICT_CXX98_)
|
||||||
return Uniform();
|
return Uniform();
|
||||||
#else
|
#else
|
||||||
return static_cast<double>(rand_r(&rseed)) / (static_cast<double>(RAND_MAX) + 1.0);
|
return static_cast<double>(rand_r(&rseed)) / (static_cast<double>(RAND_MAX) + 1.0);
|
||||||
|
|||||||
@ -15,7 +15,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(__GNUC__)
|
#if !defined(__GNUC__)
|
||||||
#define fopen64 fopen
|
#define fopen64 std::fopen
|
||||||
#endif
|
#endif
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
// NOTE: sprintf_s is not equivalent to snprintf,
|
// NOTE: sprintf_s is not equivalent to snprintf,
|
||||||
@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
#define off64_t off_t
|
#define off64_t off_t
|
||||||
#define fopen64 fopen
|
#define fopen64 std::fopen
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -52,6 +52,7 @@ typedef long int64_t;
|
|||||||
namespace xgboost {
|
namespace xgboost {
|
||||||
/*! \brief namespace for helper utils of the project */
|
/*! \brief namespace for helper utils of the project */
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
|
||||||
/*! \brief error message buffer length */
|
/*! \brief error message buffer length */
|
||||||
const int kPrintBuffer = 1 << 12;
|
const int kPrintBuffer = 1 << 12;
|
||||||
|
|
||||||
@ -86,10 +87,10 @@ void HandlePrint(const char *msg);
|
|||||||
|
|
||||||
#ifdef XGBOOST_STRICT_CXX98_
|
#ifdef XGBOOST_STRICT_CXX98_
|
||||||
// these function pointers are to be assigned
|
// these function pointers are to be assigned
|
||||||
extern void (*Printf)(const char *fmt, ...);
|
extern "C" void (*Printf)(const char *fmt, ...);
|
||||||
extern void (*Assert)(int exp, const char *fmt, ...);
|
extern "C" void (*Assert)(int exp, const char *fmt, ...);
|
||||||
extern void (*Check)(int exp, const char *fmt, ...);
|
extern "C" void (*Check)(int exp, const char *fmt, ...);
|
||||||
extern void (*Error)(const char *fmt, ...);
|
extern "C" void (*Error)(const char *fmt, ...);
|
||||||
#else
|
#else
|
||||||
/*! \brief printf, print message to the console */
|
/*! \brief printf, print message to the console */
|
||||||
inline void Printf(const char *fmt, ...) {
|
inline void Printf(const char *fmt, ...) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user