GSATThreadMPI.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. //Lance thread de synchronisation MPI
  35. mpiSync = std::thread(mpiWait, this);
  36. //Attend resultat
  37. for(int i = 0; i < nbThread; i++){
  38. threads[i].join();
  39. }
  40. mpiSync.join();
  41. //Si c'est le proc qui à trouvé
  42. if(find) {
  43. sleep(1); //Laisse le temps aux autres de se couper
  44. //Affiche resultat
  45. printf("-------------------------------------------------------------------------------------------------------\n");
  46. printf(GREEN);
  47. printf("s %s\n",find?"SATISFIABLE":"NOT FOUND");
  48. printf(YELLOW);
  49. printf("c real time : %.4f seconds\n", result.calcTime);
  50. printf("c [processor:%d][thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  51. world_rank,
  52. result.threadId,
  53. result.nbIteration,
  54. result.heuristicFill,
  55. result.heuristicSolve,
  56. result.nbSatisfiedClausesFill,
  57. result.nbSatisfiedClausesSolve);
  58. printf(RESET);
  59. fflush(stdout);
  60. }
  61. return find;
  62. }
  63. void GSATThreadMPI::isEnd() {
  64. end = true;
  65. }
  66. /* --- Private --- */
  67. void GSATThreadMPI::solverThread(int id) {
  68. //Resolution
  69. double startTime = gsat[id]->realTime();
  70. bool solve = false;
  71. int cpt = 0;
  72. while(!end && !solve){
  73. //Pour eviter que les processeurs effectue tous les meme calculs
  74. int fill = rand() % 4 + 2;
  75. int h = rand() % 7;
  76. solve = gsat[id]->start(fill, h);
  77. if(solve) {
  78. printf(CYAN);
  79. }
  80. printf("c [processor:%d][thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  81. world_rank,
  82. id,
  83. gsat[id]->getNbIterations(),
  84. gsat[id]->getHeuristicFill(),
  85. gsat[id]->getHeuristicSolve(),
  86. gsat[id]->getNbSatisfiedClausesFill(),
  87. gsat[id]->getNbSatisfiedClausesSolve());
  88. if(solve) {
  89. printf(RESET);
  90. }
  91. cpt++;
  92. }
  93. //Si 1er arreter
  94. if(!end && solve) {
  95. mpiNotify(world_rank);
  96. end = true;
  97. find = true;
  98. result.threadId = id;
  99. result.calcTime = gsat[id]->realTime() - startTime;
  100. result.nbIteration = gsat[id]->getNbIterations();
  101. result.heuristicFill = gsat[id]->getHeuristicFill();
  102. result.heuristicSolve = gsat[id]->getHeuristicSolve();
  103. result.nbSatisfiedClausesFill = gsat[id]->getNbSatisfiedClausesFill();
  104. result.nbSatisfiedClausesSolve = gsat[id]->getNbSatisfiedClausesSolve();
  105. }
  106. }