A triangle counting assignment for A.U.TH Parallel and distributed systems class.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
3.2 KiB

  1. /*!
  2. * \file main.cpp
  3. * \brief Main application file
  4. *
  5. * \author
  6. * Christos Choutouridis AEM:8997
  7. * <cchoutou@ece.auth.gr>
  8. */
  9. #include <iostream>
  10. #include <string>
  11. #include <exception>
  12. #include <utils.h>
  13. #include <config.h>
  14. // Global session data
  15. session_t session;
  16. /*!
  17. * A small command line argument parser
  18. * \return The status of the operation
  19. */
  20. bool get_options(int argc, char* argv[]){
  21. bool status =true;
  22. // iterate over the passed arguments
  23. for (int i=1 ; i<argc ; ++i) {
  24. std::string arg(argv[i]); // get current argument
  25. if (arg == "-i" || arg == "--input") {
  26. session.inputMatrix = InputMatrix::MTX;
  27. if (i+1 < argc)
  28. session.mtxFile = std::ifstream(argv[++i]);
  29. else
  30. status = false;
  31. }
  32. else if (arg == "-g" || arg == "--generate")
  33. session.inputMatrix = InputMatrix::GENERATE;
  34. else if (arg == "-s" || arg == "--size")
  35. session.size = (i+1 < argc) ? std::atoi(argv[++i]) : session.size;
  36. else if (arg == "-p" || arg == "--probability")
  37. session.probability = (i+1 < argc) ? std::atof(argv[++i]) : session.probability;
  38. else if (arg == "--print") {
  39. session.print = true;
  40. session.print_size = (i+1 < argc) ? std::atoi(argv[++i]) : session.print_size;
  41. }
  42. else if (arg == "--make_symmetric")
  43. session.makeSymmetric = true;
  44. else if (arg == "-t" || arg == "--timing")
  45. session.timing = true;
  46. else if (arg == "-h" || arg == "--help") {
  47. std::cout << "Help message\n";
  48. exit(0);
  49. }
  50. else {
  51. std::cout << "Error message\n";
  52. status = false;
  53. }
  54. }
  55. return status;
  56. }
  57. int main(int argc, char* argv[]) try {
  58. Timing timer;
  59. matrix A;
  60. // try to read command line
  61. if (!get_options(argc, argv))
  62. exit(1);
  63. // get or generate matrix
  64. if (session.inputMatrix == InputMatrix::GENERATE) {
  65. std::cout << "Initialize matrix with size: " << session.size << " and probability: " << session.probability << '\n';
  66. timer.start();
  67. A.size(session.size);
  68. init_ER_graph(A, session.probability);
  69. timer.stop();
  70. if (session.timing) timer.print_dt();
  71. }
  72. else {
  73. std::cout << "Read matrix from file\n";
  74. timer.start();
  75. if (session.makeSymmetric && !Mtx::is_triangular<matrix::indexType> (session.mtxFile))
  76. throw std::runtime_error("Error: Matrix is not strictly upper or lower");
  77. if (!Mtx::load (A, session.mtxFile)) {
  78. throw std::runtime_error("Error: fail to load matrix");
  79. }
  80. timer.stop();
  81. std::cout << "Matrix size: " << A.size() << " and capacity: " << A.capacity() <<'\n';
  82. if (session.timing) timer.print_dt();
  83. }
  84. if (session.print) {
  85. std::cout << "Array A:\n";
  86. print_ER_graph (A);
  87. }
  88. std::cout << "count triangles\n";
  89. timer.start();
  90. std::cout << "There are " << triang_count(A) << " triangles\n";
  91. timer.stop();
  92. if (session.timing) timer.print_dt();
  93. return 0;
  94. }
  95. catch (std::exception& e) {
  96. //we probably pollute the user's screen. Comment `cerr << ...` if you don't like it.
  97. std::cerr << e.what() << '\n';
  98. exit(1);
  99. }