Clustering.hpp
Go to the documentation of this file.
1 /*
2  * STRUMPACK -- STRUctured Matrices PACKage, Copyright (c) 2014, The
3  * Regents of the University of California, through Lawrence Berkeley
4  * National Laboratory (subject to receipt of any required approvals
5  * from the U.S. Dept. of Energy). All rights reserved.
6  *
7  * If you have questions about your rights to use or distribute this
8  * software, please contact Berkeley Lab's Technology Transfer
9  * Department at TTD@lbl.gov.
10  *
11  * NOTICE. This software is owned by the U.S. Department of Energy. As
12  * such, the U.S. Government has been granted for itself and others
13  * acting on its behalf a paid-up, nonexclusive, irrevocable,
14  * worldwide license in the Software to reproduce, prepare derivative
15  * works, and perform publicly and display publicly. Beginning five
16  * (5) years after the date permission to assert copyright is obtained
17  * from the U.S. Department of Energy, and subject to any subsequent
18  * five (5) year renewals, the U.S. Government is granted for itself
19  * and others acting on its behalf a paid-up, nonexclusive,
20  * irrevocable, worldwide license in the Software to reproduce,
21  * prepare derivative works, distribute copies to the public, perform
22  * publicly and display publicly, and to permit others to do so.
23  *
24  * Developers: Pieter Ghysels, Francois-Henry Rouet, Xiaoye S. Li.
25  * (Lawrence Berkeley National Lab, Computational Research
26  * Division).
27  *
28  */
35 #ifndef STRUMPACK_CLUSTERING_HPP
36 #define STRUMPACK_CLUSTERING_HPP
37 
38 #include <random>
39 #include <vector>
40 
42 #include "dense/DenseMatrix.hpp"
43 
44 namespace strumpack {
45 
51  enum class ClusteringAlgorithm {
52  NATURAL,
53  TWO_MEANS,
54  KD_TREE,
55  PCA,
56  COBBLE
57  };
58 
65  inline std::string get_name(ClusteringAlgorithm c) {
66  switch (c) {
67  case ClusteringAlgorithm::NATURAL: return "natural";
68  case ClusteringAlgorithm::TWO_MEANS: return "2means";
69  case ClusteringAlgorithm::KD_TREE: return "kdtree";
70  case ClusteringAlgorithm::PCA: return "PCA";
71  case ClusteringAlgorithm::COBBLE: return "cobble";
72  default: return "unknown";
73  }
74  }
75 
82  inline ClusteringAlgorithm
83  get_clustering_algorithm(const std::string& c) {
84  if (c == "natural") return ClusteringAlgorithm::NATURAL;
85  else if (c == "2means") return ClusteringAlgorithm::TWO_MEANS;
86  else if (c == "kdtree") return ClusteringAlgorithm::KD_TREE;
87  else if (c == "pca") return ClusteringAlgorithm::PCA;
88  else if (c == "cobble") return ClusteringAlgorithm::COBBLE;
89  else {
90  std::cerr << "WARNING: binary tree clustering not recognized,"
91  << " setting to recursive 2 means (2means)."
92  << std::endl;
94  }
95  }
96 
97 
98  template<typename T> void
99  pca_partition(DenseMatrix<T>& p, std::vector<std::size_t>& nc, int* perm);
100  template<typename T> structured::ClusterTree
101  recursive_pca(DenseMatrix<T>& p, std::size_t cluster_size, int* perm);
102 
103  template<typename T> void
104  cobble_partition(DenseMatrix<T>& p, std::vector<std::size_t>& nc, int* perm);
105  template<typename T> structured::ClusterTree
106  recursive_cobble(DenseMatrix<T>& p, std::size_t cluster_size, int* perm);
107 
108  template<typename T> structured::ClusterTree
109  recursive_2_means(DenseMatrix<T>& p, std::size_t cluster_size,
110  int* perm, std::mt19937& generator);
111 
112  template<typename T> void
113  kd_partition(DenseMatrix<T>& p, std::vector<std::size_t>& nc,
114  std::size_t cluster_size, int* perm);
115  template<typename T> structured::ClusterTree
116  recursive_kd(DenseMatrix<T>& p, std::size_t cluster_size, int* perm);
117 
142  template<typename scalar_t>
145  std::vector<int>& perm, std::size_t cluster_size) {
147  perm.resize(p.cols());
148  std::iota(perm.begin(), perm.end(), 1);
149  switch (algo) {
151  tree.size = p.cols();
152  tree.refine(cluster_size);
153  } break;
155  std::mt19937 gen(1); // reproducible
156  tree = recursive_2_means(p, cluster_size, perm.data(), gen);
157  } break;
159  tree = recursive_kd(p, cluster_size, perm.data()); break;
161  tree = recursive_pca(p, cluster_size, perm.data()); break;
163  tree = recursive_cobble(p, cluster_size, perm.data()); break;
164  default:
165  std::cerr << "ERROR: clustering type not recognized." << std::endl;
166  }
167  return tree;
168  }
169 
170 
171 } // end namespace strumpack
172 
173 #endif // STRUMPACK_CLUSTERING_HPP
This file contains the ClusterTree class definition.
Contains the DenseMatrix and DenseMatrixWrapper classes, simple wrappers around BLAS/LAPACK style den...
This class represents a matrix, stored in column major format, to allow direct use of BLAS/LAPACK rou...
Definition: DenseMatrix.hpp:138
std::size_t cols() const
Definition: DenseMatrix.hpp:230
const scalar_t * data() const
Definition: DenseMatrix.hpp:241
The cluster tree, or partition tree that represents the partitioning of the rows or columns of a hier...
Definition: ClusterTree.hpp:67
const ClusterTree & refine(int leaf_size)
Definition: ClusterTree.hpp:104
int size
Definition: ClusterTree.hpp:73
Definition: StrumpackOptions.hpp:43
ClusteringAlgorithm get_clustering_algorithm(const std::string &c)
Definition: Clustering.hpp:83
ClusteringAlgorithm
Definition: Clustering.hpp:51
structured::ClusterTree binary_tree_clustering(ClusteringAlgorithm algo, DenseMatrix< scalar_t > &p, std::vector< int > &perm, std::size_t cluster_size)
Definition: Clustering.hpp:144
std::string get_name(ReorderingStrategy method)