00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 subroutine psmile_get_intersect (ninter, nmyint, nnull, &
00012 num_intersect_per_grid, &
00013 num_dummy_intersect_per_grid, &
00014 lastag, ierror)
00015
00016
00017
00018 use PRISM_constants
00019 use PSMILe_common, only : PSMILe_nullify_Enddef_search, num_req_types
00020 use PSMILe, dummy_interface => PSMILe_get_intersect
00021 implicit none
00022
00023
00024
00025 Integer, Intent (In) :: ninter
00026
00027
00028
00029
00030 Integer, Intent (In) :: nmyint
00031
00032
00033
00034
00035 Integer, Intent (In) :: nnull
00036
00037
00038
00039
00040 Integer, Intent (In) :: num_intersect_per_grid(:),
00041 num_dummy_intersect_per_grid(:)
00042
00043 Integer, Intent (In) :: lastag
00044
00045
00046
00047
00048
00049
00050 Integer, Intent (Out) :: ierror
00051
00052
00053
00054
00055
00056
00057
00058 Logical, Parameter :: new_search = .true.
00059
00060
00061
00062 Double Precision, parameter :: tol = 1d-6
00063
00064
00065
00066
00067
00068 Integer :: ipart
00069
00070
00071
00072 Integer :: i
00073 Type (Enddef_search) :: search
00074 Logical :: new_intersection
00075
00076
00077
00078
00079 Integer :: index
00080 Integer :: lstatus (MPI_STATUS_SIZE)
00081 #ifndef PRISM_with_MPI2
00082 Integer, Allocatable :: statuses (:, :)
00083 #endif
00084
00085
00086
00087 Integer :: dest, icomp, n
00088
00089 Integer :: fin_extra (2)
00090 Integer, Allocatable :: fin_requests (:)
00091
00092
00093
00094 Integer, Parameter :: nerrp = 3
00095 Integer :: ierrp (nerrp)
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 Character(len=len_cvs_string), save :: mycvs =
00162 '$Id: psmile_get_intersect.F90 2804 2010-12-07 10:07:10Z hanke $'
00163
00164
00165
00166 #ifdef VERBOSE
00167 print 9990, trim(ch_id), ninter, nmyint, nnull
00168
00169 call psmile_flushstd
00170 #endif /* VERBOSE */
00171
00172
00173
00174 ierror = 0
00175
00176 paction%n = 0
00177 paction%ninter = ninter
00178 paction%lastag = lastag
00179
00180 paction%nmyint = 0
00181
00182 paction%grid2receive = .false.
00183 allocate(paction%n_answer2recv_per_grid(Number_of_Grids_allocated))
00184 paction%n_answer2recv_per_grid(:) = num_intersect_per_grid(:) - num_dummy_intersect_per_grid(:)
00185 new_intersection = .true.
00186
00187
00188
00189
00190
00191 paction%n_answer = 0
00192
00193 paction%n_answer2recv = ninter - nnull
00194
00195
00196
00197
00198 paction%nloc_recv = 0
00199
00200
00201
00202 paction%n_selected = 0
00203
00204
00205
00206
00207 paction%n_fin = 0
00208 paction%n_fin2recv = 0
00209 do icomp = 1, n_act_comp
00210 paction%n_fin2recv = paction%n_fin2recv + comp_infos(icomp)%size
00211 end do
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 paction%nreq = num_req_types - 1 + paction%n_answer2recv
00223
00224 Allocate (paction%lrequest(paction%nreq), stat = ierror)
00225 if ( ierror /= 0 ) then
00226 ierrp (1) = paction%nreq
00227 ierror = PRISM_Error_Alloc
00228 call psmile_error ( ierror, 'paction%lrequest', &
00229 ierrp, 1, __FILE__, __LINE__ )
00230 return
00231 endif
00232
00233 if (paction%n_answer2recv > 0) then
00234 Allocate (paction%loc_messages(msgloc_size, paction%n_answer2recv), &
00235 stat = ierror)
00236 if ( ierror /= 0 ) then
00237 ierrp (1) = msgloc_size * paction%n_answer2recv
00238 ierror = PRISM_Error_Alloc
00239 call psmile_error ( ierror, 'paction%loc_messages', &
00240 ierrp, 1, __FILE__, __LINE__ )
00241 return
00242 endif
00243 end if
00244
00245 paction%lrequest (:) = MPI_REQUEST_NULL
00246
00247
00248
00249
00250
00251 if (paction%n_answer < paction%n_answer2recv) then
00252 call MPI_Irecv (paction%msgreq, nd_msgint, MPI_INTEGER, &
00253 MPI_ANY_SOURCE, reqtag, comm_psmile, &
00254 paction%lrequest(1), ierror)
00255 if ( ierror /= MPI_SUCCESS ) then
00256 ierrp (1) = ierror
00257 ierror = PRISM_Error_MPI
00258
00259 call psmile_error ( ierror, 'MPI_Irecv', &
00260 ierrp, 1, __FILE__, __LINE__ )
00261 return
00262 endif
00263 #ifdef DEBUG
00264 print *, ' Posting Irecv request(1) ', paction%lrequest(1), 'with tag ', reqtag
00265 #endif /* DEBUG */
00266 endif
00267
00268
00269
00270 if (paction%n_fin2recv > 0) then
00271 call MPI_Irecv (paction%msg_extra, nd_msgextra, MPI_INTEGER, &
00272 MPI_ANY_SOURCE, exttag, comm_psmile, &
00273 paction%lrequest(4), ierror)
00274
00275 if ( ierror /= MPI_SUCCESS ) then
00276 ierrp (1) = ierror
00277 ierror = PRISM_Error_MPI
00278
00279 call psmile_error ( ierror, 'MPI_Irecv', &
00280 ierrp, 1, __FILE__, __LINE__ )
00281 return
00282 endif
00283 #ifdef DEBUG
00284 print *, ' Posting Irecv request(4) ', paction%lrequest(4), 'with tag ', exttag
00285 #endif /* DEBUG */
00286 endif
00287
00288 call psmile_nullify_enddef_search(search)
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 do while ( paction%n < paction%ninter .or. &
00305 paction%n_answer < paction%n_answer2recv .or. &
00306 paction%nloc_recv < paction%n_answer2recv .or. &
00307 paction%n_selected > 0 .or. &
00308 paction%grid2receive )
00309
00310
00311
00312 if (paction%ninter > 0 .and. &
00313 paction%n < paction%ninter .and. &
00314 new_intersection) then
00315
00316
00317
00318 #ifdef PRISM_ASSERTION
00319 if (paction%lrequest(2) /= MPI_REQUEST_NULL) then
00320 call psmile_assert ( __FILE__, __LINE__, &
00321 'paction%lrequest(2) is not finished !')
00322 endif
00323 #endif
00324 #define CONS_REMAP_DEADLOCK_WORKAROUND
00325 #ifndef CONS_REMAP_DEADLOCK_WORKAROUND
00326 call MPI_Irecv (paction%msgint, nd_msgint, MPI_INTEGER, &
00327 MPI_ANY_SOURCE, paction%lastag, comm_psmile, &
00328 paction%lrequest(2), ierror)
00329 #else
00330 call MPI_Irecv (paction%msgint, nd_msgint, MPI_INTEGER, &
00331 maxval(paction%intersect_ranks), paction%lastag, comm_psmile, &
00332 paction%lrequest(2), ierror)
00333 paction%intersect_ranks(maxloc(paction%intersect_ranks)) = -1
00334 #endif
00335
00336 if ( ierror /= MPI_SUCCESS ) then
00337 ierrp (1) = ierror
00338 ierror = PRISM_Error_MPI
00339
00340 call psmile_error ( ierror, 'MPI_Irecv', &
00341 ierrp, 1, __FILE__, __LINE__ )
00342 return
00343 endif
00344 #ifdef DEBUG
00345 print *, ' Posting Irecv request(2) ', paction%lrequest(2), 'with tag ', paction%lastag
00346 #endif /* DEBUG */
00347 endif
00348
00349
00350
00351
00352 #ifdef DEBUG
00353 print 9960, trim(ch_id), paction%n, paction%ninter, &
00354 paction%n_answer, paction%n_answer2recv, &
00355 paction%grid2receive, &
00356 paction%nloc_recv, paction%n_answer2recv, &
00357 paction%n_selected
00358
00359 call psmile_flushstd
00360 do i = 1, paction%nreq
00361 print *, ' list of requests ', i, paction%lrequest(i)
00362 enddo
00363 call psmile_flushstd
00364 #endif /* DEBUG */
00365 call MPI_Waitany (paction%nreq, paction%lrequest, index, lstatus, ierror)
00366
00367 if ( ierror /= MPI_SUCCESS ) then
00368 ierrp (1) = ierror
00369 ierrp (2) = lstatus (MPI_SOURCE)
00370 ierrp (3) = lstatus (MPI_TAG)
00371
00372 ierror = PRISM_Error_MPI
00373
00374 print *, 'lstatus', lstatus
00375 call psmile_error ( ierror, 'MPI_Waitany', &
00376 ierrp, 3, __FILE__, __LINE__ )
00377 return
00378 endif
00379
00380 #ifdef DEBUG
00381 print *, trim(ch_id), ": n, ninter, sender, index", &
00382 paction%n, paction%ninter, lstatus (MPI_SOURCE), index
00383 call psmile_flushstd
00384 #endif /* DEBUG */
00385
00386 if (index == MPI_UNDEFINED) then
00387 #ifdef PRISM_ASSERTION
00388 call psmile_assert ( __FILE__, __LINE__, &
00389 'request list is empty')
00390 #endif /* PRISM_ASSERTION */
00391 exit
00392 endif
00393
00394
00395
00396 if (index == 3) then
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406 #ifdef PRISM_ASSERTION
00407 if (lstatus(MPI_TAG) /= grdtag) then
00408 call psmile_assert ( __FILE__, __LINE__, &
00409 'wrong message tag; must be grdtag')
00410 endif
00411 #endif
00412
00413 #ifdef PRISM_with_MPI2
00414 call MPI_Waitall ((ndim_3d+2)*search%npart-1, paction%recv_req(2:), &
00415 MPI_STATUSES_IGNORE, ierror)
00416 #else
00417 Allocate (statuses (MPI_STATUS_SIZE,search%npart*(ndim_3d+2)-1), &
00418 stat = ierror)
00419 if ( ierror /= 0 ) then
00420 ierrp (1) = MPI_STATUS_SIZE * (search%npart*(ndim_3d+2)-1)
00421 ierror = PRISM_Error_Alloc
00422 call psmile_error ( ierror, 'statuses', &
00423 ierrp, 1, __FILE__, __LINE__ )
00424 return
00425 endif
00426
00427 call MPI_Waitall ((ndim_3d+2)*search%npart-1, paction%recv_req(2:), &
00428 statuses, ierror)
00429 #endif
00430 if ( ierror /= MPI_SUCCESS ) then
00431 ierrp (1) = ierror
00432
00433 ierror = PRISM_Error_MPI
00434
00435 call psmile_error ( ierror, 'MPI_Waitall', &
00436 ierrp, 1, __FILE__, __LINE__ )
00437 return
00438 endif
00439
00440 #ifndef PRISM_with_MPI2
00441 Deallocate (statuses)
00442 #endif
00443 Deallocate (paction%recv_req)
00444
00445 paction%grid2receive = .false.
00446
00447
00448
00449 call psmile_search_donor_cells (search, tol, ierror )
00450 if (ierror > 0) return
00451
00452
00453
00454
00455
00456
00457
00458
00459 if (search%datatype == MPI_REAL) then
00460 do ipart = 1, search%npart
00461 do i = 1, ndim_3d
00462 Deallocate (search%search_real(i, ipart)%vector)
00463 end do
00464 end do
00465
00466 Deallocate (search%search_real)
00467
00468 else if (search%datatype == MPI_DOUBLE_PRECISION) then
00469 do ipart = 1, search%npart
00470 do i = 1, ndim_3d
00471 Deallocate (search%search_dble(i, ipart)%vector)
00472 end do
00473 end do
00474
00475 Deallocate (search%search_dble)
00476
00477 #if defined ( PRISM_QUAD_TYPE )
00478 else if (search%datatype == MPI_REAL16) then
00479 do ipart = 1, search%npart
00480 do i = 1, ndim_3d
00481 Deallocate (search%search_quad(i, ipart)%vector)
00482 end do
00483 end do
00484
00485 Deallocate (search%search_quad)
00486 #endif
00487 endif
00488
00489 if (Associated(search%search_mask)) then
00490 do ipart = 1, search%npart
00491 Deallocate(search%search_mask(ipart)%vector)
00492 end do
00493
00494 Deallocate (search%search_mask)
00495 endif
00496
00497 if (Associated(search%global_index)) then
00498 do ipart = 1, search%npart
00499 Deallocate(search%global_index(ipart)%vector)
00500 end do
00501 Deallocate (search%global_index)
00502 endif
00503
00504 Deallocate (search%msgint)
00505 Deallocate (search%dims)
00506 Deallocate (search%shape)
00507 Deallocate (search%range)
00508
00509
00510
00511 new_intersection = .true.
00512
00513 else
00514
00515 new_intersection = .false.
00516
00517
00518
00519 call psmile_enddef_action (search, index, lstatus, ierror)
00520 if (ierror > 0) return
00521 endif
00522
00523 end do
00524
00525
00526
00527
00528
00529
00530 if (paction%n_fin2recv > 0) then
00531
00532 Allocate (fin_requests (paction%n_fin2recv), stat = ierror)
00533 if ( ierror /= 0 ) then
00534 ierrp (1) = paction%n_fin2recv
00535 ierror = PRISM_Error_Alloc
00536 call psmile_error ( ierror, 'fin_requests', &
00537 ierrp, 1, __FILE__, __LINE__ )
00538 return
00539 endif
00540
00541 fin_extra (1) = PSMILe_Finalize_extra_search
00542 fin_extra (2) = 1
00543
00544 n = 0
00545 do icomp = 1, n_act_comp
00546 do i = 1, comp_infos(icomp)%size
00547 dest = comp_infos(icomp)%psmile_ranks(i)
00548
00549 n = n + 1
00550
00551 call MPI_Isend (fin_extra, 2, MPI_INTEGER, &
00552 dest, exttag, comm_psmile, &
00553 fin_requests(n), ierror)
00554
00555 if ( ierror /= MPI_SUCCESS ) then
00556 ierrp (1) = ierror
00557 ierrp (2) = dest
00558 ierrp (3) = exttag
00559 ierror = PRISM_Error_Send
00560
00561 call psmile_error ( ierror, 'MPI_Isend', &
00562 ierrp, 3, __FILE__, __LINE__ )
00563 return
00564 endif
00565 #ifdef DEBUGX
00566 print *, ' Posting Isend request ', fin_requests(n), &
00567 ' with tag ', exttag, ' to dest ', dest
00568 call psmile_flushstd
00569 #endif /* DEBUGX */
00570 end do
00571 end do
00572
00573
00574
00575
00576
00577
00578
00579 do while (paction%n_fin < paction%n_fin2recv .or. &
00580 paction%n_selected > 0)
00581 #ifdef DEBUGX
00582 print 9950, trim(ch_id), paction%n_fin, paction%n_fin2recv
00583 do i = 4, 5
00584 print *, ' list of request ', i, paction%lrequest(i)
00585 enddo
00586 call psmile_flushstd
00587 #endif /* DEBUGX */
00588 call MPI_Waitany (2, paction%lrequest(4:5), index, lstatus, ierror)
00589
00590 if ( ierror /= MPI_SUCCESS ) then
00591 ierrp (1) = ierror
00592 ierrp (2) = lstatus (MPI_SOURCE)
00593 ierrp (3) = lstatus (MPI_TAG)
00594
00595 ierror = PRISM_Error_MPI
00596
00597 print *, 'lstatus', lstatus
00598 call psmile_error ( ierror, 'MPI_Waitany', &
00599 ierrp, 3, __FILE__, __LINE__ )
00600 return
00601 endif
00602 #ifdef DEBUGX
00603 print *, ' returned index ', index
00604 #endif /* DEBUGX */
00605 index = index + (4-1)
00606
00607 call psmile_enddef_action (search, index, lstatus, ierror)
00608 if (ierror > 0) return
00609
00610 enddo
00611
00612
00613
00614 #ifdef PRISM_with_MPI2
00615 call MPI_Waitall (paction%n_fin2recv, fin_requests, &
00616 MPI_STATUSES_IGNORE, ierror)
00617 #else
00618 Allocate (statuses(MPI_STATUS_SIZE, paction%n_fin2recv), &
00619 stat = ierror)
00620 if (ierror > 0) then
00621 ierrp (1) = ierror
00622 ierrp (2) = paction%n_fin2recv * MPI_STATUS_SIZE
00623
00624 ierror = PRISM_Error_Alloc
00625 call psmile_error ( ierror, 'statuses', &
00626 ierrp, 2, __FILE__, __LINE__ )
00627 return
00628 endif
00629
00630 call MPI_Waitall (paction%n_fin2recv, fin_requests, statuses, ierror)
00631
00632 Deallocate (statuses)
00633 #endif
00634
00635 if ( ierror /= MPI_SUCCESS ) then
00636 ierrp (1) = ierror
00637
00638 ierror = PRISM_Error_MPI
00639
00640 call psmile_error ( ierror, 'MPI_Waitall', &
00641 ierrp, 1, __FILE__, __LINE__ )
00642 return
00643 endif
00644
00645 Deallocate (fin_requests)
00646
00647 endif
00648
00649
00650
00651
00652
00653 if (paction%n_answer2recv > 0) then
00654 Deallocate (paction%loc_messages)
00655 endif
00656
00657 Deallocate (paction%lrequest)
00658 Deallocate (paction%n_answer2recv_per_grid)
00659
00660
00661
00662 #ifdef DEBUG
00663
00664
00665
00666 do i = 1, Number_of_Methods_allocated
00667 if (Methods(i)%status == PSMILe_status_defined) then
00668 call psmile_print_send_info (i, 0, 'end of psmile_get_intersect')
00669 endif
00670 end do
00671 #endif /* DEBUG */
00672
00673 #ifdef VERBOSE
00674 print 9980, trim(ch_id), ierror
00675 call psmile_flushstd
00676 #endif /* VERBOSE */
00677
00678
00679
00680
00681
00682 #ifdef VERBOSE
00683
00684 9990 format (1x, a, ': psmile_get_intersect: ninter', i4, &
00685 ', nmyint', i4, ', nnull', i4)
00686 9980 format (1x, a, ': psmile_get_intersect: eof ierror =', i3)
00687
00688 #endif /* VERBOSE */
00689
00690 #ifdef DEBUG
00691 9960 format (1x, a, ': psmile_get_intersect: calling waitany: n, ninter', 2i4, ';',&
00692 /2x, 'n_answer, n_answer2recv', 2i4, &
00693 '; grid2receive ', l1, &
00694 '; nloc_recv, nreq-num_req_types+1', 2i4, &
00695 '; n_selected', i4)
00696 9950 format (1x, a, ': psmile_get_intersect: calling wait: n_fin, n_fin2recv', 2i4)
00697 #endif /* DEBUG */
00698 end subroutine PSMILe_get_intersect