GSATThreadMPI.cpp 3.6 KB

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