fix all utils
This commit is contained in:
parent
0162bb7034
commit
1581de08da
@ -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);
|
||||||
@ -75,7 +78,7 @@ const char DecodeTable[] = {
|
|||||||
};
|
};
|
||||||
static const char EncodeTable[] =
|
static const char EncodeTable[] =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
} // namespace base64
|
} // namespace base64
|
||||||
/*! \brief the stream that reads from base64, note we take from file pointers */
|
/*! \brief the stream that reads from base64, note we take from file pointers */
|
||||||
class Base64InStream: public IStream {
|
class Base64InStream: public IStream {
|
||||||
public:
|
public:
|
||||||
@ -132,19 +135,19 @@ class Base64InStream: public IStream {
|
|||||||
{
|
{
|
||||||
// second byte
|
// second byte
|
||||||
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch != EOF && !isspace(tmp_ch)),
|
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch != EOF && !isspace(tmp_ch)),
|
||||||
"invalid base64 format");
|
"invalid base64 format");
|
||||||
nvalue |= DecodeTable[tmp_ch] << 12;
|
nvalue |= DecodeTable[tmp_ch] << 12;
|
||||||
*cptr++ = (nvalue >> 16) & 0xFF; --tlen;
|
*cptr++ = (nvalue >> 16) & 0xFF; --tlen;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// third byte
|
// third byte
|
||||||
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch != EOF && !isspace(tmp_ch)),
|
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch != EOF && !isspace(tmp_ch)),
|
||||||
"invalid base64 format");
|
"invalid base64 format");
|
||||||
// handle termination
|
// handle termination
|
||||||
if (tmp_ch == '=') {
|
if (tmp_ch == '=') {
|
||||||
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch == '='), "invalid base64 format");
|
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch == '='), "invalid base64 format");
|
||||||
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch == EOF || isspace(tmp_ch)),
|
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch == EOF || isspace(tmp_ch)),
|
||||||
"invalid base64 format");
|
"invalid base64 format");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
nvalue |= DecodeTable[tmp_ch] << 6;
|
nvalue |= DecodeTable[tmp_ch] << 6;
|
||||||
@ -157,10 +160,10 @@ class Base64InStream: public IStream {
|
|||||||
{
|
{
|
||||||
// fourth byte
|
// fourth byte
|
||||||
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch != EOF && !isspace(tmp_ch)),
|
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch != EOF && !isspace(tmp_ch)),
|
||||||
"invalid base64 format");
|
"invalid base64 format");
|
||||||
if (tmp_ch == '=') {
|
if (tmp_ch == '=') {
|
||||||
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch == EOF || isspace(tmp_ch)),
|
utils::Check((tmp_ch = reader_.GetChar(), tmp_ch == EOF || isspace(tmp_ch)),
|
||||||
"invalid base64 format");
|
"invalid base64 format");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
nvalue |= DecodeTable[tmp_ch];
|
nvalue |= DecodeTable[tmp_ch];
|
||||||
@ -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_
|
||||||
|
|||||||
@ -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_
|
||||||
|
|||||||
@ -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_
|
||||||
|
|||||||
@ -111,5 +111,4 @@ struct ParallelGroupBuilder {
|
|||||||
};
|
};
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
} // namespace xgboost
|
} // namespace xgboost
|
||||||
#endif
|
#endif // XGBOOST_UTILS_GROUP_DATA_H_
|
||||||
|
|
||||||
|
|||||||
@ -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_
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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; }
|
||||||
@ -25,6 +29,6 @@ typedef int bst_omp_uint;
|
|||||||
#else
|
#else
|
||||||
typedef unsigned bst_omp_uint;
|
typedef unsigned bst_omp_uint;
|
||||||
#endif
|
#endif
|
||||||
} // namespace xgboost
|
} // namespace xgboost
|
||||||
|
|
||||||
#endif // XGBOOST_UTILS_OMP_H_
|
#endif // XGBOOST_UTILS_OMP_H_
|
||||||
|
|||||||
@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -741,7 +744,7 @@ class QuantileSketchTemplate {
|
|||||||
* \tparam DType type of data content
|
* \tparam DType type of data content
|
||||||
* \tparam RType type of rank
|
* \tparam RType type of rank
|
||||||
*/
|
*/
|
||||||
template<typename DType, typename RType=unsigned>
|
template<typename DType, typename RType = unsigned>
|
||||||
class WQuantileSketch :
|
class WQuantileSketch :
|
||||||
public QuantileSketchTemplate<DType, RType, WQSummary<DType, RType> >{
|
public QuantileSketchTemplate<DType, RType, WQSummary<DType, RType> >{
|
||||||
};
|
};
|
||||||
@ -751,7 +754,7 @@ class WQuantileSketch :
|
|||||||
* \tparam DType type of data content
|
* \tparam DType type of data content
|
||||||
* \tparam RType type of rank
|
* \tparam RType type of rank
|
||||||
*/
|
*/
|
||||||
template<typename DType, typename RType=unsigned>
|
template<typename DType, typename RType = unsigned>
|
||||||
class WXQuantileSketch :
|
class WXQuantileSketch :
|
||||||
public QuantileSketchTemplate<DType, RType, WXQSummary<DType, RType> >{
|
public QuantileSketchTemplate<DType, RType, WXQSummary<DType, RType> >{
|
||||||
};
|
};
|
||||||
@ -760,11 +763,11 @@ class WXQuantileSketch :
|
|||||||
* \tparam DType type of data content
|
* \tparam DType type of data content
|
||||||
* \tparam RType type of rank
|
* \tparam RType type of rank
|
||||||
*/
|
*/
|
||||||
template<typename DType, typename RType=unsigned>
|
template<typename DType, typename RType = unsigned>
|
||||||
class GKQuantileSketch :
|
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_
|
||||||
|
|||||||
@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,17 +83,18 @@ inline void Shuffle(std::vector<T> &data) {
|
|||||||
struct Random{
|
struct Random{
|
||||||
/*! \brief set random number seed */
|
/*! \brief set random number seed */
|
||||||
inline void Seed(unsigned sd) {
|
inline void Seed(unsigned sd) {
|
||||||
this->rseed = sd;
|
this->rseed = sd;
|
||||||
#if defined(_MSC_VER)||defined(_WIN32)
|
#if defined(_MSC_VER) || defined(_WIN32)
|
||||||
::xgboost::random::Seed(sd);
|
::xgboost::random::Seed(sd);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
/*! \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,
|
||||||
// todo, replace with another PRNG
|
// but rand_r is only used in objective-inl.hpp, won't affect speed in general
|
||||||
#if defined(_MSC_VER)||defined(_WIN32)||defined(XGBOOST_STRICT_CXX98_)
|
// todo, replace with another PRNG
|
||||||
|
#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);
|
||||||
|
|||||||
@ -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>
|
||||||
@ -27,7 +29,7 @@ class ThreadBuffer {
|
|||||||
this->buf_size = 30;
|
this->buf_size = 30;
|
||||||
}
|
}
|
||||||
~ThreadBuffer(void) {
|
~ThreadBuffer(void) {
|
||||||
if(init_end) this->Destroy();
|
if (init_end) this->Destroy();
|
||||||
}
|
}
|
||||||
/*!\brief set parameter, will also pass the parameter to factory */
|
/*!\brief set parameter, will also pass the parameter to factory */
|
||||||
inline void SetParam(const char *name, const char *val) {
|
inline void SetParam(const char *name, const char *val) {
|
||||||
@ -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();
|
||||||
@ -114,11 +116,12 @@ class ThreadBuffer {
|
|||||||
inline ElemFactory &get_factory(void) {
|
inline ElemFactory &get_factory(void) {
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
inline const ElemFactory &get_factory(void) const{
|
inline const ElemFactory &get_factory(void) const {
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
// 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;
|
||||||
@ -147,7 +150,7 @@ class ThreadBuffer {
|
|||||||
* this implementation is like producer-consumer style
|
* this implementation is like producer-consumer style
|
||||||
*/
|
*/
|
||||||
inline void RunLoader(void) {
|
inline void RunLoader(void) {
|
||||||
while(!destroy_signal) {
|
while (!destroy_signal) {
|
||||||
// sleep until loading is needed
|
// sleep until loading is needed
|
||||||
loading_need.Wait();
|
loading_need.Wait();
|
||||||
std::vector<Elem> &buf = current_buf ? bufB : bufA;
|
std::vector<Elem> &buf = current_buf ? bufB : bufA;
|
||||||
@ -155,7 +158,7 @@ class ThreadBuffer {
|
|||||||
for (i = 0; i < buf_size ; ++i) {
|
for (i = 0; i < buf_size ; ++i) {
|
||||||
if (!factory.LoadNext(buf[i])) {
|
if (!factory.LoadNext(buf[i])) {
|
||||||
int &end = current_buf ? endB : endA;
|
int &end = current_buf ? endB : endA;
|
||||||
end = i; // marks the termination
|
end = i; // marks the termination
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,7 +169,7 @@ class ThreadBuffer {
|
|||||||
}
|
}
|
||||||
/*!\brief entry point of loader thread */
|
/*!\brief entry point of loader thread */
|
||||||
inline static XGBOOST_THREAD_PREFIX LoaderEntry(void *pthread) {
|
inline static XGBOOST_THREAD_PREFIX LoaderEntry(void *pthread) {
|
||||||
static_cast< ThreadBuffer<Elem,ElemFactory>* >(pthread)->RunLoader();
|
static_cast< ThreadBuffer<Elem, ElemFactory>* >(pthread)->RunLoader();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
/*!\brief start loader thread */
|
/*!\brief start loader thread */
|
||||||
@ -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_
|
||||||
|
|||||||
@ -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>
|
||||||
@ -26,7 +28,7 @@
|
|||||||
#else
|
#else
|
||||||
#ifdef _FILE_OFFSET_BITS
|
#ifdef _FILE_OFFSET_BITS
|
||||||
#if _FILE_OFFSET_BITS == 32
|
#if _FILE_OFFSET_BITS == 32
|
||||||
#pragma message ("Warning: FILE OFFSET BITS defined to be 32 bit")
|
#pragma message("Warning: FILE OFFSET BITS defined to be 32 bit")
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -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];
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user