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

Last change on this file since 1952 was 1939, checked in by hellstea, 8 years ago

last commit documented

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