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

Last change on this file since 2000 was 2000, checked in by knoop, 5 years ago

Forced header and separation lines into 80 columns

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