Index: palm/trunk/SOURCE/exchange_horiz.f90
===================================================================
--- palm/trunk/SOURCE/exchange_horiz.f90	(revision 1114)
+++ palm/trunk/SOURCE/exchange_horiz.f90	(revision 1128)
@@ -20,5 +20,7 @@
 ! Current revisions:
 ! -----------------
-! 
+! modifications for asynchronous transfer,
+! local variables req, wait_stat are global now, and have been moved to module
+! pegrid
 !
 ! Former revisions:
@@ -90,16 +92,11 @@
 
 #if ! defined( __check )
+
+    CALL cpu_log( log_point_s(2), 'exchange_horiz', 'start' )
+
 #if defined( __parallel )
-    INTEGER, DIMENSION(4)                 ::  req
-    INTEGER, DIMENSION(MPI_STATUS_SIZE,4) ::  wait_stat
-#endif
-
-
-    CALL cpu_log( log_point_s(2), 'exchange_horiz', 'start' )
-
-#if defined( __parallel )
-
-!
-!-- Exchange of lateral boundary values
+
+!
+!-- Exchange in x-direction of lateral boundaries
     IF ( pdims(1) == 1  .OR.  mg_switch_to_pe0 )  THEN
 !
@@ -130,19 +127,35 @@
        ELSE
 
-          req = 0
-!
-!--       Send left boundary, receive right one (asynchronous)
-          CALL MPI_ISEND( ar(nzb,nys-nbgp_local,nxl),   1, type_yz(grid_level), &
-                          pleft, 0, comm2d, req(1), ierr )
-          CALL MPI_IRECV( ar(nzb,nys-nbgp_local,nxr+1), 1, type_yz(grid_level), &
-                          pright, 0, comm2d, req(2), ierr )
-!
-!--       Send right boundary, receive left one (asynchronous)
-          CALL MPI_ISEND( ar(nzb,nys-nbgp_local,nxr+1-nbgp_local), 1,          &
-                          type_yz(grid_level), pright, 1, comm2d, req(3), ierr )
-          CALL MPI_IRECV( ar(nzb,nys-nbgp_local,nxl-nbgp_local),   1,          &
-                          type_yz(grid_level), pleft,  1, comm2d, req(4), ierr )
-
-          CALL MPI_WAITALL( 4, req, wait_stat, ierr )
+!
+!--       In case of background communication switched on, exchange is done
+!--       either along x or along y
+          IF ( send_receive == 'lr'  .OR.  send_receive == 'al' )  THEN
+
+             IF ( .NOT. sendrecv_in_background )  THEN
+                req(1:4)  = 0
+                req_count = 0
+             ENDIF
+!
+!--          Send left boundary, receive right one (asynchronous)
+             CALL MPI_ISEND( ar(nzb,nys-nbgp_local,nxl),   1, type_yz(grid_level), &
+                             pleft, req_count, comm2d, req(req_count+1), ierr )
+             CALL MPI_IRECV( ar(nzb,nys-nbgp_local,nxr+1), 1, type_yz(grid_level), &
+                             pright, req_count, comm2d, req(req_count+2), ierr )
+!
+!--          Send right boundary, receive left one (asynchronous)
+             CALL MPI_ISEND( ar(nzb,nys-nbgp_local,nxr+1-nbgp_local), 1,          &
+                             type_yz(grid_level), pright, req_count+1, comm2d,    &
+                             req(req_count+3), ierr )
+             CALL MPI_IRECV( ar(nzb,nys-nbgp_local,nxl-nbgp_local),   1,          &
+                             type_yz(grid_level), pleft,  req_count+1, comm2d,    &
+                             req(req_count+4), ierr )
+
+             IF ( .NOT. sendrecv_in_background )  THEN
+                CALL MPI_WAITALL( 4, req, wait_stat, ierr )
+             ELSE
+                req_count = req_count + 4
+             ENDIF
+
+          ENDIF
 
        ENDIF
@@ -179,19 +192,36 @@
        ELSE
 
-          req = 0
-!
-!--       Send front boundary, receive rear one (asynchronous)
-          CALL MPI_ISEND( ar(nzb,nys,nxl-nbgp_local),   1, type_xz(grid_level), &
-                          psouth, 0, comm2d, req(1), ierr )
-          CALL MPI_IRECV( ar(nzb,nyn+1,nxl-nbgp_local), 1, type_xz(grid_level), &
-                          pnorth, 0, comm2d, req(2), ierr )
-!
-!--       Send rear boundary, receive front one (asynchronous)
-          CALL MPI_ISEND( ar(nzb,nyn-nbgp_local+1,nxl-nbgp_local), 1,          &
-                          type_xz(grid_level), pnorth, 1, comm2d, req(3), ierr )
-          CALL MPI_IRECV( ar(nzb,nys-nbgp_local,nxl-nbgp_local),   1,          &
-                          type_xz(grid_level), psouth, 1, comm2d, req(4), ierr )
-
-          CALL MPI_WAITALL( 4, req, wait_stat, ierr )
+!
+!--       In case of background communication switched on, exchange is done
+!--       either along x or along y
+          IF ( send_receive == 'lr'  .OR.  send_receive == 'al' )  THEN
+
+             IF ( .NOT. sendrecv_in_background )  THEN
+                req(1:4)  = 0
+                req_count = 0
+             ENDIF
+
+!
+!--          Send front boundary, receive rear one (asynchronous)
+             CALL MPI_ISEND( ar(nzb,nys,nxl-nbgp_local),   1, type_xz(grid_level), &
+                             psouth, req_count, comm2d, req(req_count+1), ierr )
+             CALL MPI_IRECV( ar(nzb,nyn+1,nxl-nbgp_local), 1, type_xz(grid_level), &
+                             pnorth, req_count, comm2d, req(req_count+2), ierr )
+!
+!--          Send rear boundary, receive front one (asynchronous)
+             CALL MPI_ISEND( ar(nzb,nyn-nbgp_local+1,nxl-nbgp_local), 1,          &
+                             type_xz(grid_level), pnorth, req_count+1, comm2d,    &
+                             req(req_count+3), ierr )
+             CALL MPI_IRECV( ar(nzb,nys-nbgp_local,nxl-nbgp_local),   1,          &
+                             type_xz(grid_level), psouth, req_count+1, comm2d,    &
+                             req(req_count+4), ierr )
+
+             IF ( .NOT. sendrecv_in_background )  THEN
+                CALL MPI_WAITALL( 4, req, wait_stat, ierr )
+             ELSE
+                req_count = req_count + 4
+             ENDIF
+
+          ENDIF
 
        ENDIF
