Main.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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-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. break;
  38. case '?':
  39. help(argv[0]);
  40. }
  41. }
  42. if(!optI) {
  43. help(argv[0]);
  44. }
  45. //Init MPI
  46. MPI_Init(NULL, NULL);
  47. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  48. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  49. GSATThreadMPI* gsat = new GSATThreadMPI(nbThread, 3, gsatArg);
  50. if(world_rank == 0) {
  51. printf("c nbProcessors: %d\n", world_size);
  52. printf("c nbThreads: %d\n", nbThread);
  53. printf("c nbVariables: %d\n", gsat->getNbVariables());
  54. printf("c nbClauses: %d\n", gsat->getNbClauses());
  55. }
  56. gsat->solve();
  57. MPI_Finalize();
  58. }
  59. void mpiWait(GSATThreadMPI* gsat) {
  60. int buff;
  61. MPI_Recv(&buff, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  62. if(buff != world_rank) {
  63. gsat->isEnd();
  64. }
  65. }
  66. void mpiNotify(int rank) {
  67. for(int i = 0; i < world_size; i++) {
  68. MPI_Send(&rank, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  69. }
  70. }