fix for migration
[charm.git] / src / ck-core / ckmemcheckpoint.C
index d4798bec50ae555f4ddd3032fe7683a95ff9f840..c4dc028c8dd537c689026fdc02c8c05a34bcbdc7 100644 (file)
@@ -74,6 +74,7 @@ void noopck(const char*, ...)
 #define STREAMING_INFORMHOME                    1
 CpvDeclare(int, _crashedNode);
 CpvDeclare(int, use_checksum);
+CpvDeclare(int, resilience);
 CpvDeclare(int, _remoteCrashedNode);
 
 // static, so that it is accessible from Converse part
@@ -125,12 +126,18 @@ CpvDeclare(CkCheckPTMessage**, localProcChkpBuf);
 //store the checkpoint of the buddy to compare
 //do not need the whole msg, can be the checksum
 CpvDeclare(CkCheckPTMessage*, buddyBuf);
+CpvDeclare(CkCheckPTMessage*, recoverProcBuf);
+CpvDeclare(CkCheckPTMessage*, recoverArrayBuf);
 //pointer of the checkpoint going to be written
 CpvDeclare(int, curPointer);
 CpvDeclare(int, recvdRemote);
 CpvDeclare(int, recvdLocal);
 CpvDeclare(int, localChkpDone);
 CpvDeclare(int, remoteChkpDone);
+CpvDeclare(int, remoteStarted);
+CpvDeclare(int, localStarted);
+CpvDeclare(int, remoteReady);
+CpvDeclare(int, localReady);
 CpvDeclare(int, recvdArrayChkp);
 CpvDeclare(int, recvdProcChkp);
 CpvDeclare(int, localChecksum);
@@ -143,11 +150,13 @@ static inline void _handleProcData(PUP::er &p,CmiBool create= CmiTrue);
 static int askPhaseHandlerIdx;
 static int recvPhaseHandlerIdx;
 static int askProcDataHandlerIdx;
+static int askRecoverDataHandlerIdx;
 static int restartBcastHandlerIdx;
 static int recoverProcDataHandlerIdx;
 static int restartBeginHandlerIdx;
 static int recvRemoteChkpHandlerIdx;
 static int replicaDieHandlerIdx;
+static int replicaChkpStartHandlerIdx;
 static int replicaDieBcastHandlerIdx;
 static int replicaRecoverHandlerIdx;
 static int replicaChkpDoneHandlerIdx;
@@ -427,8 +436,13 @@ void CkMemCheckPT::pup(PUP::er& p)
 void CkMemCheckPT::getIter(){
   localDecided = true;
   localMaxIter = maxIter+1;
+  if(CkMyPe()==0){
+    CkPrintf("local max iter is %d\n",localMaxIter);
+  }
   contribute(sizeof(int),&localMaxIter,CkReduction::max_int,CkCallback(CkReductionTarget(CkMemCheckPT,recvMaxIter),thisProxy));
   int elemCount = CkCountChkpSyncElements();
+  if(CkMyPe()==0)
+    startTime = CmiWallTimer();
   if(elemCount == 0){
     contribute(CkCallback(CkReductionTarget(CkMemCheckPT,startChkp),thisProxy[0]));
   }
@@ -444,12 +458,15 @@ void CkMemCheckPT::recvIter(int iter){
 
 void CkMemCheckPT::recvMaxIter(int iter){
   localDecided = false;
+  if(CkMyPe()==0)
+    CkPrintf("checkpoint iteration is %d\n",iter);
   CKLOCMGR_LOOP(mgr->recvChkpIter(iter););
 }
 
 void CkMemCheckPT::reachChkpIter(){
   recvIterCount++;
   elemCount = CkCountChkpSyncElements();
+  //CkPrintf("[%d] received %d local %d\n",CkMyPe(),recvIterCount, elemCount);
   if(recvIterCount == elemCount){
     recvIterCount = 0;
     contribute(CkCallback(CkReductionTarget(CkMemCheckPT,startChkp),thisProxy[0]));
@@ -457,7 +474,7 @@ void CkMemCheckPT::reachChkpIter(){
 }
 
 void CkMemCheckPT::startChkp(){
-  CkPrintf("start checkpoint\n");
+  CkPrintf("start checkpoint at %lf in %lf\n",CmiWallTimer(),CmiWallTimer()-startTime);
   CkStartMemCheckpoint(cpCallback);
 }
 
@@ -565,10 +582,11 @@ void CkMemCheckPT::recoverEntry(CkArrayCheckPTMessage *msg)
 
 // loop through my checkpoint table and ask checkpointed array elements
 // to send me checkpoint data.
-void CkMemCheckPT::doItNow(int starter, CkCallback &cb)
+//void CkMemCheckPT::doItNow(int starter, CkCallback &cb)
+void CkMemCheckPT::doItNow(int starter)
 {
   checkpointed = 1;
-  cpCallback = cb;
+  //cpCallback = cb;
   cpStarter = starter;
   inCheckpointing = 1;
   if (CkMyPe() == cpStarter) {
@@ -643,7 +661,7 @@ void pupAllElements(PUP::er &p){
   p | numElements;
   if(!p.isUnpacking()){
     MemElementPacker packer(p);
-    CKLOCMGR_LOOP(mgr->iterate(packer););
+    CKLOCMGR_LOOP(mgr->iterateLocal(packer););
     packer.writeCheckpoint();
   }
 #endif
@@ -729,16 +747,26 @@ void CkMemCheckPT::startCheckpoint(){
       CpvAccess(localChecksum) = checksum;
       char *chkpMsg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes+sizeof(int));
       *(int *)(chkpMsg+CmiMsgHeaderSizeBytes) = CpvAccess(localChecksum);
-      CmiSetHandler(chkpMsg,recvRemoteChkpHandlerIdx);
-      CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,CmiMsgHeaderSizeBytes+sizeof(int),chkpMsg);
+      //only one reaplica will send
+      if(CmiMyPartition()==0){
+        CmiSetHandler(chkpMsg,recvRemoteChkpHandlerIdx);
+        CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,CmiMsgHeaderSizeBytes+sizeof(int),chkpMsg);
+      }
     }else{
       envelope * env = (envelope *)(UsrToEnv((CkCheckPTMessage *)CkCopyMsg((void **)&msg)));
       CkPackMessage(&env);
-      CmiSetHandler(env,recvRemoteChkpHandlerIdx);
-      CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
+      if(CmiMyPartition()==0){
+        CmiSetHandler(env,recvRemoteChkpHandlerIdx);
+        CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
+      }
+    }
+    if(CmiMyPartition()==0){
+      notifyReplica = 1;
+      thisProxy[CkMyPe()].doneComparison(true);
     }
   }
   if(CpvAccess(recvdRemote)==1){
+    //only partition 1 will do it
     //compare the checkpoint 
     int size = CpvAccess(chkpBuf)[pointer]->len;
     if(CpvAccess(use_checksum)){
@@ -762,29 +790,41 @@ void CkMemCheckPT::startCheckpoint(){
   }
   else{
     if(CkReplicaAlive()==0){//TODO add flag if sent already but the replica hasn't recovered when the next checkpoint
-      if(CkMyPe() == CpvAccess(_remoteCrashedNode))
-      {        
-        int pointer = CpvAccess(curPointer);
-        //send the proc data
-        CkCheckPTMessage * procMsg = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(localProcChkpBuf)[pointer]);
-        procMsg->pointer = pointer;
-        envelope * env = (envelope *)(UsrToEnv(procMsg));
+      //control when the message can be sent: after the crashed replica change the phase number, otherwise buffer it
+      if(CpvAccess(remoteReady)==1){
+        if(CkMyPe() == CpvAccess(_remoteCrashedNode))
+        {      
+          int pointer = CpvAccess(curPointer);
+          //send the proc data
+          CkCheckPTMessage * procMsg = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(localProcChkpBuf)[pointer]);
+          procMsg->pointer = pointer;
+          envelope * env = (envelope *)(UsrToEnv(procMsg));
+          CkPackMessage(&env);
+          CmiSetHandler(env,recoverRemoteProcDataHandlerIdx);
+          CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
+          if(CkMyPe() == CpvAccess(_remoteCrashedNode)){
+            CkPrintf("[%d] sendProcdata at %lf\n",CkMyPe(),CmiWallTimer());
+          }
+        }
+        //send the array checkpoint data
+        CkCheckPTMessage * msg = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(chkpBuf)[pointer]);
+        msg->pointer = CpvAccess(curPointer);
+        envelope * env = (envelope *)(UsrToEnv(msg));
         CkPackMessage(&env);
-        CmiSetHandler(env,recoverRemoteProcDataHandlerIdx);
+        CmiSetHandler(env,recoverRemoteArrayDataHandlerIdx);
         CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
+        if(CkMyPe() == CpvAccess(_remoteCrashedNode))
+          CkPrintf("[%d] sendArraydata at %lf\n",CkMyPe(),CmiWallTimer());
+      }else{
         if(CkMyPe() == CpvAccess(_remoteCrashedNode)){
-          CkPrintf("[%d] sendProcdata\n",CkMyPe());
+          int pointer = CpvAccess(curPointer);
+          CpvAccess(recoverProcBuf) = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(localProcChkpBuf)[pointer]);
+          (CpvAccess(recoverProcBuf))->pointer = pointer;
         }
+        CpvAccess(recoverArrayBuf) = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(chkpBuf)[pointer]);
+        (CpvAccess(recoverArrayBuf))->pointer = CpvAccess(curPointer);
+        CpvAccess(localReady) = 1;
       }
-      //send the array checkpoint data
-      CkCheckPTMessage * msg = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(chkpBuf)[pointer]);
-      msg->pointer = CpvAccess(curPointer);
-      envelope * env = (envelope *)(UsrToEnv(msg));
-      CkPackMessage(&env);
-      CmiSetHandler(env,recoverRemoteArrayDataHandlerIdx);
-      CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
-      if(CkMyPe() == CpvAccess(_remoteCrashedNode))
-        CkPrintf("[%d] sendArraydata\n",CkMyPe());
       //can continue work, no need to wait for my replica
       int _ret = 1;
       notifyReplica = 1;
@@ -816,26 +856,31 @@ void CkMemCheckPT::doneRComparison(int ret){
     }else{
       CmiPrintf("[%d][%d] Local checkpoint finished in %f seconds at %lf, waiting for replica ... \n", CmiMyPartition(),CkMyPe(), CmiWallTimer()-startTime,CmiWallTimer());
     }
-    if(notifyReplica == 0){
-      //notify the replica am done
-      char *msg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes);
-      CmiSetHandler(msg,replicaChkpDoneHandlerIdx);
-      CmiRemoteSyncSendAndFree(0,CmiMyPartition()^1,CmiMsgHeaderSizeBytes,(char *)msg);
-      notifyReplica = 1;
-    }
- // }
- /* else{
+  //}
+  /*else{
     CkPrintf("[%d][%d] going to RollBack %d at %lf checkpoint in %lf\n", CmiMyPartition(),CkMyPe(),ret,CmiWallTimer(), CmiWallTimer()-startTime);
     startTime = CmiWallTimer();
     thisProxy.RollBack();
   }*/
+  if(notifyReplica == 0){
+    //notify the replica am done
+    char *msg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes+sizeof(int));
+    *(int *)(msg+CmiMsgHeaderSizeBytes) = ret;
+    CmiSetHandler(msg,replicaChkpDoneHandlerIdx);
+    CmiRemoteSyncSendAndFree(0,CmiMyPartition()^1,CmiMsgHeaderSizeBytes+sizeof(int),(char *)msg);
+    notifyReplica = 1;
+  }
 }
 
 void CkMemCheckPT::doneBothComparison(){
   CpvAccess(recvdRemote) = 0;
+  CpvAccess(remoteReady) = 0;
+  CpvAccess(localReady) = 0;
   CpvAccess(recvdLocal) = 0;
   CpvAccess(localChkpDone) = 0;
   CpvAccess(remoteChkpDone) = 0;
+  CpvAccess(remoteStarted) = 0;
+  CpvAccess(localStarted) = 0;
   CpvAccess(_remoteCrashedNode) = -1;
   CkMemCheckPT::replicaAlive = 1;
   int size = CpvAccess(chkpBuf)[CpvAccess(curPointer)]->len;
@@ -851,7 +896,7 @@ void CkMemCheckPT::doneBothComparison(){
 void CkMemCheckPT::RollBack(){
   //restore group data
   checkpointed = 0;
-  CkMemCheckPT::inRestarting = 1;
+  inRestarting = 1;
   int pointer = CpvAccess(curPointer)^1;//use the previous one
   CkCheckPTMessage* chkpMsg = CpvAccess(chkpBuf)[pointer];
   PUP::fromMem p(chkpMsg->packData);   
@@ -1143,22 +1188,22 @@ void CkMemCheckPT::RollBack(){
     void CkMemCheckPT::restart(int diePe)
     {
 #if CMK_MEM_CHECKPOINT
+      thisFailedPe = diePe;
       double curTime = CmiWallTimer();
-      if (CkMyPe() == diePe){
+      if (CkMyPe() == thisFailedPe){
         restartT = CmiWallTimer();
         CkPrintf("[%d] Process data restored in %f seconds\n", CkMyPe(), curTime - startTime);
       }
       stage = (char*)"resetLB";
       startTime = curTime;
-      if (CkMyPe() == diePe)
+      if (CkMyPe() == thisFailedPe)
         CkPrintf("[%d] CkMemCheckPT ----- restart.\n",CkMyPe());
 
-#if CK_NO_PROC_POOL
-      failed(diePe);   // add into the list of failed pes
-#endif
-      thisFailedPe = diePe;
+//#if CK_NO_PROC_POOL
+//      failed(diePe); // add into the list of failed pes
+//#endif
 
-      if (CkMyPe() == diePe) CmiAssert(ckTable.length() == 0);
+//      if (CkMyPe() == diePe) CmiAssert(ckTable.length() == 0);
 
       inRestarting = 1;
 
@@ -1208,7 +1253,8 @@ void CkMemCheckPT::RollBack(){
     void CkMemCheckPT::resetReductionMgr()
     {
       if (CkMyPe() == thisFailedPe) 
-        CkPrintf("[%d] CkMemCheckPT ----- resetReductionMgr\n",CkMyPe());
+        CkPrintf("[%d] CkMemCheckPT ----- resetReductionMgr at %lf\n",CkMyPe(),CmiWallTimer());
+      stage = (char *)"resetReductionMgr";
       int numGroups = CkpvAccess(_groupIDTable)->size();
       for(int i=0;i<numGroups;i++) {
         CkGroupID gID = (*CkpvAccess(_groupIDTable))[i];
@@ -1221,8 +1267,11 @@ void CkMemCheckPT::RollBack(){
       if(CmiNumPartition()==1){
         barrier(CkCallback(CkIndex_CkMemCheckPT::recoverBuddies(), thisProxy));
       }
-      else
+      else{
+       if (CkMyPe() == thisFailedPe) 
+         CkPrintf("[%d] CkMemCheckPT ----- resetReductionMgr ends at %lf\n",CkMyPe(),CmiWallTimer());
         barrier(CkCallback(CkReductionTarget(CkMemCheckPT, recoverArrayElements), thisProxy));
+      }
     }
 
     // recover the lost buddies
@@ -1311,10 +1360,8 @@ void CkMemCheckPT::RollBack(){
     {
       double curTime = CmiWallTimer();
       int len = ckTable.length();
-      //CkPrintf("[%d] CkMemCheckPT ----- %s len: %d in %f seconds \n",CkMyPe(), stage, len, curTime-startTime);
+      CkPrintf("[%d] CkMemCheckPT ----- %s len: %d in %f seconds \n",CkMyPe(), stage, len, curTime-startTime);
       stage = (char *)"recoverArrayElements";
-      if (CkMyPe() == thisFailedPe)
-        CmiPrintf("[%d] CkMemCheckPT ----- %s starts at %f \n",CkMyPe(), stage, curTime);
       startTime = curTime;
       int flag = 0;
       // recover all array elements
@@ -1384,6 +1431,8 @@ void CkMemCheckPT::RollBack(){
       delete [] gmap;
 #endif
       DEBUGF("[%d] recoverArrayElements restore %d objects\n", CkMyPe(), count);
+      CkPrintf("[%d] recoverArrayElements restore %d objects\n", CkMyPe(), count);
+      //if (CkMyPe() == thisFailedPe)
 
       CKLOCMGR_LOOP(mgr->doneInserting(););
 
@@ -1459,20 +1508,21 @@ void CkMemCheckPT::RollBack(){
       }
       CKLOCMGR_LOOP(mgr->resumeFromChkp(););
       inRestarting = 0;
-
+      maxIter = -1;
 #if CMK_CONVERSE_MPI   
       if(CmiNumPartition()!=1){
         CpvAccess(recvdProcChkp) = 0;
         CpvAccess(recvdArrayChkp) = 0;
         CpvAccess(curPointer)^=1;
         //notify my replica, restart is done
-        if (CkMyPe() == 0){
+        if (CkMyPe() == 0&&CpvAccess(resilience)!=1){
           char *msg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes);
           CmiSetHandler(msg,replicaRecoverHandlerIdx);
           CmiRemoteSyncSendAndFree(CkMyPe(),CmiMyPartition()^1,CmiMsgHeaderSizeBytes,(char *)msg);
         }
       }
       if (CmiMyPe() == BuddyPE(thisFailedPe)) {
+        lastPingTime = CmiWallTimer();
         CcdCallOnCondition(CcdPERIODIC_1s,(CcdVoidFn)pingCheckHandler,NULL);
       }
 #endif
@@ -1493,10 +1543,19 @@ void CkMemCheckPT::RollBack(){
     void CkMemCheckPT::recoverFromSoftFailure()
     {
       inRestarting = 0;
+      maxIter = -1;
       CpvAccess(recvdRemote) = 0;
       CpvAccess(recvdLocal) = 0;
       CpvAccess(localChkpDone) = 0;
       CpvAccess(remoteChkpDone) = 0;
+      CpvAccess(remoteReady) = 0;
+      CpvAccess(localReady) = 0;
+      inCheckpointing = 0;
+      notifyReplica = 0;
+      CpvAccess(remoteStarted) = 0;
+      CpvAccess(localStarted) = 0;
+      CpvAccess(_remoteCrashedNode) = -1;
+      CkMemCheckPT::replicaAlive = 1;
       inCheckpointing = 0;
       notifyReplica = 0;
       if(CkMyPe() == 0){
@@ -1536,9 +1595,19 @@ void CkMemCheckPT::RollBack(){
       // store user callback and user data
       CkMemCheckPT::cpCallback = cb;
 
+
+      //send to my replica that checkpoint begins 
+      if(CkReplicaAlive()==1){
+        char * msg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes);
+        CmiSetHandler(msg, replicaChkpStartHandlerIdx);
+        CmiRemoteSyncSendAndFree(0,CmiMyPartition()^1,CmiMsgHeaderSizeBytes,msg);
+      }
+      CpvAccess(localStarted) = 1;
       // broadcast to start check pointing
-      CProxy_CkMemCheckPT checkptMgr(ckCheckPTGroupID);
-      checkptMgr.doItNow(CkMyPe(), cb);
+      if(CmiNumPartition()==1||(CmiNumPartition()==2&&CpvAccess(remoteStarted)==1)||CkReplicaAlive()==0){
+        CProxy_CkMemCheckPT checkptMgr(ckCheckPTGroupID);
+        checkptMgr.doItNow(CkMyPe());
+      }
 #else
       // when mem checkpoint is disabled, invike cb immediately
       CkPrintf("Warning: In-Memory checkpoint has been disabled! Please use -syncft when build Charm++\n");
@@ -1571,7 +1640,7 @@ void CkMemCheckPT::RollBack(){
 #if CMK_MEM_CHECKPOINT
 #if CMK_USE_BARRIER
       if(CkMyPe()!=_diePE){
-        printf("restar begin on %d\n",CkMyPe());
+        printf("restart begin on %d at %lf\n",CkMyPe(),CmiWallTimer());
         char *restartmsg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes);
         CmiSetHandler(restartmsg, restartBeginHandlerIdx);
         CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
@@ -1583,8 +1652,8 @@ void CkMemCheckPT::RollBack(){
       static int count = 0;
       CmiAssert(CkMyPe() == _diePE);
       count ++;
-      if (count == CkNumPes()) {
-        printf("restart begin on %d\n",CkMyPe());
+      if (count == CkNumPes()||(CpvAccess(resilience)==1&&count==1)) {
+        printf("restart begin on %d at %lf\n",CkMyPe(),CmiWallTimer());
         CkRestartCheckPointCallback(NULL, NULL);
         count = 0;
       }
@@ -1686,7 +1755,7 @@ void CkMemCheckPT::RollBack(){
     }
 
     static void recvRemoteChkpHandler(char *msg){
-//#if CMK_USE_CHECKSUM
+      CpvAccess(remoteChkpDone) = 1;
       if(CpvAccess(use_checksum)){
         if(CkMyPe()==0)
           CmiPrintf("[%d][%d] receive checksum at %lf\n",CmiMyPartition(),CkMyPe(),CmiWallTimer());
@@ -1701,7 +1770,6 @@ void CkMemCheckPT::RollBack(){
           }
         }
       }else{
-//#else
         envelope *env = (envelope *)msg;
         CkUnpackMessage(&env);
         CkCheckPTMessage* chkpMsg = (CkCheckPTMessage *)(EnvToUsr(env));
@@ -1723,7 +1791,6 @@ void CkMemCheckPT::RollBack(){
           CpvAccess(buddyBuf) = chkpMsg;
         }
       }
-//#endif
     }
 
     static void replicaRecoverHandler(char *msg){
@@ -1737,8 +1804,9 @@ void CkMemCheckPT::RollBack(){
 
     static void replicaChkpDoneHandler(char *msg){
       CpvAccess(remoteChkpDone) = 1;
+      int ret = *(int *)(msg+CmiMsgHeaderSizeBytes);
       if(CpvAccess(localChkpDone) == 1)
-        CProxy_CkMemCheckPT(ckCheckPTGroupID).ckLocalBranch()->doneRComparison(CkNumPes());
+        CProxy_CkMemCheckPT(ckCheckPTGroupID).ckLocalBranch()->doneRComparison(ret);
       CmiFree(msg);
     }
 
@@ -1750,19 +1818,31 @@ void CkMemCheckPT::RollBack(){
 #endif
     }
 
+    static void replicaChkpStartHandler(char * msg){
+      CpvAccess(remoteStarted) =1;
+      if(CpvAccess(localStarted)==1){    
+        CProxy_CkMemCheckPT checkptMgr(ckCheckPTGroupID);
+        checkptMgr.doItNow(0);
+      }
+    }
+
 
     static void replicaDieBcastHandler(char *msg){
       int diePe = *(int *)(msg+CmiMsgHeaderSizeBytes);
       CpvAccess(_remoteCrashedNode) = diePe;
-      CkMemCheckPT::replicaAlive = 0;
       if(CkMyPe()==diePe){
         CmiPrintf("pe %d in replicad word die\n",diePe);
         CmiPrintf("replicaAlive %d\n",CkMemCheckPT::replicaAlive);
         fflush(stdout);
       }
-      find_spare_mpirank(diePe,CmiMyPartition()^1);
+      if(CpvAccess(resilience)!=1||CkMyPe()!=diePe){
+        find_spare_mpirank(diePe,CmiMyPartition()^1);
+      }
       //broadcast to my partition to get local max iter
-      CProxy_CkMemCheckPT(ckCheckPTGroupID).ckLocalBranch()->getIter();
+      if(CpvAccess(resilience)!=1){
+        CkMemCheckPT::replicaAlive = 0;
+        CProxy_CkMemCheckPT(ckCheckPTGroupID).ckLocalBranch()->getIter();
+      }
       CmiFree(msg);
     }
 
@@ -1796,7 +1876,17 @@ void CkMemCheckPT::RollBack(){
         _diePE = CpvAccess(_crashedNode);
         char *restartmsg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes);
         CmiSetHandler(restartmsg, restartBeginHandlerIdx);
-        CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+        //CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+#if CMK_USE_BARRIER
+      //CmiPrintf("before reduce\n");  
+       if(CpvAccess(resilience)==1){
+         CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+       }else
+         CmiReduce(restartmsg,CmiMsgHeaderSizeBytes,doNothingMsg);
+      //CmiPrintf("after reduce\n");   
+#else
+       CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+#endif 
       }
     }
 
@@ -1818,7 +1908,17 @@ void CkMemCheckPT::RollBack(){
         //CmiPrintf("[%d] send to die pe %d\n",CkMyPe(),_diePE);
         char *restartmsg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes);
         CmiSetHandler(restartmsg, restartBeginHandlerIdx);
-        CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+        //CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+#if CMK_USE_BARRIER
+      //CmiPrintf("before reduce\n");  
+       if(CpvAccess(resilience)==1){
+         CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+       }else
+         CmiReduce(restartmsg,CmiMsgHeaderSizeBytes,doNothingMsg);
+      //CmiPrintf("after reduce\n");   
+#else
+       CmiSyncSendAndFree(_diePE, CmiMsgHeaderSizeBytes, (char *)restartmsg);
+#endif 
       }
     }
 
@@ -1827,6 +1927,65 @@ void CkMemCheckPT::RollBack(){
       CpvAccess(_curRestartPhase)--;
       CkMemCheckPT::inRestarting = 1;
       CmiFree(msg);
+      //notify the buddy in the replica now i can receive the checkpoint msg
+      if(CpvAccess(resilience)!=1||CpvAccess(_crashedNode)==CmiMyPe()){
+        char *rmsg = (char*)CmiAlloc(CmiMsgHeaderSizeBytes);
+        CmiSetHandler(rmsg, askRecoverDataHandlerIdx);
+        CmiRemoteSyncSendAndFree(CmiMyPe(),CmiMyPartition()^1,CmiMsgHeaderSizeBytes,(char *)rmsg);
+        //timer start
+        if(CpvAccess(_crashedNode)==CkMyPe())
+          CkMemCheckPT::startTime = restartT = CmiWallTimer();
+        if(CpvAccess(_crashedNode)==CmiMyPe())
+          CmiPrintf("[%d][%d] ask for checkpoint data in replica at %lf\n",CmiMyPartition(),CmiMyPe(),CmiWallTimer());
+      }else{
+        CpvAccess(curPointer)^=1;
+        CkMemCheckPT::inRestarting = 1;
+        _resume_charm_message();
+        _diePE = CpvAccess(_crashedNode);
+      }
+    }
+    
+    static void askRecoverDataHandler(char * msg){
+      if(CmiMyPe() == CpvAccess(_remoteCrashedNode))
+       CmiPrintf("[%d][%d] receive replica phase change at %lf\n",CmiMyPartition(),CmiMyPe(),CmiWallTimer());
+      if(CpvAccess(resilience)!=1){
+        CpvAccess(remoteReady)=1;
+        if(CpvAccess(localReady)==1){
+          if(CmiMyPe() == CpvAccess(_remoteCrashedNode))
+          {    
+            envelope * env = (envelope *)(UsrToEnv(CpvAccess(recoverProcBuf)));
+            CkPackMessage(&env);
+            CmiSetHandler(env,recoverRemoteProcDataHandlerIdx);
+            CmiRemoteSyncSendAndFree(CmiMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
+            CmiPrintf("[%d] sendProcdata after request at \n",CmiMyPe(),CmiWallTimer());
+          }
+          //send the array checkpoint data
+          envelope * env = (envelope *)(UsrToEnv(CpvAccess(recoverArrayBuf)));
+          CkPackMessage(&env);
+          CmiSetHandler(env,recoverRemoteArrayDataHandlerIdx);
+          CmiRemoteSyncSendAndFree(CmiMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
+          if(CmiMyPe() == CpvAccess(_remoteCrashedNode))
+            CmiPrintf("[%d] sendArraydata after request\n",CmiMyPe());
+        }
+      }else{
+        find_spare_mpirank(CkMyPe(),CmiMyPartition()^1);
+        int pointer = CpvAccess(curPointer)^1;
+        CkCheckPTMessage * procMsg = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(localProcChkpBuf)[pointer]);
+        procMsg->pointer = pointer;
+        envelope * env = (envelope *)(UsrToEnv(procMsg));
+        CkPackMessage(&env);
+        CmiSetHandler(env,recoverRemoteProcDataHandlerIdx);
+        CmiRemoteSyncSendAndFree(CmiMyPe(),CmiMyPartition()^1,env->getTotalsize(),(char *)env);
+        CmiPrintf("[%d] sendProcdata after request at %lf\n",CmiMyPe(),CmiWallTimer());
+        
+        CkCheckPTMessage * arrayMsg = (CkCheckPTMessage *)CkCopyMsg((void **)&CpvAccess(chkpBuf)[pointer]);
+        arrayMsg->pointer = pointer;
+        envelope * env1 = (envelope *)(UsrToEnv(arrayMsg));
+        CkPackMessage(&env1);
+        CmiSetHandler(env1,recoverRemoteArrayDataHandlerIdx);
+        CmiRemoteSyncSendAndFree(CmiMyPe(),CmiMyPartition()^1,env1->getTotalsize(),(char *)env1);
+        CmiPrintf("[%d] sendArraydata after request\n",CmiMyPe());
+      }
     }
     // called on crashed processor
     static void recoverProcDataHandler(char *msg)
@@ -2027,10 +2186,21 @@ void CkMemCheckPT::RollBack(){
         arg_where = CkCheckPoint_inDISK;
       }
       CpvInitialize(int, use_checksum);
+      CpvInitialize(int, resilience);
       CpvAccess(use_checksum)=0;
+      CpvAccess(resilience)=0;
       if(CmiGetArgFlagDesc(argv, "+use_checksum", "use checksum strategy")){
         CpvAccess(use_checksum)=1;
       }
+      if(CmiGetArgFlagDesc(argv, "+strong_resilience", "use strong resilience")){
+        CpvAccess(resilience)=1;
+      }
+      if(CmiGetArgFlagDesc(argv, "+weak_resilience", "use strong resilience")){
+        CpvAccess(resilience)=2;
+      }
+      if(CmiGetArgFlagDesc(argv, "+medium_resilience", "use strong resilience")){
+        CpvAccess(resilience)=3;
+      }
       // initiliazing _crashedNode variable
       CpvInitialize(int, _crashedNode);
       CpvInitialize(int, _remoteCrashedNode);
@@ -2174,6 +2344,7 @@ void CkMemCheckPT::RollBack(){
 #if CMK_MEM_CHECKPOINT
         notifyHandlerIdx = CkRegisterHandler((CmiHandler)notifyHandler);
         askProcDataHandlerIdx = CkRegisterHandler((CmiHandler)askProcDataHandler);
+        askRecoverDataHandlerIdx = CkRegisterHandler((CmiHandler)askRecoverDataHandler);
         recoverProcDataHandlerIdx = CkRegisterHandler((CmiHandler)recoverProcDataHandler);
         restartBcastHandlerIdx = CkRegisterHandler((CmiHandler)restartBcastHandler);
         restartBeginHandlerIdx = CkRegisterHandler((CmiHandler)restartBeginHandler);
@@ -2181,6 +2352,7 @@ void CkMemCheckPT::RollBack(){
         //for replica
         recvRemoteChkpHandlerIdx = CkRegisterHandler((CmiHandler)recvRemoteChkpHandler);
         replicaDieHandlerIdx = CkRegisterHandler((CmiHandler)replicaDieHandler);
+        replicaChkpStartHandlerIdx = CkRegisterHandler((CmiHandler)replicaChkpStartHandler);
         replicaDieBcastHandlerIdx = CkRegisterHandler((CmiHandler)replicaDieBcastHandler);
         replicaRecoverHandlerIdx = CkRegisterHandler((CmiHandler)replicaRecoverHandler);
         replicaChkpDoneHandlerIdx = CkRegisterHandler((CmiHandler)replicaChkpDoneHandler);
@@ -2199,10 +2371,16 @@ void CkMemCheckPT::RollBack(){
         CpvInitialize(CkCheckPTMessage **, chkpBuf);
         CpvInitialize(CkCheckPTMessage **, localProcChkpBuf);
         CpvInitialize(CkCheckPTMessage *, buddyBuf);
+        CpvInitialize(CkCheckPTMessage *, recoverProcBuf);
+        CpvInitialize(CkCheckPTMessage *, recoverArrayBuf);
         CpvInitialize(int,curPointer);
         CpvInitialize(int,recvdLocal);
         CpvInitialize(int,localChkpDone);
         CpvInitialize(int,remoteChkpDone);
+        CpvInitialize(int,remoteStarted);
+        CpvInitialize(int,localStarted);
+        CpvInitialize(int,localReady);
+        CpvInitialize(int,remoteReady);
         CpvInitialize(int,recvdRemote);
         CpvInitialize(int,recvdProcChkp);
         CpvInitialize(int,localChecksum);
@@ -2211,6 +2389,8 @@ void CkMemCheckPT::RollBack(){
 
         CpvAccess(procChkptBuf) = NULL;
         CpvAccess(buddyBuf) = NULL;
+        CpvAccess(recoverProcBuf) = NULL;
+        CpvAccess(recoverArrayBuf) = NULL;
         CpvAccess(chkpBuf) = new CkCheckPTMessage *[2];
         CpvAccess(localProcChkpBuf) = new CkCheckPTMessage *[2];
         CpvAccess(chkpBuf)[0] = NULL;
@@ -2222,6 +2402,10 @@ void CkMemCheckPT::RollBack(){
         CpvAccess(recvdLocal) = 0;
         CpvAccess(localChkpDone) = 0;
         CpvAccess(remoteChkpDone) = 0;
+        CpvAccess(remoteStarted) = 0;
+        CpvAccess(localStarted) = 0;
+        CpvAccess(localReady) = 0;
+        CpvAccess(remoteReady) = 0;
         CpvAccess(recvdRemote) = 0;
         CpvAccess(recvdProcChkp) = 0;
         CpvAccess(localChecksum) = 0;