GSATThread.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. 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 end;
  53. }
  54. void GSATThread::printResult() {
  55. if(end) {
  56. printf(GREEN "s SATISFIABLE\n" RESET);
  57. } else {
  58. printf(RED "NOT FOUND\n" RESET);
  59. }
  60. //printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
  61. if(!end && calcTime == 0) {
  62. //Si on à pas trouver
  63. calcTime = gsat[0]->realTime() - time[0];
  64. }
  65. printf(YELLOW);
  66. if(end) {
  67. printf("c [thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  68. result.threadId,
  69. result.nbIteration,
  70. result.heuristicFill,
  71. result.heuristicSolve,
  72. result.nbSatisfiedClausesFill,
  73. result.nbSatisfiedClausesSolve);
  74. }
  75. printf("c real time : %.4f seconds\n", calcTime);
  76. printf(RESET);
  77. }
  78. /* --- Private --- */
  79. void GSATThread::solverThread(int id, bool verbose) {
  80. //Resolution
  81. bool solve = false;
  82. int cpt = 0;
  83. while(!end && !solve && (maxIteration == 0 || cpt < maxIteration)){
  84. solve = gsat[id]->start();
  85. if(verbose) {
  86. if(solve) {
  87. printf(CYAN);
  88. }
  89. printf("c [thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  90. id,
  91. gsat[id]->getNbIterations(),
  92. gsat[id]->getHeuristicFill(),
  93. gsat[id]->getHeuristicSolve(),
  94. gsat[id]->getNbSatisfiedClausesFill(),
  95. gsat[id]->getNbSatisfiedClausesSolve());
  96. if(solve) {
  97. printf(RESET);
  98. }
  99. }
  100. cpt++;
  101. }
  102. //Si 1er arreter
  103. if(!end && solve) {
  104. end = true;
  105. calcTime = gsat[id]->realTime() - time[id];
  106. result.threadId = id;
  107. result.nbIteration = gsat[id]->getNbIterations();
  108. result.heuristicFill = gsat[id]->getHeuristicFill();
  109. result.heuristicSolve = gsat[id]->getHeuristicSolve();
  110. result.nbSatisfiedClausesFill = gsat[id]->getNbSatisfiedClausesFill();
  111. result.nbSatisfiedClausesSolve = gsat[id]->getNbSatisfiedClausesSolve();
  112. }
  113. }