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

Last change on this file since 2789 was 2718, checked in by maronga, 7 years ago

deleting of deprecated files; headers updated where needed

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