conv-com/convcomlibstrategy.h

Go to the documentation of this file.
00001 #ifndef CONVCOMMLIBSTRATEGY_H
00002 #define CONVCOMMLIBSTRATEGY_H
00003 
00004 #include "converse.h"
00005 #include "pup.h"
00006 #include "cklists.h"
00007 
00008 //An abstract data structure that holds a converse message and which
00009 //can be buffered by the communication library Message holder is a
00010 //wrapper around a message. Has other useful data like destination
00011 //processor list for a multicast etc.
00012 
00013 class MessageHolder : public PUP::able {
00014  public:
00015     int dest_proc;
00016     char *data;
00017     int size;
00018     MessageHolder *next; // also used for the refield at the receiver
00019     int isDummy;
00020     
00021     //For multicast, the user can pass the pelist and list of Pes he
00022     //wants to send the data to.
00023     int npes;
00024     int *pelist;
00025     
00026     MessageHolder() 
00027         {dest_proc = size = isDummy = 0; data = NULL;}    
00028 
00029     MessageHolder(CkMigrateMessage *m) {}
00030 
00031     inline MessageHolder(char * msg, int proc, int sz) {
00032         data = msg;
00033         dest_proc = proc;
00034         size = sz;
00035         
00036         isDummy = 0;
00037         
00038         npes = 0;
00039         pelist = 0;
00040 
00041     }
00042 
00043     inline ~MessageHolder() {
00044         /*
00045           if(pelist != NULL && npes > 0)
00046           delete[] pelist;
00047         */
00048     }
00049 
00050     inline char * getMessage() {
00051         return data;
00052     }
00053 
00054     inline int getSize() {
00055       return size;
00056     }
00057 
00058     inline void * operator new(size_t size) {
00059         return CmiAlloc(size);
00060     }
00061 
00062     inline void operator delete (void *buf) {
00063         CmiFree(buf);
00064     }
00065 
00066     virtual void pup(PUP::er &p);
00067     PUPable_decl(MessageHolder);
00068 };
00069 
00070 #define CONVERSE_STRATEGY 0     //The strategy works for converse programs
00071 #define NODEGROUP_STRATEGY 1    //Node group level optimizations 
00072 #define GROUP_STRATEGY 2        //Charm Processor level optimizations
00073 #define ARRAY_STRATEGY 3        //Array level optimizations
00074 
00075 //Class that defines the entry methods that a Converse level strategy
00076 //must define. To write a new strategy inherit from this class and
00077 //define the virtual methods.  Every strategy can also define its own
00078 //constructor and have any number of arguments. Also call the parent
00079 //class methods in the virtual methods.
00080 
00081 class Strategy : public PUP::able{
00082  protected:
00083     int type;
00084     int isStrategyBracketed;
00085     int myInstanceID;
00086     int destinationHandler;
00087 
00088     //Charm strategies for modularity may have converse strategies in
00089     //them.  For the code to work in both Charm and converse, this
00090     //variable can be used.    
00091     Strategy *converseStrategy;
00092     Strategy *higherLevel;
00093 
00094  public:
00095     Strategy();
00096     Strategy(CkMigrateMessage *m) : PUP::able(m) {
00097         converseStrategy = this;
00098         higherLevel = this;
00099     }
00100 
00101     inline void setBracketed(){isStrategyBracketed = 1;}
00102     inline int isBracketed(){return isStrategyBracketed;}
00103 
00104     //Called for each message
00105     virtual void insertMessage(MessageHolder *msg) {}
00106     
00107     //Called after all chares and groups have finished depositing their 
00108     //messages on that processor.
00109     virtual void doneInserting() {}
00110 
00111     inline void setInstance(int instid){myInstanceID = instid;}
00112     inline int getInstance(){return myInstanceID;}
00113     inline int getType() {return type;}
00114     inline void setType(int t) {type = t;}
00115 
00116     inline void setDestination(int handler) {destinationHandler = handler;}
00117     inline int getDestination() {return destinationHandler;}
00118 
00119     inline void setConverseStrategy(Strategy *s){
00120         converseStrategy = s;
00121     }
00122 
00123     inline Strategy * getConverseStrategy() {
00124         return converseStrategy;
00125     }
00126 
00127     inline void setHigherLevel(Strategy *s) {
00128         higherLevel = s;
00129     }
00130 
00131     Strategy * getHigherLevel() {
00132       return higherLevel;
00133     }
00134 
00135     //Called when a message is received in the strategy handler
00136     virtual void handleMessage(void *msg) {}
00137     
00138     //This method can be used to deliver a message through the correct class
00139     //when converse does not know if the message was originally sent from
00140     //converse itself of from a higher level language like charm
00141     virtual void deliverer(char*, int) {
00142         CmiAbort("Strategy::deliverer: If used, should be first redefined\n");
00143     };
00144 
00145     //Each strategy must define his own Pup interface.
00146     virtual void pup(PUP::er &p);
00147     PUPable_decl(Strategy);
00148 };
00149 
00150 //Enables a list of strategies to be stored in a message through the
00151 //pupable framework
00152 class StrategyWrapper  {
00153  public:
00154     Strategy **s_table;
00155     int nstrats;
00156     int total_nstrats;
00157 
00158     void pup(PUP::er &p);
00159 };
00160 PUPmarshall(StrategyWrapper);
00161 
00162 //Table of strategies. Each entry in the table points to a strategy.
00163 //Strategies can change during the execution of the program but the
00164 //StrategyTableEntry stores some persistent information for the
00165 //strategy. The communication library on receiving a message, calls
00166 //the strategy in this table given by the strategy id in the message.
00167 
00168 struct StrategyTableEntry {
00169     Strategy *strategy;
00170     //A buffer for all strategy messages
00171     CkQ<MessageHolder*> tmplist;
00172     
00173     int numElements;   //used by the array listener, 
00174                        //could also be used for other objects
00175     int elementCount;  //Count of how many elements have deposited
00176                        //their data
00177 
00178     //Used during a fence barrier at the begining or during the
00179     //learning phases. Learning is only available for Charm++
00180     //programs.
00181     int nEndItr;       //#elements that called end iteration
00182     int call_doneInserting; //All elements deposited their data
00183     int flag;          //status of the strategy, 
00184                        //whether it is new, learned,or returned unchanged 
00185     StrategyTableEntry();
00186 
00187     void reset();
00188 };
00189 
00190 typedef CkVec<StrategyTableEntry> StrategyTable;
00191 
00192 /* Global variables needed by some strategy */
00193 CsvExtern(int, pipeBcastPropagateHandle);
00194 CsvExtern(int, pipeBcastPropagateHandle_frag);
00195 
00196 #endif

Generated on Sun Jun 29 13:29:11 2008 for Charm++ by  doxygen 1.5.1