Serialization for tree in progress.
This commit is contained in:
@@ -15,6 +15,7 @@ namespace table {
|
||||
class DenseMatrix {
|
||||
|
||||
private:
|
||||
|
||||
boost::interprocess::offset_ptr<double> ptr_;
|
||||
|
||||
int n_rows_;
|
||||
|
||||
@@ -57,7 +57,12 @@ class DistributedTable: public boost::noncopyable {
|
||||
void AssignLeafNodes_(
|
||||
const core::metric_kernels::AbstractMetric &metric_in,
|
||||
const std::vector<TreeType *> &top_leaf_nodes,
|
||||
std::vector<int> *point_assignments) {
|
||||
std::vector<int> *point_assignments,
|
||||
bool *points_assigned_to_node) {
|
||||
|
||||
for(unsigned int i = 0; i < top_leaf_nodes.size(); i++) {
|
||||
points_assigned_to_node[i] = 0;
|
||||
}
|
||||
|
||||
// Loop through each point and find the closest leaf node.
|
||||
for(int i = 0; i < owned_table_->n_entries(); i++) {
|
||||
@@ -65,6 +70,8 @@ class DistributedTable: public boost::noncopyable {
|
||||
owned_table_->get(i, &point);
|
||||
|
||||
// Loop through each leaf node.
|
||||
double min_squared_mid_distance = std::numeric_limits<double>::max();
|
||||
int min_index = -1;
|
||||
for(unsigned int j = 0; j < top_leaf_nodes.size(); j++) {
|
||||
const typename TreeType::BoundType &leaf_node_bound =
|
||||
top_leaf_nodes[j]->bound();
|
||||
@@ -72,7 +79,15 @@ class DistributedTable: public boost::noncopyable {
|
||||
// Compute the squared mid-distance.
|
||||
double squared_mid_distance = leaf_node_bound.MidDistanceSq(
|
||||
metric_in, point);
|
||||
if(squared_mid_distance < min_squared_mid_distance) {
|
||||
min_squared_mid_distance = squared_mid_distance;
|
||||
min_index = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Output the assignments.
|
||||
point_assignments->push_back(min_index);
|
||||
points_assigned_to_node[min_index] = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,7 +329,7 @@ class DistributedTable: public boost::noncopyable {
|
||||
|
||||
// The master builds the top tree, and sends the leaf nodes to
|
||||
// the rest.
|
||||
std::vector<TableType *> top_leaf_nodes;
|
||||
std::vector<TreeType *> top_leaf_nodes;
|
||||
if(table_outbox_group_comm.rank() == 0) {
|
||||
sampled_table.IndexData(
|
||||
metric_in, leaf_size, table_outbox_group_comm.size());
|
||||
@@ -332,7 +347,35 @@ class DistributedTable: public boost::noncopyable {
|
||||
|
||||
// Assign each point to one of the leaf nodes.
|
||||
std::vector<int> point_assignments;
|
||||
AssignLeafNodes_(metric_in, top_leaf_nodes, &point_assignments);
|
||||
bool *local_points_assigned_to_node = new bool[top_leaf_nodes.size()];
|
||||
bool *points_assigned_to_node = NULL;
|
||||
AssignLeafNodes_(
|
||||
metric_in, top_leaf_nodes, &point_assignments,
|
||||
local_points_assigned_to_node);
|
||||
|
||||
// Do a reduction to find whether at least one point has been
|
||||
// assigned to each partition.
|
||||
if(table_outbox_group_comm.rank() == 0) {
|
||||
points_assigned_to_node = new bool[top_leaf_nodes.size()];
|
||||
boost::mpi::reduce(
|
||||
table_outbox_group_comm, local_points_assigned_to_node,
|
||||
top_leaf_nodes.size(), points_assigned_to_node,
|
||||
std::logical_or<bool>(), 0);
|
||||
|
||||
|
||||
printf("Checking:\n");
|
||||
for(unsigned int i = 0; i < top_leaf_nodes.size(); i++) {
|
||||
printf(" %d ", points_assigned_to_node[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
else {
|
||||
boost::mpi::reduce(
|
||||
table_outbox_group_comm, local_points_assigned_to_node,
|
||||
top_leaf_nodes.size(), std::logical_or<bool>(), 0);
|
||||
}
|
||||
|
||||
delete[] points_assigned_to_node;
|
||||
}
|
||||
|
||||
void get(
|
||||
|
||||
@@ -26,6 +26,13 @@
|
||||
*/
|
||||
|
||||
#include <boost/interprocess/offset_ptr.hpp>
|
||||
#include "core/table/memory_mapped_file.h"
|
||||
|
||||
namespace core {
|
||||
namespace table {
|
||||
extern core::table::MemoryMappedFile *global_m_file_;
|
||||
};
|
||||
};
|
||||
|
||||
namespace core {
|
||||
namespace tree {
|
||||
@@ -65,15 +72,38 @@ class GeneralBinarySpaceTree {
|
||||
friend class boost::serialization::access;
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive &ar, const unsigned int version) {
|
||||
void save(Archive &ar, const unsigned int version) const {
|
||||
ar & bound_;
|
||||
ar & begin_;
|
||||
ar & count_;
|
||||
ar & left_;
|
||||
ar & right_;
|
||||
ar & stat_;
|
||||
GeneralBinarySpaceTree *left_ptr = left_.get();
|
||||
GeneralBinarySpaceTree *right_ptr = right_.get();
|
||||
ar & left_ptr;
|
||||
ar & right_ptr;
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void load(Archive &ar, const unsigned int version) {
|
||||
ar & bound_;
|
||||
ar & begin_;
|
||||
ar & count_;
|
||||
ar & stat_;
|
||||
GeneralBinarySpaceTree *left_ptr_in =
|
||||
(core::table::global_m_file_) ?
|
||||
core::table::global_m_file_->Construct<GeneralBinarySpaceTree>() :
|
||||
new GeneralBinarySpaceTree();
|
||||
GeneralBinarySpaceTree *right_ptr_in =
|
||||
(core::table::global_m_file_) ?
|
||||
core::table::global_m_file_->Construct<GeneralBinarySpaceTree>() :
|
||||
new GeneralBinarySpaceTree();
|
||||
ar & left_ptr_in;
|
||||
ar & right_ptr_in;
|
||||
left_ = left_ptr_in;
|
||||
right_ = right_ptr_in;
|
||||
}
|
||||
BOOST_SERIALIZATION_SPLIT_MEMBER()
|
||||
|
||||
~GeneralBinarySpaceTree() {
|
||||
if(left_ != NULL) {
|
||||
delete left_.get();
|
||||
|
||||
+6
-2
@@ -199,11 +199,15 @@ int main(int argc, char *argv[]) {
|
||||
// Wait until the memory allocator is in synch.
|
||||
world.barrier();
|
||||
|
||||
// Read the distributed table once per each compute node, and put a
|
||||
// barrier.
|
||||
// Read the distributed table once per each compute node, and build
|
||||
// the tree, and barrier.
|
||||
if(membership_key == 0) {
|
||||
distributed_table =
|
||||
InitDistributedTable(world, local_group_comm);
|
||||
|
||||
core::metric_kernels::LMetric<2> l2_metric;
|
||||
distributed_table->IndexData(
|
||||
l2_metric, local_group_comm, 3, 0.5);
|
||||
}
|
||||
world.barrier();
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
#define MLPACK_KDE_KDE_DUALTREE_H
|
||||
|
||||
#include <armadillo>
|
||||
#include "boost/math/distributions/normal.hpp"
|
||||
#include <boost/math/distributions/normal.hpp>
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include "core/monte_carlo/mean_variance_pair.h"
|
||||
#include "core/metric_kernels/kernel.h"
|
||||
#include "core/tree/statistic.h"
|
||||
@@ -17,6 +18,10 @@ namespace mlpack {
|
||||
namespace kde {
|
||||
class KdePostponed {
|
||||
|
||||
private:
|
||||
|
||||
friend class boost::serialization::access;
|
||||
|
||||
public:
|
||||
|
||||
double densities_l_;
|
||||
@@ -27,6 +32,14 @@ class KdePostponed {
|
||||
|
||||
double used_error_;
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive &ar, const unsigned int version) {
|
||||
ar & densities_l_;
|
||||
ar & densities_u_;
|
||||
ar & pruned_;
|
||||
ar & used_error_;
|
||||
}
|
||||
|
||||
KdePostponed() {
|
||||
}
|
||||
|
||||
@@ -354,6 +367,10 @@ class KdeDelta {
|
||||
|
||||
class KdeSummary {
|
||||
|
||||
private:
|
||||
|
||||
friend class boost::serialization::access;
|
||||
|
||||
public:
|
||||
|
||||
double densities_l_;
|
||||
@@ -364,6 +381,14 @@ class KdeSummary {
|
||||
|
||||
double used_error_u_;
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive &ar, const unsigned int version) {
|
||||
ar & densities_l_;
|
||||
ar & densities_u_;
|
||||
ar & pruned_l_;
|
||||
ar & used_error_u_;
|
||||
}
|
||||
|
||||
KdeSummary() {
|
||||
SetZero();
|
||||
}
|
||||
@@ -564,6 +589,9 @@ class KdeSummary {
|
||||
class KdeStatistic {
|
||||
|
||||
private:
|
||||
|
||||
friend class boost::serialization::access;
|
||||
|
||||
KdeStatistic(const KdeStatistic &stat_in) {
|
||||
}
|
||||
|
||||
@@ -573,6 +601,12 @@ class KdeStatistic {
|
||||
|
||||
mlpack::kde::KdeSummary summary_;
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive &ar, const unsigned int version) {
|
||||
ar & postponed_;
|
||||
ar & summary_;
|
||||
}
|
||||
|
||||
KdeStatistic() {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user