source: palm/trunk/SOURCE/pmc_handle_communicator_mod.f90 @ 4640

Last change on this file since 4640 was 4629, checked in by raasch, 4 years ago

support for MPI Fortran77 interface (mpif.h) removed

  • Property svn:keywords set to Id
File size: 18.0 KB
Line 
1!> @file pmc_handle_communicator_mod.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2020 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: pmc_handle_communicator_mod.f90 4629 2020-07-29 09:37:56Z suehring $
27! support for MPI Fortran77 interface (mpif.h) removed
28!
29! 4360 2020-01-07 11:25:50Z suehring
30! Corrected "Former revisions" section
31!
32! 3888 2019-04-12 09:18:10Z hellstea
33! Missing MPI_BCAST of anterpolation_buffer_width added.
34!
35! 3885 2019-04-11 11:29:34Z kanani
36! Changes related to global restructuring of location messages and introduction
37! of additional debug messages
38!
39! 3819 2019-03-27 11:01:36Z hellstea
40! Adjustable anterpolation buffer introduced on all nest boundaries, it is controlled
41! by the new nesting_parameters parameter anterpolation_buffer_width.
42!
43! 3655 2019-01-07 16:51:22Z knoop
44! nestpar renamed to nesting_parameters
45!
46! 1762 2016-02-25 12:31:13Z hellstea
47! Initial revision by K. Ketelsen
48!
49! Description:
50! ------------
51! Handle MPI communicator in PALM model coupler
52!-------------------------------------------------------------------------------!
53 MODULE PMC_handle_communicator
54#if defined( __parallel )
55    USE kinds
56
57    USE MPI
58
59    USE pmc_general,                                                            &
60        ONLY: pmc_status_ok, pmc_status_error, pmc_max_models
61    USE control_parameters,                                                     &
62        ONLY: message_string
63
64    IMPLICIT NONE
65
66
67    TYPE pmc_layout
68
69       CHARACTER(LEN=32) ::  name
70
71       INTEGER  ::  id            !<
72       INTEGER  ::  parent_id     !<
73       INTEGER  ::  npe_total     !<
74
75       REAL(wp) ::  lower_left_x  !<
76       REAL(wp) ::  lower_left_y  !<
77
78    END TYPE pmc_layout
79
80    PUBLIC  pmc_status_ok, pmc_status_error
81
82    INTEGER, PARAMETER, PUBLIC ::  pmc_error_npes        = 1  !< illegal number of processes
83    INTEGER, PARAMETER, PUBLIC ::  pmc_namelist_error    = 2  !< error(s) in nesting_parameters namelist
84    INTEGER, PARAMETER, PUBLIC ::  pmc_no_namelist_found = 3  !< no couple layout namelist found
85
86    INTEGER ::  m_world_comm  !< global nesting communicator
87    INTEGER ::  m_my_cpl_id   !< coupler id of this model
88    INTEGER ::  m_parent_id   !< coupler id of parent of this model
89    INTEGER ::  m_ncpl        !< number of couplers given in nesting_parameters namelist
90
91    TYPE(pmc_layout), PUBLIC, DIMENSION(pmc_max_models) ::  m_couplers  !< information of all couplers
92
93    INTEGER, PUBLIC ::  m_model_comm          !< communicator of this model
94    INTEGER, PUBLIC ::  m_to_parent_comm      !< communicator to the parent
95    INTEGER, PUBLIC ::  m_world_rank          !<
96    INTEGER         ::  m_world_npes          !<
97    INTEGER, PUBLIC ::  m_model_rank          !<
98    INTEGER, PUBLIC ::  m_model_npes          !<
99    INTEGER         ::  m_parent_remote_size  !< number of processes in the parent model
100    INTEGER         ::  peer_comm             !< peer_communicator for inter communicators
101
102    INTEGER, DIMENSION(pmc_max_models), PUBLIC ::  m_to_child_comm    !< communicator to the child(ren)
103    INTEGER, DIMENSION(:), POINTER, PUBLIC ::  pmc_parent_for_child   !<
104
105
106    INTERFACE pmc_is_rootmodel
107       MODULE PROCEDURE pmc_is_rootmodel
108    END INTERFACE pmc_is_rootmodel
109
110    INTERFACE pmc_get_model_info
111       MODULE PROCEDURE pmc_get_model_info
112    END INTERFACE pmc_get_model_info
113
114    PUBLIC pmc_get_model_info, pmc_init_model, pmc_is_rootmodel
115
116 CONTAINS
117
118 SUBROUTINE pmc_init_model( comm, nesting_datatransfer_mode, nesting_mode,      &
119                            anterpolation_buffer_width, pmc_status )
120
121    USE control_parameters,                                                     &
122        ONLY:  message_string
123
124    USE pegrid,                                                                 &
125        ONLY:  myid
126
127      IMPLICIT NONE
128
129    CHARACTER(LEN=8), INTENT(INOUT) ::  nesting_mode               !<
130    CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_datatransfer_mode  !<
131
132    INTEGER, INTENT(INOUT) ::  anterpolation_buffer_width          !< Boundary buffer width for anterpolation
133    INTEGER, INTENT(INOUT) ::  comm        !<
134    INTEGER, INTENT(INOUT) ::  pmc_status  !<
135
136    INTEGER ::  childcount     !<
137    INTEGER ::  i              !<
138    INTEGER ::  ierr           !<
139    INTEGER ::  istat          !<
140    INTEGER ::  m_my_cpl_rank  !<
141    INTEGER ::  tag            !<
142
143    INTEGER, DIMENSION(pmc_max_models)   ::  activeparent  ! I am active parent for this child ID
144    INTEGER, DIMENSION(pmc_max_models+1) ::  start_pe
145
146    pmc_status   = pmc_status_ok
147    comm         = -1
148    m_world_comm = MPI_COMM_WORLD
149    m_my_cpl_id  = -1
150    childcount   =  0
151    activeparent = -1
152    start_pe(:)  =  0
153
154    CALL MPI_COMM_RANK( MPI_COMM_WORLD, m_world_rank, istat )
155    CALL MPI_COMM_SIZE( MPI_COMM_WORLD, m_world_npes, istat )
156!
157!-- Only process 0 of root model reads
158    IF ( m_world_rank == 0 )  THEN
159
160       CALL read_coupling_layout( nesting_datatransfer_mode, nesting_mode,      &
161                                  anterpolation_buffer_width, pmc_status )
162
163       IF ( pmc_status /= pmc_no_namelist_found  .AND.                          &
164            pmc_status /= pmc_namelist_error )                                  &
165       THEN
166!
167!--       Calculate start PE of every model
168          start_pe(1) = 0
169          DO  i = 2, m_ncpl+1
170             start_pe(i) = start_pe(i-1) + m_couplers(i-1)%npe_total
171          ENDDO
172
173!
174!--       The sum of numbers of processes requested by all the domains
175!--       must be equal to the total number of processes of the run
176          IF ( start_pe(m_ncpl+1) /= m_world_npes )  THEN
177             WRITE ( message_string, '(2A,I6,2A,I6,A)' )                        &
178                             'nesting-setup requires different number of ',     &
179                             'MPI procs (', start_pe(m_ncpl+1), ') than ',      &
180                             'provided (', m_world_npes,')'
181             CALL message( 'pmc_init_model', 'PA0229', 3, 2, 0, 6, 0 )
182          ENDIF
183
184       ENDIF
185
186    ENDIF
187!
188!-- Broadcast the read status. This synchronises all other processes with
189!-- process 0 of the root model. Without synchronisation, they would not
190!-- behave in the correct way (e.g. they would not return in case of a
191!-- missing NAMELIST).
192    CALL MPI_BCAST( pmc_status, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
193
194    IF ( pmc_status == pmc_no_namelist_found )  THEN
195!
196!--    Not a nested run; return the MPI_WORLD communicator
197       comm = MPI_COMM_WORLD
198       RETURN
199
200    ELSEIF ( pmc_status == pmc_namelist_error )  THEN
201!
202!--    Only the root model gives the error message. Others are aborted by the
203!--    message-routine with MPI_ABORT. Must be done this way since myid and
204!--    comm2d have not yet been assigned at this point.
205       IF ( m_world_rank == 0 )  THEN
206          message_string = 'errors in \$nesting_parameters'
207          CALL message( 'pmc_init_model', 'PA0223', 3, 2, 0, 6, 0 )
208       ENDIF
209
210    ENDIF
211
212    CALL MPI_BCAST( m_ncpl,          1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
213    CALL MPI_BCAST( start_pe, m_ncpl+1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
214!
215!-- Broadcast coupling layout
216    DO  i = 1, m_ncpl
217       CALL MPI_BCAST( m_couplers(i)%name, LEN( m_couplers(i)%name ),           &
218                       MPI_CHARACTER, 0, MPI_COMM_WORLD, istat )
219       CALL MPI_BCAST( m_couplers(i)%id,           1, MPI_INTEGER, 0,           &
220                       MPI_COMM_WORLD, istat )
221       CALL MPI_BCAST( m_couplers(i)%Parent_id,    1, MPI_INTEGER, 0,           &
222                       MPI_COMM_WORLD, istat )
223       CALL MPI_BCAST( m_couplers(i)%npe_total,    1, MPI_INTEGER, 0,           &
224                       MPI_COMM_WORLD, istat )
225       CALL MPI_BCAST( m_couplers(i)%lower_left_x, 1, MPI_REAL,    0,           &
226                       MPI_COMM_WORLD, istat )
227       CALL MPI_BCAST( m_couplers(i)%lower_left_y, 1, MPI_REAL,    0,           &
228                       MPI_COMM_WORLD, istat )
229    ENDDO
230    CALL MPI_BCAST( nesting_mode, LEN( nesting_mode ), MPI_CHARACTER, 0,        &
231                    MPI_COMM_WORLD, istat )
232    CALL MPI_BCAST( nesting_datatransfer_mode, LEN(nesting_datatransfer_mode),  &
233                    MPI_CHARACTER, 0, MPI_COMM_WORLD, istat )
234    CALL MPI_BCAST( anterpolation_buffer_width, 1, MPI_INT, 0, MPI_COMM_WORLD,  &
235                    istat )
236!
237!-- Assign global MPI processes to individual models by setting the couple id
238    DO  i = 1, m_ncpl
239       IF ( m_world_rank >= start_pe(i)  .AND.  m_world_rank < start_pe(i+1) )  &
240       THEN
241          m_my_cpl_id = i
242          EXIT
243       ENDIF
244    ENDDO
245    m_my_cpl_rank = m_world_rank - start_pe(i)
246!
247!-- MPI_COMM_WORLD is the communicator for ALL models (MPI-1 approach).
248!-- The communictors for the individual models as created by MPI_COMM_SPLIT.
249!-- The color of the model is represented by the coupler id
250    CALL MPI_COMM_SPLIT( MPI_COMM_WORLD, m_my_cpl_id, m_my_cpl_rank, comm,      &
251                         istat )
252!
253!-- Get size and rank of the model running on this process
254    CALL  MPI_COMM_RANK( comm, m_model_rank, istat )
255    CALL  MPI_COMM_SIZE( comm, m_model_npes, istat )
256!
257!-- Broadcast (from process 0) the parent id and id of every model
258    DO  i = 1, m_ncpl
259       CALL MPI_BCAST( m_couplers(i)%parent_id, 1, MPI_INTEGER, 0,              &
260                       MPI_COMM_WORLD, istat )
261       CALL MPI_BCAST( m_couplers(i)%id,        1, MPI_INTEGER, 0,              &
262                       MPI_COMM_WORLD, istat )
263    ENDDO
264!
265!-- Save the current model communicator for pmc internal use
266    m_model_comm = comm
267
268!
269!-- Create intercommunicator between the parent and children.
270!-- MPI_INTERCOMM_CREATE creates an intercommunicator between 2 groups of
271!-- different colors.
272!-- The grouping was done above with MPI_COMM_SPLIT.
273!-- A duplicate of MPI_COMM_WORLD is created and used as peer communicator
274!-- (peer_comm) for MPI_INTERCOMM_CREATE.
275    CALL MPI_COMM_DUP( MPI_COMM_WORLD, peer_comm, ierr ) 
276    DO  i = 2, m_ncpl
277       IF ( m_couplers(i)%parent_id == m_my_cpl_id )  THEN
278!
279!--       Identify all children models of the current model and create
280!--       inter-communicators to connect between the current model and its
281!--       children models.
282          tag = 500 + i
283          CALL MPI_INTERCOMM_CREATE( comm, 0, peer_comm, start_pe(i),           &
284                                     tag, m_to_child_comm(i), istat)
285          childcount = childcount + 1
286          activeparent(i) = 1
287       ELSEIF ( i == m_my_cpl_id)  THEN
288!
289!--       Create an inter-communicator to connect between the current
290!--       model and its parent model.   
291          tag = 500 + i
292          CALL MPI_INTERCOMM_CREATE( comm, 0, peer_comm,                        &
293                                     start_pe(m_couplers(i)%parent_id),         &
294                                     tag, m_to_parent_comm, istat )
295       ENDIF
296    ENDDO
297!
298!-- If I am a parent, count the number of children I have.
299!-- Although this loop is symmetric on all processes, the "activeparent" flag
300!-- is true (==1) on the respective individual process only.
301    ALLOCATE( pmc_parent_for_child(childcount+1) )
302
303    childcount = 0
304    DO  i = 2, m_ncpl
305       IF ( activeparent(i) == 1 )  THEN
306          childcount = childcount + 1
307          pmc_parent_for_child(childcount) = i
308       ENDIF
309    ENDDO
310!
311!-- Get the size of the parent model
312    IF ( m_my_cpl_id > 1 )  THEN
313       CALL MPI_COMM_REMOTE_SIZE( m_to_parent_comm, m_parent_remote_size,       &
314                                  istat )
315    ELSE
316!
317!--    The root model does not have a parent
318       m_parent_remote_size = -1
319    ENDIF
320!
321!-- Set myid to non-zero value except for the root domain. This is a setting
322!-- for the message routine which is called at the end of pmci_init. That
323!-- routine outputs messages for myid = 0, only. However, myid has not been
324!-- assigened so far, so that all processes of the root model would output a
325!-- message. To avoid this, set myid to some other value except for process 0
326!-- of the root domain.
327    IF ( m_world_rank /= 0 )  myid = 1
328
329 END SUBROUTINE PMC_init_model
330
331
332
333 SUBROUTINE pmc_get_model_info( comm_world_nesting, cpl_id, cpl_name,           &
334                                cpl_parent_id, lower_left_x, lower_left_y,      &
335                                ncpl, npe_total, request_for_cpl_id )
336!
337!-- Provide module private variables of the pmc for PALM
338
339    USE kinds
340
341    IMPLICIT NONE
342
343    CHARACTER(LEN=*), INTENT(OUT), OPTIONAL ::  cpl_name   !<
344
345    INTEGER, INTENT(IN), OPTIONAL ::  request_for_cpl_id   !<
346
347    INTEGER, INTENT(OUT), OPTIONAL ::  comm_world_nesting  !<
348    INTEGER, INTENT(OUT), OPTIONAL ::  cpl_id              !<
349    INTEGER, INTENT(OUT), OPTIONAL ::  cpl_parent_id       !<
350    INTEGER, INTENT(OUT), OPTIONAL ::  ncpl                !<
351    INTEGER, INTENT(OUT), OPTIONAL ::  npe_total           !<
352
353    INTEGER ::  requested_cpl_id                           !<
354
355    REAL(wp), INTENT(OUT), OPTIONAL ::  lower_left_x       !<
356    REAL(wp), INTENT(OUT), OPTIONAL ::  lower_left_y       !<
357
358!
359!-- Set the requested coupler id
360    IF ( PRESENT( request_for_cpl_id ) )  THEN
361       requested_cpl_id = request_for_cpl_id
362!
363!--    Check for allowed range of values
364       IF ( requested_cpl_id < 1  .OR.  requested_cpl_id > m_ncpl )  RETURN
365    ELSE
366       requested_cpl_id = m_my_cpl_id
367    ENDIF
368!
369!-- Return the requested information
370    IF ( PRESENT( comm_world_nesting )  )  THEN
371       comm_world_nesting = m_world_comm
372    ENDIF
373    IF ( PRESENT( cpl_id )        )  THEN
374       cpl_id = requested_cpl_id
375    ENDIF
376    IF ( PRESENT( cpl_parent_id ) )  THEN
377       cpl_parent_id = m_couplers(requested_cpl_id)%parent_id
378    ENDIF
379    IF ( PRESENT( cpl_name )      )  THEN
380       cpl_name = m_couplers(requested_cpl_id)%name
381    ENDIF
382    IF ( PRESENT( ncpl )          )  THEN
383       ncpl = m_ncpl
384    ENDIF
385    IF ( PRESENT( npe_total )     )  THEN
386       npe_total = m_couplers(requested_cpl_id)%npe_total
387    ENDIF
388    IF ( PRESENT( lower_left_x )  )  THEN
389       lower_left_x = m_couplers(requested_cpl_id)%lower_left_x
390    ENDIF
391    IF ( PRESENT( lower_left_y )  )  THEN
392       lower_left_y = m_couplers(requested_cpl_id)%lower_left_y
393    ENDIF
394
395 END SUBROUTINE pmc_get_model_info
396
397
398
399 LOGICAL function pmc_is_rootmodel( )
400
401    IMPLICIT NONE
402
403    pmc_is_rootmodel = ( m_my_cpl_id == 1 )
404
405 END FUNCTION pmc_is_rootmodel
406
407
408
409 SUBROUTINE read_coupling_layout( nesting_datatransfer_mode, nesting_mode,      &
410      anterpolation_buffer_width, pmc_status )
411
412    IMPLICIT NONE
413
414    CHARACTER(LEN=8), INTENT(INOUT) ::  nesting_mode
415    CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_datatransfer_mode
416   
417    INTEGER, INTENT(INOUT)      ::  anterpolation_buffer_width     !< Boundary buffer width for anterpolation
418    INTEGER(iwp), INTENT(INOUT) ::  pmc_status
419    INTEGER(iwp)                ::  bad_llcorner
420    INTEGER(iwp)                ::  i
421    INTEGER(iwp)                ::  istat
422
423    TYPE(pmc_layout), DIMENSION(pmc_max_models) ::  domain_layouts
424
425    NAMELIST /nesting_parameters/  domain_layouts, nesting_datatransfer_mode,  &
426                                   nesting_mode, anterpolation_buffer_width
427   
428!
429!-- Initialize some coupling variables
430    domain_layouts(1:pmc_max_models)%id = -1
431    m_ncpl =   0
432
433    pmc_status = pmc_status_ok
434!
435!-- Open the NAMELIST-file and read the nesting layout
436    CALL check_open( 11 )
437    READ ( 11, nesting_parameters, IOSTAT=istat )
438!
439!-- Set filepointer to the beginning of the file. Otherwise process 0 will later
440!-- be unable to read the inipar-NAMELIST
441    REWIND ( 11 )
442
443    IF ( istat < 0 )  THEN
444!
445!--    No nesting_parameters-NAMELIST found
446       pmc_status = pmc_no_namelist_found
447       RETURN
448    ELSEIF ( istat > 0 )  THEN
449!
450!--    Errors in reading nesting_parameters-NAMELIST
451       pmc_status = pmc_namelist_error
452       RETURN
453    ENDIF
454!
455!-- Output location message
456    CALL location_message( 'initialize communicators for nesting', 'start' )
457!
458!-- Assign the layout to the corresponding internally used variable m_couplers
459    m_couplers = domain_layouts
460!
461!-- Get the number of nested models given in the nesting_parameters-NAMELIST
462    DO  i = 1, pmc_max_models
463!
464!--    When id=-1 is found for the first time, the list of domains is finished
465       IF ( m_couplers(i)%id == -1  .OR.  i == pmc_max_models )  THEN
466          IF ( m_couplers(i)%id == -1 )  THEN
467             m_ncpl = i - 1
468             EXIT
469          ELSE
470             m_ncpl = pmc_max_models
471          ENDIF
472       ENDIF
473    ENDDO
474!
475!-- Make sure that all domains have equal lower left corner in case of vertical
476!-- nesting
477    IF ( nesting_mode == 'vertical' )  THEN
478       bad_llcorner = 0
479       DO  i = 1, m_ncpl
480          IF ( domain_layouts(i)%lower_left_x /= 0.0_wp .OR.                    &
481               domain_layouts(i)%lower_left_y /= 0.0_wp )  THEN
482             bad_llcorner = bad_llcorner + 1
483             domain_layouts(i)%lower_left_x = 0.0_wp
484             domain_layouts(i)%lower_left_y = 0.0_wp
485          ENDIF
486       ENDDO
487       IF ( bad_llcorner /= 0)  THEN
488          WRITE ( message_string, *)  'at least one dimension of lower ',       &
489                                      'left corner of one domain is not 0. ',   &
490                                      'All lower left corners were set to (0, 0)'
491          CALL message( 'read_coupling_layout', 'PA0427', 0, 0, 0, 6, 0 )
492       ENDIF
493    ENDIF
494
495    CALL location_message( 'initialize communicators for nesting', 'finished' )
496
497 END SUBROUTINE read_coupling_layout
498
499#endif
500 END MODULE pmc_handle_communicator
Note: See TracBrowser for help on using the repository browser.