ccba6dedca2189e01f4611e6065d8726fbb70784
[charm.git] / examples / charm++ / state_space_searchengine / BalancedTree / searchEngineAPI.C
1 #ifndef __SEARCHENGINEAPI__
2 #define __SEARCHENGINEAPI__
3
4 #include "searchEngine.h"
5
6 /*   framework for search engine */
7
8
9 extern int branchfactor;
10 extern int depth;
11 extern int initial_grainsize;
12 extern int target;
13
14 class BTreeStateBase : public StateBase
15 {
16 public:
17    int depth;
18    long long index;
19 };
20
21 void createInitialChildren(Solver *solver)
22 {
23     BTreeStateBase *root = (BTreeStateBase*)solver->registerRootState(sizeof(BTreeStateBase), 0, 1);
24     root->index = 0;
25     root->depth = 0;
26     solver->process(root);
27 }
28
29 inline void createChildren( StateBase *_base , Solver* solver, bool parallel)
30 {
31     BTreeStateBase base = *((BTreeStateBase*)_base);
32     long long t = 1;
33
34     for(int i=0; i<target; i++)
35          t = t << 1;
36     for(int childIndex=0; childIndex<branchfactor; childIndex++)
37     {
38         long long thisindex = base.index * branchfactor + childIndex;
39         //CkPrintf(" t = %lld, based index=%lld _basedindex=%lld  thisindex=%lld, depth=%d\n", t, base.index, ((BTreeStateBase*)_base)->index, thisindex, base.depth);
40         if(base.depth == depth-1)
41         {
42             if( t-1 == thisindex)
43                 solver->reportSolution();
44         }
45         else{
46             BTreeStateBase *child  = (BTreeStateBase*)solver->registerState(sizeof(BTreeStateBase), childIndex, branchfactor);
47             child->depth = base.depth + 1;
48             child->index = base.index * branchfactor + childIndex; 
49             if(parallel) {
50                 solver->process(child);
51             }
52         }
53     }
54 }
55
56 int parallelLevel()
57 {
58     return initial_grainsize;
59 }
60
61 int searchDepthLimit()
62 {
63     return 1;
64 }
65
66 SE_Register(BTreeStateBase, createInitialChildren, createChildren, parallelLevel, searchDepthLimit);
67
68 #endif