GSATThread.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "GSATThread.hpp"
  5. #include "color.h"
  6. /* --- Public --- */
  7. GSATThread::GSATThread(int argc, char** argv) {
  8. gsat = std::vector<GSAT*>(NB_THREAD);
  9. time = std::vector<double>(NB_THREAD);
  10. threads = std::vector<std::thread>(NB_THREAD);
  11. for(int i = 0; i < NB_THREAD; i++) {
  12. gsat[i] = new GSAT();
  13. gsat[i]->setParameters(argc,argv);
  14. gsat[i]->initialize();
  15. time[i] = 0;
  16. }
  17. }
  18. GSATThread::~GSATThread() {
  19. for(int i = 0; i < NB_THREAD; i++) {
  20. delete gsat[i];
  21. }
  22. }
  23. bool GSATThread::solve() {
  24. return this->solve(0, true);
  25. }
  26. bool GSATThread::solve(int maxIteration) {
  27. return this->solve(maxIteration, true);
  28. }
  29. bool GSATThread::solve(bool verbose) {
  30. return this->solve(0, verbose);
  31. }
  32. bool GSATThread::solve(int _maxIteration, bool verbose) {
  33. threads.clear();
  34. end = false;
  35. calcTime = 0;
  36. maxIteration = _maxIteration;
  37. //Lance thread
  38. for(int i = 0; i < NB_THREAD; i++) {
  39. time[i] = gsat[i]->realTime();
  40. threads[i] = std::thread(&GSATThread::solverThread, this, i, verbose);
  41. }
  42. //Attend resultat
  43. for(int i = 0; i < NB_THREAD; i++){
  44. threads[i].join();
  45. }
  46. //Resultat
  47. if(verbose) {
  48. printf("------------------------------------------------------------------------------------------\n");
  49. this->printResult();
  50. }
  51. return end;
  52. }
  53. void GSATThread::printResult() {
  54. if(end) {
  55. printf(GREEN "s SATISFIABLE\n" RESET);
  56. } else {
  57. printf(RED "NOT FOUND\n" RESET);
  58. }
  59. //printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
  60. if(!end && calcTime == 0) {
  61. //Si on à pas trouver
  62. calcTime = gsat[0]->realTime() - time[0];
  63. }
  64. printf(YELLOW);
  65. if(end) {
  66. printf("c [thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  67. result.threadId,
  68. result.nbIteration,
  69. result.heuristicFill,
  70. result.heuristicSolve,
  71. result.nbSatisfiedClausesFill,
  72. result.nbSatisfiedClausesSolve);
  73. }
  74. printf("c real time : %.4f seconds\n", calcTime);
  75. printf(RESET);
  76. }
  77. /* --- Private --- */
  78. void GSATThread::solverThread(int id, bool verbose) {
  79. //Resolution
  80. bool solve = false;
  81. int cpt = 0;
  82. while(!end && !solve && (maxIteration == 0 || cpt < maxIteration)){
  83. solve = gsat[id]->start();
  84. if(verbose) {
  85. if(solve) {
  86. printf(CYAN);
  87. }
  88. printf("c [thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  89. id,
  90. gsat[id]->getNbIterations(),
  91. gsat[id]->getHeuristicFill(),
  92. gsat[id]->getHeuristicSolve(),
  93. gsat[id]->getNbSatisfiedClausesFill(),
  94. gsat[id]->getNbSatisfiedClausesSolve());
  95. if(solve) {
  96. printf(RESET);
  97. }
  98. }
  99. cpt++;
  100. }
  101. //Si 1er arreter
  102. if(!end && solve) {
  103. end = true;
  104. calcTime = gsat[id]->realTime() - time[id];
  105. result.threadId = id;
  106. result.nbIteration = gsat[id]->getNbIterations();
  107. result.heuristicFill = gsat[id]->getHeuristicFill();
  108. result.heuristicSolve = gsat[id]->getHeuristicSolve();
  109. result.nbSatisfiedClausesFill = gsat[id]->getNbSatisfiedClausesFill();
  110. result.nbSatisfiedClausesSolve = gsat[id]->getNbSatisfiedClausesSolve();
  111. }
  112. }