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

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

last commit documented

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