GSATThreadMPI.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. this->mpiSendResult(DISPLAY_RANK);
  46. }
  47. return find;
  48. }
  49. void GSATThreadMPI::end() {
  50. this->end(true);
  51. }
  52. void GSATThreadMPI::end(bool endOrNot) {
  53. bEnd = endOrNot;
  54. }
  55. void GSATThreadMPI::printResult() {
  56. printf(GREEN);
  57. printf("s %s\n",find?"SATISFIABLE":"NOT FOUND");
  58. printf(YELLOW);
  59. printf("c real time : %.4f seconds\n", result.calcTime);
  60. printf("c [pid:%6d][process:%2d][thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  61. result.pid,
  62. result.rank,
  63. result.threadId,
  64. result.nbIteration,
  65. result.heuristicFill,
  66. result.heuristicSolve,
  67. result.nbSatisfiedClausesFill,
  68. result.nbSatisfiedClausesSolve);
  69. printf(RESET);
  70. fflush(stdout);
  71. }
  72. void GSATThreadMPI::setResult(bool isFind, int pid, int rank, int thread, double time, unsigned int nbIte, unsigned int heuriFill, unsigned int heuriSolve, unsigned int satisfFill, unsigned int satisfSolve) {
  73. find = isFind;
  74. result.pid = pid;
  75. result.rank = rank;
  76. result.threadId = thread;
  77. result.calcTime = time;
  78. result.nbIteration = nbIte;
  79. result.heuristicFill = heuriFill;
  80. result.heuristicSolve = heuriSolve;
  81. result.nbSatisfiedClausesFill = satisfFill;
  82. result.nbSatisfiedClausesSolve = satisfSolve;
  83. }
  84. /* --- Private --- */
  85. void GSATThreadMPI::solverThread(int id) {
  86. //Resolution
  87. double startTime = gsat[id]->realTime();
  88. bool solve = false;
  89. int cpt = 0;
  90. while(!bEnd && !solve){
  91. //Pour eviter que les processeurs effectue tous les meme calculs
  92. int fill = rand() % 4 + 2;
  93. int h = rand() % 7;
  94. solve = gsat[id]->start(fill, h);
  95. if(solve) {
  96. printf(CYAN);
  97. }
  98. printf("c [pid:%6d][process:%2d][thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  99. getpid(),
  100. world_rank,
  101. id,
  102. gsat[id]->getNbIterations(),
  103. gsat[id]->getHeuristicFill(),
  104. gsat[id]->getHeuristicSolve(),
  105. gsat[id]->getNbSatisfiedClausesFill(),
  106. gsat[id]->getNbSatisfiedClausesSolve());
  107. if(solve) {
  108. printf(RESET);
  109. }
  110. cpt++;
  111. }
  112. //Si 1er arreter
  113. if(!bEnd && solve) {
  114. this->mpiNotify(world_rank);
  115. this->end(true);
  116. find = true;
  117. this->setResult(
  118. true,
  119. getpid(),
  120. world_rank,
  121. id, gsat[id]->realTime() - startTime,
  122. gsat[id]->getNbIterations(),
  123. gsat[id]->getHeuristicFill(),
  124. gsat[id]->getHeuristicSolve(),
  125. gsat[id]->getNbSatisfiedClausesFill(),
  126. gsat[id]->getNbSatisfiedClausesSolve()
  127. );
  128. }
  129. }
  130. void GSATThreadMPI::mpiWait(GSATThreadMPI* gsat) {
  131. int buff;
  132. MPI_Recv(&buff, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  133. if(buff != world_rank) {
  134. gsat->end();
  135. }
  136. }
  137. void GSATThreadMPI::mpiNotify(int rank) {
  138. MPI_Request request;
  139. for(int i = 0; i < world_size; i++) {
  140. MPI_Isend(&rank, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &request);
  141. }
  142. }
  143. void GSATThreadMPI::mpiSendResult(int rankTo) {
  144. MPI_Request request;
  145. //Envoi des infos au rank determiné
  146. MPI_Isend(&result.pid, 1, MPI_INT, rankTo, 0, MPI_COMM_WORLD, &request);
  147. MPI_Isend(&result.rank, 1, MPI_INT, rankTo, 0, MPI_COMM_WORLD, &request);
  148. MPI_Isend(&result.threadId, 1, MPI_INT, rankTo, 0, MPI_COMM_WORLD, &request);
  149. MPI_Isend(&result.calcTime, 1, MPI_DOUBLE, rankTo, 0, MPI_COMM_WORLD, &request);
  150. MPI_Isend(&result.nbIteration, 1, MPI_UNSIGNED, rankTo, 0, MPI_COMM_WORLD, &request);
  151. MPI_Isend(&result.heuristicFill, 1, MPI_UNSIGNED, rankTo, 0, MPI_COMM_WORLD, &request);
  152. MPI_Isend(&result.heuristicSolve, 1, MPI_UNSIGNED, rankTo, 0, MPI_COMM_WORLD, &request);
  153. MPI_Isend(&result.nbSatisfiedClausesFill, 1, MPI_UNSIGNED, rankTo, 0, MPI_COMM_WORLD, &request);
  154. MPI_Isend(&result.nbSatisfiedClausesSolve, 1, MPI_UNSIGNED, rankTo, 0, MPI_COMM_WORLD, &request);
  155. }