Main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: %s -i file.cnf\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 nbProcessors: %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, send;
  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 [processor:%d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  50. world_rank,
  51. gsat->getNbIterations(),
  52. gsat->getHeuristicFill(),
  53. gsat->getHeuristicSolve(),
  54. gsat->getNbSatisfiedClausesFill(),
  55. gsat->getNbSatisfiedClausesSolve());
  56. if(end) {
  57. printf(RESET);
  58. }
  59. //Regarde si quelqu'un a trouver
  60. int flag;
  61. MPI_Test(&sync, &flag, MPI_STATUS_IGNORE);
  62. if(flag) {
  63. stop = true;
  64. }
  65. }
  66. //Si on est le processus qui à trouvé
  67. if(end) {
  68. //Avertit les autres
  69. for(int i = 0; i < world_size; i++) {
  70. MPI_Isend(&world_rank, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &send);
  71. }
  72. //Attend la fin des autres processus
  73. for(int i = 0; i < world_size; i++) {
  74. if(i != world_rank) {
  75. int buff;
  76. MPI_Recv(&buff, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  77. }
  78. }
  79. //Affiche le resultat
  80. printf("--------------------------------------------------------------------------------------------\n");
  81. printf(GREEN);
  82. printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
  83. printf(YELLOW);
  84. printf("c real time : %.4f seconds\n", gsat->realTime() - startTime);
  85. printf("c [processor:%d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  86. world_rank,
  87. gsat->getNbIterations(),
  88. gsat->getHeuristicFill(),
  89. gsat->getHeuristicSolve(),
  90. gsat->getNbSatisfiedClausesFill(),
  91. gsat->getNbSatisfiedClausesSolve());
  92. printf(RESET);
  93. } else {
  94. //Indique au processus qui a trouvé que ce processus a finit de travailler
  95. int buff = 1;
  96. MPI_Send(&buff, 1, MPI_INT, syncBuff, 0, MPI_COMM_WORLD);
  97. }
  98. MPI_Finalize();
  99. }