init commit

This commit is contained in:
tqchen
2014-02-06 15:50:50 -08:00
parent 225aa9841b
commit 57fef8bc54
11 changed files with 703 additions and 0 deletions

52
utils/xgboost_stream.h Normal file
View 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

52
utils/xgboost_stream.h~ Normal file
View 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
View 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

58
utils/xgboost_utils.h~ Normal file
View File

@@ -0,0 +1,58 @@
#ifndef _XGBOOST_UTILS_H_
#define _XGBOOST_UTILS_H_
#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 utils{
inline void Error( const char *msg ){
fprintf( stderr, "%s\n",msg );
exit( -1 );
}
inline void Assert( bool exp ){
if( !exp ) error( "assert_error" );
}
inline void Assert( bool exp, const char *msg ){
if( !exp ) error( msg );
}
inline void Warning( const char *msg ){
fprintf( stderr, "warning:%s\n",msg );
}
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