<commit_before>/** @file distributed_kde_dev.h
 *
 *  @author <NAME> (<EMAIL>)
 */

#ifndef MLPACK_DISTRIBUTED_KDE_DISTRIBUTED_KDE_DEV_H
#define MLPACK_DISTRIBUTED_KDE_DISTRIBUTED_KDE_DEV_H

#include "core/gnp/distributed_dualtree_dfs_dev.h"
#include "core/metric_kernels/lmetric.h"
#include "core/table/memory_mapped_file.h"
#include "mlpack/distributed_kde/distributed_kde.h"

namespace core {
namespace table {
extern core::table::MemoryMappedFile *global_m_file_;
};
};

namespace mlpack {
namespace distributed_kde {

template<typename DistributedTableType>
DistributedTableType *DistributedKde<DistributedTableType>::query_table() {
  return query_table_;
}

template<typename DistributedTableType>
DistributedTableType *DistributedKde<DistributedTableType>::reference_table() {
  return reference_table_;
}

template<typename DistributedTableType>
typename DistributedKde<DistributedTableType>::GlobalType &
DistributedKde<DistributedTableType>::global() {
  return global_;
}

template<typename DistributedTableType>
bool DistributedKde<DistributedTableType>::is_monochromatic() const {
  return is_monochromatic_;
}

template<typename DistributedTableType>
void DistributedKde<DistributedTableType>::Compute(
  const mlpack::distributed_kde::DistributedKdeArguments <
  DistributedTableType > &arguments_in,
  mlpack::kde::KdeResult< std::vector<double> > *result_out) {

  // Barrier so that every process is here.
  world_->barrier();

  boost::mpi::timer timer;

  // Instantiate a dual-tree algorithm of the KDE.
  core::gnp::DistributedDualtreeDfs <
  mlpack::distributed_kde::DistributedKde<DistributedTableType> >
  distributed_dualtree_dfs;
  distributed_dualtree_dfs.Init(world_, *this);

  // Compute the result.
  distributed_dualtree_dfs.Compute(* arguments_in.metric_, result_out);

  if(world_->rank() == 0) {
    printf("Spent %g seconds in computation.\n", timer.elapsed());
  }
}

template<typename DistributedTableType>
void DistributedKde<DistributedTableType>::Init(
  boost::mpi::communicator &world_in,
  mlpack::distributed_kde::DistributedKdeArguments <
  DistributedTableType > &arguments_in) {

  world_ = &world_in;
  reference_table_ = arguments_in.reference_table_;
  if(arguments_in.query_table_ == NULL) {
    is_monochromatic_ = true;
    query_table_ = reference_table_;
  }
  else {
    is_monochromatic_ = false;
    query_table_ = arguments_in.query_table_;
  }

  // Declare the global constants.
  global_.Init(
    reference_table_, query_table_, arguments_in.bandwidth_, is_monochromatic_,
    arguments_in.relative_error_, arguments_in.probability_,
    arguments_in.kernel_, false);
}

template<typename DistributedTableType>
void DistributedKde<DistributedTableType>::set_bandwidth(
  double bandwidth_in) {
  global_.set_bandwidth(bandwidth_in);
}

template<typename DistributedTableType>
bool DistributedKde<DistributedTableType>::ConstructBoostVariableMap_(
  const std::vector<std::string> &args,
  boost::program_options::variables_map *vm) {

  boost::program_options::options_description desc("Available options");
  desc.add_options()(
    "help", "Print this information."
  )(
    "references_in",
    boost::program_options::value<std::string>()->default_value(
      "random_dataset.csv"),
    "REQUIRED file containing reference data."
  )(
    "queries_in",
    boost::program_options::value<std::string>(),
    "OPTIONAL file containing query positions.  If omitted, KDE computes "
    "the leave-one-out density at each reference point."
  )(
    "random_generate_n_attributes",
    boost::program_options::value<int>()->default_value(3),
    "Generate the datasets on the fly of the specified dimension."
  )(
    "random_generate_n_entries",
    boost::program_options::value<int>()->default_value(20),
    "Generate the datasets on the fly of the specified number of points."
  )(
    "densities_out",
    boost::program_options::value<std::string>()->default_value(
      "densities_out.csv"),
    "OPTIONAL file to store computed densities."
  )(
    "kernel",
    boost::program_options::value<std::string>()->default_value("epan"),
    "Kernel function used by KDE.  One of:\n"
    "  epan, gaussian"
  )(
    "bandwidth",
    boost::program_options::value<double>()->default_value(0.2),
    "OPTIONAL kernel bandwidth, if you set --bandwidth_selection flag, "
    "then the --bandwidth will be ignored."
  )
  (
    "probability",
    boost::program_options::value<double>()->default_value(1.0),
    "Probability guarantee for the approximation of KDE."
  )(
    "relative_error",
    boost::program_options::value<double>()->default_value(0.01),
    "Relative error for the approximation of KDE."
  )(
    "leaf_size",
    boost::program_options::value<int>()->default_value(20),
    "Maximum number of points at a leaf of the tree."
  )(
    "top_tree_sample_probability",
    boost::program_options::value<double>()->default_value(0.2),
    "The portion of points sampled on each MPI process for building the top tree."
  );

  boost::program_options::command_line_parser clp(args);
  clp.style(boost::program_options::command_line_style::default_style
            ^ boost::program_options::command_line_style::allow_guessing);
  try {
    boost::program_options::store(clp.options(desc).run(), *vm);
  }<commit_msg>Default bandwidth value changed for experiments.<commit_after>/** @file distributed_kde_dev.h
 *
 *  @author <NAME> (<EMAIL>)
 */

#ifndef MLPACK_DISTRIBUTED_KDE_DISTRIBUTED_KDE_DEV_H
#define MLPACK_DISTRIBUTED_KDE_DISTRIBUTED_KDE_DEV_H

#include "core/gnp/distributed_dualtree_dfs_dev.h"
#include "core/metric_kernels/lmetric.h"
#include "core/table/memory_mapped_file.h"
#include "mlpack/distributed_kde/distributed_kde.h"

namespace core {
namespace table {
extern core::table::MemoryMappedFile *global_m_file_;
};
};

namespace mlpack {
namespace distributed_kde {

template<typename DistributedTableType>
DistributedTableType *DistributedKde<DistributedTableType>::query_table() {
  return query_table_;
}

template<typename DistributedTableType>
DistributedTableType *DistributedKde<DistributedTableType>::reference_table() {
  return reference_table_;
}

template<typename DistributedTableType>
typename DistributedKde<DistributedTableType>::GlobalType &
DistributedKde<DistributedTableType>::global() {
  return global_;
}

template<typename DistributedTableType>
bool DistributedKde<DistributedTableType>::is_monochromatic() const {
  return is_monochromatic_;
}

template<typename DistributedTableType>
void DistributedKde<DistributedTableType>::Compute(
  const mlpack::distributed_kde::DistributedKdeArguments <
  DistributedTableType > &arguments_in,
  mlpack::kde::KdeResult< std::vector<double> > *result_out) {

  // Barrier so that every process is here.
  world_->barrier();

  boost::mpi::timer timer;

  // Instantiate a dual-tree algorithm of the KDE.
  core::gnp::DistributedDualtreeDfs <
  mlpack::distributed_kde::DistributedKde<DistributedTableType> >
  distributed_dualtree_dfs;
  distributed_dualtree_dfs.Init(world_, *this);

  // Compute the result.
  distributed_dualtree_dfs.Compute(* arguments_in.metric_, result_out);

  if(world_->rank() == 0) {
    printf("Spent %g seconds in computation.\n", timer.elapsed());
  }
}

template<typename DistributedTableType>
void DistributedKde<DistributedTableType>::Init(
  boost::mpi::communicator &world_in,
  mlpack::distributed_kde::DistributedKdeArguments <
  DistributedTableType > &arguments_in) {

  world_ = &world_in;
  reference_table_ = arguments_in.reference_table_;
  if(arguments_in.query_table_ == NULL) {
    is_monochromatic_ = true;
    query_table_ = reference_table_;
  }
  else {
    is_monochromatic_ = false;
    query_table_ = arguments_in.query_table_;
  }

  // Declare the global constants.
  global_.Init(
    reference_table_, query_table_, arguments_in.bandwidth_, is_monochromatic_,
    arguments_in.relative_error_, arguments_in.probability_,
    arguments_in.kernel_, false);
}

template<typename DistributedTableType>
void DistributedKde<DistributedTableType>::set_bandwidth(
  double bandwidth_in) {
  global_.set_bandwidth(bandwidth_in);
}

template<typename DistributedTableType>
bool DistributedKde<DistributedTableType>::ConstructBoostVariableMap_(
  const std::vector<std::string> &args,
  boost::program_options::variables_map *vm) {

  boost::program_options::options_description desc("Available options");
  desc.add_options()(
    "help", "Print this information."
  )(
    "references_in",
    boost::program_options::value<std::string>()->default_value(
      "random_dataset.csv"),
    "REQUIRED file containing reference data."
  )(
    "queries_in",
    boost::program_options::value<std::string>(),
    "OPTIONAL file containing query positions.  If omitted, KDE computes "
    "the leave-one-out density at each reference point."
  )(
    "random_generate_n_attributes",
    boost::program_options::value<int>()->default_value(3),
    "Generate the datasets on the fly of the specified dimension."
  )(
    "random_generate_n_entries",
    boost::program_options::value<int>()->default_value(20),
    "Generate the datasets on the fly of the specified number of points."
  )(
    "densities_out",
    boost::program_options::value<std::string>()->default_value(
      "densities_out.csv"),
    "OPTIONAL file to store computed densities."
  )(
    "kernel",
    boost::program_options::value<std::string>()->default_value("epan"),
    "Kernel function used by KDE.  One of:\n"
    "  epan, gaussian"
  )(
    "bandwidth",
    boost::program_options::value<double>()->default_value(0.05),
    "OPTIONAL kernel bandwidth, if you set --bandwidth_selection flag, "
    "then the --bandwidth will be ignored."
  )
  (
    "probability",
    boost::program_options::value<double>()->default_value(1.0),
    "Probability guarantee for the approximation of KDE."
  )(
    "relative_error",
    boost::program_options::value<double>()->default_value(0.01),
    "Relative error for the approximation of KDE."
  )(
    "leaf_size",
    boost::program_options::value<int>()->default_value(20),
    "Maximum number of points at a leaf of the tree."
  )(
    "top_tree_sample_probability",
    boost::program_options::value<double>()->default_value(0.2),
    "The portion of points sampled on each MPI process for building the top tree."
  );

  boost::program_options::command_line_parser clp(args);
  clp.style(boost::program_options::command_line_style::default_style
            ^ boost::program_options::command_line_style::allow_guessing);
  try {
    boost::program_options::store(clp.options(desc).run(), *vm);
  }
{'fraction_non_alphanumeric': 0.11980048936570675, 'fraction_numerical': 0.002917372482589874, 'mean_word_length': 5.621183800623053, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5664628', 'n_tokens_mistral': 3414, 'n_tokens_neox': 3278, 'n_words': 613}