Index: XIOS/dev/branch_yushan/extern/src_ep_dev/ep_barrier.cpp
===================================================================
--- XIOS/dev/branch_yushan/extern/src_ep_dev/ep_barrier.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/extern/src_ep_dev/ep_barrier.cpp	(revision 1063)
@@ -1,13 +1,12 @@
 #include "ep_lib.hpp"
 #include <mpi.h>
-//#include "ep_declaration.hpp"
+#include "ep_declaration.hpp"
 
 namespace ep_lib
 {
 
-
-
   int MPI_Barrier(MPI_Comm comm)
   {
+    if(comm.is_intercomm) return MPI_Barrier_intercomm(comm);
 
     if(comm.is_ep)
@@ -20,4 +19,5 @@
       {
         ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm > (comm.mpi_comm);
+
         ::MPI_Barrier(mpi_comm);
       }
@@ -27,5 +27,5 @@
       return 0;
     }
-    else if(comm.mpi_comm !=0 )
+    else if(comm.mpi_comm != MPI_COMM_NULL_STD )
     {
       ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.mpi_comm);
@@ -37,7 +37,28 @@
   }
 
+
+  int MPI_Barrier_intercomm(MPI_Comm comm)
+  {
+
+    int ep_rank_loc = comm.ep_comm_ptr->size_rank_info[1].first;
+
+    MPI_Barrier_local(comm);
+
+    if(ep_rank_loc == 0)
+    {
+      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm > (comm.ep_comm_ptr->intercomm->mpi_inter_comm);
+      ::MPI_Barrier(mpi_comm);
+    }
+
+    MPI_Barrier_local(comm);
+
+    return 0;
+
+  }
+
+
   int MPI_Barrier_local(MPI_Comm comm)
   {
-    //Message_Check(comm);
+    Message_Check(comm);
     comm.ep_barrier->wait();
   }
Index: XIOS/dev/branch_yushan/extern/src_ep_dev/ep_dup.cpp
===================================================================
--- XIOS/dev/branch_yushan/extern/src_ep_dev/ep_dup.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/extern/src_ep_dev/ep_dup.cpp	(revision 1063)
@@ -75,5 +75,8 @@
     }
 
-    if(!comm.mpi_comm) return 0;
+    if(comm.is_intercomm) return MPI_Comm_dup_intercomm(comm, newcomm);
+
+    // for intracomm
+    if(comm.mpi_comm == MPI_COMM_NULL_STD) return 0;
 
 
@@ -103,4 +106,65 @@
   }
 
+  int MPI_Comm_dup_intercomm(MPI_Comm comm, MPI_Comm *newcomm)
+  {
+    
+    if(comm.mpi_comm == MPI_COMM_NULL_STD) return 0;
+
+    int my_rank = comm.ep_comm_ptr->size_rank_info[1].first;
+    int num_ep  = comm.ep_comm_ptr->size_rank_info[1].second;
+
+
+    if(0 == my_rank)
+    {
+      MPI_Info info;
+      MPI_Comm *out_comm;
+      ::MPI_Comm mpi_dup;
+
+      ::MPI_Comm in_comm = static_cast< ::MPI_Comm>(comm.mpi_comm);
+
+      ::MPI_Comm_dup(in_comm, &mpi_dup);
+
+      MPI_Comm_create_endpoints(mpi_dup, num_ep, info, out_comm);
+
+      ::MPI_Comm mpi_inter;
+
+      ::MPI_Comm_dup(static_cast< ::MPI_Comm>(comm.ep_comm_ptr->intercomm->mpi_inter_comm), &mpi_inter);
+      
+      for(int i=0; i<num_ep; i++)
+      {
+        out_comm[i].ep_comm_ptr->comm_label = comm.ep_comm_ptr->comm_list[i].ep_comm_ptr->comm_label;
+        out_comm[i].ep_comm_ptr->intercomm = new ep_intercomm;
+
+        out_comm[i].ep_comm_ptr->intercomm->mpi_inter_comm = mpi_inter;
+        out_comm[i].is_intercomm = true;
+
+        out_comm[i].ep_comm_ptr->intercomm->intercomm_rank_map = new RANK_MAP;
+        out_comm[i].ep_comm_ptr->intercomm->local_rank_map = new RANK_MAP;
+        out_comm[i].ep_comm_ptr->intercomm->remote_rank_map = new RANK_MAP;
+
+        out_comm[i].ep_comm_ptr->intercomm->intercomm_rank_map = comm.ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->intercomm_rank_map;
+        out_comm[i].ep_comm_ptr->intercomm->local_rank_map = comm.ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->local_rank_map;
+        out_comm[i].ep_comm_ptr->intercomm->remote_rank_map = comm.ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->remote_rank_map;
+
+        out_comm[i].ep_comm_ptr->intercomm->local_comm = comm.ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->local_comm;        
+        out_comm[i].ep_comm_ptr->intercomm->intercomm_tag = comm.ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->intercomm_tag;
+
+        for(int j =0; j<3; j++)
+        {
+          out_comm[i].ep_comm_ptr->intercomm->size_rank_info[j] = comm.ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->size_rank_info[j];
+        }
+
+      }
+
+      comm.ep_comm_ptr->comm_list->mem_bridge = out_comm;
+    }
+
+    MPI_Barrier(comm);
+
+    *newcomm = comm.ep_comm_ptr->comm_list->mem_bridge[my_rank];
+    
+
+    return MPI_SUCCESS;
+  }
 
 
Index: XIOS/dev/branch_yushan/extern/src_ep_dev/ep_free.cpp
===================================================================
--- XIOS/dev/branch_yushan/extern/src_ep_dev/ep_free.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/extern/src_ep_dev/ep_free.cpp	(revision 1063)
@@ -10,8 +10,4 @@
   int MPI_Comm_free(MPI_Comm *comm)
   {
-    //if(comm == NULL) return 0;
-
-    MPI_Barrier(*comm);
-
 
     if(! comm->is_ep)
@@ -27,30 +23,17 @@
     }
 
+    else if(comm->is_intercomm)
+    {
+      return MPI_Comm_free_intercomm(comm);
+    }
+
     else
     {
-      if(comm->mpi_comm == MPI_COMM_NULL_STD ) return 0;
+      int ep_rank_loc, num_ep;
 
-      int ep_rank, ep_rank_loc, mpi_rank;
-      int ep_size, num_ep, mpi_size;
-
-      ep_rank = comm->ep_comm_ptr->size_rank_info[0].first;
       ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first;
-      mpi_rank = comm->ep_comm_ptr->size_rank_info[2].first;
-      ep_size = comm->ep_comm_ptr->size_rank_info[0].second;
       num_ep = comm->ep_comm_ptr->size_rank_info[1].second;
-      mpi_size = comm->ep_comm_ptr->size_rank_info[2].second;
 
       MPI_Barrier(*comm);
-
-
-      #pragma omp critical (memory_free)
-      if(comm->is_intercomm && comm->ep_comm_ptr->intercomm != NULL)
-      {
-        if(comm->ep_comm_ptr->intercomm->local_rank_map) comm->ep_comm_ptr->intercomm->local_rank_map->clear();
-        if(comm->ep_comm_ptr->intercomm->remote_rank_map) comm->ep_comm_ptr->intercomm->remote_rank_map->clear();
-        if(comm->ep_comm_ptr->intercomm->intercomm_rank_map) comm->ep_comm_ptr->intercomm->intercomm_rank_map->clear();
-        Debug("intercomm local/remote/intercomm_rank_map emptied\n");
-      }
-
 
       if(ep_rank_loc == 0)
@@ -58,6 +41,4 @@
         Debug("comm is EP, mpi_comm_ptr != NULL\n");
 
-
-/*
         if(comm->my_buffer != NULL)
         {
@@ -69,5 +50,5 @@
           if(comm->my_buffer->buf_char != NULL) delete[] comm->my_buffer->buf_char; Debug("buf_char freed\n");
         }
-*/
+
         if(comm->ep_barrier != NULL)
         {
@@ -77,5 +58,5 @@
 
 
-        if(comm->rank_map != NULL)
+        if( ! comm->rank_map->empty() )
         {
           comm->rank_map->clear();
@@ -83,23 +64,9 @@
         }
 
-
-        if(comm->is_intercomm && comm->ep_comm_ptr->intercomm->mpi_inter_comm != MPI_COMM_NULL_STD)
-        {
-          ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm>(comm->ep_comm_ptr->intercomm->mpi_inter_comm);
-
-          ::MPI_Comm_free(&mpi_comm);
-          //comm->ep_comm_ptr->intercomm->mpi_inter_comm = NULL;
-          Debug("mpi_intercomm freed\n");
-        }
-
-
-
         for(int i=0; i<num_ep; i++)
         {
-          if(comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->message_queue != NULL)
-          {
-            comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->message_queue->clear();
-            Debug("message queue freed\n");
-          }
+          comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->message_queue->clear();
+          Debug("message queue freed\n");
+          
 
           if(comm->ep_comm_ptr->comm_list[i].ep_comm_ptr != NULL)
@@ -114,6 +81,5 @@
           ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm>(comm->mpi_comm);
           ::MPI_Comm_free(&mpi_comm);
-          //comm->mpi_comm = NULL;
-//          printf("label = %d, mpi_comm freed\n", comm->ep_comm_ptr->comm_label);
+          Debug("mpi_comm freed\n");
         }
 
@@ -128,4 +94,81 @@
   }
 
+  int MPI_Comm_free_intercomm(MPI_Comm *comm)
+  {
+    int ep_rank_loc, num_ep;
+
+    ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first;
+    num_ep = comm->ep_comm_ptr->size_rank_info[1].second;
+
+    //MPI_Barrier(*comm);
+
+    if(ep_rank_loc == 0)
+    {
+      Debug("comm is EP, mpi_comm_ptr != NULL\n");
+
+      if(comm->my_buffer != NULL)
+      {
+        if(comm->my_buffer->buf_int != NULL) delete[] comm->my_buffer->buf_int; Debug("buf_int freed\n");
+        if(comm->my_buffer->buf_float != NULL) delete[] comm->my_buffer->buf_float; Debug("buf_float freed\n");
+        if(comm->my_buffer->buf_double != NULL) delete[] comm->my_buffer->buf_double; Debug("buf_double freed\n");
+        if(comm->my_buffer->buf_long != NULL) delete[] comm->my_buffer->buf_long; Debug("buf_long freed\n");
+        if(comm->my_buffer->buf_ulong != NULL) delete[] comm->my_buffer->buf_ulong; Debug("buf_ulong freed\n");
+        if(comm->my_buffer->buf_char != NULL) delete[] comm->my_buffer->buf_char; Debug("buf_char freed\n");
+      }
+
+      if(comm->ep_barrier != NULL)
+      {
+        comm->ep_barrier->~OMPbarrier();
+        Debug("ep_barrier freed\n");
+      }
+
+
+      if( ! comm->rank_map->empty() )
+      {
+        comm->rank_map->clear();
+        Debug("rank_map emptied\n");
+      }
+
+      for(int i=0; i<num_ep; i++)
+      {
+        comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->message_queue->clear();
+        Debug("message queue freed\n");
+
+        #pragma omp critical (memory_free)
+        if(comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm != NULL)
+        {
+          comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->local_rank_map->clear();
+          comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->remote_rank_map->clear();
+          comm->ep_comm_ptr->comm_list[i].ep_comm_ptr->intercomm->intercomm_rank_map->clear();
+          Debug("intercomm local/remote/intercomm_rank_map emptied\n");
+        }  
+
+        if(comm->ep_comm_ptr->comm_list[i].ep_comm_ptr != NULL)
+        {
+          delete comm->ep_comm_ptr->comm_list[i].ep_comm_ptr;
+          Debug("ep_comm_ptr freed\n");
+        }
+      }
+
+      if(comm->mpi_comm != MPI_COMM_NULL_STD)
+      {
+        ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm>(comm->mpi_comm);
+        ::MPI_Comm_free(&mpi_comm);
+        Debug("mpi_comm freed\n");
+      }
+
+      // if(comm->ep_comm_ptr->intercomm->mpi_inter_comm != MPI_COMM_NULL_STD)
+      // {
+         // ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm>(comm->ep_comm_ptr->comm_list->ep_comm_ptr->intercomm->mpi_inter_comm);
+         // ::MPI_Comm_free(&mpi_comm);
+      //   Debug("mpi_intercomm freed\n");
+      // }
+
+     if(comm != NULL) {delete[] comm->ep_comm_ptr->comm_list; Debug("comm freed\n");}
+
+    }
+    
+    return 0;
+  }
 
 
Index: XIOS/dev/branch_yushan/extern/src_ep_dev/ep_lib_intercomm.hpp
===================================================================
--- XIOS/dev/branch_yushan/extern/src_ep_dev/ep_lib_intercomm.hpp	(revision 1060)
+++ XIOS/dev/branch_yushan/extern/src_ep_dev/ep_lib_intercomm.hpp	(revision 1063)
@@ -11,4 +11,10 @@
   typedef void* MPI_Op;
   #endif
+  
+  int MPI_Comm_dup_intercomm(MPI_Comm comm, MPI_Comm *newcomm);
+
+  int MPI_Comm_free_intercomm(MPI_Comm* comm);
+
+  int MPI_Barrier_intercomm(MPI_Comm comm);
 
   int Message_Check_intercomm(MPI_Comm comm);
Index: XIOS/dev/branch_yushan/inputs/iodef.xml
===================================================================
--- XIOS/dev/branch_yushan/inputs/iodef.xml	(revision 1060)
+++ XIOS/dev/branch_yushan/inputs/iodef.xml	(revision 1063)
@@ -12,5 +12,5 @@
 
 
-   <file_definition type="multiple_file" par_access="collective" output_freq="6h" output_level="10" enabled=".TRUE.">
+   <file_definition type="one_file" par_access="collective" output_freq="6h" output_level="10" enabled=".TRUE.">
      <file id="output" name="output">
         <field field_ref="field_A_zoom" name="field_A" />
Index: XIOS/dev/branch_yushan/src/client.cpp
===================================================================
--- XIOS/dev/branch_yushan/src/client.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/src/client.cpp	(revision 1063)
@@ -247,5 +247,5 @@
       }
 
-      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
+      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); ++it)
         MPI_Comm_free(&(*it));
       
Index: XIOS/dev/branch_yushan/src/context_server.cpp
===================================================================
--- XIOS/dev/branch_yushan/src/context_server.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/src/context_server.cpp	(revision 1063)
@@ -123,5 +123,5 @@
     //printf("enter checkPendingRequest\n");
     if(!pendingRequest.empty())
-    for(it=pendingRequest.begin();it!=pendingRequest.end();it++)
+    for(it=pendingRequest.begin();it!=pendingRequest.end();++it)
     {
       rank=it->first;
@@ -136,5 +136,6 @@
       }
     }
-
+    
+    if(!recvRequest.empty())
     for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++)
     {
Index: XIOS/dev/branch_yushan/src/io/onetcdf4.cpp
===================================================================
--- XIOS/dev/branch_yushan/src/io/onetcdf4.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/src/io/onetcdf4.cpp	(revision 1063)
@@ -518,5 +518,5 @@
                                  const std::vector<StdSize>& scount, const int* data)
       {
-          CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data);
+         CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data);
       }
 
@@ -528,5 +528,5 @@
                                  const std::vector<StdSize>& scount, const float* data)
       {
-          CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data);
+         CNetCdfInterface::putVaraType(grpid, varid, &sstart[0], &scount[0], data);
       }
 
Index: XIOS/dev/branch_yushan/src/node/context.cpp
===================================================================
--- XIOS/dev/branch_yushan/src/node/context.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/src/node/context.cpp	(revision 1063)
@@ -260,4 +260,6 @@
        MPI_Comm_dup(interComm, &interCommServer);
        comms.push_back(interCommServer);
+       
+       printf("comm_dup OK\n");
      }
      server = new CContextServer(this,intraCommServer,interCommServer);
Index: XIOS/dev/branch_yushan/src/server.cpp
===================================================================
--- XIOS/dev/branch_yushan/src/server.cpp	(revision 1060)
+++ XIOS/dev/branch_yushan/src/server.cpp	(revision 1063)
@@ -101,5 +101,5 @@
              MPI_Intercomm_create(intraComm,0,CXios::globalComm,clientLeader,0,&newComm) ;
              interComm.push_back(newComm) ;
-             //printf("after inter create, interComm.size = %lu\n", interComm.size());
+             printf("after inter create, interComm.size = %lu\n", interComm.size());
            }
          }
@@ -166,5 +166,5 @@
       {
         if (CXios::usingOasis) oasis_finalize();
-        else  {MPI_Finalize() ; printf("CServer::finalize called MPI_finalize\n");}
+        //else  {MPI_Finalize() ; printf("CServer::finalize called MPI_finalize\n");}
       }
 
@@ -213,5 +213,5 @@
         
 
-        for(it=interComm.begin();it!=interComm.end();it++)
+        for(it=interComm.begin();it!=interComm.end();++it)
         {
            MPI_Status status ;
@@ -441,5 +441,5 @@
        bool finished ;
        map<string,CContext*>::iterator it ;
-       for(it=contextList.begin();it!=contextList.end();it++)
+       for(it=contextList.begin();it!=contextList.end();++it)
        {
          finished=it->second->checkBuffersAndListen();
Index: XIOS/dev/branch_yushan/src/xios_server.f90
===================================================================
--- XIOS/dev/branch_yushan/src/xios_server.f90	(revision 1060)
+++ XIOS/dev/branch_yushan/src/xios_server.f90	(revision 1063)
@@ -9,4 +9,6 @@
     CALL init_wait
     CALL xios_init_server
+    
+    CALL MPI_FINALIZE(ierr)
 
   END PROGRAM server_main
