source: palm/trunk/SOURCE/pmc_handle_communicator_mod.f90 @ 1850

Last change on this file since 1850 was 1850, checked in by maronga, 8 years ago

added _mod string to several filenames to meet the naming convection for modules

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