Main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. bool end = false;
  34. bool stop = false;
  35. while(!end && !stop){
  36. //Pour eviter que les processeurs effectue tous les meme calculs
  37. int fill = rand() % 4 + 2;
  38. int h = rand() % 7;
  39. end = gsat->start(fill, h);
  40. //Affiche calcul
  41. if(end) {
  42. printf(CYAN);
  43. }
  44. printf("c [processor:%d][iteration:%5d][file:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  45. world_rank,
  46. gsat->getNbIterations(),
  47. gsat->getHeuristicFill(),
  48. gsat->getHeuristicSolve(),
  49. gsat->getNbSatisfiedClausesFill(),
  50. gsat->getNbSatisfiedClausesSolve());
  51. if(end) {
  52. printf(RESET);
  53. }
  54. //Regarde si quelqu'un a trouver
  55. for(int i = 0; i < world_size; i++){
  56. int buff;
  57. if(i == world_rank) {
  58. buff = end ? 1 : 0;
  59. }
  60. MPI_Bcast(&buff, 1, MPI_INT, i, MPI_COMM_WORLD);
  61. if(i != world_rank && buff && !stop) {
  62. stop = true;
  63. }
  64. }
  65. }
  66. if(end) {
  67. printf("--------------------------------------------------------------------------------------------\n");
  68. printf(GREEN);
  69. printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
  70. printf(YELLOW);
  71. printf("c real time : %.4f seconds\n", gsat->realTime() - startTime);
  72. printf("c [processor:%d][iteration:%5d][file:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
  73. world_rank,
  74. gsat->getNbIterations(),
  75. gsat->getHeuristicFill(),
  76. gsat->getHeuristicSolve(),
  77. gsat->getNbSatisfiedClausesFill(),
  78. gsat->getNbSatisfiedClausesSolve());
  79. printf(RESET);
  80. }
  81. MPI_Finalize();
  82. }