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

Last change on this file since 1779 was 1779, checked in by raasch, 8 years ago

pmc array management changed from linked list to sequential loop; further small changes and cosmetics for the pmc

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