Main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "GSAT/GSAT.hpp"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <mpi.h>
  7. #include "color.h"
  8. using namespace std;
  9. void help(char* prog) {
  10. fprintf(stderr, "usage: mpirun -n int ./%s -i file.cnf\n\t-n Number of process to use\n\t-i CNF file to solve\n", prog);
  11. exit(1);
  12. }
  13. int main(int argc, char* argv[]) {
  14. if(argc < 3) {
  15. help(argv[0]);
  16. }
  17. //Init MPI
  18. int world_size;
  19. int world_rank;
  20. MPI_Init(NULL, NULL);
  21. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  22. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  23. GSAT* gsat = new GSAT();
  24. gsat->setParameters(argc,argv);
  25. gsat->initialize();
  26. if(world_rank == 0) {
  27. printf("c nbProcess: %d\n", world_size);
  28. printf("c nbVariables: %d\n",gsat->getNbVariables());
  29. printf("c nbClauses: %d\n",gsat->getNbClauses());
  30. }
  31. double startTime = gsat->realTime();
  32. srand(getpid());
  33. //Attente d'un message de fin
  34. int syncBuff;
  35. MPI_Request sync, finish;
  36. MPI_Irecv(&syncBuff, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &sync);
  37. //Calcul
  38. bool end = false;
  39. bool stop = false;
  40. while(!end && !stop){
  41. //Pour eviter que les processeurs effectue tous les meme calculs
  42. int fill = rand() % 4 + 2;
  43. int h = rand() % 7;
  44. end = gsat->start(fill, h);
  45. //Affiche calcul
  46. if(end) {
  47. printf(CYAN);
  48. }
  49. printf("c [pid:%6d][process:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  50. getpid(),
  51. world_rank,
  52. gsat->getNbIterations(),
  53. gsat->getHeuristicFill(),
  54. gsat->getHeuristicSolve(),
  55. gsat->getNbSatisfiedClausesFill(),
  56. gsat->getNbSatisfiedClausesSolve());
  57. if(end) {
  58. printf(RESET);
  59. }
  60. //Regarde si quelqu'un a trouver
  61. int flag;
  62. MPI_Test(&sync, &flag, MPI_STATUS_IGNORE);
  63. if(flag) {
  64. stop = true;
  65. }
  66. }
  67. fflush(stdout);
  68. //Si on est le processus qui à trouvé
  69. if(end) {
  70. //Reverifie si quelqu'un d'autre a trouver avant
  71. int flag;
  72. MPI_Test(&sync, &flag, MPI_STATUS_IGNORE);
  73. if(flag) {
  74. stop = true;
  75. }
  76. if(!stop) {
  77. //Avertit les autres
  78. for(int i = 0; i < world_size; i++) {
  79. MPI_Isend(&world_rank, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &finish);
  80. }
  81. //Envoi les infos de resolution au processus de rank 0
  82. int pid, nbIte, heuriFill, heuriSolve, satisfFill, satisfSolve;
  83. pid = getpid();
  84. MPI_Isend(&pid, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &finish);
  85. MPI_Isend(&world_rank, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &finish);
  86. nbIte = gsat->getNbIterations();
  87. MPI_Isend(&nbIte, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &finish);
  88. heuriFill = gsat->getHeuristicFill();
  89. MPI_Isend(&heuriFill, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &finish);
  90. heuriSolve = gsat->getHeuristicSolve();
  91. MPI_Isend(&heuriSolve, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &finish);
  92. satisfFill = gsat->getNbSatisfiedClausesFill();
  93. MPI_Isend(&satisfFill, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &finish);
  94. satisfSolve = gsat->getNbSatisfiedClausesSolve();
  95. MPI_Isend(&satisfSolve, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &finish);
  96. }
  97. }
  98. //L'affichage est fait par le processus de rank 0
  99. if(world_rank == 0) {
  100. //Attend las infos venants de processus qui à trouvé
  101. int pid, rank, nbIte, heuriFill, heuriSolve, satisfFill, satisfSolve;
  102. MPI_Recv(&pid, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  103. MPI_Recv(&rank, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  104. MPI_Recv(&nbIte, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  105. MPI_Recv(&heuriFill, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  106. MPI_Recv(&heuriSolve, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  107. MPI_Recv(&satisfFill, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  108. MPI_Recv(&satisfSolve, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  109. //Attend pour que tous les autres porcessus se termine
  110. sleep(1);
  111. //Affiche le resultat
  112. printf("------------------------------------------------------------------------------------------------------\n");
  113. printf(GREEN);
  114. printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
  115. printf(YELLOW);
  116. printf("c real time : %.4f seconds\n", gsat->realTime() - startTime);
  117. printf("c [pid:%6d][process:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  118. pid,
  119. rank,
  120. nbIte,
  121. heuriFill,
  122. heuriSolve,
  123. satisfFill,
  124. satisfSolve);
  125. printf(RESET);
  126. }
  127. MPI_Finalize();
  128. }