dfpnParallel.h
Go to the documentation of this file.
00001 /* dfpnParallel.h
00002  */
00003 #ifndef OSL_DFPNPARALLEL_H
00004 #define OSL_DFPNPARALLEL_H
00005 
00006 #include "osl/checkmate/dfpn.h"
00007 #include "osl/misc/lightMutex.h"
00008 
00009 namespace osl
00010 {
00011   namespace checkmate
00012   {
00013     class DfpnShared
00014     {
00015     public:
00016       struct ThreadData
00017       {
00018         HashKey restart_key;
00019         volatile int depth;
00020         volatile bool restart;
00021         LightMutex mutex;
00022         ThreadData() : depth(0), restart(false)
00023         {
00024         }
00025         void clear()
00026         {
00027           restart = false;
00028           restart_key = HashKey();
00029         }
00030       }
00031 #ifdef __GNUC__
00032       __attribute__ ((aligned (64)))
00033 #endif
00034         ;
00035       volatile bool stop_all;
00036       CArray<ThreadData, 32> data;
00037       DfpnShared() : stop_all(false)
00038       {
00039       }
00040       void restartThreads(const HashKey& key, int depth, unsigned int threads)
00041       {
00042         for (int i=0; i<32; ++i)
00043           if ((1u << i) & threads) {
00044             SCOPED_LOCK(lk, data[i].mutex);
00045             if (! data[i].restart || data[i].depth > depth) {
00046               data[i].restart_key = key;
00047               data[i].depth = depth;
00048               data[i].restart = true;
00049             }
00050           }
00051       }
00052       void clear()
00053       {
00054         stop_all = false;
00055         for (size_t i=0; i<data.size(); ++i)
00056           data[i].clear();
00057       }
00058     };
00059 #ifdef OSL_DFPN_SMP
00060     class DfpnParallel : boost::noncopyable
00061     {
00062     private:
00063       DfpnTable *table;
00064       boost::scoped_array<Dfpn> workers;
00065       size_t num_threads;
00066       // working data
00067       const NumEffectState *state;
00068       HashKey key;
00069       PathEncoding path;
00070       Move last_move;
00071       size_t limit;
00072       struct WorkerData
00073       {
00074         Move best_move;
00075         PieceStand proof;
00076         ProofDisproof result;
00077       };
00078       boost::scoped_array<WorkerData> worker_data;
00079       DfpnShared shared;
00080     public:
00081       explicit DfpnParallel(size_t num_threads=0);
00082       ~DfpnParallel();
00083       void setTable(DfpnTable *new_table);
00084       
00085       const ProofDisproof 
00086       hasCheckmateMove(const NumEffectState& state, const HashKey& key,
00087                        const PathEncoding& path, size_t limit, Move& best_move,
00088                        Move last_move=Move::INVALID(), vector<Move> *pv=0);
00089       const ProofDisproof 
00090       hasCheckmateMove(const NumEffectState& state, const HashKey& key,
00091                        const PathEncoding& path, size_t limit, Move& best_move, PieceStand& proof,
00092                        Move last_move=Move::INVALID(), vector<Move> *pv=0);
00093       const ProofDisproof
00094       hasEscapeMove(const NumEffectState& state, 
00095                     const HashKey& key, const PathEncoding& path, 
00096                     size_t limit, Move last_move);
00097 
00098       size_t nodeCount() const;
00099       const DfpnTable& currentTable() const { return *table; }
00100       void analyze(const PathEncoding& path,
00101                    const NumEffectState& state, const vector<Move>& moves) const;
00102 
00103       void stopNow() 
00104       {
00105         shared.stop_all = true;
00106       }
00107 
00108       struct AttackWorker;
00109       struct DefenseWorker;
00110       friend struct AttackWorker;
00111       friend struct DefenseWorker;
00112     };
00113 #endif
00114   }
00115 }
00116 
00117 
00118 #endif /* OSL_DFPNPARALLEL_H */
00119 // ;;; Local Variables:
00120 // ;;; mode:c++
00121 // ;;; c-basic-offset:2
00122 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines