00001
00002
00003
00004
00005
00006
00007 #ifndef __SEARCHENGINE__
00008 #define __SEARCHENGINE__
00009
00010 #include "charm++.h"
00011 #include "cmipool.h"
00012
00013 #define ADAPTIVE 1
00014
00015 class Solver;
00016 class StateBase;
00017 class SearchNodeMsg;
00018 class CProxy_SearchConductor;
00019
00020 extern CProxy_SearchConductor searchEngineProxy;
00021
00022 typedef void (*SE_createInitialChildrenFn)(Solver *solver);
00023 typedef void (*SE_createChildrenFn)( StateBase *_base , Solver* solver, bool parallel);
00024 typedef int (*SE_parallelLevelFn)();
00025 typedef int (*SE_searchDepthLimitFn)();
00026 typedef double (*SE_lowerBoundFn)(StateBase *_base);
00027
00028 extern SE_lowerBoundFn _lowerBoundFn;
00029
00030 void SE_register(SE_createInitialChildrenFn f1,
00031 SE_createChildrenFn f2,
00032 SE_parallelLevelFn f3,
00033 SE_searchDepthLimitFn f4,
00034 SE_lowerBoundFn f5 = NULL
00035 );
00036
00037 class StateBase
00038 {
00039 };
00040
00041 class Solver {
00042 protected:
00043 UShort parentBits;
00044 unsigned int* parentPtr;
00045 int searchLevel;
00046 public:
00047
00048 virtual StateBase *registerRootState(size_t size, unsigned int childnum, unsigned int totalNumChildren)=0;
00049 virtual StateBase *registerState(size_t size, unsigned int childnum, unsigned int totalNumChildren) = 0;
00050 virtual void process(StateBase *state)=0;
00051 virtual void deleteState(StateBase *state) = 0;
00052 virtual void setParentInfo(SearchNodeMsg *msg, int l) = 0;
00053 virtual inline void reportSolution();
00054 virtual void setPriority(StateBase *state, int p)=0;
00055 #ifdef BRANCHBOUND
00056 inline void updateCost(double c);
00057 #endif
00058 };
00059
00060 #include "searchEngine_impl.h"
00061
00062 #define MASK 0XFF
00063 #define ENTRYGRAIN 0.002 //2ms
00064 extern void registerSE();
00065 extern int se_statesize;
00066
00067
00068 #ifdef BRANCHBOUND
00069 #if ! ADAPTIVE
00070 #define SE_Register(state, f1, f2, f3, f4, f5) \
00071 void registerSE() { \
00072 SE_register(f1, f2, f3, f4, f5); \
00073 } \
00074 void createMultipleChildren(SearchGroup* myGroup, StateBase *parent, SequentialSolver* solver, bool parallel) { \
00075 StateBase *state; \
00076 double minCost = myGroup->getCost(); \
00077 double lb = f5(parent); \
00078 if(lb<minCost) \
00079 f2(parent, solver, false); \
00080 while((state=solver->dequeue()) != NULL) \
00081 { \
00082 minCost = myGroup->getCost(); \
00083 lb = f5(parent); \
00084 if(lb>=minCost) \
00085 continue; \
00086 f2(state, solver, parallel); \
00087 } \
00088 }
00089 #else
00090 #define SE_Register(state, f1, f2, f3, f4, f5) \
00091 void registerSE() { \
00092 SE_register(f1, f2, f3, f4, f5); \
00093 } \
00094 void createMultipleChildren(SearchGroup* myGroup, StateBase *parent, SequentialSolver* solver, bool parallel) { \
00095 StateBase *_state; \
00096 double avgentrytime = 0; \
00097 int processed_nodes = 1; \
00098 double instrument_start; \
00099 double accumulate_time = 0; \
00100 double minCost = myGroup->getCost(); \
00101 double lb = f5(parent); \
00102 instrument_start = CkWallTimer(); \
00103 if(lb<minCost) \
00104 f2(parent, solver, false); \
00105 accumulate_time = avgentrytime = CkWallTimer() - instrument_start; \
00106 while((_state=solver->dequeue()) != NULL) \
00107 { \
00108 minCost = myGroup->getCost(); \
00109 lb = f5(parent); \
00110 if(lb>=minCost) \
00111 continue; \
00112 if(processed_nodes == 20) \
00113 { \
00114 avgentrytime = (CkWallTimer() - instrument_start)/20; \
00115 } \
00116 f2(_state, solver, parallel); \
00117 accumulate_time += avgentrytime; \
00118 if(accumulate_time > ENTRYGRAIN) \
00119 { solver-> dequeue_multiple(avgentrytime, processed_nodes);} \
00120 processed_nodes++; \
00121 } \
00122 }
00123 #endif
00124
00125 #else
00126 #define registerSE_DEF(state, f1, f2, f3, f4) \
00127 void registerSE() { \
00128 SE_register(f1, f2, f3, f4); \
00129 }
00130 #if ! ADAPTIVE
00131 #define SE_Register(state, f1, f2, f3, f4) \
00132 registerSE_DEF(state, f1, f2, f3, f4) \
00133 void createMultipleChildren(StateBase *parent, SequentialSolver* solver, bool parallel) { \
00134 f2(parent, solver, false); \
00135 StateBase *state; \
00136 while((state=solver->dequeue()) != NULL) \
00137 { \
00138 f2(state, solver, parallel); \
00139 } \
00140 }
00141 #else
00142 #define SE_Register(state, f1, f2, f3, f4) \
00143 registerSE_DEF(state, f1, f2, f3, f4) \
00144 void createMultipleChildren(StateBase *parent, SequentialSolver* solver, bool parallel) { \
00145 StateBase *state; \
00146 double avgentrytime = 0; \
00147 int processed_nodes = 1; \
00148 double instrument_start; \
00149 double accumulate_time = 0; \
00150 f2(parent, solver, false); \
00151 instrument_start = CkWallTimer();\
00152 accumulate_time = avgentrytime = CkWallTimer() - instrument_start; \
00153 while((state=solver->dequeue()) != NULL) \
00154 { \
00155 if(processed_nodes == 20) \
00156 { \
00157 avgentrytime = (CkWallTimer() - instrument_start)/20; \
00158 } \
00159 f2(state, solver, parallel); \
00160 accumulate_time += avgentrytime; \
00161 if(accumulate_time > ENTRYGRAIN) \
00162 { solver-> dequeue_multiple(avgentrytime, processed_nodes);} \
00163 processed_nodes++; \
00164 } \
00165 }
00166 #endif
00167
00168 #endif
00169
00170 #endif