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

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

Bugfix, set filepointer to the beginning of the file after namelist read

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