GSATThreadMPI.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. result.pid,
  65. result.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. void GSATThreadMPI::setResult(int pid, int rank, int thread, double time, unsigned int nbIte, unsigned int heuriFill, unsigned int heuriSolve, unsigned int statisfFill, unsigned int satisfSolve) {
  76. result.pid = pid;
  77. result.rank = rank;
  78. result.threadId = thread;
  79. result.calcTime = time;
  80. result.nbIteration = nbIte;
  81. result.heuristicFill = heuriFill;
  82. result.heuristicSolve = heuriSolve;
  83. result.nbSatisfiedClausesFill = statisfFill;
  84. result.nbSatisfiedClausesSolve = statisfFill;
  85. }
  86. /* --- Private --- */
  87. void GSATThreadMPI::solverThread(int id) {
  88. //Resolution
  89. double startTime = gsat[id]->realTime();
  90. bool solve = false;
  91. int cpt = 0;
  92. while(!bEnd && !solve){
  93. //Pour eviter que les processeurs effectue tous les meme calculs
  94. int fill = rand() % 4 + 2;
  95. int h = rand() % 7;
  96. solve = gsat[id]->start(fill, h);
  97. if(solve) {
  98. printf(CYAN);
  99. }
  100. printf("c [pid:%6d][process:%2d][thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  101. getpid(),
  102. world_rank,
  103. id,
  104. gsat[id]->getNbIterations(),
  105. gsat[id]->getHeuristicFill(),
  106. gsat[id]->getHeuristicSolve(),
  107. gsat[id]->getNbSatisfiedClausesFill(),
  108. gsat[id]->getNbSatisfiedClausesSolve());
  109. if(solve) {
  110. printf(RESET);
  111. }
  112. cpt++;
  113. }
  114. //Si 1er arreter
  115. if(!bEnd && solve) {
  116. this->mpiNotify(world_rank);
  117. this->end(true);
  118. find = true;
  119. this->setResult(
  120. getpid(),
  121. world_rank,
  122. id, gsat[id]->realTime() - startTime,
  123. gsat[id]->getNbIterations(),
  124. gsat[id]->getHeuristicFill(),
  125. gsat[id]->getHeuristicSolve(),
  126. gsat[id]->getNbSatisfiedClausesFill(),
  127. gsat[id]->getNbSatisfiedClausesSolve()
  128. );
  129. }
  130. }
  131. void GSATThreadMPI::mpiWait(GSATThreadMPI* gsat) {
  132. int buff;
  133. MPI_Recv(&buff, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  134. if(buff != world_rank) {
  135. gsat->end();
  136. }
  137. }
  138. void GSATThreadMPI::mpiNotify(int rank) {
  139. MPI_Request request;
  140. for(int i = 0; i < world_size; i++) {
  141. MPI_Isend(&rank, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &request);
  142. }
  143. }