Array section helper classes
authorSameer Kumar <skumar2@uiuc.edu>
Sun, 6 Jun 2004 21:58:16 +0000 (21:58 +0000)
committerSameer Kumar <skumar2@uiuc.edu>
Sun, 6 Jun 2004 21:58:16 +0000 (21:58 +0000)
src/ck-com/ComlibSectionInfo.C [new file with mode: 0644]
src/ck-com/ComlibSectionInfo.h [new file with mode: 0644]

diff --git a/src/ck-com/ComlibSectionInfo.C b/src/ck-com/ComlibSectionInfo.C
new file mode 100644 (file)
index 0000000..cf8191d
--- /dev/null
@@ -0,0 +1,113 @@
+
+#include "ComlibManager.h"
+#include "ComlibSectionInfo.h"
+
+ComlibMulticastMsg * ComlibSectionInfo::getNewMulticastMessage
+(CharmMessageHolder *cmsg){
+    
+    if(cmsg->sec_id == NULL || cmsg->sec_id->_nElems == 0)
+        return NULL;
+
+    void *m = cmsg->getCharmMessage();
+    envelope *env = UsrToEnv(m);
+    
+    if(cmsg->sec_id->_cookie.sInfo.cInfo.id != 0) 
+        CmiAbort("In correct section\n");
+
+    CkPackMessage(&env);
+    int sizes[2];
+    sizes[0] = cmsg->sec_id->_nElems;
+    sizes[1] = env->getTotalsize();                
+    
+    cmsg->sec_id->_cookie.sInfo.cInfo.id = MaxSectionID ++;
+    
+    ComlibPrintf("Creating new comlib multicast message %d, %d\n", sizes[0], sizes[1]);
+    
+    ComlibMulticastMsg *msg = new(sizes, 0) ComlibMulticastMsg;
+    msg->nIndices = cmsg->sec_id->_nElems;
+    msg->_cookie.sInfo.cInfo.instId = instanceID;
+    msg->_cookie.sInfo.cInfo.id = MaxSectionID - 1;
+    msg->_cookie.sInfo.cInfo.status = COMLIB_MULTICAST_NEW_SECTION;
+    msg->_cookie.type = COMLIB_MULTICAST_MESSAGE;
+    msg->_cookie.pe = CkMyPe();
+    
+    memcpy(msg->indices, cmsg->sec_id->_elems, 
+           sizes[0] * sizeof(CkArrayIndexMax));
+    memcpy(msg->usrMsg, env, sizes[1] * sizeof(char));         
+    envelope *newenv = UsrToEnv(msg);
+    
+    newenv->getsetArrayMgr() = env->getsetArrayMgr();
+    newenv->getsetArraySrcPe() = env->getsetArraySrcPe();
+    newenv->getsetArrayEp() = env->getsetArrayEp();
+    newenv->getsetArrayHops() = env->getsetArrayHops();
+    newenv->getsetArrayIndex() = env->getsetArrayIndex();
+
+    // for trace projections
+    newenv->setEvent(env->getEvent());
+    newenv->setSrcPe(env->getSrcPe());
+    
+    CkPackMessage(&newenv);        
+    return (ComlibMulticastMsg *)EnvToUsr(newenv);
+
+    return NULL;
+}
+
+void ComlibSectionInfo::unpack(envelope *cb_env, 
+                               CkVec<CkArrayIndexMax> *&dest_indices, 
+                               envelope *&env) {
+    
+    dest_indices = new CkVec<CkArrayIndexMax>;
+    
+    ComlibMulticastMsg *ccmsg = (ComlibMulticastMsg *)EnvToUsr(cb_env);
+    for(int count = 0; count < ccmsg->nIndices; count++){
+        CkArrayIndexMax idx = ccmsg->indices[count];
+        int dest_proc = CkArrayID::CkLocalBranch(destArrayID)->lastKnown(idx);
+        
+        if(dest_proc == CkMyPe())
+            dest_indices->insertAtEnd(idx);                        
+    }            
+    
+    envelope *usrenv = (envelope *) ccmsg->usrMsg;
+    env = (envelope *)CmiAlloc(usrenv->getTotalsize());
+    memcpy(env, ccmsg->usrMsg, usrenv->getTotalsize());
+}
+
+
+void ComlibSectionInfo::processOldSectionMessage(CharmMessageHolder *cmsg) {
+    int cur_sec_id = ComlibSectionInfo::getSectionID(*cmsg->sec_id);
+
+    //Old section id, send the id with the message
+    CkMcastBaseMsg *cbmsg = (CkMcastBaseMsg *)cmsg->getCharmMessage();
+    cbmsg->_cookie.sInfo.cInfo.id = cur_sec_id;
+    cbmsg->_cookie.sInfo.cInfo.status = COMLIB_MULTICAST_OLD_SECTION;
+}
+
+void ComlibSectionInfo::initSectionID(CkSectionID *sid){
+    
+    if(sid->npes > 0) 
+        return;
+
+    sid->pelist = new int[CkNumPes()];
+    sid->npes = 0;
+    
+    int count = 0, acount = 0;
+
+    for(acount = 0; acount < sid->_nElems; acount++){
+        int p = CkArrayID::CkLocalBranch(destArrayID)->
+            lastKnown(sid->_elems[acount]);
+        
+        if(p == -1) CkAbort("Invalid Section\n");        
+        for(count = 0; count < sid->npes; count ++)
+            if(sid->pelist[count] == p)
+                break;
+        
+        if(count == sid->npes) {
+            sid->pelist[sid->npes ++] = p;
+        }
+    }   
+}
+
+void ComlibSectionInfo::localMulticast(envelope *env){
+    ComlibArrayInfo::localMulticast(&localDestIndexVec, env);
+}
+
diff --git a/src/ck-com/ComlibSectionInfo.h b/src/ck-com/ComlibSectionInfo.h
new file mode 100644 (file)
index 0000000..52d8843
--- /dev/null
@@ -0,0 +1,123 @@
+#ifndef COMLIB_SECTION_INFO
+#define COMLIB_SECTION_INFO
+
+/***********
+  Helper classes that help strategies manage array sections 
+***********/
+
+/* Hash key that lets a strategy access a section id data structure
+   given the source processor and the MaxSectionId on that processor
+*/
+class ComlibSectionHashKey{
+ public:
+    int srcPe;
+    int id;
+    ComlibSectionHashKey(int _pe, int _id):srcPe(_pe), id(_id){};
+
+    //These routines allow ComlibSectionHashKey to be used in
+    //  a CkHashtableT
+    CkHashCode hash(void) const;
+    static CkHashCode staticHash(const void *a,size_t);
+    int compare(const ComlibSectionHashKey &ind) const;
+    static int staticCompare(const void *a,const void *b,size_t);
+};
+
+/*********** CkHashTable functions ******************/
+inline CkHashCode ComlibSectionHashKey::hash(void) const
+{
+    register int _id = id;
+    register int _pe = srcPe;
+    
+    register CkHashCode ret = (_id << 16) + _pe;
+    return ret;
+}
+
+inline int ComlibSectionHashKey::compare(const ComlibSectionHashKey &k2) const
+{
+    if(id == k2.id && srcPe == k2.srcPe)
+        return 1;
+    
+    return 0;
+}
+
+/*For calls to qsort*/
+inline int intCompare(void *a, void *b){
+    int a1 = *(int *) a;
+    int b1 = *(int *) b;
+
+    if(a1 < b1)
+        return -1;
+    
+    if(a1 == b1)
+        return 0;
+
+    if(a1 > b1)
+        return 1;
+
+    return 0;
+}    
+
+//ComlibSectionHashKey CODE
+inline int ComlibSectionHashKey::staticCompare(const void *k1, const void *k2, 
+                                                size_t ){
+    return ((const ComlibSectionHashKey *)k1)->
+                compare(*(const ComlibSectionHashKey *)k2);
+}
+
+inline CkHashCode ComlibSectionHashKey::staticHash(const void *v,size_t){
+    return ((const ComlibSectionHashKey *)v)->hash();
+}
+
+/*************** End CkHashtable Functions *****************/
+
+
+/*** Class that helps a communication library strategy manage array
+     sections
+***************/
+
+class ComlibSectionInfo {
+    /* Array ID of the array section */
+    CkArrayID destArrayID;
+    
+    //Unique section id for this section
+    //Will be used to access a hashtable on remote processors
+    int MaxSectionID;
+
+    //Instance ID of the strategy
+    int instanceID;
+
+    CkVec<CkArrayIndexMax> localDestIndexVec;
+
+ public:
+
+    ComlibSectionInfo() { 
+        destArrayID.setZero(); MaxSectionID = 1; instanceID = 0;    
+    }
+
+    ComlibSectionInfo(CkArrayID dest, int instance){
+        destArrayID = dest;
+        instanceID = instance;
+
+        MaxSectionID = 1;
+    }
+    
+    void initSectionID(CkSectionID *sid);
+    
+    void processOldSectionMessage(CharmMessageHolder *cmsg);
+
+    ComlibMulticastMsg *getNewMulticastMessage(CharmMessageHolder *cmsg);
+
+    void unpack(envelope *cb_env, CkVec<CkArrayIndexMax> *&destIndices, 
+                envelope *&env);
+
+    void localMulticast(envelope *env);
+
+    static inline int getSectionID(CkSectionID id) {
+        return id._cookie.sInfo.cInfo.id;
+    }
+    
+};
+
+PUPbytes(ComlibSectionInfo);
+
+#endif