Преглед изворни кода

Ajout mutex pour attribut end

Loquicom пре 6 година
родитељ
комит
6604b57d24

+ 9 - 3
Partie_1/Hybride/GSATThreadMPI.cpp

@@ -57,9 +57,15 @@ void GSATThreadMPI::end() {
 }
 
 void GSATThreadMPI::end(bool endOrNot) {
+	std::lock_guard<std::mutex> guard(mEnd);
 	bEnd = endOrNot;
 }
 
+bool GSATThreadMPI::isEnd() {
+	std::lock_guard<std::mutex> guard(mEnd);
+	return bEnd;
+}
+
 void GSATThreadMPI::printResult() {
 	printf(GREEN);
 	printf("s %s\n",find?"SATISFIABLE":"NOT FOUND");
@@ -98,7 +104,7 @@ void GSATThreadMPI::solverThread(int id) {
 	double startTime = gsat[id]->realTime();
 	bool solve = false;
 	int cpt = 0;
-	while(!bEnd && !solve){
+	while(!this->isEnd() && !solve){
 		//Pour eviter que les processeurs effectue tous les meme calculs
         int fill = rand() % 4 + 2;
         int h = rand() % 7;
@@ -121,9 +127,9 @@ void GSATThreadMPI::solverThread(int id) {
 		cpt++;
 	}
 	//Si 1er arreter
-	if(!bEnd && solve) {
+	if(!this->isEnd() && solve) {
 		this->mpiNotify(world_rank);
-		this->end(true);
+		this->end();
 		find = true;
 		this->setResult(
 			true,

+ 3 - 0
Partie_1/Hybride/GSATThreadMPI.hpp

@@ -4,6 +4,7 @@
 /* --- Include --- */
 #include <vector>
 #include <thread>
+#include <mutex>
 #include "GSAT/GSAT.hpp"
 
 /* --- Constante --- */
@@ -32,6 +33,7 @@ class GSATThreadMPI {
 
 		void end();
 		void end(bool);
+		bool isEnd();
 
 		void printResult();
 
@@ -43,6 +45,7 @@ class GSATThreadMPI {
 
 	private:
 		bool bEnd;
+		std::mutex mEnd;
 		bool find;
 		int nbThread;
 		std::vector<GSAT*> gsat;

+ 22 - 8
Partie_1/Thread/GSATThread.cpp

@@ -39,7 +39,7 @@ bool GSATThread::solve(bool verbose) {
 
 bool GSATThread::solve(int _maxIteration, bool verbose) {
 	threads.clear();
-	end = false;
+	this->end(false);
 	calcTime = 0;
 	maxIteration = _maxIteration;
 	//Lance thread
@@ -56,22 +56,36 @@ bool GSATThread::solve(int _maxIteration, bool verbose) {
 		printf("-----------------------------------------------------------------------------------------\n");
 		this->printResult();
 	}
-  	return end;
+  	return this->isEnd();
+}
+
+void GSATThread::end() {
+	this->end(true);
+}
+
+void GSATThread::end(bool endOrNot) {
+	std::lock_guard<std::mutex> guard(mEnd);
+	bEnd = endOrNot;
+}
+
+bool GSATThread::isEnd() {
+	std::lock_guard<std::mutex> guard(mEnd);
+	return bEnd;
 }
 
 void GSATThread::printResult() {
-	if(end) {
+	if(this->isEnd()) {
 		printf(GREEN "s SATISFIABLE\n" RESET);
 	} else {
 		printf(RED "NOT FOUND\n" RESET);
 	}
 	//printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
-	if(!end && calcTime == 0) {
+	if(!this->isEnd() && calcTime == 0) {
 		//Si on à pas trouver
 		calcTime = gsat[0]->realTime() - time[0];
 	}
 	printf(YELLOW);
-	if(end) {
+	if(this->isEnd()) {
 		printf("c [thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
 			result.threadId,
 			result.nbIteration,
@@ -90,7 +104,7 @@ void GSATThread::solverThread(int id, bool verbose) {
 	//Resolution
 	bool solve = false;
 	int cpt = 0;
-	while(!end && !solve && (maxIteration == 0 || cpt < maxIteration)){
+	while(!this->isEnd() && !solve && (maxIteration == 0 || cpt < maxIteration)){
 		solve = gsat[id]->start();
 		if(verbose) {
 			if(solve) {
@@ -110,8 +124,8 @@ void GSATThread::solverThread(int id, bool verbose) {
 		cpt++;
 	}
 	//Si 1er arreter
-	if(!end && solve) {
-		end = true;
+	if(!this->isEnd() && solve) {
+		this->end();
 		calcTime = gsat[id]->realTime() - time[id];
 		result.threadId = id;
 		result.nbIteration = gsat[id]->getNbIterations();

+ 10 - 1
Partie_1/Thread/GSATThread.hpp

@@ -4,6 +4,7 @@
 /* --- Include --- */
 #include <vector>
 #include <thread>
+#include <mutex>
 #include "GSAT/GSAT.hpp"
 
 /* --- Constante --- */
@@ -23,17 +24,25 @@ class GSATThread {
 	public:
 		GSATThread(int, int, char**);
 		~GSATThread();
+
 		bool solve();
 		bool solve(int);
 		bool solve(bool);
 		bool solve(int, bool);
+
+		void end();
+		void end(bool);
+		bool isEnd();
+
 		void printResult();
+
 		/* --- Getter --- */
 		inline unsigned int getNbVariables() {return gsat[0]->getNbVariables();}
 		inline unsigned int getNbClauses() {return gsat[0]->getNbClauses();}
 
 	private:
-		bool end;
+		bool bEnd;
+		std::mutex mEnd;
 		double calcTime;
 		int nbThread;
 		std::vector<GSAT*> gsat;

+ 26 - 13
Partie_2/EMA/GSATThread.cpp

@@ -39,13 +39,13 @@ bool GSATThread::solve() {
 }
 
 bool GSATThread::solve(bool verbose) {
-	end = false;
+	this->end(false);
 	calcTime = 0;
 	//Lance Initialise
 	printf("--- Initialize --------------------------------------------------------------------------\n");
 	this->initBandit(verbose);
 	//Si la reponse n'a pas été trouvé durant l'initialisation
-	if(!end) {
+	if(!this->isEnd()) {
 		//Lance les bandit avec la méthode choisit
 		printf("--- Search ------------------------------------------------------------------------------\n");
 		this->runBandit(verbose);
@@ -55,22 +55,35 @@ bool GSATThread::solve(bool verbose) {
 		printf("--- Result ------------------------------------------------------------------------------\n");
 		this->printResult();
 	}
-  	return end;
+  	return this->isEnd();
+}
+
+void GSATThread::end() {
+	this->end(true);
+}
+
+void GSATThread::end(bool endOrNot) {
+	std::lock_guard<std::mutex> guard(mEnd);
+	bEnd = endOrNot;
+}
+
+bool GSATThread::isEnd() {
+	std::lock_guard<std::mutex> guard(mEnd);
+	return bEnd;
 }
 
 void GSATThread::printResult() {
-	if(end) {
+	if(this->isEnd()) {
 		printf(GREEN "s SATISFIABLE\n" RESET);
 	} else {
 		printf(RED "NOT FOUND\n" RESET);
 	}
-	//printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
-	if(!end && calcTime == 0) {
+	if(!this->isEnd() && calcTime == 0) {
 		//Si on à pas trouver
 		calcTime = gsat[0]->realTime() - time[0];
 	}
 	printf(YELLOW);
-	if(end) {
+	if(this->isEnd()) {
 		printf("c [thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
 			result.threadId,
 			result.nbIteration,
@@ -113,12 +126,12 @@ void GSATThread::runBandit(bool verbose) {
 void GSATThread::initThread(int id, bool verbose) {
 	//Les threads vont jouer les bandits pour etablir les moyennes
 	bool solve = false;
-	while(!end && !solve && !cb.queueIsEmpty()){
+	while(!this->isEnd() && !solve && !cb.queueIsEmpty()){
 		solve = this->calc(id, verbose);
 	}
 	//Si 1er arreter
-	if(!end && solve) {
-		end = true;
+	if(!this->isEnd() && solve) {
+		this->end();
 		calcTime = gsat[id]->realTime() - time[id];
 		result.threadId = id;
 		result.nbIteration = gsat[id]->getNbIterations();
@@ -132,12 +145,12 @@ void GSATThread::initThread(int id, bool verbose) {
 void GSATThread::runThread(int id, bool verbose) {
 	//Les threads vont jouer les bandits avec la méthode choisit
 	bool solve = false;
-	while(!end && !solve){
+	while(!this->isEnd() && !solve){
 		solve = this->calc(id, verbose);
 	}
 	//Si 1er arreter
-	if(!end && solve) {
-		end = true;
+	if(!this->isEnd() && solve) {
+		this->end();
 		calcTime = gsat[id]->realTime() - time[id];
 		result.threadId = id;
 		result.nbIteration = gsat[id]->getNbIterations();

+ 8 - 1
Partie_2/EMA/GSATThread.hpp

@@ -4,6 +4,7 @@
 /* --- Include --- */
 #include <vector>
 #include <thread>
+#include <mutex>
 #include "GSAT/GSAT.hpp"
 #include "ControlBandit.hpp"
 
@@ -30,6 +31,11 @@ class GSATThread {
 
 		bool solve();
 		bool solve(bool);
+
+		void end();
+		void end(bool);
+		bool isEnd();
+
 		void printResult();
 
 		/* --- Getter --- */
@@ -38,7 +44,8 @@ class GSATThread {
 
 	private:
 		ControlBandit cb;
-		bool end;
+		bool bEnd;
+		std::mutex mEnd;
 		double calcTime;
 		int nbThread;
 		std::vector<GSAT*> gsat;

+ 26 - 13
Partie_2/Moyenne/GSATThread.cpp

@@ -39,13 +39,13 @@ bool GSATThread::solve() {
 }
 
 bool GSATThread::solve(bool verbose) {
-	end = false;
+	this->end(false);
 	calcTime = 0;
 	//Lance Initialise
 	printf("--- Initialize --------------------------------------------------------------------------\n");
 	this->initBandit(verbose);
 	//Si la reponse n'a pas été trouvé durant l'initialisation
-	if(!end) {
+	if(!this->isEnd()) {
 		//Lance les bandit avec la méthode choisit
 		printf("--- Search ------------------------------------------------------------------------------\n");
 		this->runBandit(verbose);
@@ -55,22 +55,35 @@ bool GSATThread::solve(bool verbose) {
 		printf("--- Result ------------------------------------------------------------------------------\n");
 		this->printResult();
 	}
-  	return end;
+  	return this->isEnd();
+}
+
+void GSATThread::end() {
+	this->end(true);
+}
+
+void GSATThread::end(bool endOrNot) {
+	std::lock_guard<std::mutex> guard(mEnd);
+	bEnd = endOrNot;
+}
+
+bool GSATThread::isEnd() {
+	std::lock_guard<std::mutex> guard(mEnd);
+	return bEnd;
 }
 
 void GSATThread::printResult() {
-	if(end) {
+	if(this->isEnd()) {
 		printf(GREEN "s SATISFIABLE\n" RESET);
 	} else {
 		printf(RED "NOT FOUND\n" RESET);
 	}
-	//printf("s %s\n",end?"SATISFIABLE":"NOT FOUND");
-	if(!end && calcTime == 0) {
+	if(!this->isEnd() && calcTime == 0) {
 		//Si on à pas trouver
 		calcTime = gsat[0]->realTime() - time[0];
 	}
 	printf(YELLOW);
-	if(end) {
+	if(this->isEnd()) {
 		printf("c [thread:%2d][iteration:%4d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
 			result.threadId,
 			result.nbIteration,
@@ -113,12 +126,12 @@ void GSATThread::runBandit(bool verbose) {
 void GSATThread::initThread(int id, bool verbose) {
 	//Les threads vont jouer les bandits pour etablir les moyennes
 	bool solve = false;
-	while(!end && !solve && !cb.queueIsEmpty()){
+	while(!this->isEnd() && !solve && !cb.queueIsEmpty()){
 		solve = this->calc(id, verbose);
 	}
 	//Si 1er arreter
-	if(!end && solve) {
-		end = true;
+	if(!this->isEnd() && solve) {
+		this->end();
 		calcTime = gsat[id]->realTime() - time[id];
 		result.threadId = id;
 		result.nbIteration = gsat[id]->getNbIterations();
@@ -132,12 +145,12 @@ void GSATThread::initThread(int id, bool verbose) {
 void GSATThread::runThread(int id, bool verbose) {
 	//Les threads vont jouer les bandits avec la méthode choisit
 	bool solve = false;
-	while(!end && !solve){
+	while(!this->isEnd() && !solve){
 		solve = this->calc(id, verbose);
 	}
 	//Si 1er arreter
-	if(!end && solve) {
-		end = true;
+	if(!this->isEnd() && solve) {
+		this->end();
 		calcTime = gsat[id]->realTime() - time[id];
 		result.threadId = id;
 		result.nbIteration = gsat[id]->getNbIterations();

+ 8 - 1
Partie_2/Moyenne/GSATThread.hpp

@@ -4,6 +4,7 @@
 /* --- Include --- */
 #include <vector>
 #include <thread>
+#include <mutex>
 #include "GSAT/GSAT.hpp"
 #include "ControlBandit.hpp"
 
@@ -30,6 +31,11 @@ class GSATThread {
 
 		bool solve();
 		bool solve(bool);
+
+		void end();
+		void end(bool);
+		bool isEnd();
+
 		void printResult();
 
 		/* --- Getter --- */
@@ -38,7 +44,8 @@ class GSATThread {
 
 	private:
 		ControlBandit cb;
-		bool end;
+		bool bEnd;
+		std::mutex mEnd;
 		double calcTime;
 		int nbThread;
 		std::vector<GSAT*> gsat;