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

Last change on this file since 1811 was 1809, checked in by raasch, 9 years ago

last commit documented

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