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

Last change on this file since 4381 was 4360, checked in by suehring, 4 years ago

Bugfix in output of time-averaged plant-canopy quanities; Output of plant-canopy data only where tall canopy is defined; land-surface model: fix wrong location strings; tests: update urban test case; all source code files: copyright update

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