Index: LMDZ5/branches/testing/libf/dyn3dmem/mod_hallo.F90
===================================================================
--- LMDZ5/branches/testing/libf/dyn3dmem/mod_hallo.F90	(revision 1669)
+++ LMDZ5/branches/testing/libf/dyn3dmem/mod_hallo.F90	(revision 1864)
@@ -1,9 +1,9 @@
 module mod_Hallo
-USE parallel
+USE parallel_lmdz
 implicit none
   logical,save :: use_mpi_alloc
-  integer, parameter :: MaxRequest=200
   integer, parameter :: MaxProc=512
-  integer, parameter :: MaxBufferSize=1024*1024*100
+  integer, parameter :: DefaultMaxBufferSize=1024*1024*100
+  integer, SAVE :: MaxBufferSize=0
   integer, parameter :: ListSize=1000
   
@@ -28,8 +28,9 @@
   type request_SR
     integer :: NbRequest=0
+    integer :: NbRequestMax=0
     integer :: BufferSize
     integer :: Pos
     integer :: Index 
-    type(Hallo),dimension(MaxRequest) :: Hallo
+    type(Hallo), POINTER :: Hallo(:)
     integer :: MSG_Request
   end type request_SR
@@ -63,5 +64,6 @@
 
   subroutine Init_mod_hallo
-  USE dimensions
+  USE dimensions_mod
+  USE IOIPSL
     implicit none
     integer :: jj_nb_gather(0:mpi_size-1)
@@ -70,5 +72,10 @@
     Buffer_Pos(Index_Pos)=1
     MaxBufferSize_Used=0
-
+!$OMP MASTER     
+    MaxBufferSize=DefaultMaxBufferSize
+    CALL getin("mpi_buffer_size",MaxBufferSize)
+!$OMP END MASTER
+!$OMP BARRIER
+    
     IF (use_mpi_alloc .AND. using_mpi) THEN
       CALL create_global_mpi_buffer
@@ -77,8 +84,11 @@
     ENDIF
      
+!$OMP MASTER     
      jj_nb_gather(:)=0
      jj_nb_gather(0)=jjp1
      
      CALL create_distrib(jj_nb_gather,distrib_gather) 
+!$OMP END MASTER
+!$OMP BARRIER
 
   end subroutine init_mod_hallo
@@ -176,5 +186,5 @@
   
   
-  subroutine Init_Hallo(Field,Stride,NbLevel,offset,size,NewHallo)
+  subroutine New_Hallo(Field,Stride,NbLevel,offset,size,Ptr_request)
     integer :: Stride
     integer :: NbLevel
@@ -182,6 +192,22 @@
     integer :: offset
     real, dimension(Stride,NbLevel),target :: Field
-    type(Hallo) :: NewHallo
-    
+    type(request_SR),pointer :: Ptr_request
+    type(Hallo),POINTER :: NewHallos(:),HalloSwitch(:), NewHallo
+    
+    Ptr_Request%NbRequest=Ptr_Request%NbRequest+1
+    IF(Ptr_Request%NbRequestMax==0) THEN
+       Ptr_Request%NbRequestMax=10
+       ALLOCATE(Ptr_Request%Hallo(Ptr_Request%NbRequestMax))
+    ELSE IF ( Ptr_Request%NbRequest > Ptr_Request%NbRequestMax) THEN
+      Ptr_Request%NbRequestMax=INT(Ptr_Request%NbRequestMax*1.2)
+      ALLOCATE(NewHallos(Ptr_Request%NbRequestMax))
+      NewHallos(1:Ptr_Request%NbRequest-1)=Ptr_Request%hallo(1:Ptr_Request%NbRequest-1)
+      HalloSwitch=>Ptr_Request%hallo
+      Ptr_Request%hallo=>NewHallos
+      DEALLOCATE(HalloSwitch)
+    ENDIF
+    
+    NewHallo=>Ptr_Request%hallo(Ptr_Request%NbRequest)
+          
     NewHallo%Field=>Field
     NewHallo%Stride=Stride
@@ -190,9 +216,8 @@
     NewHallo%offset=offset
     
-    
-  end subroutine Init_Hallo
+  end subroutine New_Hallo
   
   subroutine Register_SendField(Field,ij,ll,offset,size,target,a_request)
-  USE dimensions
+  USE dimensions_mod
   implicit none
 
@@ -204,15 +229,11 @@
 
       Ptr_Request=>a_request%RequestSend(target)
-      Ptr_Request%NbRequest=Ptr_Request%NbRequest+1
-      if (Ptr_Request%NbRequest>=MaxRequest) then
-        print *,'STOP :: La taille de MaxRequest dans mod_hallo.F90 est trop petite !!!!'
-        stop
-      endif      
-      call Init_Hallo(Field,ij,ll,offset,size,Ptr_request%Hallo(Ptr_Request%NbRequest))
+
+      call New_Hallo(Field,ij,ll,offset,size,Ptr_request)
       
    end subroutine Register_SendField      
       
   subroutine Register_RecvField(Field,ij,ll,offset,size,target,a_request)
-  USE dimensions
+  USE dimensions_mod
   implicit none
 
@@ -224,12 +245,6 @@
 
       Ptr_Request=>a_request%RequestRecv(target)
-      Ptr_Request%NbRequest=Ptr_Request%NbRequest+1
-      
-      if (Ptr_Request%NbRequest>=MaxRequest) then
-        print *,'STOP :: La taille de MaxRequest dans mod_hallo.F90 est trop petite !!!!'
-        stop
-      endif   
             
-      call Init_Hallo(Field,ij,ll,offset,size,Ptr_request%Hallo(Ptr_Request%NbRequest))
+      call New_Hallo(Field,ij,ll,offset,size,Ptr_request)
 
       
@@ -237,5 +252,5 @@
   
   subroutine Register_SwapField(FieldS,FieldR,ij,ll,jj_Nb_New,a_request)
-  USE dimensions
+  USE dimensions_mod
       implicit none
 
@@ -285,5 +300,5 @@
   
   subroutine Register_SwapFieldHallo(FieldS,FieldR,ij,ll,jj_Nb_New,Up,Down,a_request)
-  USE dimensions
+  USE dimensions_mod
   
       implicit none
@@ -338,6 +353,6 @@
 
   SUBROUTINE Register_SwapField1d_u(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -369,6 +384,6 @@
 
   SUBROUTINE Register_SwapField2d_u1d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
     IMPLICIT NONE
     
@@ -403,6 +418,6 @@
 
   SUBROUTINE Register_SwapField3d_u(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -438,6 +453,6 @@
 
  SUBROUTINE Register_SwapField1d_u2d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
 
       IMPLICIT NONE
@@ -470,6 +485,6 @@
 
   SUBROUTINE Register_SwapField2d_u2d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
 
       IMPLICIT NONE
@@ -505,6 +520,6 @@
 
   SUBROUTINE Register_SwapField3d_u2d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -544,6 +559,6 @@
 
   SUBROUTINE Register_SwapField1d_v(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -575,6 +590,6 @@
 
   SUBROUTINE Register_SwapField2d_v1d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
    
@@ -609,6 +624,6 @@
 
   SUBROUTINE Register_SwapField3d_v(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -645,6 +660,6 @@
 
   SUBROUTINE Register_SwapField1d_v2d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -676,6 +691,6 @@
 
   SUBROUTINE Register_SwapField2d_v2d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -710,6 +725,6 @@
 
   SUBROUTINE Register_SwapField3d_v2d(FieldS,FieldR,new_dist,a_request,old_dist,up,down)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
     
@@ -745,6 +760,6 @@
 
   SUBROUTINE Register_SwapField_gen_u(FieldS,FieldR,ll,old_dist,new_dist,Up,Down,a_request)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
       IMPLICIT NONE
    
@@ -787,5 +802,5 @@
 !$OMP DO SCHEDULE(STATIC,OMP_CHUNK)           
         DO l=1,ll
-          FieldR(ijb:ije,:)=FieldS(ijb:ije,:)              
+          FieldR(ijb:ije,l)=FieldS(ijb:ije,l)              
         ENDDO
 !$OMP END DO NOWAIT
@@ -798,6 +813,6 @@
 
   SUBROUTINE Register_SwapField_gen_v(FieldS,FieldR,ll,old_dist,new_dist,Up,Down,a_request)
-  USE parallel
-  USE dimensions
+  USE parallel_lmdz
+  USE dimensions_mod
     IMPLICIT NONE
     
@@ -858,5 +873,5 @@
   
   subroutine Register_Hallo(Field,ij,ll,RUp,Rdown,SUp,SDown,a_request)
-  USE dimensions
+  USE dimensions_mod
       implicit none
 
@@ -925,5 +940,5 @@
 
   subroutine Register_Hallo_u(Field,ll,RUp,Rdown,SUp,SDown,a_request)
-  USE dimensions
+  USE dimensions_mod
       implicit none
 #ifdef CPP_MPI
@@ -990,5 +1005,5 @@
 
   subroutine Register_Hallo_v(Field,ll,RUp,Rdown,SUp,SDown,a_request)
-  USE dimensions
+  USE dimensions_mod
       implicit none
 #ifdef CPP_MPI
@@ -1055,5 +1070,5 @@
     
     subroutine SendRequest(a_Request)
-    USE dimensions
+    USE dimensions_mod
       implicit none
 
@@ -1179,5 +1194,5 @@
    
    subroutine WaitRequest(a_Request)
-   USE dimensions
+   USE dimensions_mod
    implicit none
    
@@ -1273,5 +1288,5 @@
      
    subroutine WaitSendRequest(a_Request)
-   USE dimensions
+   USE dimensions_mod
    implicit none
    
@@ -1329,5 +1344,5 @@
     
    subroutine WaitRecvRequest(a_Request)
-   USE dimensions
+   USE dimensions_mod
    implicit none
    
@@ -1410,5 +1425,5 @@
     
     subroutine CopyField(FieldS,FieldR,ij,ll,jj_Nb_New)
-    USE dimensions
+    USE dimensions_mod
   
       implicit none
@@ -1448,5 +1463,5 @@
 
   subroutine CopyFieldHallo(FieldS,FieldR,ij,ll,jj_Nb_New,Up,Down)
-  USE dimensions
+  USE dimensions_mod
   
       implicit none
@@ -1488,5 +1503,5 @@
 
    subroutine Gather_field_u(field_loc,field_glo,ll)
-   USE dimensions
+   USE dimensions_mod
    implicit none
      integer :: ll
@@ -1511,5 +1526,5 @@
         
    subroutine Gather_field_v(field_loc,field_glo,ll)
-   USE dimensions
+   USE dimensions_mod
    implicit none
      integer :: ll
@@ -1539,5 +1554,5 @@
      
    subroutine Scatter_field_u(field_glo,field_loc,ll)
-   USE dimensions
+   USE dimensions_mod
    implicit none
      integer :: ll
@@ -1573,5 +1588,5 @@
 
    subroutine Scatter_field_v(field_glo,field_loc,ll)
-   USE dimensions
+   USE dimensions_mod
    implicit none
      integer :: ll
