X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fthreadgenesorter.h;fp=src%2Fthreadgenesorter.h;h=57d10520611b4e702f53860ffff8364d74d0ff98;hb=623097444fac1993a86e6d73b203bc3c6d731c11;hp=0000000000000000000000000000000000000000;hpb=69cfdf79b7597ee6133dc3add1a759bc94861fa6;p=germs.git diff --git a/src/threadgenesorter.h b/src/threadgenesorter.h new file mode 100644 index 0000000..57d1052 --- /dev/null +++ b/src/threadgenesorter.h @@ -0,0 +1,136 @@ +/*************************************************************************** + * Copyright (C) 2008 by Michael Andreen * + * andreen@student.chalmers.se * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * + ***************************************************************************/ + +#ifndef __THREADGENESORTER_H__ +#define __THREADGENESORTER_H__ + +#include "genesorter.h" +#include "geneorder.h" +#include "model.h" + +#include + +#include +#include +#include + +extern "C" void* t_sorter(void* arg); +extern "C" void* t_worker(void* arg); + +/** + * Finding the set of all possible sorting sequences. + * + * \author Michael Andreen + */ +class ThreadGeneSorter : public GeneSorter{ + friend void* t_sorter(void* arg); + friend void* t_worker(void* arg); + public: + + struct ScoreCmp { + template + bool operator()(T s1, T s2){ + return s1.first < s2.first; + } + }; + typedef std::priority_queue,std::vector >, ScoreCmp > SolutionsQueue; + + struct SortUnit{ + double _score; + GeneOrder _go; + ActionList _al; + SortUnit(double score,const GeneOrder& go,const ActionList& al) : _score(score),_go(go),_al(al){} + bool operator<(const SortUnit& su) const{ + return _score < su._score; + } + }; + + /** + * Creates a new threaded GeneSorter. + * + * \param threads The number of worker threads that you want to spawn. + */ + ThreadGeneSorter(const Model& m, int threads = 2); + + /** + * Takes a GeneOrder, and Model. + * + * Starts the work to find all possible sorting sequences. + */ + void start(const GeneOrder& go); + + /** + * Wait for sorter to end. + */ + void join(); + + /** + * Tell the sorter to stop execution. + */ + void stop(); + + /** + * Number of possible solutions. + */ + size_t size(); + + /** + * Waits for progress. + * + * \param time Max time to wait for. + * \param solutions The minimum number of solutions we'll wait for. + */ + double progress(int time, int solutions = 1); + + /** + * Get the full list of solutions. + */ + SolutionsQueue solutions(); + + ~ThreadGeneSorter(){}; + + private: + + void sorter(const GeneOrder& go); + void worker(); + + typedef std::priority_queue SortQueue; + //! Queue for possible sort alternatives. + SortQueue _queue; + //! List of full sort sequences. + SolutionsQueue _solutions; + + bool _done; + + int _workers; + + Model _model; + + pthread_mutex_t _queuelock; + pthread_mutex_t _solutionslock; + + pthread_cond_t _addedSolution; + pthread_cond_t _waiting; + pthread_cond_t _addedTask; + + pthread_t _tid; +}; + +#endif