Commit 75d7679e authored by Gaurav Kukreja's avatar Gaurav Kukreja

Parallel-Minimax Working Code

Signed-off-by: 's avatarGaurav Kukreja <mailme.gaurav@gmail.com>
parent 44c58013
This diff is collapsed.
......@@ -21,6 +21,12 @@
#include "eval.h"
#include "network.h"
#define pretty_print(name, val) printf("%s = %d: %s: %s: %d\n", name, val, __FILE__,__FUNCTION__,__LINE__);
int thread_rank;
int num_threads;
/* Global, static vars */
NetworkLoop l;
......@@ -47,7 +53,7 @@ char* host = 0; /* not used on default */
int rport = 23412;
/* local channel */
int lport = 23412;
int lport = 13375;
/* change evaluation after move? */
bool changeEval = true;
......@@ -83,6 +89,11 @@ void MyDomain::sendBoard()
void MyDomain::received(char* str)
{
for(int i=1;i<num_threads;i++)
MPI_Send (str, 1024, MPI_CHAR, i, 10,
MPI_COMM_WORLD);
if (strncmp(str, "quit", 4)==0) {
l.exit();
return;
......@@ -273,9 +284,6 @@ void parseArgs(int argc, char* argv[])
}
}
int thread_rank;
int num_threads;
int main(int argc, char* argv[])
{
......@@ -284,9 +292,15 @@ int main(int argc, char* argv[])
MPI_Comm_size(MPI_COMM_WORLD, &num_threads);
MPI_Comm_rank(MPI_COMM_WORLD, &thread_rank);
if(thread_rank == 0)
parseArgs(argc, argv);
#if 0
if(thread_rank < 4)
myColor = Board::color1;
else {
myColor = Board::color2;
thread_rank = thread_rank - 4;
}
#endif
SearchStrategy* ss = SearchStrategy::create(strategyNo);
if (verbose)
printf("Using strategy '%s' ...\n", ss->name());
......@@ -296,12 +310,115 @@ int main(int argc, char* argv[])
ss->setEvaluator(&ev);
ss->registerCallbacks(new SearchCallbacks(verbose));
if(thread_rank == 0) {
MyDomain d(lport);
if (host) d.addConnection(host, rport);
l.install(&d);
l.run();
}
else
{
bool exit_loop = false;
while(!exit_loop)
{
char state_str[1024];
MPI_Status mpi_st;
pretty_print("thread_rank", thread_rank);
MPI_Recv (state_str, 1024, MPI_CHAR, 0, 10, MPI_COMM_WORLD, &mpi_st);
pretty_print("thread_rank", thread_rank);
if (strncmp(state_str, "quit", 4)==0) {
l.exit();
return 0;
}
if (strncmp(state_str, "pos ", 4)!=0) return 0;
b.setState(state_str+4);
if (verbose) {
printf("\n\n==========================================\n");
printf(state_str+4);
}
int state = b.validState();
if ((state == Board::empty))
continue;
if ((state != Board::valid1) &&
(state != Board::valid2)) {
printf("%s\n", Board::stateDescription(state));
switch(state) {
case Board::timeout1:
case Board::timeout2:
case Board::win1:
case Board::win2:
// l.exit();
exit_loop = true;
default:
break;
}
return 0;
}
if (b.actColor() & myColor) {
struct timeval t1, t2;
gettimeofday(&t1,0);
Move m = b.bestMove();
gettimeofday(&t2,0);
int msecsPassed =
(1000* t2.tv_sec + t2.tv_usec / 1000) -
(1000* t1.tv_sec + t1.tv_usec / 1000);
printf("%s ", (myColor == Board::color1) ? "O":"X");
if (m.type == Move::none) {
printf(" can not draw any move ?! Sorry.\n");
return 0;
}
printf("draws '%s' (after %d.%03d secs)...\n",
m.name(), msecsPassed/1000, msecsPassed%1000);
b.playMove(m, msecsPassed);
// sendBoard();
if (changeEval)
ev.changeEvaluation();
/* stop player at win position */
int state = b.validState();
if ((state != Board::valid1) &&
(state != Board::valid2)) {
printf("%s\n", Board::stateDescription(state));
switch(state) {
case Board::timeout1:
case Board::timeout2:
case Board::win1:
case Board::win2:
// l.exit();
exit_loop = true;
default:
break;
}
}
maxMoves--;
if (maxMoves == 0) {
printf("Terminating because given number of moves drawn.\n");
// broadcast("quit\n");
// l.exit();
exit_loop = true;
}
}
pretty_print("*********** Exit_loop", exit_loop);
}
}
MPI_Finalize();
}
......@@ -45,7 +45,7 @@ static int msecsToPlay[3] = {0,0,0};
/* to set verbosity of NetworkLoop implementation */
extern int verbose;
#define DEFAULT_DOMAIN_PORT 23412
#define DEFAULT_DOMAIN_PORT 13375
#define DEFAULT_DOMAIN_DIFF 50
/* remote channel */
......
......@@ -17,6 +17,8 @@
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
extern int maxDepth;
extern int thread_rank;
extern int num_threads;
......@@ -54,7 +56,7 @@ class MinimaxStrategy: public SearchStrategy
};
int current_depth=0;
#define MAX_DEPTH 3
#define MAX_DEPTH maxDepth
int MinimaxStrategy::minimax()
{
......@@ -121,25 +123,27 @@ int MinimaxStrategy::minimax()
}
bestEval = sign*bestEval;
// if(thread_rank==0)
// {
if(current_depth == 0)
{
if(thread_rank==0)
{
Move *moves=NULL;
int *eval_results;
moves=(Move*)malloc((num_threads -1)*sizeof(Move));
eval_results=(int*)malloc((num_threads - 1)*sizeof(int));
// }
//all threads send value to thread 0
MPI_Gather (&_bestMove, sizeof(Move), MPI_BYTE,
moves, sizeof(Move), MPI_BYTE, 0, MPI_COMM_WORLD);
MPI_Gather (&bestEval, 1, MPI_INT,
eval_results, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
if(thread_rank == 0)
for(int i=1;i<num_threads;i++)
{
for(i=0;i<num_threads-1;i++)
MPI_Status status;
MPI_Recv((void*)&moves[i-1], sizeof(Move), MPI_BYTE, i, 10,
MPI_COMM_WORLD, &status);
MPI_Recv(&eval_results[i-1], 1, MPI_INT, i, 10,
MPI_COMM_WORLD, &status);
}
bestestMove=_bestMove;
for(int i=0;i<num_threads-1;i++)
{
if(sign*eval_results[i] > sign*bestEval)
{
......@@ -147,35 +151,45 @@ int MinimaxStrategy::minimax()
bestestMove=moves[i];
}
}
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast (&bestestMove, sizeof(Move), MPI_BYTE, 0,
for(int i=1;i<num_threads;i++)
{
MPI_Send (&bestestMove, sizeof(Move), MPI_BYTE, i, 10,
MPI_COMM_WORLD);
MPI_Bcast (&bestEval, 1, MPI_INT, 0,
MPI_Send (&bestEval, 1, MPI_INT, i, 10,
MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
}
else
{
MPI_Send (&_bestMove, sizeof(Move), MPI_BYTE, 0, 10,
MPI_COMM_WORLD);
MPI_Send (&bestEval, 1, MPI_INT, 0, 10,
MPI_COMM_WORLD);
MPI_Status status;
MPI_Recv(&bestestMove, sizeof(Move), MPI_BYTE, 0, 10,
MPI_COMM_WORLD, &status);
MPI_Recv(&bestEval, 1, MPI_INT, 0, 10,
MPI_COMM_WORLD, &status);
}
foundBestMove(0, bestestMove, bestEval);
if(current_depth == 0)
finishedNode(0,0);
}
return bestEval;
}
void MinimaxStrategy::searchBestMove()
{
// KUKU : Here we have to implement the minimax strategy
// Minimax strategy tries to minimize the maximum possible outcome of opponent.
// At each turn, we check for each move the max positive outcome for opponent.
// We choose the move for which the max is least.
// To check this, we look at more than one levels in the Game Tree.
// KUKU : Here we have to implement the minimax strategy
// Minimax strategy tries to minimize the maximum possible outcome of opponent.
// At each turn, we check for each move the max positive outcome for opponent.
// We choose the move for which the max is least.
// To check this, we look at more than one levels in the Game Tree.
// we try to maximize bestEvaluation
/* int bestEval = minEvaluation();
/* int bestEval = minEvaluation();
int eval;
Move m;
......@@ -199,7 +213,7 @@ void MinimaxStrategy::searchBestMove()
}
finishedNode(0,0);
*/
*/
minimax();
}
......
......@@ -29,7 +29,7 @@ static int secsToPlay = -1;
/* to set verbosity of NetworkLoop implementation */
extern int verbose;
#define DEFAULT_DOMAIN_PORT 23412
#define DEFAULT_DOMAIN_PORT 13375
/* remote channel */
static char* host = 0; /* not used on default */
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment