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

Last change on this file since 1935 was 1933, checked in by hellstea, 8 years ago

last commit documented

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