Skip to content
Snippets Groups Projects
communication.f90 54.4 KiB
Newer Older
  • Learn to ignore specific revisions
  •         if (LUseO) call mpi_isend(a%s(0,yoffset,1),blocklength,                &
    
                           MPI_DOUBLE_PRECISION,neighbour_w_rank,sendtag,   &
                           MPI_COMM_HORIZ, requests_ew(4), ierr)
    
            recvtag = 1011
            if (LUseT) call mpi_isend(a%st(1,0,0),1,                &
                           halo_wet(level,m),neighbour_w_rank,sendtag,   &
                           MPI_COMM_HORIZ, requests_ewT(4), ierr)
    
            ! Wait for East <-> West communication to complete
            if (halo_size == 1) then
              ! Wait for North <-> South communication to complete
    
              if (LUseO) call mpi_waitall(4,requests_ns, MPI_STATUSES_IGNORE, ierr)
              if (LUseT) call mpi_waitall(4,requests_nsT, MPI_STATUSES_IGNORE, ierr)
    
            if (LUseO) call mpi_waitall(4,requests_ew, MPI_STATUSES_IGNORE, ierr)
            if (LUseT) call mpi_waitall(4,requests_ewT, MPI_STATUSES_IGNORE, ierr)
    
          end if
          if (comm_measuretime) then
            call finish_timer(t_haloswap(level,m))
          end if
        end if
    
      end subroutine haloswap_mnh
    
    1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
    !==================================================================
      subroutine haloswap(level,m, &  ! multigrid- and processor- level
                          a)          ! data field
        implicit none
        integer, intent(in) :: level
        integer, intent(in) :: m
        type(scalar3d), intent(inout) :: a
        integer :: a_n  ! horizontal grid size
        integer :: nz   ! vertical grid size
        integer, dimension(2) :: p_horiz
        integer :: stepsize
        integer :: ierr, rank, sendtag, recvtag
        integer :: stat(MPI_STATUS_SIZE)
        integer :: halo_size
        integer :: neighbour_n_rank
        integer :: neighbour_s_rank
        integer :: neighbour_e_rank
        integer :: neighbour_w_rank
        integer :: yoffset, blocklength
        integer, dimension(4) :: requests_ns
        integer, dimension(4) :: requests_ew
    
        halo_size = comm_param%halo_size
    
        ! Do nothing if we are only using one processor
        if (m > 0) then
          if (comm_measuretime) then
            call start_timer(t_haloswap(level,m))
          end if
          a_n = a%ix_max-a%ix_min+1
          nz = a%grid_param%nz
          stepsize = 2**(pproc-m)
    
          ! Work out rank, only execute on relevant processes
          call mpi_comm_rank(MPI_COMM_HORIZ, rank, ierr)
          call mpi_cart_coords(MPI_COMM_HORIZ,rank,dim_horiz,p_horiz,ierr)
    
          ! Work out ranks of neighbours
          ! W -> E
          call mpi_cart_shift(MPI_COMM_HORIZ,1, stepsize, &
                              neighbour_w_rank,neighbour_e_rank,ierr)
          ! N -> S
          call mpi_cart_shift(MPI_COMM_HORIZ,0, stepsize, &
                              neighbour_n_rank,neighbour_s_rank,ierr)
          if ( (stepsize == 1) .or.                   &
             (  (mod(p_horiz(1),stepsize) == 0) .and. &
                (mod(p_horiz(2),stepsize) == 0) ) ) then
            if (halo_size == 1) then
              ! Do not include corners in send/recv
              yoffset = 1
              blocklength = a_n*(nz+2)*halo_size
            else
              yoffset = 1-halo_size
              blocklength = (a_n+2*halo_size)*(nz+2)*halo_size
            end if
            ! Receive from north
            recvtag = 2
            call mpi_irecv(a%s(0,0-(halo_size-1),1),1,                  &
                           halo_ns(level,m),neighbour_n_rank,recvtag,   &
                           MPI_COMM_HORIZ, requests_ns(1), ierr)
            ! Receive from south
            recvtag = 3
            call mpi_irecv(a%s(0,a_n+1,1),1,                            &
                           halo_ns(level,m),neighbour_s_rank,recvtag,   &
                           MPI_COMM_HORIZ, requests_ns(2), ierr)
            ! Send to south
            sendtag = 2
            call mpi_isend(a%s(0,a_n-(halo_size-1),1),1,                &
                           halo_ns(level,m),neighbour_s_rank,sendtag,   &
                           MPI_COMM_HORIZ, requests_ns(3), ierr)
            ! Send to north
            sendtag = 3
            call mpi_isend(a%s(0,1,1),1,                                &
                           halo_ns(level,m),neighbour_n_rank,sendtag,   &
                           MPI_COMM_HORIZ, requests_ns(4), ierr)
            if (halo_size > 1) then
              ! Wait for North <-> South communication to complete
              call mpi_waitall(4,requests_ns, MPI_STATUSES_IGNORE, ierr)
            end if
            ! Receive from west
            recvtag = 0
            call mpi_irecv(a%s(0,yoffset,0-(halo_size-1)),blocklength,    &
                           MPI_DOUBLE_PRECISION,neighbour_w_rank,recvtag, &
                           MPI_COMM_HORIZ, requests_ew(1), ierr)
            ! Receive from east
            sendtag = 1
            call mpi_irecv(a%s(0,yoffset,a_n+1),blocklength,          &
                           MPI_DOUBLE_PRECISION,neighbour_e_rank,recvtag, &
                           MPI_COMM_HORIZ, requests_ew(2), ierr)
            ! Send to east
            sendtag = 0
            call mpi_isend(a%s(0,yoffset,a_n-(halo_size-1)),blocklength,  &
                           MPI_DOUBLE_PRECISION,neighbour_e_rank,sendtag, &
                           MPI_COMM_HORIZ, requests_ew(3), ierr)
            ! Send to west
            recvtag = 1
            call mpi_isend(a%s(0,yoffset,1),blocklength,                &
                           MPI_DOUBLE_PRECISION,neighbour_w_rank,sendtag,   &
                           MPI_COMM_HORIZ, requests_ew(4), ierr)
            ! Wait for East <-> West communication to complete
            if (halo_size == 1) then
              ! Wait for North <-> South communication to complete
              call mpi_waitall(4,requests_ns, MPI_STATUSES_IGNORE, ierr)
            end if
            call mpi_waitall(4,requests_ew, MPI_STATUSES_IGNORE, ierr)
          end if
          if (comm_measuretime) then
            call finish_timer(t_haloswap(level,m))
          end if
        end if
      end subroutine haloswap
    !==================================================================
    !  Collect from a(level,m) and store on less processors
    !  in b(level,m-1)
    !
    !  Example for pproc-m = 1, i.e. stepsize = 2:
    !
    !   NW (0,0)  <--  NE (0,2)
    !
    !     ^      .
    !     !        .
    !                .
    !   SW (2,0)       SE (2,2) [send to 0,0]
    !
    !==================================================================
      subroutine collect(level,m, &    ! multigrid and processor level
                         a, &          ! IN: data on level (level,m)
                         b)            ! OUT: data on level (level,m-1)
        implicit none
        integer, intent(in) :: level
        integer, intent(in) :: m
        type(scalar3d), intent(in) :: a
        type(scalar3d), intent(inout) :: b
        integer :: a_n, b_n   ! horizontal grid sizes
        integer :: nz ! vertical grid size
        integer, dimension(2) :: p_horiz
        integer :: stepsize
        integer :: ierr, source_rank, dest_rank, rank, recv_tag, send_tag, iz
        logical :: corner_nw, corner_ne, corner_sw, corner_se
        integer :: recv_request(3)
    
        call start_timer(t_collect(m))
    
        stepsize = 2**(pproc-m)
    
        a_n = a%ix_max-a%ix_min+1
        b_n = b%ix_max-b%ix_min+1
        nz = b%grid_param%nz
    
        ! Work out rank, only execute on relevant processes
        call mpi_comm_rank(MPI_COMM_HORIZ, rank, ierr)
        ! Store position in process grid in in p_horiz
        ! Note we can NOT use cart_shift as we need diagonal neighburs as well
        call mpi_cart_coords(MPI_COMM_HORIZ,rank,dim_horiz,p_horiz,ierr)
    
        ! Ignore all processes that do not participate at this level
        if ( (stepsize .eq. 1) .or. ((mod(p_horiz(1),stepsize) == 0) .and. (mod(p_horiz(2),stepsize)) == 0)) then
          ! Determine position in local 2x2 block
          if (stepsize .eq. 1) then
            corner_nw = ((mod(p_horiz(1),2) == 0) .and. (mod(p_horiz(2),2) == 0))
            corner_ne = ((mod(p_horiz(1),2) == 0) .and. (mod(p_horiz(2),2) == 1))
            corner_sw = ((mod(p_horiz(1),2) == 1) .and. (mod(p_horiz(2),2) == 0))
            corner_se = ((mod(p_horiz(1),2) == 1) .and. (mod(p_horiz(2),2) == 1))
          else
            corner_nw = ((mod(p_horiz(1)/stepsize,2) == 0) .and. (mod(p_horiz(2)/stepsize,2) == 0))
            corner_ne = ((mod(p_horiz(1)/stepsize,2) == 0) .and. (mod(p_horiz(2)/stepsize,2) == 1))
            corner_sw = ((mod(p_horiz(1)/stepsize,2) == 1) .and. (mod(p_horiz(2)/stepsize,2) == 0))
            corner_se = ((mod(p_horiz(1)/stepsize,2) == 1) .and. (mod(p_horiz(2)/stepsize,2) == 1))
          end if
          ! NW receives from the other three processes
          if ( corner_nw ) then
            ! Receive from NE
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1),p_horiz(2)+stepsize/), &
                               source_rank, &
                               ierr)
            recv_tag = 0
            call mpi_irecv(b%s(0,1,b_n/2+1),1,sub_interior(level,m-1), source_rank, recv_tag, MPI_COMM_HORIZ, &
                               recv_request(1),ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Collect: receive from NE failed in mpi_irecv().")
    #endif
             ! Receive from SW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)+stepsize,p_horiz(2)/), &
                               source_rank, &
                               ierr)
            recv_tag = 1
            call mpi_irecv(b%s(0,b_n/2+1,1),1,sub_interior(level,m-1), source_rank, recv_tag, MPI_COMM_HORIZ, &
                               recv_request(2),ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Collect: receive from SW failed in mpi_irecv().")
    #endif
            ! Receive from SE
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)+stepsize,p_horiz(2)+stepsize/), &
                               source_rank, &
                               ierr)
            recv_tag = 2
            call mpi_irecv(b%s(0,b_n/2+1,b_n/2+1),1,sub_interior(level,m-1), source_rank, recv_tag, MPI_COMM_HORIZ, &
                               recv_request(3),ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Collect: receive from SE failed in mpi_irecv().")
    #endif
            ! Copy local data while waiting for data from other processes
            do iz=0,nz+1
              b%s(iz,1:a_n,1:a_n) = a%s(iz,1:a_n,1:a_n)
            end do
            ! Wait for receives to complete before proceeding
            call mpi_waitall(3,recv_request,MPI_STATUSES_IGNORE,ierr)
          end if
          if ( corner_ne ) then
            ! Send to NW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1),p_horiz(2)-stepsize/), &
                               dest_rank, &
                               ierr)
            send_tag = 0
            call mpi_send(a%s(0,1,1),1,interior(level,m),dest_rank,send_tag,MPI_COMM_HORIZ,ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Collect: send from NE failed in mpi_send().")
    #endif
          end if
          if ( corner_sw ) then
            ! Send to NW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)-stepsize,p_horiz(2)/), &
                               dest_rank, &
                               ierr)
            send_tag = 1
            call mpi_send(a%s(0,1,1),1,interior(level,m),dest_rank,send_tag,MPI_COMM_HORIZ,ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Collect: send from SW failed in mpi_send().")
    #endif
          end if
          if ( corner_se ) then
            ! send to NW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)-stepsize,p_horiz(2)-stepsize/), &
                               dest_rank, &
                               ierr)
            send_tag = 2
            call mpi_send(a%s(0,1,1),1,interior(level,m),dest_rank,send_tag,MPI_COMM_HORIZ,ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Collect: send from SE failed in mpi_send().")
    #endif
          end if
    
        end if
        call finish_timer(t_collect(m))
    
      end subroutine collect
    
    !==================================================================
    !  Distribute data in a(level,m-1) and store in b(level,m)
    !
    !  Example for p-m = 1, i.e. stepsize = 2:
    !
    !   NW (0,0)  -->  NE (2,0)
    !
    !     !      .
    !     v        .
    !                .
    !   SW (0,2)       SE (2,2) [receive from to 0,0]
    !==================================================================
      subroutine distribute(level,m, &  ! multigrid and processor level
                            a,       &  ! IN: Data on level (level,m-1)
                            b)          ! OUT: Data on level (level,m)
        implicit none
        integer, intent(in) :: level
        integer, intent(in) :: m
        type(scalar3d), intent(in) :: a
        type(scalar3d), intent(inout) :: b
        integer :: a_n, b_n   ! horizontal grid sizes
        integer :: nz ! vertical grid size
        integer, dimension(2) :: p_horiz
        integer :: stepsize
        integer :: ierr, source_rank, dest_rank, send_tag, recv_tag, rank, iz
        integer :: stat(MPI_STATUS_SIZE)
        integer :: send_request(3)
        logical :: corner_nw, corner_ne, corner_sw, corner_se
    
        call start_timer(t_distribute(m))
    
        stepsize = 2**(pproc-m)
    
        a_n = a%ix_max-a%ix_min+1
        b_n = b%ix_max-b%ix_min+1
        nz = a%grid_param%nz
    
        ! Work out rank, only execute on relevant processes
        call mpi_comm_rank(MPI_COMM_HORIZ, rank, ierr)
        call mpi_cart_coords(MPI_COMM_HORIZ,rank,dim_horiz,p_horiz,ierr)
    
        ! Ignore all processes that do not participate at this level
        if ( (stepsize .eq. 1) .or. ((mod(p_horiz(1),stepsize) == 0) .and. (mod(p_horiz(2),stepsize)) == 0)) then
          ! Work out coordinates in local 2 x 2 block
          if (stepsize .eq. 1) then
            corner_nw = ((mod(p_horiz(1),2) == 0) .and. (mod(p_horiz(2),2) == 0))
            corner_ne = ((mod(p_horiz(1),2) == 0) .and. (mod(p_horiz(2),2) == 1))
            corner_sw = ((mod(p_horiz(1),2) == 1) .and. (mod(p_horiz(2),2) == 0))
            corner_se = ((mod(p_horiz(1),2) == 1) .and. (mod(p_horiz(2),2) == 1))
          else
            corner_nw = ((mod(p_horiz(1)/stepsize,2) == 0) .and. (mod(p_horiz(2)/stepsize,2) == 0))
            corner_ne = ((mod(p_horiz(1)/stepsize,2) == 0) .and. (mod(p_horiz(2)/stepsize,2) == 1))
            corner_sw = ((mod(p_horiz(1)/stepsize,2) == 1) .and. (mod(p_horiz(2)/stepsize,2) == 0))
            corner_se = ((mod(p_horiz(1)/stepsize,2) == 1) .and. (mod(p_horiz(2)/stepsize,2) == 1))
          end if
          if ( corner_nw ) then
            ! (Asynchronous) send to NE
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1),p_horiz(2)+stepsize/), &
                               dest_rank, &
                               ierr)
    
            send_tag = 0
            call mpi_isend(a%s(0,1,a_n/2+1), 1,sub_interior(level,m-1),dest_rank, send_tag, &
                           MPI_COMM_HORIZ,send_request(1),ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Distribute: send to NE failed in mpi_isend().")
    #endif
            ! (Asynchronous) send to SW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)+stepsize,p_horiz(2)/), &
                               dest_rank, &
                               ierr)
            send_tag = 1
            call mpi_isend(a%s(0,a_n/2+1,1),1,sub_interior(level,m-1), dest_rank, send_tag, &
                           MPI_COMM_HORIZ, send_request(2), ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Distribute: send to SW failed in mpi_isend().")
    #endif
            ! (Asynchronous) send to SE
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)+stepsize,p_horiz(2)+stepsize/), &
                               dest_rank, &
                               ierr)
            send_tag = 2
            call mpi_isend(a%s(0,a_n/2+1,a_n/2+1),1,sub_interior(level,m-1), dest_rank, send_tag, &
                          MPI_COMM_HORIZ, send_request(3), ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Distribute: send to SE failed in mpi_isend().")
    #endif
            ! While sending, copy local data
            do iz=0,nz+1
              b%s(iz,1:b_n,1:b_n) = a%s(iz,1:b_n,1:b_n)
            end do
            ! Only proceed when async sends to complete
            call mpi_waitall(3, send_request, MPI_STATUSES_IGNORE, ierr)
          end if
          if ( corner_ne ) then
    
            ! Receive from NW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1),p_horiz(2)-stepsize/), &
                               source_rank, &
                               ierr)
            recv_tag = 0
            call mpi_recv(b%s(0,1,1),1,interior(level,m),source_rank,recv_tag,MPI_COMM_HORIZ,stat,ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Distribute: receive on NE failed in mpi_recv().")
    #endif
          end if
          if ( corner_sw ) then
            ! Receive from NW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)-stepsize,p_horiz(2)/), &
                               source_rank, &
                               ierr)
            recv_tag = 1
            call mpi_recv(b%s(0,1,1),1,interior(level,m),source_rank,recv_tag,MPI_COMM_HORIZ,stat,ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Distribute: receive on SW failed in mpi_recv().")
    #endif
          end if
          if ( corner_se ) then
            ! Receive from NW
            call mpi_cart_rank(MPI_COMM_HORIZ, &
                               (/p_horiz(1)-stepsize,p_horiz(2)-stepsize/), &
                               source_rank, &
                               ierr)
            recv_tag = 2
            call mpi_recv(b%s(0,1,1),1,interior(level,m),source_rank,recv_tag,MPI_COMM_HORIZ,stat,ierr)
    #ifndef NDEBUG
      if (ierr .ne. 0) &
        call fatalerror("Distribute: receive on NW failed in mpi_recv().")
    #endif
          end if
    
        end if
        call finish_timer(t_distribute(m))
    
      end subroutine distribute
    
    end module communication