|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*!
- * \file main.cpp
- * \brief Main application file
- *
- * \author
- * Christos Choutouridis AEM:8997
- * <cchoutou@ece.auth.gr>
- */
-
- #include <iostream>
- #include <string>
- #include <exception>
-
- #include <utils.h>
- #include <config.h>
-
- // Global session data
- session_t session;
- Log logger;
-
- /*!
- * A small command line argument parser
- * \return The status of the operation
- */
- bool get_options(int argc, char* argv[]){
- bool status =true;
-
- // iterate over the passed arguments
- for (int i=1 ; i<argc ; ++i) {
- std::string arg(argv[i]); // get current argument
-
- if (arg == "-i" || arg == "--input") {
- session.inputMatrix = InputMatrix::MTX;
- if (i+1 < argc)
- session.mtxFile = std::ifstream(argv[++i]);
- else
- status = false;
- }
- else if (arg == "-o" || arg == "--output") {
- session.outputMode = OutputMode::FILE;
- if (i+1 < argc)
- session.outFile = std::ofstream(argv[++i]);
- else
- status = false;
- }
- else if (arg == "-g" || arg == "--generate") {
- session.inputMatrix = InputMatrix::GENERATE;
- if (i+2 < argc) {
- session.gen_size = std::atoi(argv[++i]);
- session.gen_prob = std::atof(argv[++i]);
- }
- else
- status = false;
- }
- else if (arg == "-n" || arg == "--max_trheads") {
- session.max_threads = (i+1 < argc) ? std::atoi(argv[++i]) : session.max_threads;
- }
- else if (arg == "-t" || arg == "--timing")
- session.timing = true;
- else if (arg == "-v" || arg == "--verbose")
- session.verbose = true;
- else if (arg == "--triangular_only")
- session.makeSymmetric = false;
- else if (arg == "--validate_mtx")
- session.validate_mtx = true;
- else if (arg == "--print_count")
- session.print_count = true;
- else if (arg == "--print_graph") {
- session.mtx_print = true;
- session.mtx_print_size = (i+1 < argc) ? std::atoi(argv[++i]) : session.mtx_print_size;
- }
- else if (arg == "-h" || arg == "--help") {
- std::cout << "Help message\n";
- exit(0);
- }
- else { // parse error
- std::cout << "Error message\n";
- status = false;
- }
- }
-
- // Input checkers
- if (session.inputMatrix == InputMatrix::UNSPECIFIED) {
- std::cout << "Error message\n";
- status = false;
- }
- return status;
- }
-
- /*!
- * get or generate matrix
- * \param A Reference to matrix for output (move using RVO)
- * \param timer Reference to timer utility to access time printing functionality
- */
- void prepare_matrix (matrix& A, Timing& timer) {
- if (session.inputMatrix == InputMatrix::GENERATE) {
- logger << "Initialize matrix with size: " << session.gen_size << " and probability: " << session.gen_prob << logger.endl;
- timer.start();
- A.size(session.gen_size);
- init_ER_graph(A, session.gen_prob);
- timer.stop();
- timer.print_dt("generate matrix");
- }
- else {
- logger << "Read matrix from file" << logger.endl;
- timer.start();
- if (session.validate_mtx && !Mtx::is_triangular<matrix::indexType> (session.mtxFile))
- throw std::runtime_error("Error: Matrix is not strictly upper or lower");
- if (!Mtx::load (A, session.mtxFile)) {
- throw std::runtime_error("Error: fail to load matrix");
- }
- timer.stop();
- logger << "Matrix size: " << A.size() << " and capacity: " << A.capacity() << logger.endl;
- timer.print_dt("load matrix");
- }
-
- if (session.verbose && session.mtx_print) {
- logger << "\nMatrix:" << logger.endl;
- print_graph (A);
- }
- }
-
- /*
- * main program
- */
- int main(int argc, char* argv[]) try {
- Timing timer;
- matrix A;
- std::vector<value_t> c;
- index_t s;
-
- // try to read command line
- if (!get_options(argc, argv))
- exit(1);
-
- prepare_matrix(A, timer);
- threads_info();
- logger << "Create count vector" << logger.endl;
- timer.start();
- c = triang_v (A);
- timer.stop();
- timer.print_dt("create count vector");
- if (session.print_count) {
- logger << "Calculate total triangles" << logger.endl;
- timer.start();
- s = triang_count(c);
- timer.stop();
- logger << "There are " << s << " triangles" << logger.endl;
- timer.print_dt("calculate sum");
- }
- // output results
- if (session.print_count)
- triangle_out (s, (session.outputMode == OutputMode::FILE) ? session.outFile : std::cout);
- else
- vector_out (c, (session.outputMode == OutputMode::FILE) ? session.outFile : std::cout);
- return 0;
- }
- catch (std::exception& e) {
- //we probably pollute the user's screen. Comment `cerr << ...` if you don't like it.
- std::cerr << e.what() << '\n';
- exit(1);
- }
|