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

Last change on this file since 2396 was 2280, checked in by suehring, 7 years ago

document changes

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