Fixed so that no virtualization is allowed inside the memory mapped file.

This commit is contained in:
Dongryeol Lee
2010-11-19 19:39:01 +00:00
parent 5a92cce02b
commit cbb461e622
13 changed files with 120 additions and 144 deletions
@@ -155,7 +155,7 @@ void core::gnp::DualtreeDfs<ProblemType>::DualtreeBase_(
while(qnode_iterator.HasNext()) {
// Get the query point and its real index.
core::table::DenseConstPoint q_col;
core::table::DensePoint q_col;
int q_index;
qnode_iterator.Next(&q_col, &q_index);
@@ -171,7 +171,7 @@ void core::gnp::DualtreeDfs<ProblemType>::DualtreeBase_(
while(rnode_iterator.HasNext()) {
// Get the reference point and accumulate contribution.
core::table::DenseConstPoint r_col;
core::table::DensePoint r_col;
int r_col_id;
rnode_iterator.Next(&r_col, &r_col_id);
query_contribution.ApplyContribution(
@@ -504,7 +504,7 @@ void core::gnp::DualtreeDfs<ProblemType>::PostProcess_(
query_table_->get_node_iterator(qnode);
while(qnode_iterator.HasNext()) {
core::table::DenseConstPoint q_col;
core::table::DensePoint q_col;
int q_index;
qnode_iterator.Next(&q_col, &q_index);
query_results->ApplyPostponed(q_index, qnode_stat.postponed_);
@@ -17,7 +17,7 @@ class TripleDistanceSq {
private:
arma::mat distance_sq_;
std::vector< core::table::DenseConstPoint > points_;
std::vector< core::table::DensePoint > points_;
std::vector<int> point_indices_;
@@ -54,7 +54,7 @@ class TripleDistanceSq {
void ReplaceOnePoint(
const core::metric_kernels::AbstractMetric &metric_in,
const core::table::DenseConstPoint &new_point_in,
const core::table::DensePoint &new_point_in,
int new_point_index_in,
int point_pos_in) {
@@ -166,7 +166,7 @@ void core::gnp::TripletreeDfs<ProblemType>::TripletreeBase_(
table_->get_node_iterator(range_sq_in.node(0));
do {
// Get the point in the first node.
core::table::DenseConstPoint first_point;
core::table::DensePoint first_point;
int first_point_index;
first_node_it.Next(&first_point, &first_point_index);
distance_sq_set.ReplaceOnePoint(metric, first_point, first_point_index, 0);
@@ -177,7 +177,7 @@ void core::gnp::TripletreeDfs<ProblemType>::TripletreeBase_(
while(second_node_it.HasNext()) {
// Get the point in the second node.
core::table::DenseConstPoint second_point;
core::table::DensePoint second_point;
int second_point_index;
second_node_it.Next(&second_point, &second_point_index);
distance_sq_set.ReplaceOnePoint(
@@ -189,7 +189,7 @@ void core::gnp::TripletreeDfs<ProblemType>::TripletreeBase_(
while(third_node_it.HasNext()) {
// Get thet point in the third node.
core::table::DenseConstPoint third_point;
core::table::DensePoint third_point;
int third_point_index;
third_node_it.Next(&third_point, &third_point_index);
distance_sq_set.ReplaceOnePoint(
@@ -238,7 +238,7 @@ void core::gnp::TripletreeDfs<ProblemType>::TripletreeBase_(
while(node_iterator.HasNext()) {
// Get the query point and its real index.
core::table::DenseConstPoint q_col;
core::table::DensePoint q_col;
int q_index;
node_iterator.Next(&q_col, &q_index);
@@ -277,7 +277,7 @@ bool core::gnp::TripletreeDfs<ProblemType>::CanProbabilisticSummarize_(
typename ProblemType::SummaryType new_summary;
bool flag = true;
core::table::DenseConstPoint previous_query_point;
core::table::DensePoint previous_query_point;
int previous_query_point_index = -1;
for(int i = node_start_index; flag && i < 3; i++) {
@@ -293,7 +293,7 @@ bool core::gnp::TripletreeDfs<ProblemType>::CanProbabilisticSummarize_(
// The query point index and the point.
int query_point_index = -1;
core::table::DenseConstPoint query_point;
core::table::DensePoint query_point;
// The new summary.
new_summary = node_stat.summary_;
@@ -317,7 +317,7 @@ bool core::gnp::TripletreeDfs<ProblemType>::CanProbabilisticSummarize_(
metric, problem_->global(), delta,
range_in, failure_probabilities, i, query_results,
query_point, qpoint_dfs_index, query_point_index,
(const core::table::DenseConstPoint *) NULL,
(const core::table::DensePoint *) NULL,
(int *) NULL);
}
previous_query_point.Alias(query_point);
@@ -685,7 +685,7 @@ void core::gnp::TripletreeDfs<ProblemType>::PostProcess_(
qnode_stat.summary_.StartReaccumulate(problem_->global());
while(qnode_iterator.HasNext()) {
core::table::DenseConstPoint q_col;
core::table::DensePoint q_col;
int q_index;
qnode_iterator.Next(&q_col, &q_index);
query_results->ApplyPostponed(q_index, qnode_stat.postponed_);
@@ -8,7 +8,7 @@
#ifndef CORE_METRIC_KERNELS_ABSTRACT_METRIC_H
#define CORE_METRIC_KERNELS_ABSTRACT_METRIC_H
#include "core/table/abstract_point.h"
#include "core/table/dense_point.h"
namespace core {
namespace metric_kernels {
@@ -23,12 +23,12 @@ class AbstractMetric {
* Computes the distance metric between two points.
*/
virtual double Distance(
const core::table::AbstractPoint& a,
const core::table::AbstractPoint& b) const = 0;
const core::table::DensePoint& a,
const core::table::DensePoint& b) const = 0;
virtual double DistanceIneq(
const core::table::AbstractPoint& a,
const core::table::AbstractPoint& b) const = 0;
const core::table::DensePoint& a,
const core::table::DensePoint& b) const = 0;
/**
* Computes the distance metric between two points, raised to a
@@ -38,8 +38,8 @@ class AbstractMetric {
* L2 distance.
*/
virtual double DistanceSq(
const core::table::AbstractPoint &a,
const core::table::AbstractPoint &b) const = 0;
const core::table::DensePoint &a,
const core::table::DensePoint &b) const = 0;
};
};
};
@@ -20,8 +20,8 @@ class LMetricDistanceSqTrait {
public:
static double Compute(
const core::metric_kernels::AbstractMetric &metric_in,
const core::table::AbstractPoint &a,
const core::table::AbstractPoint &b) {
const core::table::DensePoint &a,
const core::table::DensePoint &b) {
return core::math::Pow<2, t_pow>(metric_in.DistanceIneq(a, b));
}
@@ -32,8 +32,8 @@ class LMetricDistanceSqTrait<2> {
public:
static double Compute(
const core::metric_kernels::AbstractMetric &metric_in,
const core::table::AbstractPoint &a,
const core::table::AbstractPoint &b) {
const core::table::DensePoint &a,
const core::table::DensePoint &b) {
return metric_in.DistanceIneq(a, b);
}
@@ -53,14 +53,14 @@ class LMetric: public core::metric_kernels::AbstractMetric {
* Computes the distance metric between two points.
*/
double Distance(
const core::table::AbstractPoint& a,
const core::table::AbstractPoint& b) const {
const core::table::DensePoint& a,
const core::table::DensePoint& b) const {
return core::math::Pow<1, t_pow>(DistanceIneq(a, b));
}
double DistanceIneq(
const core::table::AbstractPoint &a,
const core::table::AbstractPoint &b) const {
const core::table::DensePoint &a,
const core::table::DensePoint &b) const {
double distance_ineq = 0;
for(int i = 0; i < a.length(); i++) {
@@ -77,8 +77,8 @@ class LMetric: public core::metric_kernels::AbstractMetric {
* L2 distance.
*/
double DistanceSq(
const core::table::AbstractPoint &a,
const core::table::AbstractPoint &b) const {
const core::table::DensePoint &a,
const core::table::DensePoint &b) const {
return core::metric_kernels::LMetricDistanceSqTrait<t_pow>::Compute(
*this, a, b);
@@ -109,7 +109,7 @@ class DenseMatrix {
}
void MakeColumnVector(
int i, core::table::DenseConstPoint *point_out) const {
int i, core::table::DensePoint *point_out) const {
point_out->Alias(
ptr_.get() + i * n_rows_, n_rows_);
}
@@ -9,8 +9,8 @@
#define CORE_TABLE_DENSE_POINT_H
#include <armadillo>
#include <boost/interprocess/offset_ptr.hpp>
#include <boost/serialization/serialization.hpp>
#include "abstract_point.h"
#include "memory_mapped_file.h"
namespace core {
@@ -18,88 +18,44 @@ namespace table {
extern core::table::MemoryMappedFile *global_m_file_;
class DenseConstPoint: public core::table::AbstractPoint {
protected:
double *ptr_;
int n_rows_;
public:
void Print() const {
printf("Vector of length: %d\n", n_rows_);
for(int i = 0; i < n_rows_; i++) {
printf("%g ", ptr_[i]);
}
printf("\n");
}
void Init(int length) {
// Does not do anything - DenseConstPoint is only an alias.
}
const double *ptr() const {
return ptr_;
}
virtual ~DenseConstPoint() {
// A const point is always defined as an alias to a part of an
// already-existing memory block, so you do not free it.
Reset();
}
void Reset() {
ptr_ = NULL;
n_rows_ = 0;
}
DenseConstPoint() {
Reset();
}
int length() const {
return n_rows_;
}
double operator[](int i) const {
return ptr_[i];
}
void Alias(const double *ptr_in, int length_in) {
ptr_ = const_cast<double *>(ptr_in);
n_rows_ = length_in;
}
void Alias(const DenseConstPoint &point_in) {
ptr_ = const_cast<double *>(point_in.ptr());
n_rows_ = point_in.length();
}
};
class DensePoint: public DenseConstPoint {
class DensePoint {
private:
friend class boost::serialization::access;
boost::interprocess::offset_ptr<double> ptr_;
int n_rows_;
bool is_alias_;
private:
void DestructPtr_() {
if(DenseConstPoint::ptr_ != NULL && is_alias_ == false) {
if(ptr_ != NULL && is_alias_ == false) {
if(core::table::global_m_file_) {
core::table::global_m_file_->Deallocate(ptr_);
core::table::global_m_file_->Deallocate(ptr_.get());
}
else {
delete[] DenseConstPoint::ptr_;
delete[] ptr_.get();
}
}
}
public:
int length() const {
return n_rows_;
}
const double *ptr() const {
return ptr_.get();
}
double *ptr() {
return ptr_.get();
}
template<class Archive>
void save(Archive &ar, const unsigned int version) const {
@@ -129,7 +85,6 @@ class DensePoint: public DenseConstPoint {
BOOST_SERIALIZATION_SPLIT_MEMBER()
void Reset() {
DenseConstPoint::Reset();
is_alias_ = false;
}
@@ -137,50 +92,53 @@ class DensePoint: public DenseConstPoint {
Reset();
}
virtual ~DensePoint() {
~DensePoint() {
DestructPtr_();
Reset();
}
const double operator[](int i) const {
return ptr_.get()[i];
}
double &operator[](int i) {
return DenseConstPoint::ptr_[i];
return ptr_.get()[i];
}
void Init(int length_in) {
DenseConstPoint::ptr_ =
ptr_ =
(core::table::global_m_file_) ?
(double *)
core::table::global_m_file_->Allocate(sizeof(double) * length_in) :
new double[length_in];
DenseConstPoint::n_rows_ = length_in;
n_rows_ = length_in;
is_alias_ = false;
}
void Init(const std::vector<double> &vector_in) {
DenseConstPoint::ptr_ =
ptr_ =
(core::table::global_m_file_) ?
(double *)
core::table::global_m_file_->Allocate(
sizeof(double) * vector_in.size()) :
new double[vector_in.size()];
DenseConstPoint::n_rows_ = vector_in.size();
n_rows_ = vector_in.size();
for(unsigned int i = 0; i < vector_in.size(); i++) {
ptr_[i] = vector_in[i];
}
is_alias_ = false;
}
void CopyValues(const DenseConstPoint &point_in) {
void CopyValues(const DensePoint &point_in) {
memcpy(
DenseConstPoint::ptr_, point_in.ptr(),
sizeof(double) * point_in.length());
DenseConstPoint::n_rows_ = point_in.length();
ptr_.get(), point_in.ptr(), sizeof(double) * point_in.length());
n_rows_ = point_in.length();
is_alias_ = false;
}
void Copy(const DenseConstPoint &point_in) {
void Copy(const DensePoint &point_in) {
DestructPtr_();
DenseConstPoint::ptr_ =
ptr_ =
(core::table::global_m_file_) ?
(double *) core::table::global_m_file_->Allocate(
sizeof(double) * point_in.length()) :
@@ -189,39 +147,57 @@ class DensePoint: public DenseConstPoint {
}
void SetZero() {
memset(ptr_, 0, sizeof(double) * n_rows_);
memset(ptr_.get(), 0, sizeof(double) * n_rows_);
}
void Alias(double *ptr_in, int length_in) {
DenseConstPoint::ptr_ = ptr_in;
DenseConstPoint::n_rows_ = length_in;
ptr_ = ptr_in;
n_rows_ = length_in;
is_alias_ = true;
}
void Alias(const double *ptr_in, int length_in) {
ptr_ = const_cast<double *>(ptr_in);
n_rows_ = length_in;
}
void Alias(const DensePoint &point_in) {
ptr_ = const_cast<double *>(point_in.ptr());
n_rows_ = point_in.length();
}
void Add(
double scale_factor, const core::table::DenseConstPoint &point_in) {
double scale_factor, const core::table::DensePoint &point_in) {
for(int i = 0; i < point_in.length(); i++) {
ptr_[i] += scale_factor * point_in[i];
ptr_.get()[i] += scale_factor * point_in[i];
}
}
void operator+=(const core::table::DenseConstPoint &point_in) {
void operator+=(const core::table::DensePoint &point_in) {
for(int i = 0; i < point_in.length(); i++) {
ptr_[i] += point_in[i];
ptr_.get()[i] += point_in[i];
}
}
void operator/=(double scale_factor) {
for(int i = 0; i < DenseConstPoint::n_rows_; i++) {
ptr_[i] /= scale_factor;
for(int i = 0; i < n_rows_; i++) {
ptr_.get()[i] /= scale_factor;
}
}
void operator*=(double scale_factor) {
for(int i = 0; i < DenseConstPoint::n_rows_; i++) {
ptr_[i] *= scale_factor;
for(int i = 0; i < n_rows_; i++) {
ptr_.get()[i] *= scale_factor;
}
}
void Print() const {
printf("Vector of length: %d\n", n_rows_);
for(int i = 0; i < n_rows_; i++) {
printf("%g ", ptr_.get()[i]);
}
printf("\n");
}
};
};
};
@@ -85,13 +85,13 @@ class Table: public boost::noncopyable {
current_index_++;
}
void Next(core::table::DenseConstPoint *entry, int *point_id) {
void Next(core::table::DensePoint *entry, int *point_id) {
current_index_++;
table_->iterator_get_(current_index_, entry);
*point_id = table_->iterator_get_id_(current_index_);
}
void get(int i, core::table::DenseConstPoint *entry) {
void get(int i, core::table::DensePoint *entry) {
table_->iterator_get_(begin_ + i, entry);
}
@@ -99,11 +99,11 @@ class Table: public boost::noncopyable {
*point_id = table_->iterator_get_id_(begin_ + i);
}
void RandomPick(core::table::DenseConstPoint *entry) {
void RandomPick(core::table::DensePoint *entry) {
table_->iterator_get_(core::math::Random(begin_, end_), entry);
}
void RandomPick(core::table::DenseConstPoint *entry, int *point_id) {
void RandomPick(core::table::DensePoint *entry, int *point_id) {
*point_id = core::math::Random(begin_, end_);
table_->iterator_get_(*point_id, entry);
}
@@ -235,7 +235,7 @@ class Table: public boost::noncopyable {
void Save(const std::string &file_name) const {
FILE *foutput = fopen(file_name.c_str(), "w+");
for(int j = 0; j < data_.n_cols(); j++) {
core::table::DenseConstPoint point;
core::table::DensePoint point;
this->direct_get_(j, &point);
for(int i = 0; i < data_.n_rows(); i++) {
fprintf(foutput, "%g", point[i]);
@@ -265,7 +265,7 @@ class Table: public boost::noncopyable {
direct_get_(point_id, point_out);
}
void get(int point_id, core::table::DenseConstPoint *point_out) const {
void get(int point_id, core::table::DensePoint *point_out) const {
direct_get_(point_id, point_out);
}
@@ -315,7 +315,7 @@ class Table: public boost::noncopyable {
}
void direct_get_(
int point_id, core::table::DenseConstPoint *entry) const {
int point_id, core::table::DensePoint *entry) const {
if(this->IsIndexed() == false) {
data_.MakeColumnVector(point_id, entry);
}
@@ -334,7 +334,7 @@ class Table: public boost::noncopyable {
}
void iterator_get_(
int reordered_position, core::table::DenseConstPoint *entry) const {
int reordered_position, core::table::DensePoint *entry) const {
data_.MakeColumnVector(reordered_position, entry);
}
@@ -36,7 +36,7 @@ class GenMetricTree {
*furthest_distance = -1.0;
for(int i = begin; i < end; i++) {
core::table::DenseConstPoint point;
core::table::DensePoint point;
matrix.MakeColumnVector(i, &point);
double distance_between_center_and_point =
metric_in.Distance(pivot, point);
@@ -67,7 +67,7 @@ class GenMetricTree {
bounds->center().SetZero();
int end = begin + count;
core::table::DenseConstPoint col_point;
core::table::DensePoint col_point;
for(int i = begin; i < end; i++) {
matrix.MakeColumnVector(i, &col_point);
bounds->center() += col_point;
@@ -112,7 +112,7 @@ class GenMetricTree {
for(int left = first; left < end; left++) {
// Make alias of the current point.
core::table::DenseConstPoint point;
core::table::DensePoint point;
matrix.MakeColumnVector(left, &point);
// Compute the distances from the two pivots.
@@ -75,12 +75,12 @@ class TestKde {
ultra_naive_query_results.resize(query_table.n_entries());
for(int i = 0; i < query_table.n_entries(); i++) {
core::table::DenseConstPoint query_point;
core::table::DensePoint query_point;
query_table.get(i, &query_point);
ultra_naive_query_results[i] = 0;
for(int j = 0; j < reference_table.n_entries(); j++) {
core::table::DenseConstPoint reference_point;
core::table::DensePoint reference_point;
reference_table.get(j, &reference_point);
// By default, monochromaticity is assumed in the test -
@@ -259,7 +259,7 @@ class KdeResult {
// Get the iterator for the query node.
typename GlobalType::TableType::TreeIterator qnode_it =
global.query_table()->get_node_iterator(qnode);
core::table::DenseConstPoint qpoint;
core::table::DensePoint qpoint;
int qpoint_index;
// Look up the number of standard deviations.
@@ -396,13 +396,13 @@ class KdeSummary {
// Get the iterator for the query node.
typename GlobalType::TableType::TreeIterator qnode_it =
global.query_table()->get_node_iterator(qnode);
core::table::DenseConstPoint qpoint;
core::table::DensePoint qpoint;
int qpoint_index;
// Get the iterator for the reference node.
typename GlobalType::TableType::TreeIterator rnode_it =
global.reference_table()->get_node_iterator(rnode);
core::table::DenseConstPoint rpoint;
core::table::DensePoint rpoint;
int rpoint_index;
// Interval for the pivot query point.
@@ -428,7 +428,7 @@ class KdeSummary {
qnode_it.Next(&qpoint, &qpoint_index);
bool skip = false;
if(prev_qpoint_index >= 0) {
core::table::DenseConstPoint prev_qpoint;
core::table::DensePoint prev_qpoint;
global.query_table()->get(prev_qpoint_index, &prev_qpoint);
double dist = sqrt(metric.DistanceSq(qpoint, prev_qpoint));
if(dist <= movement_threshold && movement_count < 5) {
@@ -73,17 +73,17 @@ class TestNbodySimulator {
// Loop over distinct 3-tuples.
core::gnp::TripleDistanceSq triple_distance_sq;
for(int i = 0; i < table.n_entries() - 2; i++) {
core::table::DenseConstPoint i_th_point;
core::table::DensePoint i_th_point;
table.get(i, &i_th_point);
triple_distance_sq.ReplaceOnePoint(metric_in, i_th_point, i, 0);
for(int j = i + 1; j < table.n_entries() - 1; j++) {
core::table::DenseConstPoint j_th_point;
core::table::DensePoint j_th_point;
table.get(j, &j_th_point);
triple_distance_sq.ReplaceOnePoint(metric_in, j_th_point, j, 1);
for(int k = j + 1; k < table.n_entries(); k++) {
core::table::DenseConstPoint k_th_point;
core::table::DensePoint k_th_point;
table.get(k, &k_th_point);
triple_distance_sq.ReplaceOnePoint(metric_in, k_th_point, k, 2);
@@ -229,7 +229,7 @@ class NbodySimulatorResult {
// Get the iterator for the node.
typename GlobalType::TableType::TreeIterator node_it =
global.table()->get_node_iterator(node);
core::table::DenseConstPoint qpoint;
core::table::DensePoint qpoint;
int qpoint_index;
// Look up the number of standard deviations.
@@ -418,7 +418,7 @@ class NbodySimulatorSummary {
int node_index_fix,
core::gnp::TripleDistanceSq *distance_sq_out) const {
core::table::DenseConstPoint point;
core::table::DensePoint point;
for(int i = 1; i < 3; i++) {
int index = (node_index_fix + i) % 3;
table.get(random_combination[index], &point);
@@ -553,10 +553,10 @@ class NbodySimulatorSummary {
const std::vector<double> &failure_probabilities,
int node_index,
ResultType *query_results,
const core::table::DenseConstPoint &query_point,
const core::table::DensePoint &query_point,
int qpoint_dfs_index,
int query_point_index,
const core::table::DenseConstPoint *previous_query_point,
const core::table::DensePoint *previous_query_point,
int *previous_query_point_index) {
// Sampled result for the current query point.