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

Last change on this file since 2007 was 2001, checked in by knoop, 8 years ago

last commit documented

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