init commit
This commit is contained in:
52
utils/xgboost_stream.h
Normal file
52
utils/xgboost_stream.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef _XGBOOST_STREAM_H_
|
||||
#define _XGBOOST_STREAM_H_
|
||||
|
||||
#include <cstdio>
|
||||
/*!
|
||||
* \file xgboost_stream.h
|
||||
* \brief general stream interface
|
||||
* \author Tianqi Chen: tianqi.tchen@gmail.com
|
||||
*/
|
||||
namespace xgboost{
|
||||
namespace utils{
|
||||
/*! \brief interface of stream I/O, used to serialize tensor data */
|
||||
class IStream{
|
||||
public:
|
||||
/*!
|
||||
* \brief read data from stream
|
||||
* \param ptr pointer to memory buffer
|
||||
* \param size size of block
|
||||
* \return usually is the size of data readed
|
||||
*/
|
||||
virtual size_t Read( void *ptr, size_t size ) = 0;
|
||||
/*!
|
||||
* \brief write data to stream
|
||||
* \param ptr pointer to memory buffer
|
||||
* \param size size of block
|
||||
*/
|
||||
virtual void Write( const void *ptr, size_t size ) = 0;
|
||||
/*! \brief virtual destructor */
|
||||
virtual ~IStream( void ){}
|
||||
};
|
||||
|
||||
/*! \brief implementation of file i/o stream */
|
||||
class FileStream: public IStream{
|
||||
private:
|
||||
FILE *fp;
|
||||
public:
|
||||
FileStream( FILE *fp ){
|
||||
this->fp = fp;
|
||||
}
|
||||
virtual size_t Read( void *ptr, size_t size ){
|
||||
return fread( ptr, size, 1, fp );
|
||||
}
|
||||
virtual void Write( const void *ptr, size_t size ){
|
||||
fwrite( ptr, size, 1, fp );
|
||||
}
|
||||
inline void Close( void ){
|
||||
fclose( fp );
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
#endif
|
||||
67
utils/xgboost_utils.h
Normal file
67
utils/xgboost_utils.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#ifndef _XGBOOST_UTILS_H_
|
||||
#define _XGBOOST_UTILS_H_
|
||||
|
||||
/*!
|
||||
* \file xgboost_utils.h
|
||||
* \brief simple utils to support the code
|
||||
* \author Tianqi Chen: tianqi.tchen@gmail.com
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define fopen64 fopen
|
||||
#else
|
||||
|
||||
// use 64 bit offset, either to include this header in the beginning, or
|
||||
#ifdef _FILE_OFFSET_BITS
|
||||
#if _FILE_OFFSET_BITS == 32
|
||||
#warning "FILE OFFSET BITS defined to be 32 bit"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
#define off64_t off_t
|
||||
#define fopen64 fopen
|
||||
#endif
|
||||
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
extern "C"{
|
||||
#include <sys/types.h>
|
||||
};
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace xgboost{
|
||||
/*! \brief namespace for helper utils of the project */
|
||||
namespace utils{
|
||||
inline void Error( const char *msg ){
|
||||
fprintf( stderr, "Error:%s\n",msg );
|
||||
exit( -1 );
|
||||
}
|
||||
|
||||
inline void Assert( bool exp ){
|
||||
if( !exp ) Error( "AssertError" );
|
||||
}
|
||||
|
||||
inline void Assert( bool exp, const char *msg ){
|
||||
if( !exp ) Error( msg );
|
||||
}
|
||||
|
||||
inline void Warning( const char *msg ){
|
||||
fprintf( stderr, "warning:%s\n",msg );
|
||||
}
|
||||
/*! \brief replace fopen, report error when the file open fails */
|
||||
inline FILE *FopenCheck( const char *fname , const char *flag ){
|
||||
FILE *fp = fopen64( fname , flag );
|
||||
if( fp == NULL ){
|
||||
fprintf( stderr, "can not open file \"%s\"\n",fname );
|
||||
exit( -1 );
|
||||
}
|
||||
return fp;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user