简体   繁体   中英

Parallel Knight's Tour algorithm

Currently I have a knight's tour algorithm that is working.

I uses a combination of:

  • Backtracking
  • Warnsdorf's Rule

The algorithm does the following:

Checks to see if the board is solved (all squares visited) 
    If true: return true
else proceed:

Make a set of possible moves from current positions
Sort the set based on the number of moves available from those positions.

Go through the set. (Recursive call made here)
    If returned True:
       Set Solution board to appropriate number
       Return true
    else
      go back to last position
      return false.

It works okay. It is not the best solution.

I am trying to increase the speed using parallelization, in particular, using C++ threading ( #include<thread> ).

What is the algorithm for this? So far the only ways I have tried have false sharing issues, shared memory issues or won't run at all.

Granted this is for C++, after calling the #include<thread> header, a simple way of creating threads is:

#include <iostream>
#include <thread>

void testThread()
{
    std::cout<<"Thread\n";
}

int main(int argc, char * argv[])
{
    std::testThread t(testThread);
    t.join();

    return 0;
}

The std::testThread t(testThread); calls the thread creation while t.join(); joins them after completion. But this is all without using locks. If I were you I would go over same examples online - there are a ton of resources - that show how to implement locks in a safe manor.

A thing to note is that you must make sure that the sequential version of the code can actually benefit from being run in parallel since thread creation can be costly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM