GSATThread.cpp 3.3 KB

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