<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 "core/table/transform.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, typename KernelAuxType>
DistributedTableType *DistributedKde <
DistributedTableType, KernelAuxType >::query_table() {
  return query_table_;
}

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

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

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

template<typename DistributedTableType, typename KernelAuxType>
void DistributedKde<DistributedTableType, KernelAuxType>::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, KernelAuxType > >
  distributed_dualtree_dfs;
  distributed_dualtree_dfs.Init(world_, *this);
  distributed_dualtree_dfs.set_work_params(
    arguments_in.leaf_size_,
    arguments_in.max_num_levels_to_serialize_,
    arguments_in.max_num_work_to_dequeue_per_stage_);

  // Compute the result and do post-normalize.
  distributed_dualtree_dfs.Compute(* arguments_in.metric_, result_out);
  result_out->Normalize(global_);

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

template<typename DistributedTableType, typename KernelAuxType>
void DistributedKde<DistributedTableType, KernelAuxType>::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_, reference_table_->n_entries(),
    (KernelAuxType *) NULL, arguments_in.bandwidth_,
    (typename GlobalType::MeanVariancePairListType *) NULL, is_monochromatic_,
    arguments_in.relative_error_, arguments_in.absolute_error_,
    arguments_in.probability_, false);
  global_.set_effective_num_reference_points(
    world_in, reference_table_, query_table_);
}

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

bool DistributedKdeArgumentParser::ConstructBoostVariableMap(
  boost::mpi::communicator &world,
  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(5),
    "Generate the datasets on the fly of the specified dimension."
  )(
    "random_generate_n_entries",
    boost::program_options::value<int>()->default_value(100000),
    "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("gaussian"),
    "Kernel function used by KDE.  One of:\n"
    "  epan, gaussian"
  )(
    "series_expansion_type",
    boost::program_options::value<std::string>()->default_value("hypercube"),
    "Series expansion type used to compress the kernel interaction. One of:\n"
    "  hypercube, multivariate"
  )(
    "bandwidth",
    boost::program_options::value<double>()->default_value(0.5),
    "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."
  )(
    "absolute_error",
    boost::program_options::value<double>()->default_value(0.0),
    "Absolute error for the approximation of KDE per each query point."
  )(<commit_msg>Changing options.<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 "core/table/transform.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, typename KernelAuxType>
DistributedTableType *DistributedKde <
DistributedTableType, KernelAuxType >::query_table() {
  return query_table_;
}

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

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

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

template<typename DistributedTableType, typename KernelAuxType>
void DistributedKde<DistributedTableType, KernelAuxType>::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, KernelAuxType > >
  distributed_dualtree_dfs;
  distributed_dualtree_dfs.Init(world_, *this);
  distributed_dualtree_dfs.set_work_params(
    arguments_in.leaf_size_,
    arguments_in.max_num_levels_to_serialize_,
    arguments_in.max_num_work_to_dequeue_per_stage_);

  // Compute the result and do post-normalize.
  distributed_dualtree_dfs.Compute(* arguments_in.metric_, result_out);
  result_out->Normalize(global_);

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

template<typename DistributedTableType, typename KernelAuxType>
void DistributedKde<DistributedTableType, KernelAuxType>::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_, reference_table_->n_entries(),
    (KernelAuxType *) NULL, arguments_in.bandwidth_,
    (typename GlobalType::MeanVariancePairListType *) NULL, is_monochromatic_,
    arguments_in.relative_error_, arguments_in.absolute_error_,
    arguments_in.probability_, false);
  global_.set_effective_num_reference_points(
    world_in, reference_table_, query_table_);
}

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

bool DistributedKdeArgumentParser::ConstructBoostVariableMap(
  boost::mpi::communicator &world,
  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(5),
    "Generate the datasets on the fly of the specified dimension."
  )(
    "random_generate_n_entries",
    boost::program_options::value<int>()->default_value(100000),
    "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"
  )(
    "series_expansion_type",
    boost::program_options::value<std::string>()->default_value("multivariate"),
    "Series expansion type used to compress the kernel interaction. One of:\n"
    "  hypercube, multivariate"
  )(
    "bandwidth",
    boost::program_options::value<double>()->default_value(0.5),
    "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."
  )(
    "absolute_error",
    boost::program_options::value<double>()->default_value(0.0),
    "Absolute error for the approximation of KDE per each query point."
  )(
{'fraction_non_alphanumeric': 0.11188750109179842, 'fraction_numerical': 0.0024456284391649926, 'mean_word_length': 5.803327391562686, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 97, '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': '4560158', 'n_tokens_mistral': 3726, 'n_tokens_neox': 3563, 'n_words': 677}