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

Last change on this file since 1827 was 1818, checked in by maronga, 9 years ago

last commit documented / copyright update

  • Property svn:keywords set to Id
File size: 16.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-2016 Leibniz Universitaet Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: pmc_handle_communicator.f90 1818 2016-04-06 15:53:27Z maronga $
27!
28! 1808 2016-04-05 19:44:00Z raasch
29! MPI module used by default on all machines
30!
31! 1797 2016-03-21 16:50:28Z raasch
32! introduction of different datatransfer modes,
33! export of comm_world_nesting
34!
35! 1791 2016-03-11 10:41:25Z raasch
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
40!
41! 1786 2016-03-08 05:49:27Z raasch
42! Bugfix: nesting_mode is broadcast now
43!
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!
48! 1764 2016-02-28 12:45:19Z raasch
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
55!
56! 1762 2016-02-25 12:31:13Z hellstea
57! Initial revision by K. Ketelsen
58!
59! Description:
60! ------------
61! Handle MPI communicator in PALM model coupler
62!------------------------------------------------------------------------------!
63
64#if defined( __parallel )
65    USE kinds
66
67#if defined( __mpifh )
68    INCLUDE "mpif.h"
69#else
70    USE MPI
71#endif
72
73   USE pmc_general,                                                            &
74       ONLY: pmc_status_ok, pmc_status_error, pmc_max_modell
75
76   IMPLICIT NONE
77
78   TYPE pmc_layout
79
80      CHARACTER(len=32) ::  name
81
82      INTEGER  ::  id
83      INTEGER  ::  parent_id
84      INTEGER  ::  npe_total
85
86      REAL(wp) ::  lower_left_x
87      REAL(wp) ::  lower_left_y
88
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
97   ! Coupler Setup
98
99   INTEGER                                    :: m_world_comm !global nesting communicator
100   INTEGER                                    :: m_my_CPL_id  !Coupler id of this model
101   INTEGER                                    :: m_Parent_id  !Coupler id of parent of this model
102   INTEGER                                    :: m_ncpl       !Number of Couplers in layout file
103
104   TYPE(PMC_layout),DIMENSION(PMC_MAX_MODELL) :: m_couplers   !Information of all couplers
105
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
123   INTERFACE pmc_is_rootmodel
124      MODULE PROCEDURE pmc_is_rootmodel
125   END INTERFACE pmc_is_rootmodel
126
127   INTERFACE pmc_get_model_info
128      MODULE PROCEDURE pmc_get_model_info
129   END INTERFACE pmc_get_model_info
130
131   PUBLIC pmc_get_model_info, pmc_init_model, pmc_is_rootmodel
132
133 CONTAINS
134
135   SUBROUTINE pmc_init_model( comm, nesting_datatransfer_mode, nesting_mode,   &
136                              pmc_status )
137
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
147      CHARACTER(LEN=7), INTENT(OUT) ::  nesting_datatransfer_mode
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
159      comm         = -1
160      m_world_comm = MPI_COMM_WORLD
161      m_my_cpl_id  = -1
162      clientcount  =  0
163      activeserver = -1
164      start_pe(:)  =  0
165
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
171
172         CALL read_coupling_layout( nesting_datatransfer_mode, nesting_mode,   &
173                                    pmc_status )
174
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
181            DO  i = 2, m_ncpl+1
182               start_pe(i) = start_pe(i-1) + m_couplers(i-1)%npe_total
183            ENDDO
184
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
188            IF ( start_pe(m_ncpl+1) /= m_world_npes )  THEN
189               WRITE ( message_string, '(A,I6,A,I6,A)' )                       &
190                               'nesting-setup requires more MPI procs (',      &
191                               start_pe(m_ncpl+1), ') than provided (',        &
192                               m_world_npes,')'
193               CALL message( 'pmc_init_model', 'PA0229', 3, 2, 0, 6, 0 )
194            ENDIF
195
196         ENDIF
197
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 )
204
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
210
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
220
221      ENDIF
222
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)
225
226!
227!--   Broadcast coupling layout
228      DO  i = 1, m_ncpl
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 )
232         CALL MPI_BCAST( m_couplers(i)%npe_total,    1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
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 )
235!--      TO_DO: the next two calls can and should probably moved outside this loop
236         CALL MPI_BCAST( nesting_mode, LEN( nesting_mode ), MPI_CHARACTER, 0, MPI_COMM_WORLD, istat )
237         CALL MPI_BCAST( nesting_datatransfer_mode, LEN(nesting_datatransfer_mode), MPI_CHARACTER, 0, MPI_COMM_WORLD, istat )
238      ENDDO
239
240!
241!--   Assign global MPI processes to individual models by setting the couple id
242      DO  i = 1, m_ncpl
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)
250
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 )
261
262!
263!--   Broadcast (from PE 0) the parent id and id of every model
264      DO  i = 1, m_ncpl
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
270
271!
272!--   Save the current model communicator for PMC internal use
273      m_model_comm = comm
274
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
280      DO  i = 2, m_ncpl
281
282         IF ( m_couplers(i)%parent_id == m_my_cpl_id )  THEN
283!
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
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
293
294         ELSEIF ( i == m_my_cpl_id)  THEN
295!
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
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
305
306      ENDDO
307
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) )
313
314      clientcount = 0
315      DO  i = 2, m_ncpl
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
339
340   END SUBROUTINE PMC_init_model
341
342
343!
344!-- Provide module private variables of the pmc for PALM
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 )
348
349      USE kinds
350
351      IMPLICIT NONE
352
353      CHARACTER(LEN=*), INTENT(OUT), OPTIONAL ::  cpl_name
354
355      INTEGER, INTENT(IN), OPTIONAL ::  request_for_cpl_id
356
357      INTEGER, INTENT(OUT), OPTIONAL ::  comm_world_nesting
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
362
363      INTEGER ::  requested_cpl_id
364
365      REAL(wp), INTENT(OUT), OPTIONAL ::  lower_left_x
366      REAL(wp), INTENT(OUT), OPTIONAL ::  lower_left_y
367
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
381      IF ( PRESENT( comm_world_nesting )  )  THEN
382         comm_world_nesting = m_world_comm
383      ENDIF
384      IF ( PRESENT( cpl_id )        )  THEN
385         cpl_id = requested_cpl_id
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
391         cpl_name = m_couplers(requested_cpl_id)%name
392      ENDIF
393      IF ( PRESENT( ncpl )          )  THEN
394         ncpl = m_ncpl
395      ENDIF
396      IF ( PRESENT( npe_total )     )  THEN
397         npe_total = m_couplers(requested_cpl_id)%npe_total
398      ENDIF
399      IF ( PRESENT( lower_left_x )  )  THEN
400         lower_left_x = m_couplers(requested_cpl_id)%lower_left_x
401      ENDIF
402      IF ( PRESENT( lower_left_y )  )  THEN
403         lower_left_y = m_couplers(requested_cpl_id)%lower_left_y
404      ENDIF
405
406   END SUBROUTINE pmc_get_model_info
407
408
409
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
420 SUBROUTINE read_coupling_layout( nesting_datatransfer_mode, nesting_mode,     &
421                                  pmc_status )
422
423    IMPLICIT NONE
424
425    CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_mode
426    CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_datatransfer_mode
427
428    INTEGER, INTENT(INOUT) ::  pmc_status
429    INTEGER                ::  i, istat
430
431    TYPE(pmc_layout), DIMENSION(pmc_max_modell) ::  domain_layouts
432
433
434    NAMELIST /nestpar/  domain_layouts, nesting_datatransfer_mode, nesting_mode
435
436!
437!-- Initialize some coupling variables
438    domain_layouts(1:pmc_max_modell)%id = -1
439    m_ncpl =   0
440
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 )
456       RETURN
457
458    ELSEIF ( istat > 0 )  THEN
459!
460!--    Errors in reading nestpar-NAMELIST
461       pmc_status = pmc_namelist_error
462       RETURN
463
464    ENDIF
465
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
472
473!
474!-- Get the number of nested models given in the nestpar-NAMELIST
475    DO  i = 1, pmc_max_modell
476!
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
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.