Main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. //Avertit les autres
  72. for(int i = 0; i < world_size; i++) {
  73. MPI_Isend(&world_rank, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &finish);
  74. }
  75. //Si c'est oas le processus d'affichage qui a trouvé, envoi des infos
  76. if(world_rank != DISPLAY_RANK) {
  77. //Envoi les infos de resolution au processus de rank 0
  78. int pid;
  79. double time;
  80. unsigned int nbIte, heuriFill, heuriSolve, satisfFill, satisfSolve;
  81. pid = getpid();
  82. MPI_Isend(&pid, 1, MPI_INT, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  83. MPI_Isend(&world_rank, 1, MPI_INT, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  84. time = gsat->realTime() - startTime;
  85. MPI_Isend(&time, 1, MPI_DOUBLE, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  86. nbIte = gsat->getNbIterations();
  87. MPI_Isend(&nbIte, 1, MPI_UNSIGNED, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  88. heuriFill = gsat->getHeuristicFill();
  89. MPI_Isend(&heuriFill, 1, MPI_UNSIGNED, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  90. heuriSolve = gsat->getHeuristicSolve();
  91. MPI_Isend(&heuriSolve, 1, MPI_UNSIGNED, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  92. satisfFill = gsat->getNbSatisfiedClausesFill();
  93. MPI_Isend(&satisfFill, 1, MPI_UNSIGNED, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  94. satisfSolve = gsat->getNbSatisfiedClausesSolve();
  95. MPI_Isend(&satisfSolve, 1, MPI_UNSIGNED, DISPLAY_RANK, 0, MPI_COMM_WORLD, &finish);
  96. }
  97. }
  98. //L'affichage est fait par le processus un seul processus
  99. if(world_rank == DISPLAY_RANK) {
  100. int pid, rank;
  101. double time;
  102. unsigned int nbIte, heuriFill, heuriSolve, satisfFill, satisfSolve;
  103. if(end) {
  104. pid = getpid();
  105. rank = world_rank;
  106. time = gsat->realTime() - startTime;
  107. nbIte = gsat->getNbIterations();
  108. heuriFill = gsat->getHeuristicFill();
  109. heuriSolve = gsat->getHeuristicSolve();
  110. satisfFill = gsat->getNbSatisfiedClausesFill();
  111. satisfSolve = gsat->getNbSatisfiedClausesSolve();
  112. } else {
  113. //Attend las infos venants de processus qui à trouvé
  114. MPI_Recv(&pid, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  115. MPI_Recv(&rank, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  116. MPI_Recv(&time, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  117. MPI_Recv(&nbIte, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  118. MPI_Recv(&heuriFill, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  119. MPI_Recv(&heuriSolve, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  120. MPI_Recv(&satisfFill, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  121. MPI_Recv(&satisfSolve, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  122. }
  123. //Attend pour que tous les autres porcessus se termine
  124. sleep(1);
  125. //Affiche le resultat
  126. printf("------------------------------------------------------------------------------------------------------\n");
  127. printf(GREEN);
  128. printf("s SATISFIABLE\n");
  129. printf(YELLOW);
  130. printf("c real time : %.4f seconds\n", time);
  131. printf("c [pid:%6d][process:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  132. pid,
  133. rank,
  134. nbIte,
  135. heuriFill,
  136. heuriSolve,
  137. satisfFill,
  138. satisfSolve);
  139. printf(RESET);
  140. }
  141. MPI_Finalize();
  142. }