Main.cpp 5.4 KB

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