fix all utils

This commit is contained in:
tqchen 2015-07-03 18:44:01 -07:00
parent 0162bb7034
commit 1581de08da
11 changed files with 164 additions and 139 deletions

View File

@ -1,13 +1,16 @@
#ifndef XGBOOST_UTILS_BASE64_INL_H_
#define XGBOOST_UTILS_BASE64_INL_H_
/*! /*!
* Copyright 2014 by Contributors
* \file base64.h * \file base64.h
* \brief data stream support to input and output from/to base64 stream * \brief data stream support to input and output from/to base64 stream
* base64 is easier to store and pass as text format in mapreduce * base64 is easier to store and pass as text format in mapreduce
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_BASE64_INL_H_
#define XGBOOST_UTILS_BASE64_INL_H_
#include <cctype> #include <cctype>
#include <cstdio> #include <cstdio>
#include <string>
#include "./io.h" #include "./io.h"
namespace xgboost { namespace xgboost {
@ -15,7 +18,7 @@ namespace utils {
/*! \brief buffer reader of the stream that allows you to get */ /*! \brief buffer reader of the stream that allows you to get */
class StreamBufferReader { class StreamBufferReader {
public: public:
StreamBufferReader(size_t buffer_size) explicit StreamBufferReader(size_t buffer_size)
:stream_(NULL), :stream_(NULL),
read_len_(1), read_ptr_(1) { read_len_(1), read_ptr_(1) {
buffer_.resize(buffer_size); buffer_.resize(buffer_size);
@ -246,7 +249,7 @@ class Base64OutStream: public IStream {
int buf_top; int buf_top;
unsigned char buf[4]; unsigned char buf[4];
std::string out_buf; std::string out_buf;
const static size_t kBufferSize = 256; static const size_t kBufferSize = 256;
inline void PutChar(char ch) { inline void PutChar(char ch) {
out_buf += ch; out_buf += ch;
@ -260,5 +263,5 @@ class Base64OutStream: public IStream {
} }
}; };
} // namespace utils } // namespace utils
} // namespace rabit } // namespace xgboost
#endif // RABIT_LEARN_UTILS_BASE64_INL_H_ #endif // XGBOOST_UTILS_BASE64_INL_H_

View File

@ -1,11 +1,13 @@
#ifndef XGBOOST_UTILS_BITMAP_H_
#define XGBOOST_UTILS_BITMAP_H_
/*! /*!
* Copyright 2014 by Contributors
* \file bitmap.h * \file bitmap.h
* \brief a simple implement of bitmap * \brief a simple implement of bitmap
* NOTE: bitmap is only threadsafe per word access, remember this when using bitmap * NOTE: bitmap is only threadsafe per word access, remember this when using bitmap
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_BITMAP_H_
#define XGBOOST_UTILS_BITMAP_H_
#include <vector> #include <vector>
#include "./utils.h" #include "./utils.h"
#include "./omp.h" #include "./omp.h"
@ -63,4 +65,4 @@ struct BitMap {
}; };
} // namespace utils } // namespace utils
} // namespace xgboost } // namespace xgboost
#endif #endif // XGBOOST_UTILS_BITMAP_H_

View File

@ -1,10 +1,12 @@
#ifndef XGBOOST_UTILS_FMAP_H_
#define XGBOOST_UTILS_FMAP_H_
/*! /*!
* Copyright 2014 by Contributors
* \file fmap.h * \file fmap.h
* \brief helper class that holds the feature names and interpretations * \brief helper class that holds the feature names and interpretations
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_FMAP_H_
#define XGBOOST_UTILS_FMAP_H_
#include <vector> #include <vector>
#include <string> #include <string>
#include <cstring> #include <cstring>
@ -78,4 +80,4 @@ class FeatMap {
} // namespace utils } // namespace utils
} // namespace xgboost } // namespace xgboost
#endif // XGBOOST_FMAP_H_ #endif // XGBOOST_UTILS_FMAP_H_

View File

@ -111,5 +111,4 @@ struct ParallelGroupBuilder {
}; };
} // namespace utils } // namespace utils
} // namespace xgboost } // namespace xgboost
#endif #endif // XGBOOST_UTILS_GROUP_DATA_H_

View File

@ -1,11 +1,13 @@
#ifndef XGBOOST_UTILS_ITERATOR_H
#define XGBOOST_UTILS_ITERATOR_H
#include <cstdio>
/*! /*!
* Copyright 2014 by Contributors
* \file iterator.h * \file iterator.h
* \brief itertator interface * \brief itertator interface
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_ITERATOR_H_
#define XGBOOST_UTILS_ITERATOR_H_
#include <cstdio>
namespace xgboost { namespace xgboost {
namespace utils { namespace utils {
/*! /*!
@ -36,5 +38,5 @@ class IIterator {
} // namespace utils } // namespace utils
} // namespace xgboost } // namespace xgboost
#endif #endif // XGBOOST_UTILS_ITERATOR_H_

View File

@ -1,10 +1,12 @@
#ifndef XGBOOST_UTILS_MATH_H_
#define XGBOOST_UTILS_MATH_H_
/*! /*!
* Copyright 2014 by Contributors
* \file math.h * \file math.h
* \brief support additional math * \brief support additional math
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_MATH_H_
#define XGBOOST_UTILS_MATH_H_
#include <cmath> #include <cmath>
namespace xgboost { namespace xgboost {
@ -28,7 +30,8 @@ inline T LogGamma(T v) {
#if _MSC_VER >= 1800 #if _MSC_VER >= 1800
return lgamma(v); return lgamma(v);
#else #else
#pragma message ("Warning: lgamma function was not available until VS2013, poisson regression will be disabled") #pragma message("Warning: lgamma function was not available until VS2013"\
", poisson regression will be disabled")
utils::Error("lgamma function was not available until VS2013"); utils::Error("lgamma function was not available until VS2013");
return static_cast<T>(1.0); return static_cast<T>(1.0);
#endif #endif

View File

@ -1,16 +1,20 @@
#ifndef XGBOOST_UTILS_OMP_H_
#define XGBOOST_UTILS_OMP_H_
/*! /*!
* Copyright 2014 by Contributors
* \file omp.h * \file omp.h
* \brief header to handle OpenMP compatibility issues * \brief header to handle OpenMP compatibility issues
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_OMP_H_
#define XGBOOST_UTILS_OMP_H_
#if defined(_OPENMP) #if defined(_OPENMP)
#include <omp.h> #include <omp.h>
#else #else
#ifndef DISABLE_OPENMP #ifndef DISABLE_OPENMP
// use pragma message instead of warning // use pragma message instead of warning
#pragma message ("Warning: OpenMP is not available, xgboost will be compiled into single-thread code. Use OpenMP-enabled compiler to get benefit of multi-threading") #pragma message("Warning: OpenMP is not available,"\
"xgboost will be compiled into single-thread code."\
"Use OpenMP-enabled compiler to get benefit of multi-threading")
#endif #endif
inline int omp_get_thread_num() { return 0; } inline int omp_get_thread_num() { return 0; }
inline int omp_get_num_threads() { return 1; } inline int omp_get_num_threads() { return 1; }

View File

@ -1,10 +1,12 @@
#ifndef XGBOOST_UTILS_QUANTILE_H_
#define XGBOOST_UTILS_QUANTILE_H_
/*! /*!
* Copyright 2014 by Contributors
* \file quantile.h * \file quantile.h
* \brief util to compute quantiles * \brief util to compute quantiles
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_QUANTILE_H_
#define XGBOOST_UTILS_QUANTILE_H_
#include <cmath> #include <cmath>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
@ -124,7 +126,7 @@ struct WQSummary {
* \param qvalue the value we query for * \param qvalue the value we query for
* \param istart starting position * \param istart starting position
*/ */
inline Entry Query(DType qvalue, size_t &istart) const { inline Entry Query(DType qvalue, size_t &istart) const { // NOLINT(*)
while (istart < size && qvalue > data[istart].value) { while (istart < size && qvalue > data[istart].value) {
++istart; ++istart;
} }
@ -597,7 +599,7 @@ class QuantileSketchTemplate {
} }
/*! \brief save the data structure into stream */ /*! \brief save the data structure into stream */
template<typename TStream> template<typename TStream>
inline void Save(TStream &fo) const { inline void Save(TStream &fo) const { // NOLINT(*)
fo.Write(&(this->size), sizeof(this->size)); fo.Write(&(this->size), sizeof(this->size));
if (this->size != 0) { if (this->size != 0) {
fo.Write(this->data, this->size * sizeof(Entry)); fo.Write(this->data, this->size * sizeof(Entry));
@ -605,11 +607,12 @@ class QuantileSketchTemplate {
} }
/*! \brief load data structure from input stream */ /*! \brief load data structure from input stream */
template<typename TStream> template<typename TStream>
inline void Load(TStream &fi) { inline void Load(TStream &fi) { // NOLINT(*)
utils::Check(fi.Read(&this->size, sizeof(this->size)) != 0, "invalid SummaryArray 1"); utils::Check(fi.Read(&this->size, sizeof(this->size)) != 0, "invalid SummaryArray 1");
this->Reserve(this->size); this->Reserve(this->size);
if (this->size != 0) { if (this->size != 0) {
utils::Check(fi.Read(this->data, this->size * sizeof(Entry)) != 0, "invalid SummaryArray 2"); utils::Check(fi.Read(this->data, this->size * sizeof(Entry)) != 0,
"invalid SummaryArray 2");
} }
} }
}; };
@ -765,6 +768,6 @@ class GKQuantileSketch :
public QuantileSketchTemplate<DType, RType, GKSummary<DType, RType> >{ public QuantileSketchTemplate<DType, RType, GKSummary<DType, RType> >{
}; };
} // utils } // namespace utils
} // xgboost } // namespace xgboost
#endif #endif // XGBOOST_UTILS_QUANTILE_H_

View File

@ -1,12 +1,14 @@
#ifndef XGBOOST_UTILS_RANDOM_H_
#define XGBOOST_UTILS_RANDOM_H_
/*! /*!
* Copyright 2014 by Contributors
* \file xgboost_random.h * \file xgboost_random.h
* \brief PRNG to support random number generation * \brief PRNG to support random number generation
* \author Tianqi Chen: tianqi.tchen@gmail.com * \author Tianqi Chen: tianqi.tchen@gmail.com
* *
* Use standard PRNG from stdlib * Use standard PRNG from stdlib
*/ */
#ifndef XGBOOST_UTILS_RANDOM_H_
#define XGBOOST_UTILS_RANDOM_H_
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
@ -23,11 +25,11 @@ inline void Seed(unsigned seed) {
} }
/*! \brief basic function, uniform */ /*! \brief basic function, uniform */
inline double Uniform(void) { inline double Uniform(void) {
return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX)+1.0); return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX)+1.0); // NOLINT(*)
} }
/*! \brief return a real numer uniform in (0,1) */ /*! \brief return a real numer uniform in (0,1) */
inline double NextDouble2(void) { inline double NextDouble2(void) {
return (static_cast<double>(rand()) + 1.0) / (static_cast<double>(RAND_MAX)+2.0); return (static_cast<double>(rand()) + 1.0) / (static_cast<double>(RAND_MAX)+2.0); // NOLINT(*)
} }
/*! \brief return x~N(0,1) */ /*! \brief return x~N(0,1) */
inline double Normal(void) { inline double Normal(void) {
@ -73,7 +75,7 @@ inline void Shuffle(T *data, size_t sz) {
} }
// random shuffle the data inside, require PRNG // random shuffle the data inside, require PRNG
template<typename T> template<typename T>
inline void Shuffle(std::vector<T> &data) { inline void Shuffle(std::vector<T> &data) { // NOLINT(*)
Shuffle(&data[0], data.size()); Shuffle(&data[0], data.size());
} }
@ -89,7 +91,8 @@ struct Random{
/*! \brief return a real number uniform in [0,1) */ /*! \brief return a real number uniform in [0,1) */
inline double RandDouble(void) { inline double RandDouble(void) {
// 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) || defined(XGBOOST_STRICT_CXX98_) #if defined(_MSC_VER) || defined(_WIN32) || defined(XGBOOST_STRICT_CXX98_)
return Uniform(); return Uniform();

View File

@ -1,10 +1,12 @@
#ifndef XGBOOST_UTILS_THREAD_BUFFER_H_
#define XGBOOST_UTILS_THREAD_BUFFER_H_
/*! /*!
* Copyright 2014 by Contributors
* \file thread_buffer.h * \file thread_buffer.h
* \brief multi-thread buffer, iterator, can be used to create parallel pipeline * \brief multi-thread buffer, iterator, can be used to create parallel pipeline
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_THREAD_BUFFER_H_
#define XGBOOST_UTILS_THREAD_BUFFER_H_
#include <vector> #include <vector>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
@ -94,7 +96,7 @@ class ThreadBuffer {
* \param elem element to store into * \param elem element to store into
* \return whether reaches end of data * \return whether reaches end of data
*/ */
inline bool Next(Elem &elem) { inline bool Next(Elem &elem) { // NOLINT(*)
// end of buffer try to switch // end of buffer try to switch
if (buf_index == buf_size) { if (buf_index == buf_size) {
this->SwitchBuffer(); this->SwitchBuffer();
@ -119,6 +121,7 @@ class ThreadBuffer {
} }
// size of buffer // size of buffer
int buf_size; int buf_size;
private: private:
// factory object used to load configures // factory object used to load configures
ElemFactory factory; ElemFactory factory;
@ -198,7 +201,6 @@ class ThreadBuffer {
loading_need.Post(); loading_need.Post();
} }
}; };
} // namespace utils } // namespace utils
} // namespace xgboost } // namespace xgboost
#endif #endif // XGBOOST_UTILS_THREAD_BUFFER_H_

View File

@ -1,10 +1,12 @@
#ifndef XGBOOST_UTILS_UTILS_H_
#define XGBOOST_UTILS_UTILS_H_
/*! /*!
* Copyright 2014 by Contributors
* \file utils.h * \file utils.h
* \brief simple utils to support the code * \brief simple utils to support the code
* \author Tianqi Chen * \author Tianqi Chen
*/ */
#ifndef XGBOOST_UTILS_UTILS_H_
#define XGBOOST_UTILS_UTILS_H_
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#include <cstdio> #include <cstdio>
#include <string> #include <string>
@ -158,7 +160,7 @@ inline std::FILE *FopenCheck(const char *fname, const char *flag) {
// easy utils that can be directly acessed in xgboost // easy utils that can be directly acessed in xgboost
/*! \brief get the beginning address of a vector */ /*! \brief get the beginning address of a vector */
template<typename T> template<typename T>
inline T *BeginPtr(std::vector<T> &vec) { inline T *BeginPtr(std::vector<T> &vec) { // NOLINT(*)
if (vec.size() == 0) { if (vec.size() == 0) {
return NULL; return NULL;
} else { } else {
@ -174,7 +176,7 @@ inline const T *BeginPtr(const std::vector<T> &vec) {
return &vec[0]; return &vec[0];
} }
} }
inline char* BeginPtr(std::string &str) { inline char* BeginPtr(std::string &str) { // NOLINT(*)
if (str.length() == 0) return NULL; if (str.length() == 0) return NULL;
return &str[0]; return &str[0];
} }