123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include "Main.hpp"
- #include "GSATThreadMPI.hpp"
- #include "color.h"
- extern int world_rank;
- /* --- Public --- */
- GSATThreadMPI::GSATThreadMPI(int _nbThread, int argc, char** argv) {
- nbThread = _nbThread;
- gsat = std::vector<GSAT*>(_nbThread);
- threads = std::vector<std::thread>(_nbThread);
- for(int i = 0; i < _nbThread; i++) {
- gsat[i] = new GSAT();
- gsat[i]->setParameters(argc,argv);
- gsat[i]->initialize();
- }
- srand(getpid());
- }
- GSATThreadMPI::~GSATThreadMPI() {
- for(int i = 0; i < nbThread; i++) {
- delete gsat[i];
- }
- }
- bool GSATThreadMPI::solve() {
- threads.clear();
- end = false;
- find = false;
- //Lance thread
- for(int i = 0; i < nbThread; i++) {
- threads[i] = std::thread(&GSATThreadMPI::solverThread, this, i);
- }
- //Lance thread de synchronisation MPI
- mpiSync = std::thread(mpiWait, this);
- //Attend resultat
- for(int i = 0; i < nbThread; i++){
- threads[i].join();
- }
- mpiSync.join();
- //Si c'est le proc qui à trouvé
- if(find) {
- //Affiche resultat
- sleep(1); //Attend la fin des autres processus
- printf("-----------------------------------------------------------------------------------------------------------------\n");
- this->printResult();
- }
- return find;
- }
- void GSATThreadMPI::isEnd() {
- end = true;
- }
- void GSATThreadMPI::printResult() {
- printf(GREEN);
- printf("s %s\n",find?"SATISFIABLE":"NOT FOUND");
- printf(YELLOW);
- printf("c real time : %.4f seconds\n", result.calcTime);
- printf("c [pid:%6d][process:%2d][thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
- getpid(),
- world_rank,
- result.threadId,
- result.nbIteration,
- result.heuristicFill,
- result.heuristicSolve,
- result.nbSatisfiedClausesFill,
- result.nbSatisfiedClausesSolve);
- printf(RESET);
- fflush(stdout);
- }
- /* --- Private --- */
- void GSATThreadMPI::solverThread(int id) {
- //Resolution
- double startTime = gsat[id]->realTime();
- bool solve = false;
- int cpt = 0;
- while(!end && !solve){
- //Pour eviter que les processeurs effectue tous les meme calculs
- int fill = rand() % 4 + 2;
- int h = rand() % 7;
- solve = gsat[id]->start(fill, h);
- if(solve) {
- printf(CYAN);
- }
- printf("c [pid:%6d][process:%2d][thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
- getpid(),
- world_rank,
- id,
- gsat[id]->getNbIterations(),
- gsat[id]->getHeuristicFill(),
- gsat[id]->getHeuristicSolve(),
- gsat[id]->getNbSatisfiedClausesFill(),
- gsat[id]->getNbSatisfiedClausesSolve());
- if(solve) {
- printf(RESET);
- }
- cpt++;
- }
- //Si 1er arreter
- if(!end && solve) {
- mpiNotify(world_rank);
- end = true;
- find = true;
- result.threadId = id;
- result.calcTime = gsat[id]->realTime() - startTime;
- result.nbIteration = gsat[id]->getNbIterations();
- result.heuristicFill = gsat[id]->getHeuristicFill();
- result.heuristicSolve = gsat[id]->getHeuristicSolve();
- result.nbSatisfiedClausesFill = gsat[id]->getNbSatisfiedClausesFill();
- result.nbSatisfiedClausesSolve = gsat[id]->getNbSatisfiedClausesSolve();
- }
- }
|