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

Last change on this file since 4180 was 4180, checked in by scharf, 2 years ago

removed comments in 'Former revisions' section that are older than 01.01.2019

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