Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded Round Robin Tournament

I'm trying to test 8 different game-playing algorithms. These algorithms can play against each other different type of games that follow an Game interface.

So, they have to play against each other 100 games. I've done that part and it works fine. Now, I'm trying to make it multithreaded, to take advantage of the 8-cores of a computer of a friend.

I have very little experience working with threads. So, what kind of changes would I have to make in order to make my code multithreaded?

Here is the code for my single threaded version.

EDIT: The solution I thought of (with my basic knowledge) is about making a Match class, which takes two players and the game they want to play. That class would implement Runnable and I could make a thread for each of the games. My question now would be, how would I notify of the results once the run() method is finished?

Thanks

for (int p1 = 0; p1 < allPlayers.length; p1++)
{
    for (int p2 = p1 + 1; p2 < allPlayers.length; p2++)
    {    
        for (int t = 0; t < trials; t++)
        {
            int player1 = t % 2 == 0 ? p1 : p2;
            int player2 = t % 2 == 0 ? p2 : p1;
            Player[] players = new Player[] { allPlayers[player1], allPlayers[player2] };
            game.newGame();

            while (!game.isFinished())
                game.playNthMove(players[game.currentPlayer()].move(game));

            data[p1][p2][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 0 : 1]);
            data[p2][p1][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 1 : 0]);
        }
    }
}
like image 219
David Robles Avatar asked May 02 '11 11:05

David Robles


3 Answers

You shouldn't need to modify the code for the single-threaded implementation - you should just need to make a class that implements the Runnable interface and wrap that functionality in a run() method. Then you can make a Thread passing in an instance of that class, and call Thread.start().

See this for a reference.

Edit: How to get results from that thread:

It looks like the Callable Interface is what you're looking for. Here's a link with a basic explanation on how to use it. It requires a basic knowledge of how the Runnable interface is used for creating a multi-threaded application without results, so I'd recommend reading this first.

like image 104
T.K. Avatar answered Nov 19 '22 13:11

T.K.


Collection<Callable<Void>> tasks = new ArrayList<Callable<Void>();
for (int p1 = 0; p1 < allPlayers.length; p1++)
{
    for (int p2 = p1 + 1; p2 < allPlayers.length; p2++)
    {    
        for (int t = 0; t < trials; t++)
        {
            final int player1 = t % 2 == 0 ? p1 : p2;
            final int player2 = t % 2 == 0 ? p2 : p1;
            final Player[] players = new Player[] { allPlayers[player1], allPlayers[player2] };
            final int trial = t;
            tasks.add(new Callable<Void>() {
               public Void call() {
                 game.newGame();

                 while (!game.isFinished())
                    game.playNthMove(players[game.currentPlayer()].move(game));

                 data[p1][p2][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 0 : 1]);
                 data[p2][p1][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 1 : 0]);
                 return null;
              }
           });
        }
    }
}
executor.invokeAll(tasks); // called on an exector, will wait for all tasks to complete

The problem with the current design is that the game object does not look thread safe. You'll probably want a new game object for each Runnable.

like image 33
Michael Krussel Avatar answered Nov 19 '22 14:11

Michael Krussel


There is not magic bullet. You need to know the basics before asking that question. Start by reading this article at MSDN: http://msdn.microsoft.com/en-us/magazine/cc163744.aspx. It's pretty much the same for Java.

When done, come back and ask another question regarding a problem you got with your own attempt.

Sure, you might get another answer here showing you what to do. But please, do not read that answer. It's vital that you understand the basics before going further. Otherwise you'll be completely lost when your app stops working.

like image 24
jgauffin Avatar answered Nov 19 '22 13:11

jgauffin