Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using rvalue references to set a given reference tree in FastMKS and DualTreeBoruvka #799

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/mlpack/methods/emst/dtb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class DualTreeBoruvka
* @param tree Pre-built tree.
* @param metric An optional instantiated metric to use.
*/
DualTreeBoruvka(Tree* tree,
const MetricType metric = MetricType());
mlpack_deprecated DualTreeBoruvka(Tree* tree,
const MetricType metric = MetricType());

/**
* Create the DualTreeBoruvka object with an already initialized tree. This
Expand All @@ -183,7 +183,7 @@ class DualTreeBoruvka
* @param tree Pre-built tree.
* @param dataset Dataset corresponding to the pre-built tree.
*/
DualTreeBoruvka(Tree& tree,
DualTreeBoruvka(const Tree& tree,
const MetricType metric = MetricType());

/**
Expand Down Expand Up @@ -249,7 +249,7 @@ class DualTreeBoruvka
* The CleanupHelper() method taking a reference to the tree is preferred.
*
*/
void CleanupHelper(Tree* tree);
mlpack_deprecated void CleanupHelper(Tree* tree);

/**
* This function resets the values in the nodes of the tree nearest neighbor
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/emst/dtb_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ template<
typename TreeStatType,
typename TreeMatType> class TreeType>
DualTreeBoruvka<MetricType, MatType, TreeType>::DualTreeBoruvka(
Tree& tree,
const Tree& tree,
const MetricType metric) :
tree(new Tree(tree)),
data(&this->tree->Dataset()),
Expand Down
25 changes: 12 additions & 13 deletions src/mlpack/methods/fastmks/fastmks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class FastMKS
* @param single Whether or not to run single-tree search.
* @param naive Whether or not to run brute-force (naive) search.
*/
FastMKS(Tree* referenceTree,
const bool singleMode = false);
mlpack_deprecated FastMKS(Tree* referenceTree,
const bool singleMode = false);

/**
* Create the FastMKS object with an already-initialized tree built on the
Expand All @@ -139,7 +139,7 @@ class FastMKS
* @param single Whether or not to run single-tree search.
* @param naive Whether or not to run brute-force (naive) search.
*/
FastMKS(Tree& referenceTree,
FastMKS(const Tree& referenceTree,
const bool singleMode = false);

/**
Expand Down Expand Up @@ -192,19 +192,18 @@ class FastMKS
*
* @param tree Tree to use as reference data.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mark this function deprecated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To mark a method deprecated, also add mlpack_deprecated to the function definition please. i.e. mlpack_deprecated void Train(...).

*/
void Train(Tree* referenceTree);
mlpack_deprecated void Train(Tree* referenceTree);

/**
* Train the FastMKS model on the given reference tree. This takes ownership
* of the tree, so you do not need to delete it! This will throw an exception
* if the model is searching in naive mode (i.e. if Naive() == true).
* Train the FastMKS model on the given reference tree. This will throw an
* exception if the model is searching in naive mode (i.e. if Naive() == true).
*
* This method will copy the given tree. You can avoid this copy by using the
* Train() method that takes a rvalue reference to the tree.
*
* @param tree Tree to use as reference data.
*/
void Train(Tree& referenceTree);
void Train(const Tree& referenceTree);

/**
* Train the FastMKS model on the given reference tree. This takes ownership
Expand Down Expand Up @@ -267,10 +266,10 @@ class FastMKS
* @param indices Matrix to store resulting indices of max-kernel search in.
* @param kernels Matrix to store resulting max-kernel values in.
*/
void Search(Tree* querySet,
const size_t k,
arma::Mat<size_t>& indices,
arma::mat& kernels);
mlpack_deprecated void Search(Tree* querySet,
const size_t k,
arma::Mat<size_t>& indices,
arma::mat& kernels);

/**
* Search for the points in the reference set with maximum kernel evaluation
Expand All @@ -294,7 +293,7 @@ class FastMKS
* @param indices Matrix to store resulting indices of max-kernel search in.
* @param kernels Matrix to store resulting max-kernel values in.
*/
void Search(Tree& querySet,
void Search(Tree& queryTree,
const size_t k,
arma::Mat<size_t>& indices,
arma::mat& kernels);
Expand Down
10 changes: 5 additions & 5 deletions src/mlpack/methods/fastmks/fastmks_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ template<typename KernelType,
template<typename TreeMetricType,
typename TreeStatType,
typename TreeMatType> class TreeType>
FastMKS<KernelType, MatType, TreeType>::FastMKS(Tree& referenceTree,
FastMKS<KernelType, MatType, TreeType>::FastMKS(const Tree& referenceTree,
const bool singleMode) :
referenceTree(new Tree(referenceTree)),
referenceSet(&this->referenceTree->Dataset()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't set referenceSet until the tree is set. At the time of initialization here, this->referenceTree is not set, so we are setting referenceSet to something invalid. This is probably the cause of your invalid memory access in the tests.

Expand Down Expand Up @@ -225,7 +225,7 @@ template<typename KernelType,
template<typename TreeMetricType,
typename TreeStatType,
typename TreeMatType> class TreeType>
void FastMKS<KernelType, MatType, TreeType>::Train(Tree& tree)
void FastMKS<KernelType, MatType, TreeType>::Train(const Tree& tree)
{
if (naive)
throw std::invalid_argument("cannot call FastMKS::Train() with a tree when "
Expand Down Expand Up @@ -358,7 +358,7 @@ void FastMKS<KernelType, MatType, TreeType>::Search(
Tree queryTree(querySet);
Timer::Stop("tree_building");

Search(&queryTree, k, indices, kernels);
Search(queryTree, k, indices, kernels);
}

template<typename KernelType,
Expand All @@ -372,7 +372,7 @@ void FastMKS<KernelType, MatType, TreeType>::Search(
arma::Mat<size_t>& indices,
arma::mat& kernels)
{
Search(*queryTree, k, indices, kernels);
Search(*queryTree, k, indices, kernels);
}

template<typename KernelType,
Expand Down Expand Up @@ -505,7 +505,7 @@ void FastMKS<KernelType, MatType, TreeType>::Search(
// Dual-tree implementation.
Timer::Stop("computing_products");

Search(std::move(referenceTree), k, indices, kernels);
Search(*referenceTree, k, indices, kernels);
}

//! Serialize the model.
Expand Down