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

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

Introduction of different data transfer modes; restart mechanism adjusted for nested runs; parameter consistency checks for nested runs; further formatting cleanup

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