bab.hh gecode/search/parallel/engine.hh Gecode::Search::Parallel::BAB Gecode::Search::Parallel::BAB::Worker Gecode Gecode::Search Gecode::Search::Parallel /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *ChristianSchulte<schulte@gecode.org> * *Copyright: *ChristianSchulte,2009 * *Lastmodified: *$Date:2013-07-1112:30:18+0200(Thu,11Jul2013)$by$Author:schulte$ *$Revision:13840$ * *ThisfileispartofGecode,thegenericconstraint *developmentenvironment: *http://www.gecode.org * *Permissionisherebygranted,freeofcharge,toanypersonobtaining *acopyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andto *permitpersonstowhomtheSoftwareisfurnishedtodoso,subjectto *thefollowingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbe *includedinallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND, *EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEAND *NONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBE *LIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTION *OFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTION *WITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE. * */ #ifndef__GECODE_SEARCH_PARALLEL_BAB_HH__ #define__GECODE_SEARCH_PARALLEL_BAB_HH__ #include<gecode/search/parallel/engine.hh> namespaceGecode{namespaceSearch{namespaceParallel{ classBAB:publicEngine{ protected: classWorker:publicEngine::Worker{ protected: intmark; Space*best; public: Worker(Space*s,BAB&e); BAB&engine(void)const; virtualvoidrun(void); voidbetter(Space*b); voidfind(void); voidreset(Space*s,intngdl); virtual~Worker(void); }; Worker**_worker; Space*best; public: Worker*worker(unsignedinti)const; voidsolution(Space*s); BAB(Space*s,constOptions&o); virtualStatisticsstatistics(void)const; virtualvoidreset(Space*s); virtualNoGoods&nogoods(void); virtual~BAB(void); }; /* *Engine:basicaccessroutines */ forceinlineBAB& BAB::Worker::engine(void)const{ returnstatic_cast<BAB&>(_engine); } forceinlineBAB::Worker* BAB::worker(unsignedinti)const{ return_worker[i]; } forceinlinevoid BAB::Worker::reset(Space*s,intngdl){ deletecur; deletebest; best=NULL; path.reset((s==NULL)?0:ngdl); d=mark=0; idle=false; if((s==NULL)||(s->status(*this)==SS_FAILED)){ deletes; cur=NULL; }else{ cur=s; } Search::Worker::reset(); } /* *Engine:initialization */ forceinline BAB::Worker::Worker(Space*s,BAB&e) :Engine::Worker(s,e),mark(0),best(NULL){} forceinline BAB::BAB(Space*s,constOptions&o) :Engine(o),best(NULL){ //Createworkers _worker=static_cast<Worker**> (heap.ralloc(workers()*sizeof(Worker*))); //Thefirstworkergetstheentiresearchtree _worker[0]=newWorker(s,*this); //Allotherworkersstartwithnowork for(unsignedinti=1;i<workers();i++) _worker[i]=newWorker(NULL,*this); //Blockallworkers block(); //Createandstartthreads for(unsignedinti=0;i<workers();i++) Support::Thread::run(_worker[i]); } /* *Engine:searchcontrol */ forceinlinevoid BAB::Worker::better(Space*b){ m.acquire(); deletebest; best=b->clone(false); mark=path.entries(); if(cur!=NULL) cur->constrain(*best); m.release(); } forceinlinevoid BAB::solution(Space*s){ m_search.acquire(); if(best!=NULL){ s->constrain(*best); if(s->status()==SS_FAILED){ deletes; m_search.release(); return; }else{ deletebest; best=s->clone(); } }else{ best=s->clone(); } //Announcebettersolutions for(unsignedinti=0;i<workers();i++) worker(i)->better(best); boolbs=signal(); solutions.push(s); if(bs) e_search.signal(); m_search.release(); } /* *Worker:findingandstealingworking */ forceinlinevoid BAB::Worker::find(void){ //Trytofindnewwork(evenifthereisnone) for(unsignedinti=0;i<engine().workers();i++){ unsignedlongintr_d=0ul; if(Space*s=engine().worker(i)->steal(r_d)){ //Resetthisguy m.acquire(); idle=false; //Notidlebutalsodoesnothavetherootofthetree path.ngdl(0); d=0; cur=s; mark=0; if(best!=NULL) cur->constrain(*best); Search::Worker::reset(r_d); m.release(); return; } } } }}} #endif //STATISTICS:search-parallel