@@ -2,6 +2,7 @@
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#include <string>
|
||||
#include "./io.h"
|
||||
#include "../utils/io.h"
|
||||
#include "../utils/utils.h"
|
||||
#include "simple_dmatrix-inl.hpp"
|
||||
// implements data loads using dmatrix simple for now
|
||||
@@ -9,6 +10,19 @@
|
||||
namespace xgboost {
|
||||
namespace io {
|
||||
DataMatrix* LoadDataMatrix(const char *fname, bool silent, bool savebuffer) {
|
||||
int magic;
|
||||
utils::FileStream fs(utils::FopenCheck(fname, "rb"));
|
||||
utils::Check(fs.Read(&magic, sizeof(magic)) != 0, "invalid input file format");
|
||||
fs.Seek(0);
|
||||
|
||||
if (magic == DMatrixSimple::kMagic) {
|
||||
DMatrixSimple *dmat = new DMatrixSimple();
|
||||
dmat->LoadBinary(fs, silent, fname);
|
||||
fs.Close();
|
||||
return dmat;
|
||||
}
|
||||
fs.Close();
|
||||
|
||||
DMatrixSimple *dmat = new DMatrixSimple();
|
||||
dmat->CacheLoad(fname, silent, savebuffer);
|
||||
return dmat;
|
||||
|
||||
@@ -128,6 +128,17 @@ class DMatrixSimple : public DataMatrix {
|
||||
FILE *fp = fopen64(fname, "rb");
|
||||
if (fp == NULL) return false;
|
||||
utils::FileStream fs(fp);
|
||||
this->LoadBinary(fs, silent, fname);
|
||||
fs.Close();
|
||||
return true;
|
||||
}
|
||||
/*!
|
||||
* \brief load from binary stream
|
||||
* \param fs input file stream
|
||||
* \param silent whether print information during loading
|
||||
* \param fname file name, used to print message
|
||||
*/
|
||||
inline void LoadBinary(utils::IStream &fs, bool silent = false, const char *fname = NULL) {
|
||||
int magic;
|
||||
utils::Check(fs.Read(&magic, sizeof(magic)) != 0, "invalid input file format");
|
||||
utils::Check(magic == kMagic, "invalid format,magic number mismatch");
|
||||
@@ -135,16 +146,19 @@ class DMatrixSimple : public DataMatrix {
|
||||
info.LoadBinary(fs);
|
||||
FMatrixS::LoadBinary(fs, &row_ptr_, &row_data_);
|
||||
fmat.LoadColAccess(fs);
|
||||
fs.Close();
|
||||
|
||||
if (!silent) {
|
||||
printf("%lux%lu matrix with %lu entries is loaded from %s\n",
|
||||
info.num_row(), info.num_col(), row_data_.size(), fname);
|
||||
printf("%lux%lu matrix with %lu entries is loaded",
|
||||
info.num_row(), info.num_col(), row_data_.size());
|
||||
if (fname != NULL) {
|
||||
printf(" from %s\n", fname);
|
||||
} else {
|
||||
printf("\n");
|
||||
}
|
||||
if (info.group_ptr.size() != 0) {
|
||||
printf("data contains %u groups\n", (unsigned)info.group_ptr.size()-1);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*!
|
||||
* \brief save to binary file
|
||||
|
||||
@@ -102,6 +102,9 @@ class FileStream : public IStream {
|
||||
virtual void Write(const void *ptr, size_t size) {
|
||||
fwrite(ptr, size, 1, fp);
|
||||
}
|
||||
inline void Seek(size_t pos) {
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
}
|
||||
inline void Close(void) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
#else
|
||||
#ifndef DISABLE_OPENMP
|
||||
#ifndef _MSC_VER
|
||||
#warning "OpenMP is not available, compile to single thread code"
|
||||
#warning "OpenMP is not available, compile to single thread code."\
|
||||
"You may want to ungrade your compiler to enable OpenMP support,"\
|
||||
"to get benefit of multi-threading."
|
||||
#else
|
||||
// TODO add warning for msvc
|
||||
#endif
|
||||
|
||||
@@ -89,16 +89,19 @@ struct Random{
|
||||
/*! \brief set random number seed */
|
||||
inline void Seed(unsigned sd) {
|
||||
this->rseed = sd;
|
||||
#ifdef _MSC_VER
|
||||
#if defined(_MSC_VER)||defined(_WIN32)
|
||||
srand(rseed);
|
||||
#endif
|
||||
}
|
||||
/*! \brief return a real number uniform in [0,1) */
|
||||
inline double RandDouble(void) {
|
||||
#ifndef _MSC_VER
|
||||
return static_cast<double>(rand_r(&rseed)) / (static_cast<double>(RAND_MAX) + 1.0);
|
||||
// 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
|
||||
// todo, replace with another PRNG
|
||||
#if defined(_MSC_VER)||defined(_WIN32)
|
||||
return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
|
||||
#else
|
||||
return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
|
||||
return static_cast<double>(rand_r(&rseed)) / (static_cast<double>(RAND_MAX) + 1.0);
|
||||
#endif
|
||||
}
|
||||
// random number seed
|
||||
|
||||
Reference in New Issue
Block a user