treewalk.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <chrono>
  4. #include <cmath>
  5. #include <cpuid.h>
  6. #include <fstream>
  7. #include <csignal>
  8. #include <unistd.h>
  9. #include "treewalk.hpp"
  10. using namespace std;
  11. using namespace std::chrono;
  12. fstream file_out;
  13. bool checkpoint;
  14. bool stack_correct;
  15. #define SIGNAL_CHECKPOINT SIGUSR2
  16. void signal_checkpoint_handler(int signum){
  17. checkpoint=true;
  18. }
  19. void treat(const Semigroup& m){
  20. int q=ceil(float(m.conductor)/float(m.min));
  21. unsigned int rho=q*m.min-m.conductor;
  22. if(m.wilf<=rho){
  23. if((m.wilf<0) or (m.wilf<=rho and m.e>2 and m.left_primitive>1 and q>=4)){
  24. output(m,file_out);
  25. }
  26. }
  27. }
  28. void walk(Stack& stack){
  29. Semigroup *current,*son;
  30. Semigroup temp;
  31. while(not stack.is_empty() and not checkpoint){
  32. current = stack.pop();
  33. if(not cut(*current)){
  34. if(current->genus < MAX_GENUS - 1){
  35. auto it=generator_iter<CHILDREN>(*current);
  36. ind_t pos=0;
  37. while (it.move_next()){
  38. son=stack.pushed();
  39. remove_generator(*son, *current, it.get_gen(),pos++);
  40. treat(*son);
  41. }
  42. }
  43. else{
  44. auto it = generator_iter<CHILDREN>(*current);
  45. ind_t pos=0;
  46. while (it.move_next()){
  47. remove_generator(temp, *current, it.get_gen(),pos++);
  48. treat(temp);
  49. }
  50. }
  51. }
  52. }
  53. }
  54. int main(int argc, char **argv){
  55. if(argc!=2){
  56. cerr<<"Usage : "<<argv[0]<<" tasks/task_file"<<endl;
  57. exit(-1);
  58. }
  59. char* task_filename=argv[1];
  60. unsigned int ax, bx, cx, dx;
  61. if (!__get_cpuid(0x00000001, &ax, &bx, &cx, &dx)){
  62. cerr << "Unable to determine the processor type !" << endl;
  63. return EXIT_FAILURE;
  64. }
  65. if (!(cx & bit_SSSE3)){
  66. cerr << "This programm require sse3 instructions set !" << endl;
  67. return EXIT_FAILURE;
  68. }
  69. if (!(cx & bit_POPCNT)){
  70. cerr << "This programm require popcount instruction !" << endl;
  71. return EXIT_FAILURE;
  72. }
  73. fstream file_in;
  74. file_in.open(task_filename,ios::in|ios::binary);
  75. string filename=string("tasks/")+task_filename;
  76. filename+="-out";
  77. file_out.open(filename,ios::out|ios::trunc);
  78. Stack stack;
  79. char d[3*MAX_GENUS+1];
  80. char c,g,m;
  81. size_t n;
  82. file_in.read((char*)&n,8);
  83. cout<<"Stack size = "<<n<<endl;
  84. for(size_t i=0;i<n;++i){
  85. file_in.read(&c,1);
  86. file_in.read(&g,1);
  87. file_in.read(&m,1);
  88. file_in.read(d,3*MAX_GENUS+1);
  89. Semigroup* S=stack.pushed();
  90. init(*S,c,g,m,d);
  91. treat(*S);
  92. }
  93. file_in.close();
  94. checkpoint=false;
  95. signal(SIGNAL_CHECKPOINT, signal_checkpoint_handler);
  96. walk(stack);
  97. file_out.close();
  98. if(checkpoint){
  99. cout<<"Stoping exploration due to checkpoint signal reception."<<endl;
  100. filename=string("tasks/checkpoint/")+task_filename+string("-checkpoint");
  101. fstream file_stack;
  102. file_stack.open(filename.c_str(),ios::out|ios::binary);
  103. size_t size=stack.stack_size;
  104. file_stack.write((char*)(&size),8);
  105. cout<<"Checkpoint size : "<<size<<endl;
  106. for(size_t i=0;i<size;++i){
  107. record(*stack.stack[i],file_stack);
  108. }
  109. file_stack.close();
  110. return 1;
  111. }
  112. else{
  113. return 0;
  114. }
  115. }