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

Last change on this file since 2271 was 2271, checked in by sward, 7 years ago

error messages and numbers updated

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