GSATThreadMPI.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "Main.hpp"
  6. #include "GSATThreadMPI.hpp"
  7. #include "color.h"
  8. extern int world_rank;
  9. /* --- Public --- */
  10. GSATThreadMPI::GSATThreadMPI(int _nbThread, int argc, char** argv) {
  11. nbThread = _nbThread;
  12. gsat = std::vector<GSAT*>(_nbThread);
  13. threads = std::vector<std::thread>(_nbThread);
  14. for(int i = 0; i < _nbThread; i++) {
  15. gsat[i] = new GSAT();
  16. gsat[i]->setParameters(argc,argv);
  17. gsat[i]->initialize();
  18. }
  19. srand(getpid());
  20. }
  21. GSATThreadMPI::~GSATThreadMPI() {
  22. for(int i = 0; i < nbThread; i++) {
  23. delete gsat[i];
  24. }
  25. }
  26. bool GSATThreadMPI::solve() {
  27. threads.clear();
  28. end = false;
  29. find = false;
  30. //Lance thread
  31. for(int i = 0; i < nbThread; i++) {
  32. threads[i] = std::thread(&GSATThreadMPI::solverThread, this, i);
  33. }
  34. //Attend resultat
  35. for(int i = 0; i < nbThread; i++){
  36. threads[i].join();
  37. }
  38. //Si c'est le proc qui à trouvé
  39. if(find) {
  40. sleep(1); //Laisse le temps aux autres de se couper
  41. //Affiche resultat
  42. printf("-------------------------------------------------------------------------------------------------------\n");
  43. printf(GREEN);
  44. printf("s %s\n",find?"SATISFIABLE":"NOT FOUND");
  45. printf(YELLOW);
  46. printf("c real time : %.4f seconds\n", result.calcTime);
  47. printf("c [processor:%d][thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  48. world_rank,
  49. result.threadId,
  50. result.nbIteration,
  51. result.heuristicFill,
  52. result.heuristicSolve,
  53. result.nbSatisfiedClausesFill,
  54. result.nbSatisfiedClausesSolve);
  55. printf(RESET);
  56. fflush(stdout);
  57. }
  58. return find;
  59. }
  60. void GSATThreadMPI::isEnd() {
  61. end = true;
  62. }
  63. /* --- Private --- */
  64. void GSATThreadMPI::solverThread(int id) {
  65. //Resolution
  66. double startTime = gsat[id]->realTime();
  67. bool solve = false;
  68. int cpt = 0;
  69. while(!end && !solve){
  70. //Pour eviter que les processeurs effectue tous les meme calculs
  71. int fill = rand() % 4 + 2;
  72. int h = rand() % 7;
  73. solve = gsat[id]->start(fill, h);
  74. if(solve) {
  75. printf(CYAN);
  76. }
  77. printf("c [processor:%d][thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  78. world_rank,
  79. id,
  80. gsat[id]->getNbIterations(),
  81. gsat[id]->getHeuristicFill(),
  82. gsat[id]->getHeuristicSolve(),
  83. gsat[id]->getNbSatisfiedClausesFill(),
  84. gsat[id]->getNbSatisfiedClausesSolve());
  85. if(solve) {
  86. printf(RESET);
  87. }
  88. cpt++;
  89. if(id == 0) {
  90. mpiSync(this, end || solve);
  91. }
  92. }
  93. //Si 1er arreter
  94. if(!end && solve) {
  95. end = true;
  96. find = true;
  97. result.threadId = id;
  98. result.calcTime = gsat[id]->realTime() - startTime;
  99. result.nbIteration = gsat[id]->getNbIterations();
  100. result.heuristicFill = gsat[id]->getHeuristicFill();
  101. result.heuristicSolve = gsat[id]->getHeuristicSolve();
  102. result.nbSatisfiedClausesFill = gsat[id]->getNbSatisfiedClausesFill();
  103. result.nbSatisfiedClausesSolve = gsat[id]->getNbSatisfiedClausesSolve();
  104. }
  105. }