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

Last change on this file since 2868 was 2841, checked in by knoop, 7 years ago

Bugfix: wrong placement of include 'mpif.h' corrected

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