/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2009 * * Last modified: * $Date: 2011-04-11 19:28:27 +1000 (Mon, 11 Apr 2011) $ by $Author: tack $ * $Revision: 11929 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #ifndef __GECODE_SEARCH_PARALLEL_DFS_HH__ #define __GECODE_SEARCH_PARALLEL_DFS_HH__ #include namespace Gecode { namespace Search { namespace Parallel { /// %Parallel depth-first search engine class DFS : public Engine { protected: /// %Parallel depth-first search worker class Worker : public Engine::Worker { public: /// Initialize for space \a s (of size \a sz) with engine \a e Worker(Space* s, size_t sz, DFS& e); /// Provide access to engine DFS& engine(void) const; /// Start execution of worker virtual void run(void); /// Try to find some work void find(void); /// Reset engine to restart at space \a s and return new root space Space* reset(Space* s); }; /// Array of worker references Worker** _worker; public: /// Provide access to worker \a i Worker* worker(unsigned int i) const; /// \name Search control //@{ /// Report solution \a s void solution(Space* s); /// Reset engine to restart at space \a s and return new root space Space* reset(Space* s); //@} /// \name Engine interface //@{ /// Initialize for space \a s (of size \a sz) with options \a o DFS(Space* s, size_t sz, const Options& o); /// Return statistics virtual Statistics statistics(void) const; /// Destructor virtual ~DFS(void); //@} }; /* * Basic access routines */ forceinline DFS& DFS::Worker::engine(void) const { return static_cast(_engine); } forceinline DFS::Worker* DFS::worker(unsigned int i) const { return _worker[i]; } /* * Engine: initialization */ forceinline DFS::Worker::Worker(Space* s, size_t sz, DFS& e) : Engine::Worker(s,sz,e) {} forceinline DFS::DFS(Space* s, size_t sz, const Options& o) : Engine(o) { // Create workers _worker = static_cast (heap.ralloc(workers() * sizeof(Worker*))); // The first worker gets the entire search tree _worker[0] = new Worker(s,sz,*this); // All other workers start with no work for (unsigned int i=1; istatus(*this) == SS_FAILED)) { delete s; cur = NULL; Search::Worker::reset(); return NULL; } else { cur = s; Search::Worker::reset(cur); return s->clone(false); } } forceinline Space* DFS::reset(Space* s) { // All workers are marked as busy again n_busy = workers(); for (unsigned int i=1; ireset(NULL); return worker(0)->reset(s); } /* * Engine: search control */ forceinline void DFS::solution(Space* s) { m_search.acquire(); bool bs = signal(); solutions.push(s); if (bs) e_search.signal(); m_search.release(); } /* * Worker: finding and stealing working */ forceinline void DFS::Worker::find(void) { // Try to find new work (even if there is none) for (unsigned int i=0; isteal(r_d)) { // Reset this guy m.acquire(); idle = false; d = 0; cur = s; Search::Worker::reset(cur,r_d); m.release(); return; } } } }}} #endif // STATISTICS: search-parallel