Main.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "Main.hpp"
  8. using namespace std;
  9. int world_size;
  10. int world_rank;
  11. void help(char* prog) {
  12. fprintf(stderr, "usage: mpirun -n int ./%s -i file.cnf [-t int]\n\t-n Number of process to use\n\t-i CNF file to solve\n\t-t Number of threads to use\n", prog);
  13. exit(1);
  14. }
  15. int main(int argc, char* argv[]) {
  16. extern char * optarg;
  17. int nbThread = DEFAULT_NB_THREAD;
  18. int opt;
  19. bool optI = false;
  20. char** gsatArg = (char**) malloc(sizeof(char*) * 3);
  21. gsatArg[0] = (char*) malloc(sizeof(char) * strlen(argv[0]) + 1);
  22. memset(gsatArg[0], 0, strlen(argv[0]) + 1);
  23. strncpy(gsatArg[0], argv[0], strlen(argv[0]));
  24. while((opt = getopt(argc, argv, "i:t:")) != -1) {
  25. switch(opt) {
  26. case 'i':
  27. optI = true;
  28. gsatArg[1] = (char*) malloc(sizeof(char) * 3);
  29. memset(gsatArg[1], 0, 3);
  30. strncpy(gsatArg[1], "-i", 2);
  31. gsatArg[2] = (char*) malloc(sizeof(char) * strlen(optarg) + 1);
  32. memset(gsatArg[2], 0, strlen(optarg) + 1);
  33. strncpy(gsatArg[2], optarg, strlen(optarg));
  34. break;
  35. case 't':
  36. nbThread = atoi(optarg);
  37. if(nbThread < 1) {
  38. nbThread = 1;
  39. }
  40. break;
  41. case '?':
  42. help(argv[0]);
  43. }
  44. }
  45. if(!optI) {
  46. help(argv[0]);
  47. }
  48. //Init MPI
  49. int provided;
  50. MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
  51. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  52. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  53. GSATThreadMPI* gsat = new GSATThreadMPI(nbThread, 3, gsatArg);
  54. if(world_rank == 0) {
  55. printf("c nbProcess: %d\n", world_size);
  56. printf("c nbThreads: %d\n", nbThread);
  57. printf("c nbVariables: %d\n", gsat->getNbVariables());
  58. printf("c nbClauses: %d\n", gsat->getNbClauses());
  59. }
  60. gsat->solve();
  61. //Le processus d'affichage attend de recevoir les resultats à afficher
  62. if(world_rank == DISPLAY_RANK) {
  63. sleep(1); //Attend la fin des autres processus
  64. //Recup les infos
  65. int pid, rank, thread;
  66. double time;
  67. unsigned int nbIte, heuriFill, heuriSolve, satisfFill, satisfSolve;
  68. MPI_Recv(&pid, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  69. MPI_Recv(&rank, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  70. MPI_Recv(&thread, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  71. MPI_Recv(&time, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  72. MPI_Recv(&nbIte, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  73. MPI_Recv(&heuriFill, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  74. MPI_Recv(&heuriSolve, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  75. MPI_Recv(&satisfFill, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  76. MPI_Recv(&satisfSolve, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  77. //Set le resultat
  78. gsat->setResult(true, pid, rank, thread, time, nbIte, heuriFill, heuriSolve, satisfFill, satisfSolve);
  79. //Affiche resultat
  80. printf("-----------------------------------------------------------------------------------------------------------------\n");
  81. gsat->printResult();
  82. }
  83. MPI_Finalize();
  84. }