planc
Parallel Lowrank Approximation with Non-negativity Constraints
parsecommandline.hpp
Go to the documentation of this file.
1 /* Copyright Ramakrishnan Kannan 2017 */
2 
3 #ifndef COMMON_PARSECOMMANDLINE_HPP_
4 #define COMMON_PARSECOMMANDLINE_HPP_
5 
6 #include <getopt.h>
7 #include <armadillo>
8 #include <iostream>
9 #include <sstream>
10 #include <string>
12 
13 namespace planc {
15  private:
16  int m_argc;
17  char **m_argv;
18 
19  // common to all algorithms.
20  algotype m_lucalgo;
21  normtype m_input_normalization;
22  bool m_compute_error;
23  int m_num_it;
24  int m_num_k_blocks;
25  bool m_dim_tree;
26 
27  // file names
28  std::string m_Afile_name;
29  std::string m_outputfile_name;
30  // std::string m_init_file_name;
31 
32  // nmf related values
33  UWORD m_k;
34  UWORD m_globalm;
35  UWORD m_globaln;
36 
37  // algo related values
38  FVEC m_regW;
39  FVEC m_regH;
40  float m_sparsity;
41 
42  // distnmf related values
43  int m_pr;
44  int m_pc;
45 
46  // dist ntf
47  int m_num_modes;
48  UVEC m_dimensions;
49  UVEC m_proc_grids;
50  FVEC m_regularizers;
51 
52  void parseArrayofString(const char opt, const char *input) {
53  std::stringstream ss(input);
54  std::string s;
55  int i = 0;
56  if (this->m_num_modes == 0) {
57  while (getline(ss, s, ' ')) {
58  i++;
59  }
60  this->m_num_modes = i;
61  this->m_dimensions = arma::zeros<UVEC>(this->m_num_modes);
62  this->m_regularizers = arma::zeros<FVEC>(2 * this->m_num_modes);
63  this->m_proc_grids = arma::ones<UVEC>(this->m_num_modes);
64  }
65  i = 0;
66  ss.clear();
67  ss.str(input);
68  while (getline(ss, s, ' ')) {
69  switch (opt) {
70  case 'd':
71  this->m_dimensions(i++) = ::atoi(s.c_str());
72  break;
73  case 'r':
74  this->m_regularizers(i++) = ::atof(s.c_str());
75  break;
76  case 'p':
77  this->m_proc_grids(i++) = ::atoi(s.c_str());
78  break;
79  default:
80  INFO << "wrong option::" << opt << "::values::" << input << std::endl;
81  }
82  }
83  }
84 
85  public:
92  ParseCommandLine(int argc, char **argv) : m_argc(argc), m_argv(argv) {
93  this->m_num_modes = 0;
94  this->m_pr = 1;
95  this->m_pc = 1;
96  this->m_regW = arma::zeros<FVEC>(2);
97  this->m_regH = arma::zeros<FVEC>(2);
98  this->m_num_k_blocks = 1;
99  this->m_k = 20;
100  this->m_num_it = 20;
101  this->m_lucalgo = ANLSBPP;
102  this->m_compute_error = 0;
103  this->m_input_normalization = NONE;
104  this->m_dim_tree = 1;
105  }
107  void parseplancopts() {
108  int opt, long_index;
109  while ((opt = getopt_long(this->m_argc, this->m_argv,
110  "a:d:e:i:k:o:p:r:s:t:", plancopts,
111  &long_index)) != -1) {
112  switch (opt) {
113  case 'a':
114  this->m_lucalgo = static_cast<algotype>(atoi(optarg));
115  break;
116  case 'e':
117  this->m_compute_error = atoi(optarg);
118  break;
119  case 'i': {
120  std::string temp = std::string(optarg);
121  this->m_Afile_name = temp;
122  break;
123  }
124  case 'k':
125  this->m_k = atoi(optarg);
126  break;
127  case 'o': {
128  std::string temp = std::string(optarg);
129  this->m_outputfile_name = temp;
130  break;
131  }
132  case 'd':
133  case 'r':
134  case 'p':
135  parseArrayofString(opt, optarg);
136  break;
137  case 's':
138  this->m_sparsity = atof(optarg);
139  break;
140  case 't':
141  this->m_num_it = atoi(optarg);
142  break;
143  case NUMKBLOCKS:
144  this->m_num_k_blocks = atoi(optarg);
145  break;
146  case NORMALIZATION: {
147  std::string temp = std::string(optarg);
148  if (temp.compare("l2") == 0) {
149  this->m_input_normalization = normtype::L2NORM;
150  } else if (temp.compare("max") == 0) {
151  this->m_input_normalization = normtype::MAXNORM;
152  }
153  break;
154  }
155  case DIMTREE:
156  this->m_dim_tree = atoi(optarg);
157  break;
158  default:
159  std::cout << "failed while processing argument:" << optarg
160  << std::endl;
161  print_usage();
162  exit(EXIT_FAILURE);
163  }
164  }
165  // properly initialize the values for nmf cases now.
166  if (this->m_num_modes == 2) {
167  this->m_globalm = this->m_dimensions(0);
168  this->m_globaln = this->m_dimensions(1);
169  this->m_regW(0) = this->m_regularizers(0);
170  this->m_regW(1) = this->m_regularizers(1);
171  this->m_regH(0) = this->m_regularizers(2);
172  this->m_regH(1) = this->m_regularizers(3);
173  this->m_pr = this->m_proc_grids(0);
174  this->m_pc = this->m_proc_grids(1);
175  }
176  }
177 
179 
180  void printConfig() {
181  std::cout << "a::" << this->m_lucalgo << "::i::" << this->m_Afile_name
182  << "::k::" << this->m_k << "::m::" << this->m_globalm
183  << "::n::" << this->m_globaln << "::t::" << this->m_num_it
184  << "::pr::" << this->m_pr << "::pc::" << this->m_pc
185  << "::error::" << this->m_compute_error << "::regW::"
186  << "l2::" << this->m_regW(0) << "::l1::" << this->m_regW(1)
187  << "::regH::"
188  << "l2::" << this->m_regH(0) << "::l1::" << this->m_regH(1)
189  << "::num_k_blocks::" << m_num_k_blocks
190  << "::dimensions::" << this->m_dimensions
191  << "::procs::" << this->m_proc_grids
192  << "::regularizers::" << this->m_regularizers
193  << "::input normalization::" << this->m_input_normalization
194  << "::dimtree::" << this->m_dim_tree << std::endl;
195  }
196 
197  void print_usage() {
198  INFO << std::endl;
199  INFO << "distnmf usage:" << std::endl;
200  INFO << "for short arguments like -i do not use equals sign, eg -t 10"
201  << std::endl
202  << "for long arguments like --pr give key=value pair, eg --pr=4"
203  << std::endl
204  << "algorithm codes 0-MU2D, 1-HALS2D, 2-ANLSBPP2D, 3-NAIVEANLSBPP"
205  << std::endl;
206  // mpirun -np 12 distnmf algotype lowrank m n numIteration pr pc
207  INFO << "Usage 1: mpirun -np 6 distnmf -a 0/1/2/3 -k 50"
208  << "-i rand_uniform/rand_normal/rand_lowrank --dimtree 1"
209  << "-d \"21600 14400\" -t 10 -p \"3 2\" "
210  << "--normalization \"l2\" "
211  << "-r \"0.0001 0 0 0.0001\" " << std::endl;
212  // mpirun -np 12 distnmf algotype lowrank AfileName numIteration pr pc
213  INFO << "Usage 2: mpirun -np 6 distnmf -a 0/1/2/3 -k 50 --dimtree 1"
214  << "-i Ainput -t 10 -p \"3 2\" "
215  << "--normalization \"max\" "
216  << "-r \"0.0001 0 0 0.0001\" " << std::endl;
217  // mpirun -np 12 distnmf algotype lowrank Afile nmfoutput numIteration pr pc
218  INFO << "Usage 3: mpirun -np 6 distnmf -a 0/1/2/3 -k 50 --dimtree 1"
219  << "-i Ainput -o nmfoutput -t 10 -p \"3 2\" "
220  << "-r \"0.0001 0 0 0.0001\" " << std::endl;
221  // mpirun -np 12 distnmf algotype lowrank Afile nmfoutput numIteration pr pc
222  // s
223  INFO << "Usage 4: mpirun -np 6 distnmf -a 0/1/2/3 -k 50 --dimtree 1"
224  << "-i Ainput -o nmfoutput -t 10 -p \"3 2\" --sparsity=0.3"
225  << "-r \"0.0001 0 0 0.0001\" " << std::endl;
226  }
228  UWORD lowrankk() { return m_k; }
230  UWORD globalm() { return m_globalm; }
232  UWORD globaln() { return m_globaln; }
238  FVEC regW() { return m_regW; }
244  FVEC regH() { return m_regH; }
246  algotype lucalgo() { return m_lucalgo; }
252  UVEC processor_grids() { return m_proc_grids; }
259  FVEC regularizers() { return m_regularizers; }
264  UVEC dimensions() { return m_dimensions; }
265  int num_k_blocks() { return m_num_k_blocks; }
267  int iterations() { return m_num_it; }
269  float sparsity() { return m_sparsity; }
271  std::string input_file_name() { return m_Afile_name; }
276  std::string output_file_name() { return m_outputfile_name; }
281  int pr() { return m_pr; }
286  int pc() { return m_pc; }
288  int num_modes() { return m_num_modes; }
293  bool dim_tree() { return m_dim_tree; }
295  bool compute_error() { return m_compute_error; }
297  normtype input_normalization() { return this->m_input_normalization; }
298 }; // ParseCommandLine
299 } // namespace planc
300 
301 #endif // COMMON_PARSECOMMANDLINE_HPP_
#define NUMKBLOCKS
Definition: utils.h:12
FVEC regW()
L2 regularization as the first parameter and L1 as second for left lowrank factor W...
algotype lucalgo()
Returns the NMF algorithm to run. Passed as parameter –algo or -a.
#define DIMTREE
std::string output_file_name()
Returns output file name.
int pc()
Returns the number of processor columns.
Definition: utils.h:12
float sparsity()
Input parameter for generating sparse matrix. Passed as -s or –sparsity.
int num_modes()
Returns number of modes in tensors. For matrix it is two.
struct option plancopts[]
bool compute_error()
Returns whether to compute error not. Passed as parameter -e or –error.
int iterations()
Returns number of iterations. passed as -t or –iter.
#define FVEC
Definition: utils.h:55
algotype
Definition: utils.h:10
#define UVEC
Definition: utils.h:58
FVEC regH()
L2 regularization as the first parameter and L1 as second for right lowrank factor H...
int pr()
Returns the number of processor rows.
normtype
Definition: utils.h:12
void parseplancopts()
parses the command line parameters
#define INFO
Definition: utils.h:36
#define NORMALIZATION
Definition: utils.h:12
Definition: utils.h:10
#define UWORD
Definition: utils.h:60
UWORD lowrankk()
returns the low rank. Passed as parameter –lowrank or -k
ParseCommandLine(int argc, char **argv)
Constructor that takes the number of arguments and the command line parameters.
UVEC processor_grids()
Returns the process grid configuration.
UWORD globalm()
return global rows. Passed as parameter -d
std::string input_file_name()
Returns input file name. Passed as -i or –input.
void printConfig()
print the configuration received through the command line paramters
FVEC regularizers()
Returns the vector regularizers for all the modes.
ncp_factors contains the factors of the ncp every ith factor is of size n_i * k number of factors is ...
Definition: ncpfactors.hpp:20
UVEC dimensions()
Returns vector of dimensions for every mode.
normtype input_normalization()
To column normalize the input matrix.
bool dim_tree()
Enable dimension tree or not.