59e63bc minor 6233050 ok 14477f9 add namenode 75a6d34 add libhdfs opts e3c76bf minmum fix 8b3c435 chg 2035799 test code 7751b2b add debug 7690313 ok bd346b4 ok faba1dc add testload 6f7783e add testload e5f0340 ok 3ed9ec8 chg e552ac4 ask for more ram in am b2505e3 only stop nm when sucess bc696c9 add queue info f3e867e add option queue 5dc843c refactor fileio cd9c81b quick fix 1e23af2 add virtual destructor to iseekstream f165ffb fix hdfs 8cc6508 allow demo to pass in env fad4d69 ok 0fd6197 fix more 7423837 fix more d25de54 add temporal solution, run_yarn_prog.py e5a9e31 final attempt ed3bee8 add command back 0774000 add hdfs to resource 9b66e7e fix hadoop 6812f14 ok 08e1c16 change hadoop prefix back to hadoop home d6b6828 Update build.sh 146e069 bugfix: logical boundary for ring buffer 19cb685 ok 4cf3c13 Merge branch 'master' of ssh://github.com/tqchen/rabit 20daddb add tracker c57dad8 add ringbased passing and batch schedule 295d8a1 update 994cb02 add sge 014c866 OK git-subtree-dir: subtree/rabit git-subtree-split: 59e63bc1354c9ff516d72d9a6468f6c431627202
139 lines
3.3 KiB
C++
139 lines
3.3 KiB
C++
/*!
|
|
* Copyright (c) 2015 by Contributors
|
|
* \file data.h
|
|
* \brief simple data structure that could be used by model
|
|
*
|
|
* \author Tianqi Chen
|
|
*/
|
|
#ifndef RABIT_LEARN_DATA_H_
|
|
#define RABIT_LEARN_DATA_H_
|
|
|
|
#include <vector>
|
|
#include <cstdlib>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <cmath>
|
|
#include <sstream>
|
|
#include <rabit.h>
|
|
#include "../io/io.h"
|
|
|
|
namespace rabit {
|
|
// typedef index type
|
|
typedef unsigned index_t;
|
|
|
|
/*! \brief sparse matrix, CSR format */
|
|
struct SparseMat {
|
|
// sparse matrix entry
|
|
struct Entry {
|
|
// feature index
|
|
index_t findex;
|
|
// feature value
|
|
float fvalue;
|
|
};
|
|
// sparse vector
|
|
struct Vector {
|
|
const Entry *data;
|
|
index_t length;
|
|
inline const Entry &operator[](size_t i) const {
|
|
return data[i];
|
|
}
|
|
};
|
|
inline Vector operator[](size_t i) const {
|
|
Vector v;
|
|
v.data = &data[0] + row_ptr[i];
|
|
v.length = static_cast<index_t>(row_ptr[i + 1]-row_ptr[i]);
|
|
return v;
|
|
}
|
|
// load data from LibSVM format
|
|
inline void Load(const char *fname) {
|
|
io::InputSplit *in =
|
|
io::CreateInputSplit
|
|
(fname, rabit::GetRank(),
|
|
rabit::GetWorldSize());
|
|
row_ptr.clear();
|
|
row_ptr.push_back(0);
|
|
data.clear();
|
|
feat_dim = 0;
|
|
std::string line;
|
|
while (in->NextLine(&line)) {
|
|
float label;
|
|
std::istringstream ss(line);
|
|
ss >> label;
|
|
Entry e;
|
|
unsigned long fidx;
|
|
while (!ss.eof()) {
|
|
if (!(ss >> fidx)) break;
|
|
ss.ignore(32, ':');
|
|
if (!(ss >> e.fvalue)) break;
|
|
e.findex = static_cast<index_t>(fidx);
|
|
data.push_back(e);
|
|
feat_dim = std::max(fidx, feat_dim);
|
|
}
|
|
labels.push_back(label);
|
|
row_ptr.push_back(data.size());
|
|
}
|
|
delete in;
|
|
feat_dim += 1;
|
|
utils::Check(feat_dim < std::numeric_limits<index_t>::max(),
|
|
"feature dimension exceed limit of index_t"\
|
|
"consider change the index_t to unsigned long");
|
|
}
|
|
inline size_t NumRow(void) const {
|
|
return row_ptr.size() - 1;
|
|
}
|
|
// memory cost
|
|
inline size_t MemCost(void) const {
|
|
return data.size() * sizeof(Entry);
|
|
}
|
|
// maximum feature dimension
|
|
size_t feat_dim;
|
|
std::vector<size_t> row_ptr;
|
|
std::vector<Entry> data;
|
|
std::vector<float> labels;
|
|
};
|
|
|
|
// dense matrix
|
|
struct Matrix {
|
|
inline void Init(size_t nrow, size_t ncol, float v = 0.0f) {
|
|
this->nrow = nrow;
|
|
this->ncol = ncol;
|
|
data.resize(nrow * ncol);
|
|
std::fill(data.begin(), data.end(), v);
|
|
}
|
|
inline float *operator[](size_t i) {
|
|
return &data[0] + i * ncol;
|
|
}
|
|
inline const float *operator[](size_t i) const {
|
|
return &data[0] + i * ncol;
|
|
}
|
|
inline void Print(const char *fname) {
|
|
FILE *fo;
|
|
if (!strcmp(fname, "stdout")) {
|
|
fo = stdout;
|
|
} else {
|
|
fo = utils::FopenCheck(fname, "w");
|
|
}
|
|
for (size_t i = 0; i < data.size(); ++i) {
|
|
fprintf(fo, "%g", data[i]);
|
|
if ((i+1) % ncol == 0) {
|
|
fprintf(fo, "\n");
|
|
} else {
|
|
fprintf(fo, " ");
|
|
}
|
|
}
|
|
// close the filed
|
|
if (fo != stdout) fclose(fo);
|
|
}
|
|
// number of data
|
|
size_t nrow, ncol;
|
|
std::vector<float> data;
|
|
};
|
|
|
|
/*!\brief computes a random number modulo the value */
|
|
inline int Random(int value) {
|
|
return rand() % value;
|
|
}
|
|
} // namespace rabit
|
|
#endif // RABIT_LEARN_DATA_H_
|