GSATThreadMPI.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. //Affiche resultat
  44. sleep(1); //Attend la fin des autres processus
  45. printf("-----------------------------------------------------------------------------------------------------------------\n");
  46. this->printResult();
  47. }
  48. return find;
  49. }
  50. void GSATThreadMPI::isEnd() {
  51. end = true;
  52. }
  53. void GSATThreadMPI::printResult() {
  54. printf(GREEN);
  55. printf("s %s\n",find?"SATISFIABLE":"NOT FOUND");
  56. printf(YELLOW);
  57. printf("c real time : %.4f seconds\n", result.calcTime);
  58. printf("c [pid:%6d][process:%2d][thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  59. getpid(),
  60. world_rank,
  61. result.threadId,
  62. result.nbIteration,
  63. result.heuristicFill,
  64. result.heuristicSolve,
  65. result.nbSatisfiedClausesFill,
  66. result.nbSatisfiedClausesSolve);
  67. printf(RESET);
  68. fflush(stdout);
  69. }
  70. /* --- Private --- */
  71. void GSATThreadMPI::solverThread(int id) {
  72. //Resolution
  73. double startTime = gsat[id]->realTime();
  74. bool solve = false;
  75. int cpt = 0;
  76. while(!end && !solve){
  77. //Pour eviter que les processeurs effectue tous les meme calculs
  78. int fill = rand() % 4 + 2;
  79. int h = rand() % 7;
  80. solve = gsat[id]->start(fill, h);
  81. if(solve) {
  82. printf(CYAN);
  83. }
  84. printf("c [pid:%6d][process:%2d][thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  85. getpid(),
  86. world_rank,
  87. id,
  88. gsat[id]->getNbIterations(),
  89. gsat[id]->getHeuristicFill(),
  90. gsat[id]->getHeuristicSolve(),
  91. gsat[id]->getNbSatisfiedClausesFill(),
  92. gsat[id]->getNbSatisfiedClausesSolve());
  93. if(solve) {
  94. printf(RESET);
  95. }
  96. cpt++;
  97. }
  98. //Si 1er arreter
  99. if(!end && solve) {
  100. mpiNotify(world_rank);
  101. end = true;
  102. find = true;
  103. result.threadId = id;
  104. result.calcTime = gsat[id]->realTime() - startTime;
  105. result.nbIteration = gsat[id]->getNbIterations();
  106. result.heuristicFill = gsat[id]->getHeuristicFill();
  107. result.heuristicSolve = gsat[id]->getHeuristicSolve();
  108. result.nbSatisfiedClausesFill = gsat[id]->getNbSatisfiedClausesFill();
  109. result.nbSatisfiedClausesSolve = gsat[id]->getNbSatisfiedClausesSolve();
  110. }
  111. }