Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/examples/parser.hpp
Go to the documentation of this file.
00001 #ifndef PARSER_HPP
00002 #define PARSER_HPP
00003 
00008 #include <string>
00009 #include <cstdlib>
00010 #include <ext/hash_map>
00011 
00012 #include "utilities.hpp"
00013 
00014 static const char* description = "Example for SKYLARK";
00015 typedef __gnu_cxx::hash_map<const char*, const char*> string_string_map_t;
00016 typedef __gnu_cxx::hash_map<const char*, int> string_int_map_t;
00017 
00018 static inline void print_help (const string_string_map_t& help_map,
00019                                const char* option=NULL,
00020                                const char* val=NULL) {
00021   if (NULL != option) printf ("%s: Invalid arguments \"%s\" is \"%s\"\n",
00022                                                 description, option, val);
00023   else printf ("%s\n", description);
00024   string_string_map_t::const_iterator iter = help_map.begin();
00025 
00026   while (iter != help_map.end()) {
00027     printf ("%s: %s\n", (*iter).first, (*iter).second);
00028     ++iter;
00029   }
00030 
00031   exit (3);
00032 }
00033 
00034 static inline void parse_parameters (int argc, char** argv) {
00036   string_string_map_t help_map;
00037   help_map["help"] = "produce help messages";
00038 
00039   help_map["r"] = "should we use randomly generated matrices (default:1)";
00040   help_map["s"] = "seed for the random number generator (default:0)";
00041   help_map["debug"] = "print helpful messages out (default:0)";
00042   help_map["M"] = "number of rows in the distributed matrix (default:10)";
00043   help_map["N"] = "number of cols in the distributed matrix (default:5)";
00044   help_map["RHS"] = "number of cols in the RHS (default:1)";
00045   help_map["S"] = "reduced number of rows/cols after sketching (default:5)";
00046   help_map["num-threads"] = "number of threads to start (default:2)";
00047   help_map["sketch"] = "Direction of the sketch (left|right|both, def:left)";
00048   help_map["A-file"] = "path to matrix A";
00049   help_map["b-file"] = "path to matrix b";
00050   help_map["sA-file"] = "path to matrix sA";
00051   help_map["s-type"] = "transform to use (JLT|FJLT|CWT, default: JLT)";
00052   help_map["m-type"] = "matrix type (elem|cblas, default: elem)";
00053 
00054   string_int_map_t int_options_map;
00055   int_options_map["r"]                 = USE_RANDOM_INDEX;
00056   int_options_map["s"]                 = RAND_SEED_INDEX;
00057   int_options_map["debug"]             = DEBUG_INDEX;
00058   int_options_map["M"]                 = M_INDEX;
00059   int_options_map["N"]                 = N_INDEX;
00060   int_options_map["RHS"]               = N_RHS_INDEX;
00061   int_options_map["S"]                 = S_INDEX;
00062   int_options_map["num-threads"]       = NUM_THREADS_INDEX;
00063   int_options_map["sketch"]            = SKETCH_DIRECTION_INDEX;
00064 
00065   string_int_map_t chr_options_map;
00066   chr_options_map["A-file"]       = A_FILE_PATH_INDEX;
00067   chr_options_map["b-file"]       = B_FILE_PATH_INDEX;
00068   chr_options_map["sA-file"]      = SA_FILE_PATH_INDEX;
00069   chr_options_map["s-type"]       = TRANSFORM_INDEX;
00070   chr_options_map["m-type"]       = MATRIX_TYPE_INDEX;
00071 
00072   /* default initialize parameters */
00073   int_params[USE_RANDOM_INDEX]      = 1;
00074   int_params[RAND_SEED_INDEX]       = 0;
00075   int_params[DEBUG_INDEX]           = 0;
00076   int_params[M_INDEX]               = 10;
00077   int_params[N_INDEX]               = 5;
00078   int_params[N_RHS_INDEX]           = 1;
00079   int_params[S_INDEX]               = 5;
00080   int_params[NUM_THREADS_INDEX]     = 2;
00081   int_params[SKETCH_DIRECTION_INDEX]= SKETCH_LEFT;
00082 
00083   chr_params[A_FILE_PATH_INDEX]     = "";
00084   chr_params[B_FILE_PATH_INDEX]     = "";
00085   chr_params[SA_FILE_PATH_INDEX]    = "";
00086   chr_params[TRANSFORM_INDEX]       = "JLT";
00087   chr_params[MATRIX_TYPE_INDEX]     = "elem";
00088 
00089   /* parse the command line */
00090   if (!(argc&0x1)) print_help(help_map);
00091 
00092   for (int i=1; i<argc; i+=2) {
00093     char* option = argv[i];
00094     char* value = argv[i+1];
00095 
00096     if (0==strcmp(option,"help")) print_help (help_map);
00097     else if (int_options_map.end()!=int_options_map.find(option)) {
00098       int_params[int_options_map[option]] = atoi(value);
00099     } else if (chr_options_map.end()!=chr_options_map.find(option)) {
00100       chr_params[chr_options_map[option]] = value;
00101     } else {
00102       print_help (help_map,option);
00103     }
00104   }
00105 
00106   /* Make sure that all the parameters are given and are correct */
00107   if (0==int_params[USE_RANDOM_INDEX] &&
00108       0==strcmp("",chr_params[A_FILE_PATH_INDEX])) {
00109     print_help (help_map, "A-file");
00110   } else if (0==int_params[USE_RANDOM_INDEX] &&
00111              0==strcmp("",chr_params[B_FILE_PATH_INDEX])) {
00112     print_help (help_map, "A-file");
00113   } else if (0>int_params[M_INDEX]) {
00114     print_help (help_map, "M");
00115   } else if (0>int_params[N_INDEX]) {
00116     print_help (help_map, "N");
00117   } else if (0>int_params[N_RHS_INDEX]) {
00118     print_help (help_map, "RHS");
00119   } else if (0!=strcmp("JLT", chr_params[TRANSFORM_INDEX]) &&
00120              0!=strcmp("FJLT", chr_params[TRANSFORM_INDEX]) &&
00121              0!=strcmp("CWT", chr_params[TRANSFORM_INDEX])) {
00122     print_help (help_map, "type");
00123   }
00124 }
00125 
00126 #endif // PARSER_HPP