source: palm/trunk/SOURCE/pmc_handle_communicator.f90 @ 1802

Last change on this file since 1802 was 1798, checked in by raasch, 9 years ago

last commit documented

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