Index: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_message.cpp
===================================================================
--- XIOS/dev/branch_openmp/extern/src_ep_dev/ep_message.cpp	(revision 1374)
+++ XIOS/dev/branch_openmp/extern/src_ep_dev/ep_message.cpp	(revision 1520)
@@ -18,86 +18,184 @@
 namespace ep_lib
 {
-
+  int Request_Check()
+  {
+    if(EP_PendingRequests == 0 ) EP_PendingRequests = new std::list< MPI_Request* >;
+    
+    if(EP_PendingRequests->size() == 0) return 0;
+    
+    MPI_Status status;
+    MPI_Message *message;
+    int probed = false;
+    int recv_count = 0;
+    std::list<MPI_Request* >::iterator it;
+    
+    
+    for(it = EP_PendingRequests->begin(); it!=EP_PendingRequests->end(); it++)
+    { 
+      Message_Check(((*(*it))->comm));
+    }
+
+
+    for(it = EP_PendingRequests->begin(); it!=EP_PendingRequests->end(); )
+    {
+      if((*(*it))->state == 0)
+      {
+        #pragma omp critical (_query0)
+        {
+          MPI_Iprobe((*(*it))->ep_src, (*(*it))->ep_tag, ((*(*it))->comm), &probed, &status);
+          if(probed)
+          {
+            message = new MPI_Message;
+            *message = new ep_message;
+        
+            memcheck("new "<< message <<" : in ep_lib::Request_Check, message = new MPI_Message");
+            memcheck("new "<< *message <<" : in ep_lib::Request_Check, *message = new ep_message");
+          
+          
+            MPI_Improbe((*(*it))->ep_src, (*(*it))->ep_tag, (*(*it))->comm, &probed, message, &status);
+        
+          }
+        }
+      
+        
+        if(probed)
+        {
+          MPI_Get_count(&status, (*(*it))->ep_datatype, &recv_count);
+          
+          MPI_Imrecv((*(*it))->buf, recv_count, (*(*it))->ep_datatype, message, *it);
+          (*(*it))->type = 3;
+          (*(*it))->state = 1;
+
+          memcheck("delete "<< status.mpi_status <<" : in ep_lib::Request_Check, delete status.mpi_status");
+          delete status.mpi_status;          
+
+          memcheck("delete "<< *message <<" : in ep_lib::Request_Check, delete *message");
+          memcheck("delete "<< message <<" : in ep_lib::Request_Check, delete message");
+
+          delete *message;
+          delete message;
+        
+          it++;
+          continue;      
+        }              
+      }
+      
+      if((*(*it))->state == 2)
+      {
+        int ep_rank = ((*(*it))->comm)->ep_comm_ptr->size_rank_info[0].first;
+        memcheck("delete "<< (*(*it)) <<" : in ep_lib::Request_Check, delete (*(*it))");
+        
+        
+        int world_rank;
+        MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
+        if(world_rank==2) 
+        {
+          printf("ep %d erased one pending request %p\n", world_rank,*(*it));
+        }
+        
+        EP_PendingRequests->erase(it);
+        
+        memcheck("EP_PendingRequests["<<ep_rank<<"]->size() = " << EP_PendingRequests->size());
+        it = EP_PendingRequests->begin();
+        continue;
+      }
+      else it++;
+    }
+  }
+  
+  
+  
   int Message_Check(MPI_Comm comm)
   {
-    if(!comm.is_ep) return 0;
-
-    if(comm.is_intercomm)
-    {
-      return  Message_Check_intercomm(comm);
-    }
+    if(!comm->is_ep) return MPI_SUCCESS;
+
+    if(comm->is_intercomm)
+    {
+      Message_Check_intercomm(comm);
+    }
+    
+    return Message_Check_intracomm(comm);
+
+  }
+  
+  
+  int Message_Check_intracomm(MPI_Comm comm)
+  {
+    
+    int flag = true;
+    ::MPI_Status status;
+    ::MPI_Message message;
+
+    while(flag) // loop until the end of global queue
+    {
+      Debug("Message probing for intracomm\n");
+     
+      #pragma omp critical (_mpi_call)
+      {
+        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, to_mpi_comm(comm->mpi_comm), &flag, &status);
+        if(flag)
+        {
+          Debug("find message in mpi comm \n");
+          ::MPI_Mprobe(status.MPI_SOURCE, status.MPI_TAG, to_mpi_comm(comm->mpi_comm), &message, &status);
+        }
+      }
+
+      
+      if(flag)
+      {
+        MPI_Message msg = new ep_message; 
+        msg->mpi_message = new ::MPI_Message(message);
+
+        memcheck("new "<< msg <<" : in ep_lib::Message_Check, msg = new ep_message");
+        memcheck("new "<< msg->mpi_message <<" : in ep_lib::Message_Check, msg->mpi_message = new ::MPI_Message");
+              
+
+        msg->ep_tag  = bitset<15>(status.MPI_TAG >> 16).to_ulong(); 
+        int src_loc  = bitset<8> (status.MPI_TAG >> 8) .to_ulong(); 
+        int dest_loc = bitset<8> (status.MPI_TAG)	    .to_ulong();
+        int src_mpi  = status.MPI_SOURCE;
+             
+        msg->ep_src  = get_ep_rank(comm, src_loc,  src_mpi);  
+
+#ifdef _showinfo
+        printf("status.MPI_TAG = %d, src_loc = %d, dest_loc = %d, ep_tag = %d\n", status.MPI_TAG, src_loc, dest_loc, msg->ep_tag);
+#endif
+
+        msg->mpi_status = new ::MPI_Status(status);  
+        memcheck("new "<< msg->mpi_status <<" : in ep_lib::Message_Check, msg->mpi_status = new ::MPI_Status");
+
+        #pragma omp critical (_query)
+        {
+          #pragma omp flush
+          comm->ep_comm_ptr->comm_list[dest_loc]->ep_comm_ptr->message_queue->push_back(msg);  
+          int dest_mpi = comm->ep_comm_ptr->size_rank_info[2].first;
+          memcheck("message_queue["<<dest_mpi<<","<<dest_loc<<"]->size = "<<comm->ep_comm_ptr->comm_list[dest_loc]->ep_comm_ptr->message_queue->size());
+          #pragma omp flush
+        }
+      }
+    }
+
+    return MPI_SUCCESS;
+  }
+
+
+  
+  
+  
+
+
+  int Message_Check_intercomm(MPI_Comm comm)
+  {
+    if(!comm->ep_comm_ptr->intercomm->mpi_inter_comm) return 0;
+
+    Debug("Message probing for intercomm\n");
 
     int flag = true;
     ::MPI_Message message;
     ::MPI_Status status;
-    int mpi_source;
-
-    while(flag) // loop until the end of global queue
-    {
-      Debug("Message probing for intracomm\n");
-      
-      #pragma omp critical (_mpi_call)
-      {
-        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, to_mpi_comm(comm.mpi_comm), &flag, &status);
-        if(flag)
-        {
-          Debug("find message in mpi comm \n");
-          mpi_source = status.MPI_SOURCE;
-          int tag = status.MPI_TAG;
-          ::MPI_Mprobe(mpi_source, tag, to_mpi_comm(comm.mpi_comm), &message, &status);
-
-        }
-      }
-
-      
-      if(flag)
-      {
-
-        MPI_Message *msg_block = new MPI_Message; 
-        msg_block->mpi_message = new ::MPI_Message;
-        *(static_cast< ::MPI_Message*>(msg_block->mpi_message)) = message;  
-        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong(); 
-        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong(); 
-        int dest_loc      = bitset<8> (status.MPI_TAG)	    .to_ulong();
-        int src_mpi       = status.MPI_SOURCE;
-             
-        msg_block->ep_src  = get_ep_rank(comm, src_loc,  src_mpi);       
-        msg_block->mpi_status = new ::MPI_Status(status);
-
-        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
-        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
-
-
-        #pragma omp critical (_query)
-        {
-          #pragma omp flush
-          comm.ep_comm_ptr->comm_list[dest_loc].ep_comm_ptr->message_queue->push_back(*msg_block);      
-          #pragma omp flush
-        }
-        
-        delete msg_block;
-      }
-
-    }
-
-    return MPI_SUCCESS;
-  }
-
-
-
-  int Message_Check_intercomm(MPI_Comm comm)
-  {
-    if(!comm.ep_comm_ptr->intercomm->mpi_inter_comm) return 0;
-
-    Debug("Message probing for intercomm\n");
-
-    int flag = true;
-    ::MPI_Message message;
-    ::MPI_Status status;
-    int mpi_source;
     int current_ep_rank;
     MPI_Comm_rank(comm, &current_ep_rank);
 
-    while(flag) // loop until the end of global queue "comm.ep_comm_ptr->intercomm->mpi_inter_comm"
+    while(flag) // loop until the end of global queue "comm->ep_comm_ptr->intercomm->mpi_inter_comm"
     {
       Debug("Message probing for intracomm\n");
@@ -105,12 +203,9 @@
       #pragma omp critical (_mpi_call)
       {
-        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, to_mpi_comm(comm.ep_comm_ptr->intercomm->mpi_inter_comm), &flag, &status);
+        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, to_mpi_comm(comm->ep_comm_ptr->intercomm->mpi_inter_comm), &flag, &status);
         if(flag)
         {
           Debug("find message in mpi comm \n");
-          mpi_source = status.MPI_SOURCE;
-          int tag = status.MPI_TAG;
-          ::MPI_Mprobe(mpi_source, tag, to_mpi_comm(comm.ep_comm_ptr->intercomm->mpi_inter_comm), &message, &status);
-
+          ::MPI_Mprobe(status.MPI_SOURCE, status.MPI_TAG, to_mpi_comm(comm->ep_comm_ptr->intercomm->mpi_inter_comm), &message, &status);
         }
       }
@@ -120,117 +215,40 @@
       {
 
-        MPI_Message *msg_block = new MPI_Message;
-        msg_block->mpi_message = new ::MPI_Message;
-        *(static_cast< ::MPI_Message*>(msg_block->mpi_message)) = message;
-        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
-        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
-        int dest_loc      = bitset<8> (status.MPI_TAG)	    .to_ulong();
-        int src_mpi       = status.MPI_SOURCE;
-        int current_inter = comm.ep_comm_ptr->intercomm->local_rank_map->at(current_ep_rank).first;
+        MPI_Message msg = new ep_message; 
+        msg->mpi_message = new ::MPI_Message(message);
+
+        memcheck("new "<< msg <<" : in ep_lib::Message_Check, msg = new ep_message");
+        memcheck("new "<< msg->mpi_message <<" : in ep_lib::Message_Check, msg->mpi_message = new ::MPI_Message");
+              
+
+        msg->ep_tag  = bitset<15>(status.MPI_TAG >> 16).to_ulong(); 
+        int src_loc  = bitset<8> (status.MPI_TAG >> 8) .to_ulong(); 
+        int dest_loc = bitset<8> (status.MPI_TAG)	    .to_ulong();
+        int src_mpi  = status.MPI_SOURCE;
              
-        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc,  src_mpi);
-        msg_block->mpi_status = new ::MPI_Status(status);
-
-
-        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
-        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
-
+        msg->ep_src  = get_ep_rank(comm, src_loc,  src_mpi);   
+#ifdef _showinfo
+        printf("status.MPI_TAG = %d, src_loc = %d, dest_loc = %d, ep_tag = %d\n", status.MPI_TAG, src_loc, dest_loc, msg->ep_tag);
+#endif
+
+        msg->mpi_status = new ::MPI_Status(status); 
+        memcheck("new "<< msg->mpi_status <<" : in ep_lib::Message_Check, msg->mpi_status = new ::MPI_Status");
 
         #pragma omp critical (_query)
         {
           #pragma omp flush
-          comm.ep_comm_ptr->comm_list[dest_loc].ep_comm_ptr->message_queue->push_back(*msg_block);
-          #pragma omp flush
-        }
-        
-        delete msg_block;
-        
-      }
-
-    }
-
-    flag = true;
-    while(flag) // loop until the end of global queue "comm.mpi_comm"
-    {
-      Debug("Message probing for intracomm\n");
-     
-      #pragma omp critical (_mpi_call)
-      {
-        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, to_mpi_comm(comm.mpi_comm), &flag, &status);
-        if(flag)
-        {
-          Debug("find message in mpi comm \n");
-          mpi_source = status.MPI_SOURCE;
-          int tag = status.MPI_TAG;
-          ::MPI_Mprobe(mpi_source, tag, to_mpi_comm(comm.mpi_comm), &message, &status);
-
-        }
-      }
-      
-
-      if(flag)
-      {
-
-        MPI_Message *msg_block = new MPI_Message;
-        msg_block->mpi_message = new ::MPI_Message;
-        *(static_cast< ::MPI_Message*>(msg_block->mpi_message)) = message;
-        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
-        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
-        int dest_loc      = bitset<8> (status.MPI_TAG)	    .to_ulong();
-        int src_mpi       = status.MPI_SOURCE;
-        
-        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc, src_mpi);
-        msg_block->mpi_status = new ::MPI_Status(status);
-        
-
-        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
-        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
-
-
-        #pragma omp critical (_query)
-        {
-          #pragma omp flush
-          comm.ep_comm_ptr->comm_list[dest_loc].ep_comm_ptr->message_queue->push_back(*msg_block);
-          #pragma omp flush
-        }
-        
-        delete msg_block;
-        
-      }
-
-    }
+          comm->ep_comm_ptr->comm_list[dest_loc]->ep_comm_ptr->message_queue->push_back(msg);
+          memcheck("comm->ep_comm_ptr->comm_list["<<dest_loc<<"]->ep_comm_ptr->message_queue->size = "<<comm->ep_comm_ptr->comm_list[dest_loc]->ep_comm_ptr->message_queue->size());
+          #pragma omp flush
+        }
+      }
+    }
+
+    Message_Check_intracomm(comm);
 
     return MPI_SUCCESS;
   }
 
-  int Request_Check()
-  {
-    MPI_Status status;
-    MPI_Message message;
-    int probed = false;
-    int recv_count = 0;
-    std::list<MPI_Request* >::iterator it;
-    
-    for(it = EP_PendingRequests->begin(); it!=EP_PendingRequests->end(); it++)
-    { 
-      Message_Check((*it)->comm);
-    }
-
-
-    for(it = EP_PendingRequests->begin(); it!=EP_PendingRequests->end(); )
-    {
-      MPI_Improbe((*it)->ep_src, (*it)->ep_tag, (*it)->comm, &probed, &message, &status);
-      if(probed)
-      {
-        MPI_Get_count(&status, (*it)->ep_datatype, &recv_count);
-        MPI_Imrecv((*it)->buf, recv_count, (*it)->ep_datatype, &message, *it);
-        (*it)->type = 3;
-        EP_PendingRequests->erase(it);
-        it = EP_PendingRequests->begin();
-        continue;
-      }
-      it++;
-    }
-  }
+  
 
 }
